master
eater 4 years ago
commit 8b0238ece7

3
.gitignore vendored

@ -0,0 +1,3 @@
/vendor/
.idea
composer.lock

@ -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/"
}
}
}

@ -0,0 +1,32 @@
<?php
namespace BitCommunism\Twig;
use DI\Container;
use function DI\add;
use function DI\factory;
use function DI\get;
use function DI\object;
use function DI\string;
return [
'twig.paths' => 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'),
];

@ -0,0 +1,5 @@
<?php
return [
'twig.debug' => true,
];

@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: eater
* Date: 1/16/18
* Time: 5:25 PM
*/
namespace BitCommunism\Twig\Handler;
use BitCommunism\Http\Handler;
use BitCommunism\Twig\Psr7\TwigStream;
use DI\Container;
use Twig\Environment;
class Twig extends Handler
{
protected $environment;
protected $templateVariables = [];
public function __construct(Environment $environment, Container $container)
{
$this->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;
}
}

@ -0,0 +1,63 @@
<?php
namespace BitCommunism\Twig\Psr7;
use GuzzleHttp\Psr7\Stream;
use function GuzzleHttp\Psr7\stream_for;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use Psr\Http\Message\StreamInterface;
use Twig\Environment;
/** @var $stream Stream */
class TwigStream implements StreamInterface
{
use StreamDecoratorTrait;
private $environment;
private $template;
private $variables = [];
public function __construct(Environment $environment)
{
$this->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;
}
}
Loading…
Cancel
Save