-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
I would not name it Btw, to avoid breaking the goal of the loader, a map of 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 |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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
4edc031
to
02674bf
Compare
return $runtime(); | ||
} | ||
|
||
return null; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
a32a2d6
to
34c9680
Compare
3bef5b7
to
a369587
Compare
Changed to |
a369587
to
5b77e44
Compare
5b77e44
to
91c8d59
Compare
|
||
/** | ||
* {@inheritdoc} | ||
*/ |
There was a problem hiding this comment.
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.
Thank you @chalasr. |
… (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
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:
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.