Initial commit

eater 6 years ago
commit cfcb0332f3
Signed by: eater
GPG Key ID: AD2560A0F84F0759

2
.gitignore vendored

@ -0,0 +1,2 @@
/vendor/
.idea

@ -0,0 +1,13 @@
{
"name": "bitcommunism/twig",
"version": "1.0.1",
"require": {
"twig/twig": "^2.4",
"php-di/php-di": "^5.4"
},
"autoload": {
"psr-4": {
"BitCommunism\\Twig\\": "src/"
}
}
}

366
composer.lock generated

@ -0,0 +1,366 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "2157b4cf225fa03a64c5ff6d0d4eb2b0",
"packages": [
{
"name": "container-interop/container-interop",
"version": "1.2.0",
"source": {
"type": "git",
"url": "https://github.com/container-interop/container-interop.git",
"reference": "79cbf1341c22ec75643d841642dd5d6acd83bdb8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/container-interop/container-interop/zipball/79cbf1341c22ec75643d841642dd5d6acd83bdb8",
"reference": "79cbf1341c22ec75643d841642dd5d6acd83bdb8",
"shasum": ""
},
"require": {
"psr/container": "^1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Interop\\Container\\": "src/Interop/Container/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Promoting the interoperability of container objects (DIC, SL, etc.)",
"homepage": "https://github.com/container-interop/container-interop",
"time": "2017-02-14T19:40:03+00:00"
},
{
"name": "php-di/invoker",
"version": "1.3.3",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/Invoker.git",
"reference": "1f4ca63b9abc66109e53b255e465d0ddb5c2e3f7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/Invoker/zipball/1f4ca63b9abc66109e53b255e465d0ddb5c2e3f7",
"reference": "1f4ca63b9abc66109e53b255e465d0ddb5c2e3f7",
"shasum": ""
},
"require": {
"container-interop/container-interop": "~1.1"
},
"require-dev": {
"athletic/athletic": "~0.1.8",
"phpunit/phpunit": "~4.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Invoker\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Generic and extensible callable invoker",
"homepage": "https://github.com/PHP-DI/Invoker",
"keywords": [
"callable",
"dependency",
"dependency-injection",
"injection",
"invoke",
"invoker"
],
"time": "2016-07-14T13:09:58+00:00"
},
{
"name": "php-di/php-di",
"version": "5.4.6",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/PHP-DI.git",
"reference": "3f9255659595f3e289f473778bb6c51aa72abbbd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/PHP-DI/zipball/3f9255659595f3e289f473778bb6c51aa72abbbd",
"reference": "3f9255659595f3e289f473778bb6c51aa72abbbd",
"shasum": ""
},
"require": {
"container-interop/container-interop": "~1.2",
"php": ">=5.5.0",
"php-di/invoker": "^1.3.2",
"php-di/phpdoc-reader": "^2.0.1",
"psr/container": "~1.0"
},
"provide": {
"container-interop/container-interop-implementation": "^1.0",
"psr/container-implementation": "^1.0"
},
"replace": {
"mnapoli/php-di": "*"
},
"require-dev": {
"doctrine/annotations": "~1.2",
"doctrine/cache": "~1.4",
"mnapoli/phpunit-easymock": "~0.2.0",
"ocramius/proxy-manager": "~1.0|~2.0",
"phpbench/phpbench": "@dev",
"phpunit/phpunit": "~4.5"
},
"suggest": {
"doctrine/annotations": "Install it if you want to use annotations (version ~1.2)",
"doctrine/cache": "Install it if you want to use the cache (version ~1.4)",
"ocramius/proxy-manager": "Install it if you want to use lazy injection (version ~1.0 or ~2.0)"
},
"type": "library",
"autoload": {
"psr-4": {
"DI\\": "src/DI/"
},
"files": [
"src/DI/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "The dependency injection container for humans",
"homepage": "http://php-di.org/",
"keywords": [
"container",
"dependency injection",
"di"
],
"time": "2017-12-03T08:20:27+00:00"
},
{
"name": "php-di/phpdoc-reader",
"version": "2.0.1",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/PhpDocReader.git",
"reference": "83f5ead159defccfa8e7092e5b6c1c533b326d68"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/PhpDocReader/zipball/83f5ead159defccfa8e7092e5b6c1c533b326d68",
"reference": "83f5ead159defccfa8e7092e5b6c1c533b326d68",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.6"
},
"type": "library",
"autoload": {
"psr-4": {
"PhpDocReader\\": "src/PhpDocReader"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "PhpDocReader parses @var and @param values in PHP docblocks (supports namespaced class names with the same resolution rules as PHP)",
"keywords": [
"phpdoc",
"reflection"
],
"time": "2015-11-29T10:34:25+00:00"
},
{
"name": "psr/container",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/container.git",
"reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
"reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Container\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common Container Interface (PHP FIG PSR-11)",
"homepage": "https://github.com/php-fig/container",
"keywords": [
"PSR-11",
"container",
"container-interface",
"container-interop",
"psr"
],
"time": "2017-02-14T16:28:37+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.6.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296",
"reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.6-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"time": "2017-10-11T12:05:26+00:00"
},
{
"name": "twig/twig",
"version": "v2.4.4",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "eddb97148ad779f27e670e1e3f19fb323aedafeb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/eddb97148ad779f27e670e1e3f19fb323aedafeb",
"reference": "eddb97148ad779f27e670e1e3f19fb323aedafeb",
"shasum": ""
},
"require": {
"php": "^7.0",
"symfony/polyfill-mbstring": "~1.0"
},
"require-dev": {
"psr/container": "^1.0",
"symfony/debug": "~2.7",
"symfony/phpunit-bridge": "~3.3@dev"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.4-dev"
}
},
"autoload": {
"psr-0": {
"Twig_": "lib/"
},
"psr-4": {
"Twig\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Armin Ronacher",
"email": "armin.ronacher@active-4.com",
"role": "Project Founder"
},
{
"name": "Twig Team",
"homepage": "http://twig.sensiolabs.org/contributors",
"role": "Contributors"
}
],
"description": "Twig, the flexible, fast, and secure template language for PHP",
"homepage": "http://twig.sensiolabs.org",
"keywords": [
"templating"
],
"time": "2017-09-27T18:10:31+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

@ -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