From 9774f4f1a43ae7c1dfd952430b1826217ce72f0b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 23 Oct 2015 08:37:46 +0200 Subject: [PATCH] deprecated Twig_ExtensionInterface::initRuntime() --- CHANGELOG | 1 + doc/advanced.rst | 2 + doc/deprecated.rst | 7 +++ lib/Twig/Environment.php | 10 ++++ lib/Twig/Extension.php | 2 + lib/Twig/Extension/InitRuntimeInterface.php | 22 ++++++++ lib/Twig/ExtensionInterface.php | 2 + test/Twig/Tests/EnvironmentTest.php | 58 +++++++++++++++++++++ 8 files changed, 104 insertions(+) create mode 100644 lib/Twig/Extension/InitRuntimeInterface.php diff --git a/CHANGELOG b/CHANGELOG index 5047230601..e3d3e1244a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.23.0 (2015-XX-XX) + * deprecated Twig_ExtensionInterface::initRuntime() (added Twig_Extension_InitRuntimeInterface for BC) * deprecated Twig_Environment::computeAlternatives() * 1.22.3 (2015-10-13) diff --git a/doc/advanced.rst b/doc/advanced.rst index 1ecb34e8a1..65dd2cc866 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -553,6 +553,8 @@ An extension is a class that implements the following interface:: * This is where you can load some file that contains filter functions for instance. * * @param Twig_Environment $environment The current Twig_Environment instance + * + * @deprecated since 1.23 (to be removed in 2.0) */ function initRuntime(Twig_Environment $environment); diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 23b22ded69..3d9c167497 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -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.23, the ``Twig_ExtensionInterface::initRuntime()`` method is + deprecated. You have two options to avoid the deprecation notice: if you + implement this method to store the environment for your custom filters, + functions, or tests, use the ``needs_environment`` option instead; if you + have more complex needs, explicitly implement + ``Twig_Extension_InitRuntimeInterface`` (not recommended). + PEAR ---- diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 9f8ed00e30..6c26d38d1f 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -725,12 +725,22 @@ public function getCharset() /** * Initializes the runtime environment. + * + * @deprecated since 1.23 (to be removed in 2.0) */ public function initRuntime() { $this->runtimeInitialized = true; foreach ($this->getExtensions() as $extension) { + if (!$extension instanceof Twig_Extension_InitRuntimeInterface) { + $m = new ReflectionMethod($extension, 'initRuntime'); + + if ('Twig_Extension' !== $m->getDeclaringClass()->getName()) { + @trigger_error(sprintf('Defining the initRuntime() method in an extension is deprecated. Use the `needs_environment` option to get the Twig_Environment instance in filters, functions, or tests; or explicitly implement Twig_Extension_InitRuntimeInterface if needed (not recommended).', $name), E_USER_DEPRECATED); + } + } + $extension->initRuntime($this); } } diff --git a/lib/Twig/Extension.php b/lib/Twig/Extension.php index 7a3c859852..b7ab674880 100644 --- a/lib/Twig/Extension.php +++ b/lib/Twig/Extension.php @@ -12,6 +12,8 @@ abstract class Twig_Extension implements Twig_ExtensionInterface { /** * {@inheritdoc} + * + * @deprecated since 1.23 (to be removed in 2.0) */ public function initRuntime(Twig_Environment $environment) { diff --git a/lib/Twig/Extension/InitRuntimeInterface.php b/lib/Twig/Extension/InitRuntimeInterface.php new file mode 100644 index 0000000000..f16555e365 --- /dev/null +++ b/lib/Twig/Extension/InitRuntimeInterface.php @@ -0,0 +1,22 @@ + + */ +interface Twig_Extension_InitRuntimeInterface +{ +} diff --git a/lib/Twig/ExtensionInterface.php b/lib/Twig/ExtensionInterface.php index 92abf7db2e..d7ebed3290 100644 --- a/lib/Twig/ExtensionInterface.php +++ b/lib/Twig/ExtensionInterface.php @@ -22,6 +22,8 @@ interface Twig_ExtensionInterface * This is where you can load some file that contains filter functions for instance. * * @param Twig_Environment $environment The current Twig_Environment instance + * + * @deprecated since 1.23 (to be removed in 2.0) */ public function initRuntime(Twig_Environment $environment); diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 51461eb441..360f24905f 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -308,6 +308,40 @@ public function testAddMockExtension() $this->assertTrue($twig->isTemplateFresh('page', time())); } + public function testInitRuntimeWithAnExtensionUsingInitRuntimeNoDeprecation() + { + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); + $twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithoutDeprecationInitRuntime()); + + $twig->initRuntime(); + } + + /** + * @requires PHP 5.3 + */ + public function testInitRuntimeWithAnExtensionUsingInitRuntimeDeprecation() + { + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); + $twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime()); + + $this->deprecations = array(); + set_error_handler(array($this, 'handleError')); + + $twig->initRuntime(); + + $this->assertCount(1, $this->deprecations); + $this->assertContains('Defining the initRuntime() method in an extension is deprecated.', $this->deprecations[0]); + + restore_error_handler(); + } + + public function handleError($type, $msg) + { + if (E_USER_DEPRECATED === $type) { + $this->deprecations[] = $msg; + } + } + protected function getMockLoader($templateName, $templateContent) { $loader = $this->getMock('Twig_LoaderInterface'); @@ -411,3 +445,27 @@ public function getPriority() return 0; } } + +class Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime extends Twig_Extension +{ + public function initRuntime(Twig_Environment $env) + { + } + + public function getName() + { + return 'with_deprecation'; + } +} + +class Twig_Tests_EnvironmentTest_ExtensionWithoutDeprecationInitRuntime extends Twig_Extension implements Twig_Extension_InitRuntimeInterface +{ + public function initRuntime(Twig_Environment $env) + { + } + + public function getName() + { + return 'without_deprecation'; + } +}