Skip to content

Commit

Permalink
Switch to Twig 3 and rename package (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit authored Jun 2, 2020
1 parent 6f53897 commit c76b00b
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 84 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.github export-ignore
tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs.dist export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
112 changes: 112 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace Twig\DeferredExtension;

use PhpCsFixer\Config;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\Tokenizer\Tokens;

final class FilterableFixer implements FixerInterface, ConfigurableFixerInterface
{
private $fixer;
private $pathRegex;

public function __construct(FixerInterface $fixer, string $pathRegex)
{
$this->fixer = $fixer;
$this->pathRegex = $pathRegex;
}

public function isCandidate(Tokens $tokens) : bool
{
return $this->fixer->isCandidate($tokens);
}

public function isRisky() : bool
{
return $this->fixer->isRisky();
}

public function fix(\SplFileInfo $file, Tokens $tokens) : void
{
$this->fixer->fix($file, $tokens);
}

public function getName() : string
{
return (new \ReflectionClass($this))->getShortName().'/'.$this->fixer->getName();
}

public function getPriority() : int
{
return $this->fixer->getPriority();
}

public function supports(\SplFileInfo $file) : bool
{
if (1 !== preg_match($this->pathRegex, $file->getRealPath())) {
return false;
}

return $this->fixer->supports($file);
}

public function configure(array $configuration = null)
{
if ($this->fixer instanceof ConfigurableFixerInterface) {
$this->fixer->configure($configuration);
}
}
};

$header = <<<EOF
This file is part of the rybakit/twig-deferred-extension package.
(c) Eugene Leonovich <gen.work@gmail.com>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

return Config::create()
->setUsingCache(false)
->setRiskyAllowed(true)
->registerCustomFixers([
new FilterableFixer(new NativeConstantInvocationFixer(), '/\bsrc\b/'),
new FilterableFixer(new NativeFunctionInvocationFixer(), '/\bsrc\b/'),
])
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['operators' => ['=' => null, '=>' => null]],
'declare_strict_types' => true,
'native_constant_invocation' => false,
'native_function_invocation' => false,
'FilterableFixer/native_constant_invocation' => true,
'FilterableFixer/native_function_invocation' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
],
'phpdoc_align' => false,
'phpdoc_order' => true,
'phpdoc_to_comment' => false,
'phpdoc_separation' => false, // do not separate @param and @psalm-param
'return_type_declaration' => ['space_before' => 'one'],
'strict_comparison' => true,
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $header,
'location' => 'after_open',
'separate' => 'both',
],
])
;
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

install:
- travis_retry composer install
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
Deferred Twig Extension
Twig Deferred Extension
=======================

