From d98eb7b5bb869dd27088fc5b2eee35b648ba738d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 1 Oct 2016 13:54:25 -0700 Subject: [PATCH 1/2] [Config] added ClassExistenceResource --- .../Resource/ClassExistenceResource.php | 75 +++++++++++++++++++ .../Resource/ClassExistenceResourceTest.php | 54 +++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/Symfony/Component/Config/Resource/ClassExistenceResource.php create mode 100644 src/Symfony/Component/Config/Tests/Resource/ClassExistenceResourceTest.php diff --git a/src/Symfony/Component/Config/Resource/ClassExistenceResource.php b/src/Symfony/Component/Config/Resource/ClassExistenceResource.php new file mode 100644 index 0000000000000..5f683a4f8a5e0 --- /dev/null +++ b/src/Symfony/Component/Config/Resource/ClassExistenceResource.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Resource; + +/** + * ClassExistenceResource represents a class availability. + * Freshness is only evaluated against resource availability. + * + * The resource must be a fully-qualified class name. + * + * @author Fabien Potencier + */ +class ClassExistenceResource implements SelfCheckingResourceInterface, \Serializable +{ + private $resource; + private $exists; + + /** + * @param string $resource The fully-qualified class name + */ + public function __construct($resource) + { + $this->resource = $resource; + $this->exists = class_exists($resource); + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return $this->resource; + } + + /** + * @return string The file path to the resource + */ + public function getResource() + { + return $this->resource; + } + + /** + * {@inheritdoc} + */ + public function isFresh($timestamp) + { + return class_exists($this->resource) === $this->exists; + } + + /** + * {@inheritdoc} + */ + public function serialize() + { + return serialize(array($this->resource, $this->exists)); + } + + /** + * {@inheritdoc} + */ + public function unserialize($serialized) + { + list($this->resource, $this->exists) = unserialize($serialized); + } +} diff --git a/src/Symfony/Component/Config/Tests/Resource/ClassExistenceResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/ClassExistenceResourceTest.php new file mode 100644 index 0000000000000..20d5b3308d3fc --- /dev/null +++ b/src/Symfony/Component/Config/Tests/Resource/ClassExistenceResourceTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Tests\Resource; + +use Symfony\Component\Config\Resource\ClassExistenceResource; + +class ClassExistenceResourceTest extends \PHPUnit_Framework_TestCase +{ + public function testToString() + { + $res = new ClassExistenceResource('BarClass'); + $this->assertSame('BarClass', (string) $res); + } + + public function testGetResource() + { + $res = new ClassExistenceResource('BarClass'); + $this->assertSame('BarClass', $res->getResource()); + } + + public function testIsFreshWhenClassDoesNotExist() + { + $res = new ClassExistenceResource('Symfony\Component\Config\Tests\Fixtures\BarClass'); + + $this->assertTrue($res->isFresh(time())); + + eval(<<assertFalse($res->isFresh(time())); + } + + public function testIsFreshWhenClassExists() + { + $res = new ClassExistenceResource('Symfony\Component\Config\Tests\Resource\ClassExistenceResourceTest'); + + $this->assertTrue($res->isFresh(time())); + } +} From 222b56d4269c6b0f8d65cf11455dd8a3950f58cc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 1 Oct 2016 13:59:12 -0700 Subject: [PATCH 2/2] [TwigBundle] added support for ClassExistenceResource when relevant --- .../DependencyInjection/Compiler/ExtensionPass.php | 13 ++++++++++--- src/Symfony/Bundle/TwigBundle/composer.json | 2 +- .../Config/Resource/ClassExistenceResource.php | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php index a9154ccf14cca..316cc665a4b94 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php @@ -11,9 +11,13 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; +use Symfony\Component\Config\Resource\ClassExistenceResource; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; +use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Component\Yaml\Parser as YamlParser; /** * @author Jean-François Simon @@ -72,15 +76,18 @@ public function process(ContainerBuilder $container) $container->getDefinition('twig.extension.assets')->addTag('twig.extension'); } - if (class_exists('Symfony\Component\Yaml\Parser')) { + $container->addResource(new ClassExistenceResource(YamlParser::class)); + if (class_exists(YamlParser::class)) { $container->getDefinition('twig.extension.yaml')->addTag('twig.extension'); } - if (class_exists('Symfony\Component\Stopwatch\Stopwatch')) { + $container->addResource(new ClassExistenceResource(Stopwatch::class)); + if (class_exists(Stopwatch::class)) { $container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension'); } - if (class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { + $container->addResource(new ClassExistenceResource(ExpressionLanguage::class)); + if (class_exists(ExpressionLanguage::class)) { $container->getDefinition('twig.extension.expression')->addTag('twig.extension'); } } diff --git a/src/Symfony/Bundle/TwigBundle/composer.json b/src/Symfony/Bundle/TwigBundle/composer.json index 2641ccbe2947e..2f725d3cb06b2 100644 --- a/src/Symfony/Bundle/TwigBundle/composer.json +++ b/src/Symfony/Bundle/TwigBundle/composer.json @@ -27,7 +27,7 @@ "symfony/stopwatch": "~2.8|~3.0", "symfony/dependency-injection": "~2.8|~3.0", "symfony/expression-language": "~2.8|~3.0", - "symfony/config": "~2.8|~3.0", + "symfony/config": "~3.2", "symfony/finder": "~2.8|~3.0", "symfony/routing": "~2.8|~3.0", "symfony/templating": "~2.8|~3.0", diff --git a/src/Symfony/Component/Config/Resource/ClassExistenceResource.php b/src/Symfony/Component/Config/Resource/ClassExistenceResource.php index 5f683a4f8a5e0..8a9df906a643d 100644 --- a/src/Symfony/Component/Config/Resource/ClassExistenceResource.php +++ b/src/Symfony/Component/Config/Resource/ClassExistenceResource.php @@ -12,8 +12,8 @@ namespace Symfony\Component\Config\Resource; /** - * ClassExistenceResource represents a class availability. - * Freshness is only evaluated against resource availability. + * ClassExistenceResource represents a class existence. + * Freshness is only evaluated against resource existence. * * The resource must be a fully-qualified class name. *