Skip to content

Commit

Permalink
Merge pull request #9 from webhdx/twig3
Browse files Browse the repository at this point in the history
Added support for Twig 3
  • Loading branch information
gnugat authored May 28, 2020
2 parents 746f944 + 7aed6a6 commit 06fc9d8
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions spec/Memio/TwigTemplateEngine/TwigTemplateEngineSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

use Memio\PrettyPrinter\TemplateEngine;
use PhpSpec\ObjectBehavior;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class TwigTemplateEngineSpec extends ObjectBehavior
{
const TEMPLATE_PATH = '/tmp/templates';
const TEMPLATE = 'argument';
const OUTPUT = '$dateTime';

function let(\Twig_Environment $twig)
function let(Environment $twig)
{
$this->beConstructedWith($twig);
}
Expand All @@ -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();
Expand All @@ -41,7 +43,7 @@ function it_can_have_more_paths(
}

function it_renders_templates_using_twig(
\Twig_Environment $twig
Environment $twig
) {
$parameters = ['name' => 'dateTime'];

Expand Down
12 changes: 8 additions & 4 deletions src/Memio/TwigTemplateEngine/TwigExtension/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
];
}

Expand Down
11 changes: 7 additions & 4 deletions src/Memio/TwigTemplateEngine/TwigExtension/Whitespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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']),
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Memio/TwigTemplateEngine/TwigTemplateEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion templates/file.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
22 changes: 0 additions & 22 deletions templates/object.twig

This file was deleted.

22 changes: 22 additions & 0 deletions templates/objekt.twig
Original file line number Diff line number Diff line change
@@ -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 %}
}

0 comments on commit 06fc9d8

Please sign in to comment.