diff options
author | Arnold Daniels <arnold@jasny.net> | 2016-11-18 11:42:52 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2016-11-18 11:42:52 +0100 |
commit | 934b380f473b4e85e807f07d6bf516f4e227e112 (patch) | |
tree | b714c623fcb8aa43863ee2205d51a9c19849d44f | |
parent | 72a372f22c95cde490c9affaadccf3110637e554 (diff) | |
download | controller-934b380f473b4e85e807f07d6bf516f4e227e112.zip controller-934b380f473b4e85e807f07d6bf516f4e227e112.tar.gz controller-934b380f473b4e85e807f07d6bf516f4e227e112.tar.bz2 |
Simplify Twig trait
-rw-r--r-- | src/Controller/View/Twig.php | 147 |
1 files changed, 60 insertions, 87 deletions
diff --git a/src/Controller/View/Twig.php b/src/Controller/View/Twig.php index ee5d4ce..3b9b7b8 100644 --- a/src/Controller/View/Twig.php +++ b/src/Controller/View/Twig.php @@ -2,9 +2,8 @@ namespace Jasny\Controller\View; -use Jasny\Flash; +use Jasny\Controller\Session\Flash; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\ResponseInterface; /** * View using Twig @@ -15,20 +14,34 @@ trait Twig * Twig environment * @var \Twig_Environment */ - protected $twig = null; + protected $twig; + /** * Get server request * @return ServerRequestInterface */ - abstract public function getRequest(); + abstract protected function getRequest(); /** - * Get server response - * @return ResponseInterface + * Output result + * + * @param mixed $data + * @param string $format Output format as MIME or extension */ - abstract public function getResponse(); + abstract public function output($data, $format = null); + + /** + * Get path of the view files + * + * @return string + */ + protected function getViewPath() + { + return getcwd(); + } + /** * Add a global variable to the view. * @@ -48,17 +61,19 @@ trait Twig /** * Expose a function to the view. * - * @param string $name Variable name - * @param mixed $function - * @param string $as 'function' or 'filter' + * @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') { if ($as === 'function') { - $this->getTwig()->addFunction($this->createTwigFunction($name, $function)); + $function = new \Twig_SimpleFunction($name, $function ?: $name); + $this->getTwig()->addFunction($function); } elseif ($as === 'filter') { - $this->getTwig()->addFilter($this->createTwigFilter($name, $function)); + $filter = \Twig_SimpleFilter($name, $function ?: $name); + $this->getTwig()->addFilter($filter); } else { throw new \InvalidArgumentException("You should create either function or filter, not '$as'"); } @@ -67,112 +82,70 @@ trait Twig } /** - * Add extension to view - * - * @param object $extension - * @return $this - */ - public function setViewExtension($extension) - { - $this->getTwig()->addExtension($extension); - - return $this; - } - - /** - * View rendered template + * Get twig environment instance * - * @param string $name Template name - * @param array $context Template context - * @return ResponseInterface + * @return \Twig_Environment */ - public function view($name, array $context = []) + protected function createTwigEnvironment() { - if (!pathinfo($name, PATHINFO_EXTENSION)) $name .= '.html.twig'; - - $twig = $this->getTwig(); - $tmpl = $twig->loadTemplate($name); - - $response = $this->getResponse(); - $response = $response->withHeader('Content-Type', 'text/html; charset=' . $twig->getCharset()); - $response->getBody()->write($tmpl->render($context)); - - return $response; + $path = $this->getViewPath(); + $loader = new \Twig_Loader_Filesystem($path); + + return new \Twig_Environment($loader); } /** - * Get twig environment - * - * @return \Twig_Environment + * Initialize the Twig environment */ - public function getTwig() + protected function initTwig() { - if ($this->twig) return $this->twig; - - $loader = $this->getTwigLoader(); - $this->twig = $this->getTwigEnvironment($loader); + $this->twig = $this->createTwigEnvironment(); $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension']; foreach ($extensions as $name) { $class = "Jasny\Twig\\$name"; - - if (class_exists($class)) $this->setViewExtension(new $class()); + + if (class_exists($class)) { + $this->twig->addExtension(new $class()); + } } - $uri = $this->getRequest()->getUri()->getPath(); + $uri = $this->getRequest()->getUri(); $this->setViewVariable('current_url', $uri); $this->setViewVariable('flash', new Flash()); - - return $this->twig; } /** - * Get twig loasder for current working directory + * Get Twig environment * - * @return \Twig_Loader_Filesystem - */ - public function getTwigLoader() - { - return new \Twig_Loader_Filesystem(getcwd()); - } - - /** - * Get twig environment instance - * - * @param \Twig_Loader_Filesystem $loader * @return \Twig_Environment */ - public function getTwigEnvironment(\Twig_Loader_Filesystem $loader) + public function getTwig() { - return new \Twig_Environment($loader); + if (!isset($this->twig)) { + $this->initTwig(); + } + + return $this->twig; } + /** - * Create twig function + * View rendered template * - * @param string $name Name of function in view - * @param callable|null $function - * @return \Twig_SimpleFunction + * @param string $name Template name + * @param array $context Template context */ - public function createTwigFunction($name, $function) + public function view($name, array $context = []) { - if (!$name) throw new \InvalidArgumentException("Function name should not be empty"); - - return new \Twig_SimpleFunction($name, $function ?: $name); - } + if (!pathinfo($name, PATHINFO_EXTENSION)) { + $name .= '.html.twig'; + } - /** - * Create twig filter - * - * @param string $name Name of filter in view - * @param callable|null $function - * @return \Twig_SimpleFilter - */ - public function createTwigFilter($name, $function) - { - if (!$name) throw new \InvalidArgumentException("Filter name should not be empty"); + $twig = $this->getTwig(); + $tmpl = $twig->loadTemplate($name); - return new \Twig_SimpleFilter($name, $function ?: $name); + $this->output($tmpl->render($context), 'text/html; charset=' . $twig->getCharset()); } } |