From cec6750982e208dd6f4b2b7472c2fe491e8dffcf Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Sat, 14 Mar 2020 13:59:21 +0100 Subject: [PATCH 1/2] Added support for Twig 3 --- README.md | 4 ++-- composer.json | 2 +- .../TwigTemplateEngine/TwigTemplateEngineSpec.php | 10 ++++++---- src/Memio/TwigTemplateEngine/TwigExtension/Type.php | 12 ++++++++---- .../TwigTemplateEngine/TwigExtension/Whitespace.php | 11 +++++++---- src/Memio/TwigTemplateEngine/TwigTemplateEngine.php | 3 ++- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a140f4b..352abe5 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,8 @@ use Memio\Model\Method; use Memio\Model\Argument; // Initialize the code generator -$loader = new \Twig_Loader_Filesystem(__DIR__.'/templates'); -$twig = new \Twig_Environment($loader); +$loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/templates'); +$twig = new \Twig\Environment($loader); $line = new Memio\TwigTemplateEngine\TwigExtension\Line\Line(); $line->add(new Memio\TwigTemplateEngine\TwigExtension\Line\ContractLineStrategy()); diff --git a/composer.json b/composer.json index 5fb3b33..2b15fd7 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "memio/model": "^2.0", "memio/pretty-printer": "^2.0", "php": "^7.0", - "twig/twig": "^1.18 || ^2.0" + "twig/twig": "^1.18 || ^2.0 || ^3.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.10", diff --git a/spec/Memio/TwigTemplateEngine/TwigTemplateEngineSpec.php b/spec/Memio/TwigTemplateEngine/TwigTemplateEngineSpec.php index dcd8bbd..3f5f462 100644 --- a/spec/Memio/TwigTemplateEngine/TwigTemplateEngineSpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigTemplateEngineSpec.php @@ -13,6 +13,8 @@ use Memio\PrettyPrinter\TemplateEngine; use PhpSpec\ObjectBehavior; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; class TwigTemplateEngineSpec extends ObjectBehavior { @@ -20,7 +22,7 @@ class TwigTemplateEngineSpec extends ObjectBehavior const TEMPLATE = 'argument'; const OUTPUT = '$dateTime'; - function let(\Twig_Environment $twig) + function let(Environment $twig) { $this->beConstructedWith($twig); } @@ -31,8 +33,8 @@ function it_is_a_template_engine() } function it_can_have_more_paths( - \Twig_Environment $twig, - \Twig_Loader_Filesystem $loader + Environment $twig, + FilesystemLoader $loader ) { $twig->getLoader()->willReturn($loader); $loader->prependPath(self::TEMPLATE_PATH)->shouldBeCalled(); @@ -41,7 +43,7 @@ function it_can_have_more_paths( } function it_renders_templates_using_twig( - \Twig_Environment $twig + Environment $twig ) { $parameters = ['name' => 'dateTime']; diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Type.php b/src/Memio/TwigTemplateEngine/TwigExtension/Type.php index 5845055..c8f5c40 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Type.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Type.php @@ -15,27 +15,31 @@ use Memio\Model\Contract; use Memio\Model\FullyQualifiedName; use Memio\Model\Type as ModelType; +use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; +use Twig\TwigFunction; +use Twig\TwigTest; -class Type extends \Twig_Extension +class Type extends AbstractExtension { public function getFilters(): array { return [ - new \Twig_SimpleFilter('filter_namespace', [$this, 'filterNamespace']), + new TwigFilter('filter_namespace', [$this, 'filterNamespace']), ]; } public function getFunctions(): array { return [ - new \Twig_SimpleFunction('has_typehint', [$this, 'hasTypehint']), + new TwigFunction('has_typehint', [$this, 'hasTypehint']), ]; } public function getTests(): array { return [ - new \Twig_SimpleTest('contract', [$this, 'isContract']), + new TwigTest('contract', [$this, 'isContract']), ]; } diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Whitespace.php b/src/Memio/TwigTemplateEngine/TwigExtension/Whitespace.php index 8962905..d464b08 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Whitespace.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Whitespace.php @@ -15,8 +15,11 @@ use Memio\Model\Phpdoc\ParameterTag; use Memio\Model\Type as ModelType; use Memio\TwigTemplateEngine\TwigExtension\Line\Line; +use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; +use Twig\TwigFunction; -class Whitespace extends \Twig_Extension +class Whitespace extends AbstractExtension { private $line; @@ -28,15 +31,15 @@ public function __construct(Line $line) public function getFunctions(): array { return [ - new \Twig_SimpleFunction('needs_line_after', [$this->line, 'needsLineAfter']), + new TwigFunction('needs_line_after', [$this->line, 'needsLineAfter']), ]; } public function getFilters(): array { return [ - new \Twig_SimpleFilter('align', [$this, 'align']), - new \Twig_SimpleFilter('indent', [$this, 'indent']), + new TwigFilter('align', [$this, 'align']), + new TwigFilter('indent', [$this, 'indent']), ]; } diff --git a/src/Memio/TwigTemplateEngine/TwigTemplateEngine.php b/src/Memio/TwigTemplateEngine/TwigTemplateEngine.php index 669b21e..6886724 100644 --- a/src/Memio/TwigTemplateEngine/TwigTemplateEngine.php +++ b/src/Memio/TwigTemplateEngine/TwigTemplateEngine.php @@ -12,12 +12,13 @@ namespace Memio\TwigTemplateEngine; use Memio\PrettyPrinter\TemplateEngine; +use Twig\Environment; class TwigTemplateEngine implements TemplateEngine { private $twig; - public function __construct(\Twig_Environment $twig) + public function __construct(Environment $twig) { $this->twig = $twig; } From 7aed6a6200e2e6b544f9a8d93a5fd1569550f481 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Sat, 14 Mar 2020 14:46:25 +0100 Subject: [PATCH 2/2] Changed all object references to objekt --- templates/file.twig | 2 +- templates/object.twig | 22 ---------------------- templates/objekt.twig | 22 ++++++++++++++++++++++ 3 files changed, 23 insertions(+), 23 deletions(-) delete mode 100644 templates/object.twig create mode 100644 templates/objekt.twig diff --git a/templates/file.twig b/templates/file.twig index 279f4e5..1549409 100644 --- a/templates/file.twig +++ b/templates/file.twig @@ -17,5 +17,5 @@ namespace {{ file.namespace }}; {% if file.structure is contract %} {% include 'contract.twig' with { 'contract': file.structure } only %} {% else %} -{% include 'object.twig' with { 'object': file.structure } only %} +{% include 'objekt.twig' with { 'objekt': file.structure } only %} {% endif %} diff --git a/templates/object.twig b/templates/object.twig deleted file mode 100644 index 9328330..0000000 --- a/templates/object.twig +++ /dev/null @@ -1,22 +0,0 @@ -{% if object.phpdoc %}{% include 'phpdoc/structure_phpdoc.twig' with {'structure_phpdoc': object.phpdoc} %}{% endif %} -{{ object.isAbstract ? 'abstract ' }}{{ object.isFinal ? 'final ' }}class {{ object.name }} - {{- object.hasParent ? ' extends ' ~ object.Parent.name }} - {{- object.allContracts is not empty ? ' implements ' }} - {%- include 'collection/contract_collection.twig' with { 'contract_collection': object.allContracts } only %} -{ -{% include 'collection/constant_collection.twig' with { - 'constant_collection': object.allConstants -} only %} -{% if needs_line_after(object, 'constants') %} - -{% endif %} -{% include 'collection/property_collection.twig' with { - 'property_collection': object.allProperties -} only %} -{% if needs_line_after(object, 'properties') %} - -{% endif %} -{% include 'collection/method_collection.twig' with { - 'method_collection': object.allMethods -} only %} -} diff --git a/templates/objekt.twig b/templates/objekt.twig new file mode 100644 index 0000000..08afcfd --- /dev/null +++ b/templates/objekt.twig @@ -0,0 +1,22 @@ +{% if objekt.phpdoc %}{% include 'phpdoc/structure_phpdoc.twig' with {'structure_phpdoc': objekt.phpdoc} %}{% endif %} +{{ objekt.isAbstract ? 'abstract ' }}{{ objekt.isFinal ? 'final ' }}class {{ objekt.name }} + {{- objekt.hasParent ? ' extends ' ~ objekt.Parent.name }} + {{- objekt.allContracts is not empty ? ' implements ' }} + {%- include 'collection/contract_collection.twig' with { 'contract_collection': objekt.allContracts } only %} +{ +{% include 'collection/constant_collection.twig' with { + 'constant_collection': objekt.allConstants +} only %} +{% if needs_line_after(objekt, 'constants') %} + +{% endif %} +{% include 'collection/property_collection.twig' with { + 'property_collection': objekt.allProperties +} only %} +{% if needs_line_after(objekt, 'properties') %} + +{% endif %} +{% include 'collection/method_collection.twig' with { + 'method_collection': objekt.allMethods +} only %} +}