Skip to content

Commit

Permalink
Merge pull request #28 from mucan54/phalcon-implementation
Browse files Browse the repository at this point in the history
Phalcon framework implementation.
  • Loading branch information
jreviews authored Jun 20, 2024
2 parents e39ee96 + 33d0937 commit f12323b
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
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;
});
}
}

0 comments on commit f12323b

Please sign in to comment.