8000 Add a simple Twig_RuntimeLoaderInterface implementation by chalasr · Pull Request #2311 · twigphp/Twig · GitHub
[go: up one dir, main page]

Skip to content

Add a simple Twig_RuntimeLoaderInterface implementation #2311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2016

Conversation

chalasr
Copy link
Contributor
@chalasr chalasr commented Dec 22, 2016

Next to symfony/symfony#21023

This is related to the BC break reported in symfony/symfony#21008 which has been introduced in symfony/symfony#20093 when decoupling extensions from definitions.

What I propose here is to ease the upgrade to symfony 3.2+ by adding a simple Twig_RuntimeLoaderInterface implementation here, useful only when using the twig-bridge outside of the symfony fullstack framework (with the Form component for instance).

Upgrading would be as simple as:

$twig = new Twig_Environment(...);
$rendererEngine = new TwigRendererEngine(array('form_div_layout.html.twig'), $twig);
- $twig->addExtension(new FormExtension(new TwigRenderer($rendererEngine, $csrfTokenManager)));
+ $twig->addExtension(new FormExtension());
+ $twig->addRuntimeLoader(new Twig_RuntimeLoader(array(TwigRenderer::class => new TwigRenderer($rendererEngine, $csrfTokenManager)));

Instead of having to write this runtime loader yourself.
Please see symfony/symfony#21008 for details and a concrete example of how this could help.

@stof
Copy link
Member
stof commented Dec 22, 2016

I would not name it Twig_RuntimeLoader, as this gives it a very special name as a runtime loader, which it is a very specific use case instead (and not the one you should use most of the time, as it defeats all performance goals of the system). The name should describe what makes this implementation special.

Btw, to avoid breaking the goal of the loader, a map of class => factory callable would make more sense, allowing to keep the runtimes lazy-loaded.

It would then end up as this:

$twig->addRuntimeLoader(new Twig_RuntimeLoader(array(
    // even better would be to change the way the engine and the csrf token manager are
    // instantiated to keep them lazy as well, but it depends on how they are used elsewhere
    // in the app
    TwigRenderer::class => function() use($rendererEngine, $csrfTokenManager) {
       return new TwigRenderer($rendererEngine, $csrfTokenManager);
    },
));

/**
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class Twig_RuntimeLoader implements \Twig_RuntimeLoaderInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the \ must be removed, as Twig 1.x still supports PHP 5.2

return $this->mapping[$class];
}

throw new \InvalidArgumentException(sprintf('Class "%s" is not mapped as Twig runtime.', $class));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must return null when it does not support the class according to the interface

return $runtime();
}

return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the return statement can then simply be omitted to match the Twig code style rules.

* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Tests_RuntimeLoaderTest extends PHPUnit_Framework_TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing blank line before the class declaration

@chalasr chalasr force-pushed the runtime-loader branch 2 times, most recently from a32a2d6 to 34c9680 Compare December 22, 2016 16:23
@chalasr
Copy link
Contributor Author
chalasr commented Dec 22, 2016

Fixed line notes and applied @stof suggestion about mapping callables instead of instances.

For the naming of this class, I agree that the current one is unclear and not adapted. Right now I fail at finding something appropriated so if you or @xabbuh have something in mind, please tell me

@chalasr chalasr force-pushed the runtime-loader branch 9 times, most recently from 3bef5b7 to a369587 Compare December 22, 2016 23:50
@chalasr
Copy link
Contributor Author
chalasr commented Dec 22, 2016

Changed to Twig_FactoryRuntimeLoader


/**
* {@inheritdoc}
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The phpdoc can be removed.

@fabpot
Copy link
Contributor
fabpot commented Dec 23, 2016

Thank you @chalasr.

@fabpot fabpot merged commit 91c8d59 into twigphp:1.x Dec 23, 2016
fabpot added a commit that referenced this pull request Dec 23, 2016
… (chalasr)

This PR was merged into the 1.x branch.

Discussion
----------

Add a simple Twig_RuntimeLoaderInterface implementation

Next to symfony/symfony#21023

This is related to the BC break reported in symfony/symfony#21008 which has been introduced in symfony/symfony#20093 when decoupling extensions from definitions.

What I propose here is to ease the upgrade to symfony 3.2+ by adding a simple `Twig_RuntimeLoaderInterface` implementation here, useful only when using the twig-bridge outside of the symfony fullstack framework (with the Form component for instance).

Upgrading would be as simple as:

```diff
$twig = new Twig_Environment(...);
$rendererEngine = new TwigRendererEngine(array('form_div_layout.html.twig'), $twig);
- $twig->addExtension(new FormExtension(new TwigRenderer($rendererEngine, $csrfTokenManager)));
+ $twig->addExtension(new FormExtension());
+ $twig->addRuntimeLoader(new Twig_RuntimeLoader(array(TwigRenderer::class => new TwigRenderer($rendererEngine, $csrfTokenManager)));
```

Instead of having to write this runtime loader yourself.
Please see symfony/symfony#21008 for details and a concrete example of how this could help.

Commits
-------

91c8d59 Add a Twig_FactoryRuntimeLoader
@chalasr chalasr deleted the runtime-loader branch December 23, 2016 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0