From c9a2f3db8b04d8dc92c825f1bfd05ee57a89fd7f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 30 May 2017 11:57:23 +0200 Subject: [PATCH] [1.x] Allow using namespaced aliases as extension names --- lib/Twig/BaseNodeVisitor.php | 3 +++ lib/Twig/Compiler.php | 2 ++ lib/Twig/Environment.php | 21 ++++++++++++++++++-- lib/Twig/Error.php | 2 ++ lib/Twig/ExistsLoaderInterface.php | 2 ++ lib/Twig/Extension.php | 3 +++ lib/Twig/Extension/GlobalsInterface.php | 2 ++ lib/Twig/Extension/InitRuntimeInterface.php | 2 ++ lib/Twig/Extension/Profiler.php | 2 ++ lib/Twig/ExtensionInterface.php | 2 ++ lib/Twig/Node.php | 2 ++ lib/Twig/NodeVisitorInterface.php | 3 +++ lib/Twig/Parser.php | 3 +++ lib/Twig/SourceContextLoaderInterface.php | 2 ++ lib/Twig/Test/NodeTestCase.php | 8 ++++++-- lib/Twig/TokenParserInterface.php | 3 +++ src/Cache/CacheInterface.php | 2 +- src/Extension/ExtensionInterface.php | 2 +- src/Extension/GlobalsInterface.php | 11 ++++++++++ src/Extension/InitRuntimeInterface.php | 11 ++++++++++ src/Loader/ExistsLoaderInterface.php | 11 ++++++++++ src/Loader/LoaderInterface.php | 2 +- src/Loader/SourceContextLoaderInterface.php | 11 ++++++++++ src/Node/NodeCaptureInterface.php | 2 +- src/Node/NodeOutputInterface.php | 2 +- src/NodeVisitor/NodeVisitorInterface.php | 2 +- src/RuntimeLoader/RuntimeLoaderInterface.php | 2 +- src/Sandbox/SecurityPolicyInterface.php | 2 +- src/TokenParser/TokenParserInterface.php | 2 +- src/{Environment.php => Twig.php} | 2 +- test/Twig/Tests/EnvironmentTest.php | 4 ++++ 31 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 src/Extension/GlobalsInterface.php create mode 100644 src/Extension/InitRuntimeInterface.php create mode 100644 src/Loader/ExistsLoaderInterface.php create mode 100644 src/Loader/SourceContextLoaderInterface.php rename src/{Environment.php => Twig.php} (67%) diff --git a/lib/Twig/BaseNodeVisitor.php b/lib/Twig/BaseNodeVisitor.php index 30cd2beb5..fac1ec819 100644 --- a/lib/Twig/BaseNodeVisitor.php +++ b/lib/Twig/BaseNodeVisitor.php @@ -9,6 +9,9 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Environment.php'; +require_once __DIR__.'/Node.php'; + /** * Twig_BaseNodeVisitor can be used to make node visitors compatible with Twig 1.x and 2.x. * diff --git a/lib/Twig/Compiler.php b/lib/Twig/Compiler.php index 2da8c4bfd..37584f2b0 100644 --- a/lib/Twig/Compiler.php +++ b/lib/Twig/Compiler.php @@ -10,6 +10,8 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Node.php'; + /** * Compiles a node to PHP code. * diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index a41e5cb8d..c04127a7d 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -758,7 +758,7 @@ class Twig_Environment public function setLoader(Twig_LoaderInterface $loader) { - if (!$loader instanceof Twig_SourceContextLoaderInterface && 0 !== strpos(get_class($loader), 'Mock_Twig_LoaderInterface')) { + if (!$loader instanceof Twig_SourceContextLoaderInterface && 0 !== strpos(get_class($loader), 'Mock_')) { @trigger_error(sprintf('Twig loader "%s" should implement Twig_SourceContextLoaderInterface since version 1.27.', get_class($loader)), E_USER_DEPRECATED); } @@ -831,6 +831,12 @@ class Twig_Environment public function hasExtension($class) { $class = ltrim($class, '\\'); + if (!isset($this->extensionsByClass[$class]) && class_exists($class, false)) { + // For BC/FC with namespaced aliases + $class = new ReflectionClass($class); + $class = $class->name; + } + if (isset($this->extensions[$class])) { if ($class !== get_class($this->extensions[$class])) { @trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED); @@ -860,6 +866,11 @@ class Twig_Environment public function getExtension($class) { $class = ltrim($class, '\\'); + if (!isset($this->extensionsByClass[$class]) && class_exists($class, false)) { + // For BC/FC with namespaced aliases + $class = new ReflectionClass($class); + $class = $class->name; + } if (isset($this->extensions[$class])) { if ($class !== get_class($this->extensions[$class])) { @@ -938,6 +949,12 @@ class Twig_Environment } $class = ltrim($name, '\\'); + if (!isset($this->extensionsByClass[$class]) && class_exists($class, false)) { + // For BC/FC with namespaced aliases + $class = new ReflectionClass($class); + $class = $class->name; + } + if (isset($this->extensions[$class])) { if ($class !== get_class($this->extensions[$class])) { @trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED); @@ -1560,4 +1577,4 @@ class Twig_Environment } } -class_alias('Twig_Environment', 'Twig\Environment', false); +class_alias('Twig_Environment', 'Twig\Twig', false); diff --git a/lib/Twig/Error.php b/lib/Twig/Error.php index 551304588..763a47474 100644 --- a/lib/Twig/Error.php +++ b/lib/Twig/Error.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Source.php'; + /** * Twig base exception. * diff --git a/lib/Twig/ExistsLoaderInterface.php b/lib/Twig/ExistsLoaderInterface.php index 553fb4e5e..968cb21a4 100644 --- a/lib/Twig/ExistsLoaderInterface.php +++ b/lib/Twig/ExistsLoaderInterface.php @@ -27,3 +27,5 @@ interface Twig_ExistsLoaderInterface */ public function exists($name); } + +class_alias('Twig_ExistsLoaderInterface', 'Twig\Loader\ExistsLoaderInterface', false); diff --git a/lib/Twig/Extension.php b/lib/Twig/Extension.php index f1c01b7d1..20a794508 100644 --- a/lib/Twig/Extension.php +++ b/lib/Twig/Extension.php @@ -8,6 +8,9 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + +require_once __DIR__.'/Environment.php'; + abstract class Twig_Extension implements Twig_ExtensionInterface { /** diff --git a/lib/Twig/Extension/GlobalsInterface.php b/lib/Twig/Extension/GlobalsInterface.php index 5370b8e2a..922cd2c93 100644 --- a/lib/Twig/Extension/GlobalsInterface.php +++ b/lib/Twig/Extension/GlobalsInterface.php @@ -20,3 +20,5 @@ interface Twig_Extension_GlobalsInterface { } + +class_alias('Twig_Extension_GlobalsInterface', 'Twig\Extension\GlobalsInterface', false); diff --git a/lib/Twig/Extension/InitRuntimeInterface.php b/lib/Twig/Extension/InitRuntimeInterface.php index 7a075822f..1549862f4 100644 --- a/lib/Twig/Extension/InitRuntimeInterface.php +++ b/lib/Twig/Extension/InitRuntimeInterface.php @@ -20,3 +20,5 @@ interface Twig_Extension_InitRuntimeInterface { } + +class_alias('Twig_Extension_InitRuntimeInterface', 'Twig\Extension\InitRuntimeInterface', false); diff --git a/lib/Twig/Extension/Profiler.php b/lib/Twig/Extension/Profiler.php index b81473c59..14aaf4eee 100644 --- a/lib/Twig/Extension/Profiler.php +++ b/lib/Twig/Extension/Profiler.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/../Profiler/Profile.php'; + class Twig_Extension_Profiler extends Twig_Extension { private $actives = array(); diff --git a/lib/Twig/ExtensionInterface.php b/lib/Twig/ExtensionInterface.php index 81e11b494..c029a13d2 100644 --- a/lib/Twig/ExtensionInterface.php +++ b/lib/Twig/ExtensionInterface.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Environment.php'; + /** * Interface implemented by extension classes. * diff --git a/lib/Twig/Node.php b/lib/Twig/Node.php index d1f59e0b0..9251b0839 100644 --- a/lib/Twig/Node.php +++ b/lib/Twig/Node.php @@ -10,6 +10,8 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Compiler.php'; + /** * Represents a node in the AST. * diff --git a/lib/Twig/NodeVisitorInterface.php b/lib/Twig/NodeVisitorInterface.php index 0172dd90b..9879b5e5a 100644 --- a/lib/Twig/NodeVisitorInterface.php +++ b/lib/Twig/NodeVisitorInterface.php @@ -9,6 +9,9 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Environment.php'; +require_once __DIR__.'/Node.php'; + /** * Twig_NodeVisitorInterface is the interface the all node visitor classes must implement. * diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 27018e28b..d8bb4d2f0 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -10,6 +10,9 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Node.php'; +require_once __DIR__.'/TokenStream.php'; + /** * Default parser implementation. * diff --git a/lib/Twig/SourceContextLoaderInterface.php b/lib/Twig/SourceContextLoaderInterface.php index acf21e378..a6e8c4255 100644 --- a/lib/Twig/SourceContextLoaderInterface.php +++ b/lib/Twig/SourceContextLoaderInterface.php @@ -29,3 +29,5 @@ interface Twig_SourceContextLoaderInterface */ public function getSourceContext($name); } + +class_alias('Twig_SourceContextLoaderInterface', 'Twig\Loader\SourceContextLoaderInterface', false); diff --git a/lib/Twig/Test/NodeTestCase.php b/lib/Twig/Test/NodeTestCase.php index 2ef14de5c..35d0686bd 100644 --- a/lib/Twig/Test/NodeTestCase.php +++ b/lib/Twig/Test/NodeTestCase.php @@ -1,7 +1,5 @@ assertSame($ext, $twig->getExtension('Twig_Tests_EnvironmentTest_Extension')); $this->assertSame($ext, $twig->getExtension('\Twig_Tests_EnvironmentTest_Extension')); + + $this->assertTrue($twig->hasExtension('Twig\Tests\EnvironmentTest\Extension')); + $this->assertSame($ext, $twig->getExtension('Twig\Tests\EnvironmentTest\Extension')); } public function testAddExtension() @@ -557,6 +560,7 @@ class Twig_Tests_EnvironmentTest_Extension extends Twig_Extension implements Twi ); } } +class_alias('Twig_Tests_EnvironmentTest_Extension', 'Twig\Tests\EnvironmentTest\Extension', false); class Twig_Tests_EnvironmentTest_Extension_WithDeprecatedName extends Twig_Extension {