[![Build Status](https://travis-ci.org/rybakit/twig-deferred-extension.svg?branch=master)](https://travis-ci.org/rybakit/twig-deferred-extension)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/rybakit/twig-deferred-extension/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/rybakit/twig-deferred-extension/?branch=master)
[![Mentioned in Awesome Twig](https://awesome.re/mentioned-badge.svg)](https://github.com/JulienRAVIA/awesome-twig#extensions)

An extension for Twig that allows to defer block rendering.
An extension for [Twig](https://twig.symfony.com/) that allows to defer block rendering.


## Installation

The recommended way to install the extension is through [Composer](http://getcomposer.org):

```sh
$ composer require phive/twig-extensions-deferred:^1.0 # for Twig 1.x
$ composer require phive/twig-extensions-deferred:^2.0 # for Twig 2.x
```bash
composer require rybakit/twig-deferred-extension
```

> *Note that this extension requires Twig 3 or above. If you need support for older versions of Twig,
> please refer to the [legacy repository](https://github.com/rybakit/twig-extensions-deferred-legacy).*

## Initialization

```php
use Phive\Twig\Extensions\Deferred\DeferredExtension;
use Twig\DeferredExtension\DeferredExtension;
use Twig\Environment;

...
Expand Down Expand Up @@ -53,7 +53,7 @@ use Twig\Environment;
...

$twig = new Environment($loader);
$twig->addGlobal('assets', new ArrayObject());
$twig->addGlobal('assets', new \ArrayObject());
```

Then build the following set of templates:
Expand Down Expand Up @@ -140,4 +140,4 @@ The resulting html will be the following:

## License

Deferred Twig Extension is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.
The library is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.
16 changes: 7 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "phive/twig-extensions-deferred",
"name": "rybakit/twig-deferred-extension",
"description": "An extension for Twig that allows to defer block rendering",
"keywords": ["twig", "extension", "defer", "lazy"],
"homepage": "https://github.com/rybakit/twig-extensions-deferred",
"homepage": "https://github.com/rybakit/twig-deferred-extension",
"type": "library",
"license": "MIT",
"authors": [
Expand All @@ -12,25 +12,23 @@
}
],
"require": {
"twig/twig": "^2.8"
"twig/twig": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "^6.1|^7.0|^8.0|^9.0"
},
"autoload": {
"psr-4": {
"Phive\\Twig\\Extensions\\Deferred\\": "src/"
"Twig\\DeferredExtension\\": "src/"
}
},
"autoload-dev" : {
"psr-4": {
"Phive\\Twig\\Extensions\\Tests\\Deferred\\": "tests/"
"Twig\\DeferredExtension\\Tests\\": "tests/"
}
},
"config": {
"platform": {
"php": "7.0"
},
"preferred-install": {
"*": "dist"
},
Expand Down
17 changes: 14 additions & 3 deletions src/DeferredBlockNode.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<?php

namespace Phive\Twig\Extensions\Deferred;
/**
* This file is part of the rybakit/twig-deferred-extension package.
*
* (c) Eugene Leonovich <gen.work@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Twig\DeferredExtension;

use Twig\Compiler;
use Twig\Node\BlockNode;

class DeferredBlockNode extends BlockNode
final class DeferredBlockNode extends BlockNode
{
public function compile(Compiler $compiler)
public function compile(Compiler $compiler) : void
{
$name = $this->getAttribute('name');

Expand Down
29 changes: 20 additions & 9 deletions src/DeferredExtension.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
<?php

namespace Phive\Twig\Extensions\Deferred;
/**
* This file is part of the rybakit/twig-deferred-extension package.
*
* (c) Eugene Leonovich <gen.work@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Twig\DeferredExtension;

use Twig\Extension\AbstractExtension;
use Twig\Template;

class DeferredExtension extends AbstractExtension
final class DeferredExtension extends AbstractExtension
{
private $blocks = [];

public function getTokenParsers()
public function getTokenParsers() : array
{
return [new DeferredTokenParser()];
}

public function getNodeVisitors()
public function getNodeVisitors() : array
{
return [new DeferredNodeVisitor()];
}

public function defer(Template $template, $blockName)
public function defer(Template $template, string $blockName) : void
{
$templateName = $template->getTemplateName();
$this->blocks[$templateName][] = $blockName;
ob_start();
\ob_start();
}

public function resolve(Template $template, array $context, array $blocks)
public function resolve(Template $template, array $context, array $blocks) : void
{
$templateName = $template->getTemplateName();
if (empty($this->blocks[$templateName])) {
return;
}

while ($blockName = array_pop($this->blocks[$templateName])) {
$buffer = ob_get_clean();
while ($blockName = \array_pop($this->blocks[$templateName])) {
$buffer = \ob_get_clean();

$blocks[$blockName] = [$template, 'block_'.$blockName.'_deferred'];
$template->displayBlock($blockName, $context, $blocks);
Expand Down
17 changes: 14 additions & 3 deletions src/DeferredNode.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<?php

namespace Phive\Twig\Extensions\Deferred;
/**
* This file is part of the rybakit/twig-deferred-extension package.
*
* (c) Eugene Leonovich <gen.work@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Twig\DeferredExtension;

use Twig\Compiler;
use Twig\Node\Node;

class DeferredNode extends Node
final class DeferredNode extends Node
{
public function compile(Compiler $compiler)
public function compile(Compiler $compiler) : void
{
$compiler
->write("\$this->env->getExtension('".DeferredExtension::class."')->resolve(\$this, \$context, \$blocks);\n")
Expand Down
Loading

0 comments on commit c76b00b

Please sign in to comment.