summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Controller/View.php38
-rw-r--r--src/Controller/View/PHP.php40
-rw-r--r--src/Controller/View/Twig.php22
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);
+ }
}