diff --git a/README.md b/README.md index cb33c79..16da61a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/yoyo/ViewProviders/PhalconViewProvider.php b/src/yoyo/ViewProviders/PhalconViewProvider.php new file mode 100644 index 0000000..145864f --- /dev/null +++ b/src/yoyo/ViewProviders/PhalconViewProvider.php @@ -0,0 +1,64 @@ +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); + } +} \ No newline at end of file diff --git a/src/yoyo/YoyoPhalconController.php b/src/yoyo/YoyoPhalconController.php new file mode 100644 index 0000000..34f8ac7 --- /dev/null +++ b/src/yoyo/YoyoPhalconController.php @@ -0,0 +1,22 @@ +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; + } +} \ No newline at end of file diff --git a/src/yoyo/YoyoPhalconServiceProvider.php b/src/yoyo/YoyoPhalconServiceProvider.php new file mode 100644 index 0000000..2200f72 --- /dev/null +++ b/src/yoyo/YoyoPhalconServiceProvider.php @@ -0,0 +1,59 @@ +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; + }); + } +} \ No newline at end of file