diff options
author | Arnold Daniels <arnold@jasny.net> | 2016-11-17 21:40:25 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2016-11-17 21:40:25 +0100 |
commit | 1d84d01f35547bb53a78f33d41a8002f12cbf18c (patch) | |
tree | fd9892a7998db822b05229abb7ad06ead8dd58ed /src/View | |
parent | c04808754395a7c0454921dcd2e14596bea20745 (diff) | |
download | controller-1d84d01f35547bb53a78f33d41a8002f12cbf18c.zip controller-1d84d01f35547bb53a78f33d41a8002f12cbf18c.tar.gz controller-1d84d01f35547bb53a78f33d41a8002f12cbf18c.tar.bz2 |
Refactor controller. Move stuff to traits.
Diffstat (limited to 'src/View')
-rw-r--r-- | src/View/Twig.php | 178 |
1 files changed, 0 insertions, 178 deletions
diff --git a/src/View/Twig.php b/src/View/Twig.php deleted file mode 100644 index ee5d4ce..0000000 --- a/src/View/Twig.php +++ /dev/null @@ -1,178 +0,0 @@ -<?php - -namespace Jasny\Controller\View; - -use Jasny\Flash; -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\ResponseInterface; - -/** - * View using Twig - */ -trait Twig -{ - /** - * Twig environment - * @var \Twig_Environment - */ - protected $twig = null; - - /** - * Get server request - * @return ServerRequestInterface - */ - abstract public function getRequest(); - - /** - * Get server response - * @return ResponseInterface - */ - abstract public function getResponse(); - - /** - * Add a global variable to the view. - * - * @param string $name Variable name - * @param mixed $value - * @return $this - */ - public function setViewVariable($name, $value) - { - if (!$name) throw new \InvalidArgumentException("Name should not be empty"); - - $this->getTwig()->addGlobal($name, $value); - - return $this; - } - - /** - * Expose a function to the view. - * - * @param string $name Variable name - * @param mixed $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)); - } elseif ($as === 'filter') { - $this->getTwig()->addFilter($this->createTwigFilter($name, $function)); - } else { - throw new \InvalidArgumentException("You should create either function or filter, not '$as'"); - } - - return $this; - } - - /** - * Add extension to view - * - * @param object $extension - * @return $this - */ - public function setViewExtension($extension) - { - $this->getTwig()->addExtension($extension); - - return $this; - } - - /** - * View rendered template - * - * @param string $name Template name - * @param array $context Template context - * @return ResponseInterface - */ - public function view($name, array $context = []) - { - 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; - } - - /** - * Get twig environment - * - * @return \Twig_Environment - */ - public function getTwig() - { - if ($this->twig) return $this->twig; - - $loader = $this->getTwigLoader(); - $this->twig = $this->getTwigEnvironment($loader); - - $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension']; - foreach ($extensions as $name) { - $class = "Jasny\Twig\\$name"; - - if (class_exists($class)) $this->setViewExtension(new $class()); - } - - $uri = $this->getRequest()->getUri()->getPath(); - - $this->setViewVariable('current_url', $uri); - $this->setViewVariable('flash', new Flash()); - - return $this->twig; - } - - /** - * Get twig loasder for current working directory - * - * @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) - { - return new \Twig_Environment($loader); - } - - /** - * Create twig function - * - * @param string $name Name of function in view - * @param callable|null $function - * @return \Twig_SimpleFunction - */ - public function createTwigFunction($name, $function) - { - if (!$name) throw new \InvalidArgumentException("Function name should not be empty"); - - return new \Twig_SimpleFunction($name, $function ?: $name); - } - - /** - * 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"); - - return new \Twig_SimpleFilter($name, $function ?: $name); - } -} |