You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?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;
|
|
}
|
|
} |