[1.x] Allow using namespaced aliases as extension names

This commit is contained in:
Nicolas Grekas
2017-05-30 11:57:23 +02:00
parent 460712d35e
commit c9a2f3db8b
31 changed files with 116 additions and 14 deletions
+3
View File
@@ -9,6 +9,9 @@
* file that was distributed with this source code. * 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. * Twig_BaseNodeVisitor can be used to make node visitors compatible with Twig 1.x and 2.x.
* *
+2
View File
@@ -10,6 +10,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Node.php';
/** /**
* Compiles a node to PHP code. * Compiles a node to PHP code.
* *
+19 -2
View File
@@ -758,7 +758,7 @@ class Twig_Environment
public function setLoader(Twig_LoaderInterface $loader) 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); @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) public function hasExtension($class)
{ {
$class = ltrim($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 (isset($this->extensions[$class])) {
if ($class !== get_class($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); @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) public function getExtension($class)
{ {
$class = ltrim($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 (isset($this->extensions[$class])) {
if ($class !== get_class($this->extensions[$class])) { if ($class !== get_class($this->extensions[$class])) {
@@ -938,6 +949,12 @@ class Twig_Environment
} }
$class = ltrim($name, '\\'); $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 (isset($this->extensions[$class])) {
if ($class !== get_class($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); @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);
+2
View File
@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Source.php';
/** /**
* Twig base exception. * Twig base exception.
* *
+2
View File
@@ -27,3 +27,5 @@ interface Twig_ExistsLoaderInterface
*/ */
public function exists($name); public function exists($name);
} }
class_alias('Twig_ExistsLoaderInterface', 'Twig\Loader\ExistsLoaderInterface', false);
+3
View File
@@ -8,6 +8,9 @@
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Environment.php';
abstract class Twig_Extension implements Twig_ExtensionInterface abstract class Twig_Extension implements Twig_ExtensionInterface
{ {
/** /**
+2
View File
@@ -20,3 +20,5 @@
interface Twig_Extension_GlobalsInterface interface Twig_Extension_GlobalsInterface
{ {
} }
class_alias('Twig_Extension_GlobalsInterface', 'Twig\Extension\GlobalsInterface', false);
@@ -20,3 +20,5 @@
interface Twig_Extension_InitRuntimeInterface interface Twig_Extension_InitRuntimeInterface
{ {
} }
class_alias('Twig_Extension_InitRuntimeInterface', 'Twig\Extension\InitRuntimeInterface', false);
+2
View File
@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/../Profiler/Profile.php';
class Twig_Extension_Profiler extends Twig_Extension class Twig_Extension_Profiler extends Twig_Extension
{ {
private $actives = array(); private $actives = array();
+2
View File
@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Environment.php';
/** /**
* Interface implemented by extension classes. * Interface implemented by extension classes.
* *
+2
View File
@@ -10,6 +10,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Compiler.php';
/** /**
* Represents a node in the AST. * Represents a node in the AST.
* *
+3
View File
@@ -9,6 +9,9 @@
* file that was distributed with this source code. * 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. * Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
* *
+3
View File
@@ -10,6 +10,9 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Node.php';
require_once __DIR__.'/TokenStream.php';
/** /**
* Default parser implementation. * Default parser implementation.
* *
@@ -29,3 +29,5 @@ interface Twig_SourceContextLoaderInterface
*/ */
public function getSourceContext($name); public function getSourceContext($name);
} }
class_alias('Twig_SourceContextLoaderInterface', 'Twig\Loader\SourceContextLoaderInterface', false);
+6 -2
View File
@@ -1,7 +1,5 @@
<?php <?php
use PHPUnit\Framework\TestCase;
/* /*
* This file is part of Twig. * This file is part of Twig.
* *
@@ -10,6 +8,12 @@ use PHPUnit\Framework\TestCase;
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/../Environment.php';
require_once __DIR__.'/../Node.php';
use PHPUnit\Framework\TestCase;
abstract class Twig_Test_NodeTestCase extends TestCase abstract class Twig_Test_NodeTestCase extends TestCase
{ {
abstract public function getTests(); abstract public function getTests();
+3
View File
@@ -9,6 +9,9 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__.'/Parser.php';
require_once __DIR__.'/Token.php';
/** /**
* Interface implemented by token parsers. * Interface implemented by token parsers.
* *
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\Cache;
require __DIR__.'/../../lib/Twig/CacheInterface.php'; require __DIR__.'/../../lib/Twig/CacheInterface.php';
if (\false) { if (\false) {
class CacheInterface extends \Twig_CacheInterface interface CacheInterface extends \Twig_CacheInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\Extension;
require __DIR__.'/../../lib/Twig/ExtensionInterface.php'; require __DIR__.'/../../lib/Twig/ExtensionInterface.php';
if (\false) { if (\false) {
class ExtensionInterface extends \Twig_ExtensionInterface interface ExtensionInterface extends \Twig_ExtensionInterface
{ {
} }
} }
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Twig\Extension;
require __DIR__.'/../../lib/Twig/Extension/GlobalsInterface.php';
if (\false) {
interface GlobalsInterface extends \Twig_Extension_ExtensionInterface
{
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Twig\Extension;
require __DIR__.'/../../lib/Twig/Extension/InitRuntimeInterface.php';
if (\false) {
interface InitRuntimeInterface extends \Twig_Extension_InitRuntimeInterface
{
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Twig\Loader;
require __DIR__.'/../../lib/Twig/ExistsLoaderInterface.php';
if (\false) {
interface ExistsLoaderInterface extends \Twig_ExistsLoaderInterface
{
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\Loader;
require __DIR__.'/../../lib/Twig/LoaderInterface.php'; require __DIR__.'/../../lib/Twig/LoaderInterface.php';
if (\false) { if (\false) {
class LoaderInterface extends \Twig_LoaderInterface interface LoaderInterface extends \Twig_LoaderInterface
{ {
} }
} }
@@ -0,0 +1,11 @@
<?php
namespace Twig\Loader;
require __DIR__.'/../../lib/Twig/SourceContextLoaderInterface.php';
if (\false) {
interface SourceContextLoaderInterface extends \Twig_SourceContextLoaderInterface
{
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\Node;
require __DIR__.'/../../lib/Twig/NodeCaptureInterface.php'; require __DIR__.'/../../lib/Twig/NodeCaptureInterface.php';
if (\false) { if (\false) {
class NodeCaptureInterface extends \Twig_NodeCaptureInterface interface NodeCaptureInterface extends \Twig_NodeCaptureInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\Node;
require __DIR__.'/../../lib/Twig/NodeOutputInterface.php'; require __DIR__.'/../../lib/Twig/NodeOutputInterface.php';
if (\false) { if (\false) {
class NodeOutputInterface extends \Twig_NodeOutputInterface interface NodeOutputInterface extends \Twig_NodeOutputInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\NodeVisitor;
require __DIR__.'/../../lib/Twig/NodeVisitorInterface.php'; require __DIR__.'/../../lib/Twig/NodeVisitorInterface.php';
if (\false) { if (\false) {
class NodeVisitorInterface extends \Twig_NodeVisitorInterface interface NodeVisitorInterface extends \Twig_NodeVisitorInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\RuntimeLoader;
require __DIR__.'/../../lib/Twig/RuntimeLoaderInterface.php'; require __DIR__.'/../../lib/Twig/RuntimeLoaderInterface.php';
if (\false) { if (\false) {
class RuntimeLoaderInterface extends \Twig_RuntimeLoaderInterface interface RuntimeLoaderInterface extends \Twig_RuntimeLoaderInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\Sandbox;
require __DIR__.'/../../lib/Twig/Sandbox/SecurityPolicyInterface.php'; require __DIR__.'/../../lib/Twig/Sandbox/SecurityPolicyInterface.php';
if (\false) { if (\false) {
class SecurityPolicyInterface extends \Twig_Sandbox_SecurityPolicyInterface interface SecurityPolicyInterface extends \Twig_Sandbox_SecurityPolicyInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig\TokenParser;
require __DIR__.'/../../lib/Twig/TokenParserInterface.php'; require __DIR__.'/../../lib/Twig/TokenParserInterface.php';
if (\false) { if (\false) {
class TokenParserInterface extends \Twig_TokenParserInterface interface TokenParserInterface extends \Twig_TokenParserInterface
{ {
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Twig;
require __DIR__.'/../lib/Twig/Environment.php'; require __DIR__.'/../lib/Twig/Environment.php';
if (\false) { if (\false) {
class Environment extends \Twig_Environment class Twig extends \Twig_Environment
{ {
} }
} }
+4
View File
@@ -311,6 +311,9 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$this->assertSame($ext, $twig->getExtension('Twig_Tests_EnvironmentTest_Extension')); $this->assertSame($ext, $twig->getExtension('Twig_Tests_EnvironmentTest_Extension'));
$this->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() 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 class Twig_Tests_EnvironmentTest_Extension_WithDeprecatedName extends Twig_Extension
{ {