summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2017-01-12 23:51:26 +0100
committerArnold Daniels <arnold@jasny.net>2017-02-09 14:53:26 +0100
commit412c1244d2998e7b00f4fe10cd9f99155b8cc5ba (patch)
tree498aa19d888d9190e75de41fce58f6d9837f0e8d
parent45cbc48d0abcbeb0e51964c8521396f9d7b36224 (diff)
downloadcontroller-412c1244d2998e7b00f4fe10cd9f99155b8cc5ba.zip
controller-412c1244d2998e7b00f4fe10cd9f99155b8cc5ba.tar.gz
controller-412c1244d2998e7b00f4fe10cd9f99155b8cc5ba.tar.bz2
Moved view logic to own library
-rw-r--r--src/Controller.php7
-rw-r--r--src/Controller/ControllerInterface.php18
-rw-r--r--src/Controller/View.php64
-rw-r--r--src/Controller/View/Twig.php171
-rw-r--r--tests/Controller/View/TwigTest.php1
5 files changed, 104 insertions, 157 deletions
diff --git a/src/Controller.php b/src/Controller.php
index 764d6f7..95d7b3c 100644
--- a/src/Controller.php
+++ b/src/Controller.php
@@ -2,13 +2,14 @@
namespace Jasny;
+use ControllerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
- * Controller
+ * Controller base class
*/
-abstract class Controller
+abstract class Controller implements ControllerInterface
{
use Controller\Input,
Controller\Output,
@@ -59,7 +60,7 @@ abstract class Controller
/**
* Get response. set for controller
*
- * @return ResponseInterface
+ * @param ResponseInterface $response
*/
public function setResponse(ResponseInterface $response)
{
diff --git a/src/Controller/ControllerInterface.php b/src/Controller/ControllerInterface.php
new file mode 100644
index 0000000..5fbce25
--- /dev/null
+++ b/src/Controller/ControllerInterface.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace Jasny;
+
+/**
+ * Interface for controllers
+ */
+interface ControllerInterface
+{
+ /**
+ * Run the controller as function
+ *
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
+ * @return ResponseInterface
+ */
+ public function __invoke(ServerRequestInterface $request, ResponseInterface $response);
+}
diff --git a/src/Controller/View.php b/src/Controller/View.php
new file mode 100644
index 0000000..f3e39de
--- /dev/null
+++ b/src/Controller/View.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Jasny\Controller;
+
+use Jasny\ViewInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+/**
+ * View using a template engine
+ */
+trait View
+{
+ /**
+ * Get server request
+ *
+ * @return ServerRequestInterface
+ */
+ abstract public function getRequest();
+
+ /**
+ * Get response. set for controller
+ *
+ * @param ResponseInterface $response
+ */
+ abstract public function setResponse(ResponseInterface $response);
+
+ /**
+ * Get the template engine abstraction
+ *
+ * @return ViewInterface
+ */
+ abstract public function getViewer();
+
+
+ /**
+ * Get path of the view files
+ *
+ * @return string
+ */
+ protected function getViewPath()
+ {
+ return getcwd();
+ }
+
+ /**
+ * View rendered template
+ *
+ * @param string $name Template name
+ * @param array $context Template context
+ */
+ public function view($name, array $context = [])
+ {
+ $context += ['current_url', $this->getRequest()->getUri()];
+
+ if (method_exists($this, 'flash')) {
+ $context += ['flash' => $this->flash()];
+ }
+
+ $response = $this->getViewer()->view($this->getResponse(), $name, $context);
+
+ $this->setResponse($response);
+ }
+}
diff --git a/src/Controller/View/Twig.php b/src/Controller/View/Twig.php
index f74c05a..c6c69af 100644
--- a/src/Controller/View/Twig.php
+++ b/src/Controller/View/Twig.php
@@ -2,173 +2,36 @@
namespace Jasny\Controller\View;
-use Psr\Http\Message\ServerRequestInterface;
+use Jasny\Controller\View;
+use Jasny\ViewInterface;
+use Jasny\View\Twig as TwigView;
+
/**
* View using Twig
*/
trait Twig
{
- /**
- * Twig environment
- * @var \Twig_Environment
- */
- protected $twig;
-
-
- /**
- * Get server request
- *
- * @return ServerRequestInterface
- */
- abstract public function getRequest();
-
- /**
- * Output result
- *
- * @param mixed $data
- * @param string $format Output format as MIME or extension
- * @return void
- */
- abstract public function output($data, $format = null);
-
-
- /**
- * Get path of the view files
- *
- * @return string
- */
- protected function getViewPath()
- {
- return getcwd();
- }
-
- /**
- * Assert valid variable, function and filter name
- *
- * @param string $name
- * @throws \InvalidArgumentException
- */
- protected function assertViewVariableName($name)
- {
- if (!is_string($name)) {
- $type = (is_object($name) ? get_class($name) . ' ' : '') . gettype($name);
- throw new \InvalidArgumentException("Expected name to be a string, not a $type");
- }
-
- if (!preg_match('/^[a-z]\w*$/i', $name)) {
- throw new \InvalidArgumentException("Invalid name '$name'");
- }
- }
+ use View;
/**
- * Add a global variable to the view.
- *
- * @param string $name Variable name
- * @param mixed $value
- * @return $this
- */
- public function setViewVariable($name, $value)
- {
- $this->assertViewVariableName($name);
-
- $this->getTwig()->addGlobal($name, $value);
-
- return $this;
- }
-
- /**
- * Expose a function to the view.
- *
- * @param string $name Function name
- * @param string|null $function
- * @param string $as 'function' or 'filter'
- * @return $this
- */
- public function setViewFunction($name, $function = null, $as = 'function')
- {
- $this->assertViewVariableName($name);
-
- if ($as === 'function') {
- $function = new \Twig_SimpleFunction($name, $function ?: $name);
- $this->getTwig()->addFunction($function);
- } elseif ($as === 'filter') {
- $filter = new \Twig_SimpleFilter($name, $function ?: $name);
- $this->getTwig()->addFilter($filter);
- } else {
- $not = is_string($as) ? "'$as'" : 'a ' . gettype($as);
- throw new \InvalidArgumentException("You should create either a 'function' or 'filter', not $not");
- }
-
- return $this;
- }
-
- /**
- * Get twig environment instance
- *
- * @return \Twig_Environment
+ * @var ViewInterface
*/
- public function createTwigEnvironment()
- {
- $path = $this->getViewPath();
- $loader = new \Twig_Loader_Filesystem($path);
-
- return new \Twig_Environment($loader);
- }
-
+ protected $viewer;
+
+
/**
- * Initialize the Twig environment
+ * Get the template engine abstraction
+ *
+ * @return TwigView
*/
- protected function initTwig()
+ public function getViewer()
{
- $this->twig = $this->createTwigEnvironment();
-
- $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension'];
- foreach ($extensions as $name) {
- $class = "Jasny\Twig\\$name";
-
- if (class_exists($class)) {
- $this->twig->addExtension(new $class());
- }
+ if (!isset($this->viewer)) {
+ $this->viewer = new TwigView(['path' => $this->getViewPath()]);
+ $this->viewer->addDefaultExtensions();
}
-
- $this->twig->addGlobal('current_url', $this->getRequest()->getUri());
- if (method_exists($this, 'flash')) {
- $this->twig->addGlobal('flash', $this->flash());
- }
- }
-
- /**
- * Get Twig environment
- *
- * @return \Twig_Environment
- */
- public function getTwig()
- {
- if (!isset($this->twig)) {
- $this->initTwig();
- }
-
- return $this->twig;
- }
-
-
- /**
- * View rendered template
- *
- * @param string $name Template name
- * @param array $context Template context
- */
- public function view($name, array $context = [])
- {
- if (!pathinfo($name, PATHINFO_EXTENSION)) {
- $name .= '.html.twig';
- }
-
- $twig = $this->getTwig();
- $tmpl = $twig->loadTemplate($name);
-
- $this->output($tmpl->render($context), 'text/html; charset=' . $twig->getCharset());
+ return $this->viewer;
}
}
diff --git a/tests/Controller/View/TwigTest.php b/tests/Controller/View/TwigTest.php
index e46757e..8fe27c7 100644
--- a/tests/Controller/View/TwigTest.php
+++ b/tests/Controller/View/TwigTest.php
@@ -9,6 +9,7 @@ use Psr\Http\Message\UriInterface;
use Jasny\Controller\TestHelper;
/**
+ * @covers Jasny\Controller\View
* @covers Jasny\Controller\View\Twig
*/
class TwigTest extends \PHPUnit_Framework_TestCase