diff options
author | Arnold Daniels <arnold@jasny.net> | 2017-02-09 12:54:46 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2017-02-09 14:54:53 +0100 |
commit | 75ab132381ac2e9fc97daae680410e8cb3d292c3 (patch) | |
tree | bf38fd5b7b8781264df59ff79e71f740b5ca6a80 /src | |
parent | 9f2e0789a87685d6ba8f2cf10ee345d64771d95e (diff) | |
download | controller-75ab132381ac2e9fc97daae680410e8cb3d292c3.zip controller-75ab132381ac2e9fc97daae680410e8cb3d292c3.tar.gz controller-75ab132381ac2e9fc97daae680410e8cb3d292c3.tar.bz2 |
Reworked views using Jasny View
Added View/PHP trait
Ability to set a viewer
Diffstat (limited to 'src')
-rw-r--r-- | src/Controller/View.php | 38 | ||||
-rw-r--r-- | src/Controller/View/PHP.php | 40 | ||||
-rw-r--r-- | src/Controller/View/Twig.php | 22 |
3 files changed, 87 insertions, 13 deletions
diff --git a/src/Controller/View.php b/src/Controller/View.php index f3e39de..b32c285 100644 --- a/src/Controller/View.php +++ b/src/Controller/View.php @@ -12,11 +12,23 @@ use Psr\Http\Message\ResponseInterface; trait View { /** + * @var ViewInterface + */ + protected $viewer; + + /** * Get server request * * @return ServerRequestInterface */ abstract public function getRequest(); + + /** + * Get server request + * + * @return ResponseInterface + */ + abstract public function getResponse(); /** * Get response. set for controller @@ -25,12 +37,30 @@ trait View */ abstract public function setResponse(ResponseInterface $response); + /** * Get the template engine abstraction * * @return ViewInterface */ - abstract public function getViewer(); + public function getViewer() + { + if (!isset($this->viewer)) { + throw new \LogicException("Viewer has not been set"); + } + + return $this->viewer; + } + + /** + * Get the template engine abstraction + * + * @param ViewInterface $viewer + */ + public function setViewer(ViewInterface $viewer) + { + $this->viewer = $viewer; + } /** @@ -38,7 +68,7 @@ trait View * * @return string */ - protected function getViewPath() + public function getViewPath() { return getcwd(); } @@ -51,13 +81,13 @@ trait View */ public function view($name, array $context = []) { - $context += ['current_url', $this->getRequest()->getUri()]; + $context += ['current_url' => $this->getRequest()->getUri()]; if (method_exists($this, 'flash')) { $context += ['flash' => $this->flash()]; } - $response = $this->getViewer()->view($this->getResponse(), $name, $context); + $response = $this->getViewer()->render($this->getResponse(), $name, $context); $this->setResponse($response); } diff --git a/src/Controller/View/PHP.php b/src/Controller/View/PHP.php new file mode 100644 index 0000000..0f7a5b7 --- /dev/null +++ b/src/Controller/View/PHP.php @@ -0,0 +1,40 @@ +<?php + +namespace Jasny\Controller\View; + +use Jasny\Controller\View; +use Jasny\View\PHP as PHPView; + +/** + * View using PHP + */ +trait PHP +{ + use View; + + /** + * Get the template engine abstraction + * + * @return PHPView + */ + public function getViewer() + { + if (!isset($this->viewer)) { + $this->viewer = $this->createPHPView(['path' => $this->getViewPath()]); + } + + return $this->viewer; + } + + /** + * Create a twig view object. + * @ignore + * @codeCoverageIgnore + * + * @return PHPView; + */ + protected function createPHPView($options) + { + return new PHPView($options); + } +} diff --git a/src/Controller/View/Twig.php b/src/Controller/View/Twig.php index c6c69af..78df6b0 100644 --- a/src/Controller/View/Twig.php +++ b/src/Controller/View/Twig.php @@ -3,10 +3,8 @@ namespace Jasny\Controller\View; use Jasny\Controller\View; -use Jasny\ViewInterface; use Jasny\View\Twig as TwigView; - /** * View using Twig */ @@ -15,12 +13,6 @@ trait Twig use View; /** - * @var ViewInterface - */ - protected $viewer; - - - /** * Get the template engine abstraction * * @return TwigView @@ -28,10 +20,22 @@ trait Twig public function getViewer() { if (!isset($this->viewer)) { - $this->viewer = new TwigView(['path' => $this->getViewPath()]); + $this->viewer = $this->createTwigView(['path' => $this->getViewPath()]); $this->viewer->addDefaultExtensions(); } return $this->viewer; } + + /** + * Create a twig view object. + * @ignore + * @codeCoverageIgnore + * + * @return TwigView; + */ + protected function createTwigView($options) + { + return new TwigView($options); + } } |