Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phalcon framework implementation. #28

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ Yes, it's that simple! One thing to note above is the use of the protected prope
composer require clickfwd/yoyo
```

#### Phalcon Framework Installation

For phalcon, you need to add di

```php
$di->register(new \Clickfwd\Yoyo\YoyoPhalconServiceProvider());
```

and you need to add router:

```php
$router->add('/yoyo', [
'controller' => 'yoyo',
'action' => 'handle',
]);
```

and you should create a controller and inherit from `Clickfwd\Yoyo\PhalconController` class.


## Updating

After performing the usual `composer update`, remember to also update the `yoyo.js` script per the [Load Assets](#load-assets) instructions.
Expand Down
64 changes: 64 additions & 0 deletions src/yoyo/ViewProviders/PhalconViewProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Clickfwd\Yoyo\ViewProviders;

use Clickfwd\Yoyo\Interfaces\ViewProviderInterface;
use Clickfwd\Yoyo\ViewProviders\BaseViewProvider;
use Phalcon\Mvc\View;

class PhalconViewProvider extends BaseViewProvider implements ViewProviderInterface
{
protected $view;

protected $template;

protected $vars;

private $viewExtention = '.phtml';

public function __construct($view)
{
$this->view = $view;
}

public function exists($view): bool
{
return file_exists($this->view->getViewsDir() . $view . $this->viewExtention);
}

public function render($template, $vars = []): ViewProviderInterface
{
$this->template = $template;
$this->vars = $vars;

return $this;
}

public function setViewExtention($viewExtention)
{
$this->viewExtention = $viewExtention;

return $this;
}
public function makeFromString($content, $vars = []): string
{
$this->view->start();
$this->view->setContent($content);
$this->view->setVars($vars);
$this->view->finish();
return $this->view->render();
}

public function startYoyoRendering($component): void
{
}

public function stopYoyoRendering(): void
{
}

public function __toString()
{
return $this->view->render($this->template, $this->vars);
}
}
22 changes: 22 additions & 0 deletions src/yoyo/YoyoPhalconController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Clickfwd\Yoyo;

use Phalcon\Mvc\Controller;
use Clickfwd\Yoyo\Request;
use Clickfwd\Yoyo\Yoyo;

class YoyoPhalconController extends Controller
{
public function handleAction()
{
$this->view->disable();
/** @var Yoyo $yoyo */
$yoyo = $this->di->get('yoyo');
$yoyoRequest = new Request();
$yoyoRequest->mock($_REQUEST, $_SERVER);
$yoyo->bindRequest($yoyoRequest);
$this->response->setContent($yoyo->update());
return $this->response;
}
}
59 changes: 59 additions & 0 deletions src/yoyo/YoyoPhalconServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Clickfwd\Yoyo;

use Clickfwd\Yoyo\ViewProviders\PhalconViewProvider;
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
use Phalcon\Mvc\View\Simple as SimpleView;

class YoyoPhalconServiceProvider implements ServiceProviderInterface
{
private $yoyoConfig = [];

private $viewExtention = null;

public function setYoyoConfig($yoyoConfig)
{
$this->yoyoConfig = $yoyoConfig;

return $this;
}

public function setViewExtention($viewExtention)
{
$this->viewExtention = $viewExtention;

return $this;
}

public function register(DiInterface $di): void
{
$di->setShared('yoyo', function () use ($di) {
$yoyo = new Yoyo();
$yoyoConfig = $this->yoyoConfig ?? [
'url' => '/yoyo',
'namespace' => 'App\Components\\',
'scriptsPath' => 'js/',
];

$yoyo->configure($yoyoConfig);
$viewExtention = $this->viewExtention ?? null;

$yoyo->container()->singleton('yoyo.view.default', function () use ($di, $viewExtention) {
$view = $di->get('view');
$simpleView = new SimpleView();
$simpleView->setViewsDir($view->getViewsDir());
/** @var PhalconViewProvider $viewProvider */
$viewProvider = new PhalconViewProvider($simpleView);
if($viewExtention){
$viewProvider->setViewExtention($this->viewExtention);
}

return $viewProvider;
});

return $yoyo;
});
}
}
Loading