Thanks to visit codestin.com
Credit goes to github.com

Skip to content

deprecated Twig_ExtensionInterface::initRuntime() #1886

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
Oct 28, 2015

Conversation

fabpot
Copy link
Contributor

@fabpot fabpot commented Oct 23, 2015

I'm working on splitting Twig extensions into 2 different phases: compilation and runtime. The goal is to avoid having to load the runtime environment of an extension when compiling templates. That also opens the way to be able to lazy-load Twig extensions.

While working on the split, I realized that the initRuntime() method on Twig_ExtensionInterface is not needed anymore. It was added at a time needs_environment did not exist and was a way to keep the environment around for custom filters/tests/functions. But nowadays, that's not needed anymore. I did a quick search on Github, and most of the implementation I found just store the environment in a local property, which is not needed anymore. So, I propose to deprecate it in 1.x and remove it in 2.0.

@fabpot
Copy link
Contributor Author

fabpot commented Oct 23, 2015

And of course, I've just had a look at Symfony, and we are using this method in the Twig form extension. I need to see why and how to fix that.

@fabpot
Copy link
Contributor Author

fabpot commented Oct 23, 2015

Ok, it's probably to avoid a circular reference, we can probably find another way.

$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$twig->initRuntime();
$twig->loadTemplate('index');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is more semantically correct. Calling initRuntime is just a side-effect of loading a template.

@stof
Copy link
Member

stof commented Oct 23, 2015

@fabpot I suggest finding the solution in Symfony before merging this. but otherwise, 👍

@fabpot fabpot force-pushed the initruntime-deprecation branch from da585e5 to a90ce0e Compare October 24, 2015 20:16
@fabpot
Copy link
Contributor Author

fabpot commented Oct 24, 2015

To keep BC, I've added a new interface: Twig_Extension_InitRuntimeInterface which can be implemented if really needed (see changes for more information).

@fabpot fabpot force-pushed the initruntime-deprecation branch from a90ce0e to 495cbaf Compare October 24, 2015 20:23
@fabpot fabpot force-pushed the initruntime-deprecation branch 3 times, most recently from f4e4fbe to 5791584 Compare October 25, 2015 07:17
@@ -26,6 +26,13 @@ Extensions
* As of Twig 1.x, the ability to remove an extension is deprecated and the
``Twig_Environment::removeExtension()`` method will be removed in 2.0.

* As of Twig 1.x, the ``Twig_ExtensionInterface::initRuntime()`` is deprecated.
Copy link
Member

Choose a reason for hiding this comment

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

Please say As of Twig 1.23. As of 1.x does not make sense as 1.x is not a version

Copy link
Contributor

Choose a reason for hiding this comment

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

[...] the Twig_ExtensionInterface::initRuntime() method is [...]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed typo
@stof we are not using that anywhere, that should be done in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

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

Well, using the exact version for the new one already makes it simpler as we know which version it is.

@fabpot fabpot force-pushed the initruntime-deprecation branch from 5791584 to 5b8eb1b Compare October 25, 2015 08:12
*
* @param Twig_Environment $environment The current Twig_Environment instance
*/
public function initRuntime(Twig_Environment $environment);
Copy link
Member

Choose a reason for hiding this comment

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

This interface can be implemented at the same time than Twig_ExtensionInterface only in PHP 5.3.9+, as it defines the same method (remember our issues with the Validator component in Symfony 2.5).
So this makes it unsuited for the Twig 1.x lowest bound (and for usage in Symfony 2.3): https://3v4l.org/PQSTH

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, what about just making this interface a simple marker?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding a test covering the usage of this interface in the Twig testsuite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@fabpot fabpot force-pushed the initruntime-deprecation branch 2 times, most recently from f4ebda7 to d165cfc Compare October 25, 2015 08:39
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
Copy link
Member

Choose a reason for hiding this comment

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

you should call the PHPUnit error handler for any other error, to avoid loosing them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead, I've added an assertion on the deprecation message.

@fabpot fabpot force-pushed the initruntime-deprecation branch 6 times, most recently from 785e8c5 to 9bb1134 Compare October 25, 2015 14:37
@fabpot
Copy link
Contributor Author

fabpot commented Oct 25, 2015

Not sure why it does not work on 5.2.

@fabpot fabpot force-pushed the initruntime-deprecation branch from 9bb1134 to 9774f4f Compare October 25, 2015 14:49
@fabpot fabpot merged commit 9774f4f into twigphp:1.x Oct 28, 2015
fabpot added a commit that referenced this pull request Oct 28, 2015
This PR was merged into the 1.x branch.

Discussion
----------

deprecated Twig_ExtensionInterface::initRuntime()

I'm working on splitting Twig extensions into 2 different phases: compilation and runtime. The goal is to avoid having to load the runtime environment of an extension when compiling templates. That also opens the way to be able to lazy-load Twig extensions.

While working on the split, I realized that the `initRuntime()` method on `Twig_ExtensionInterface` is not needed anymore. It was added at a time `needs_environment` did not exist and was a way to keep the environment around for custom filters/tests/functions. But nowadays, that's not needed anymore. I did a quick search on Github, and most of the implementation I found just store the environment in a local property, which is not needed anymore. So, I propose to deprecate it in 1.x and remove it in 2.0.

Commits
-------

9774f4f deprecated Twig_ExtensionInterface::initRuntime()
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.

3 participants