commit 8b0238ece73e211137dfd9da64347dc120d4a9bc Author: eater <=@eater.me> Date: Tue Feb 11 12:32:50 2020 +0100 reimport diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30babff --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +.idea +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..78dfd86 --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "bitcommunism/twig", + "license": "MIT", + "require": { + "twig/twig": "^2.4", + "php-di/php-di": "^5.4" + }, + "autoload": { + "psr-4": { + "BitCommunism\\Twig\\": "src/" + } + } +} diff --git a/config/default.php b/config/default.php new file mode 100644 index 0000000..17248ad --- /dev/null +++ b/config/default.php @@ -0,0 +1,32 @@ + add([ + string('{basedir}/templates'), + __DIR__ . '/../templates', + ]), + + 'twig.filtered_paths' => factory(function (Container $c) { + $paths = $c->get('twig.paths'); + return array_filter($paths, 'is_dir'); + }), + + 'twig.loader' => get(\Twig_Loader_Filesystem::class), + 'twig.debug' => false, + 'twig' => get(\Twig_Environment::class), + + \Twig_Loader_Filesystem::class => object(\Twig_Loader_Filesystem::class) + ->constructorParameter('paths', get('twig.filtered_paths')), + + \Twig_LoaderInterface::class => get('twig.loader'), +]; \ No newline at end of file diff --git a/config/development.php b/config/development.php new file mode 100644 index 0000000..a455fbc --- /dev/null +++ b/config/development.php @@ -0,0 +1,5 @@ + true, +]; \ No newline at end of file diff --git a/src/Handler/Twig.php b/src/Handler/Twig.php new file mode 100644 index 0000000..79915c0 --- /dev/null +++ b/src/Handler/Twig.php @@ -0,0 +1,44 @@ +environment = $environment; + parent::__construct($container); + } + + public function setTemplateVariable($key, $value) { + $this->templateVariables[$key] = $value; + } + + public function appendTemplateVariables($variables) { + $this->templateVariables = array_merge($this->templateVariables, $variables); + } + + public function template($template, $variables) { + $stream = new TwigStream($this->environment); + $stream->setTemplate($template); + $stream->appendVariables($this->templateVariables); + $stream->appendVariables($variables); + + return $stream; + } +} \ No newline at end of file diff --git a/src/Psr7/TwigStream.php b/src/Psr7/TwigStream.php new file mode 100644 index 0000000..481940d --- /dev/null +++ b/src/Psr7/TwigStream.php @@ -0,0 +1,63 @@ +environment = $environment; + } + + public function setTemplate($template) + { + $this->template = $template; + $this->resetStream(); + } + + public function setVariable($name, $value) { + $this->variables[$name] = $value; + $this->resetStream(); + } + + public function appendVariables($arr) { + $this->variables = array_merge($this->variables, $arr); + $this->resetStream(); + } + + public function isWritable() + { + return false; + } + + private function resetStream() { + if (!$this->stream) { + return; + } + + $this->stream->close(); + unset($this->stream); + } + + public function createStream() { + $data = $this->environment->render($this->template, $this->variables); + $stream = stream_for(fopen('php://memory', 'w+')); + $stream->write($data); + $stream->rewind(); + + return $stream; + } +} \ No newline at end of file