From dd399677a96444664afa646df2e9f09e37c7c210 Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Fri, 29 May 2015 16:57:55 +0200 Subject: [PATCH 1/5] [TwigBundle] Reconfigure twig paths when they are updated --- .../DependencyInjection/TwigExtension.php | 14 +++- .../Config/Resource/FileExistenceResource.php | 68 ++++++++++++++++++ .../Resource/FileExistenceResourceTest.php | 70 +++++++++++++++++++ 3 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Config/Resource/FileExistenceResource.php create mode 100644 src/Symfony/Component/Config/Tests/Resource/FileExistenceResourceTest.php diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 8b28042bd4b6b..a97e81b0fb8fc 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; +use Symfony\Component\Config\Resource\FileExistenceResource; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -74,23 +75,30 @@ public function load(array $configs, ContainerBuilder $container) } else { $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace)); } + $container->addResource(new FileExistenceResource($path)); } // register bundles as Twig namespaces foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { - if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) { + $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views'; + if (is_dir($dir)) { $this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle); } + $container->addResource(new FileExistenceResource($dir)); $reflection = new \ReflectionClass($class); - if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/views')) { + $dir = dirname($reflection->getFilename()).'/Resources/views'; + if (is_dir($dir)) { $this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle); } + $container->addResource(new FileExistenceResource($dir)); } - if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) { + $dir = $container->getParameter('kernel.root_dir').'/Resources/views'; + if (is_dir($dir)) { $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); } + $container->addResource(new FileExistenceResource($dir)); if (!empty($config['globals'])) { $def = $container->getDefinition('twig'); diff --git a/src/Symfony/Component/Config/Resource/FileExistenceResource.php b/src/Symfony/Component/Config/Resource/FileExistenceResource.php new file mode 100644 index 0000000000000..c05732922b28a --- /dev/null +++ b/src/Symfony/Component/Config/Resource/FileExistenceResource.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Symfony\Component\Config\Resource; +/** + * FileExistenceResource represents a resource stored on the filesystem. + * Freshness is only evaluated against resource creation or deletion. + * + * The resource can be a file or a directory. + * + * @author Charles-Henri Bruyand + */ +class FileExistenceResource implements ResourceInterface, \Serializable +{ + private $resource; + private $exists; + /** + * Constructor. + * + * @param string $resource The file path to the resource + */ + public function __construct($resource) + { + $this->resource = $resource; + $this->exists = file_exists($resource); + } + /** + * {@inheritdoc} + */ + public function __toString() + { + return (string) $this->resource; + } + /** + * {@inheritdoc} + */ + public function getResource() + { + return $this->resource; + } + /** + * {@inheritdoc} + */ + public function isFresh($timestamp) + { + return file_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/FileExistenceResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/FileExistenceResourceTest.php new file mode 100644 index 0000000000000..56ee584b1a59b --- /dev/null +++ b/src/Symfony/Component/Config/Tests/Resource/FileExistenceResourceTest.php @@ -0,0 +1,70 @@ + + * + * 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\FileExistenceResource; + +class FileExistenceResourceTest extends \PHPUnit_Framework_TestCase +{ + protected $resource; + protected $file; + protected $time; + + protected function setUp() + { + $this->file = realpath(sys_get_temp_dir()).'/tmp.xml'; + $this->time = time(); + $this->resource = new FileExistenceResource($this->file); + } + + protected function tearDown() + { + if (file_exists($this->file)) { + unlink($this->file); + } + } + + public function testToString() + { + $this->assertSame($this->file, (string) $this->resource); + } + + public function testGetResource() + { + $this->assertSame($this->file, $this->resource->getResource(), '->getResource() returns the path to the resource'); + } + + public function testIsFreshWithExistingResource() + { + touch($this->file, $this->time); + $serialized = serialize(new FileExistenceResource($this->file)); + + $resource = unserialize($serialized); + $this->assertTrue($resource->isFresh($this->time), '->isFresh() returns true if the resource is still present'); + + unlink($this->file); + $resource = unserialize($serialized); + $this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource has been deleted'); + } + + public function testIsFreshWithAbsentResource() + { + $serialized = serialize(new FileExistenceResource($this->file)); + + $resource = unserialize($serialized); + $this->assertTrue($resource->isFresh($this->time), '->isFresh() returns true if the resource is still absent'); + + touch($this->file, $this->time); + $resource = unserialize($serialized); + $this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource has been created'); + } +} From 41715cdd1e93bd3040cb9d65e250000c83365dd1 Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Fri, 29 May 2015 17:08:21 +0200 Subject: [PATCH 2/5] fix wrong copy/paste --- .../Config/Resource/FileExistenceResource.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Symfony/Component/Config/Resource/FileExistenceResource.php b/src/Symfony/Component/Config/Resource/FileExistenceResource.php index c05732922b28a..8a574acc35ddf 100644 --- a/src/Symfony/Component/Config/Resource/FileExistenceResource.php +++ b/src/Symfony/Component/Config/Resource/FileExistenceResource.php @@ -1,4 +1,5 @@ resource = $resource; $this->exists = file_exists($resource); } + /** * {@inheritdoc} */ @@ -37,6 +43,7 @@ public function __toString() { return (string) $this->resource; } + /** * {@inheritdoc} */ @@ -44,6 +51,7 @@ public function getResource() { return $this->resource; } + /** * {@inheritdoc} */ @@ -51,6 +59,7 @@ public function isFresh($timestamp) { return file_exists($this->resource) === $this->exists; } + /** * {@inheritdoc} */ @@ -58,6 +67,7 @@ public function serialize() { return serialize(array($this->resource, $this->exists)); } + /** * {@inheritdoc} */ From feee2749fab0b5339ed6ce5e1bf5b624bd280c2f Mon Sep 17 00:00:00 2001 From: chbruyand Date: Fri, 29 May 2015 17:58:11 +0200 Subject: [PATCH 3/5] Next version of symfony/config is needed --- src/Symfony/Bundle/TwigBundle/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/composer.json b/src/Symfony/Bundle/TwigBundle/composer.json index da7f1fd603665..1f73c781e9f3b 100644 --- a/src/Symfony/Bundle/TwigBundle/composer.json +++ b/src/Symfony/Bundle/TwigBundle/composer.json @@ -27,7 +27,7 @@ "symfony/stopwatch": "~2.2|~3.0.0", "symfony/dependency-injection": "~2.6,>=2.6.6|~3.0.0", "symfony/expression-language": "~2.4|~3.0.0", - "symfony/config": "~2.2|~3.0.0", + "symfony/config": "~2.8|~3.0.0", "symfony/routing": "~2.1|~3.0.0", "symfony/templating": "~2.1|~3.0.0", "symfony/framework-bundle": "~2.7|~3.0.0" From b802c69873816074cd6edb90c82562cb04f0e432 Mon Sep 17 00:00:00 2001 From: chbruyand Date: Mon, 1 Jun 2015 09:53:04 +0200 Subject: [PATCH 4/5] Update FileExistenceResource.php --- .../Component/Config/Resource/FileExistenceResource.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Config/Resource/FileExistenceResource.php b/src/Symfony/Component/Config/Resource/FileExistenceResource.php index 8a574acc35ddf..4e68e8ac105be 100644 --- a/src/Symfony/Component/Config/Resource/FileExistenceResource.php +++ b/src/Symfony/Component/Config/Resource/FileExistenceResource.php @@ -32,7 +32,7 @@ class FileExistenceResource implements ResourceInterface, \Serializable */ public function __construct($resource) { - $this->resource = $resource; + $this->resource = (string) $resource; $this->exists = file_exists($resource); } @@ -41,7 +41,7 @@ public function __construct($resource) */ public function __toString() { - return (string) $this->resource; + return $this->resource; } /** From bde9ecd7df6b3e5e38cc6bf5e33ff5347486046b Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Tue, 2 Jun 2015 11:35:03 +0200 Subject: [PATCH 5/5] Remove unnecessary tracked path https://github.com/symfony/symfony/pull/14781/files#r31505368 not needed here as we don't change the container configuration based on the existence of this folder --- .../Bundle/TwigBundle/DependencyInjection/TwigExtension.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index a97e81b0fb8fc..df49f8b37217f 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -75,7 +75,6 @@ public function load(array $configs, ContainerBuilder $container) } else { $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace)); } - $container->addResource(new FileExistenceResource($path)); } // register bundles as Twig namespaces