Skip to content

Commit 4133aad

Browse files
committed
feature #14781 [TwigBundle] Reconfigure twig paths when they are updated (chbruyand)
This PR was squashed before being merged into the 2.8 branch (closes #14781). Discussion ---------- [TwigBundle] Reconfigure twig paths when they are updated | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14771, #14768, #14262, #14778 | License | MIT Refresh twig paths upon creation and deletion. As we don't care neither about path's modification time nor path's content, a new Resource has been added in the Config Component. Full discussion in #14778. Commits ------- 3cbff05 [TwigBundle] Reconfigure twig paths when they are updated
2 parents 99ec717 + 3cbff05 commit 4133aad

File tree

4 files changed

+159
-4
lines changed

4 files changed

+159
-4
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
1313

1414
use Symfony\Component\Config\FileLocator;
15+
use Symfony\Component\Config\Resource\FileExistenceResource;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -78,19 +79,25 @@ public function load(array $configs, ContainerBuilder $container)
7879

7980
// register bundles as Twig namespaces
8081
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
81-
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
82+
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
83+
if (is_dir($dir)) {
8284
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
8385
}
86+
$container->addResource(new FileExistenceResource($dir));
8487

8588
$reflection = new \ReflectionClass($class);
86-
if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/views')) {
89+
$dir = dirname($reflection->getFilename()).'/Resources/views';
90+
if (is_dir($dir)) {
8791
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
8892
}
93+
$container->addResource(new FileExistenceResource($dir));
8994
}
9095

91-
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
96+
$dir = $container->getParameter('kernel.root_dir').'/Resources/views';
97+
if (is_dir($dir)) {
9298
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
9399
}
100+
$container->addResource(new FileExistenceResource($dir));
94101

95102
if (!empty($config['globals'])) {
96103
$def = $container->getDefinition('twig');

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"symfony/stopwatch": "~2.2|~3.0.0",
2828
"symfony/dependency-injection": "~2.6,>=2.6.6|~3.0.0",
2929
"symfony/expression-language": "~2.4|~3.0.0",
30-
"symfony/config": "~2.2|~3.0.0",
30+
"symfony/config": "~2.8|~3.0.0",
3131
"symfony/routing": "~2.1|~3.0.0",
3232
"symfony/templating": "~2.1|~3.0.0",
3333
"symfony/framework-bundle": "~2.7|~3.0.0"
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Resource;
13+
14+
/**
15+
* FileExistenceResource represents a resource stored on the filesystem.
16+
* Freshness is only evaluated against resource creation or deletion.
17+
*
18+
* The resource can be a file or a directory.
19+
*
20+
* @author Charles-Henri Bruyand <charleshenri.bruyand@gmail.com>
21+
*/
22+
class FileExistenceResource implements ResourceInterface, \Serializable
23+
{
24+
private $resource;
25+
26+
private $exists;
27+
28+
/**
29+
* Constructor.
30+
*
31+
* @param string $resource The file path to the resource
32+
*/
33+
public function __construct($resource)
34+
{
35+
$this->resource = (string) $resource;
36+
$this->exists = file_exists($resource);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function __toString()
43+
{
44+
return $this->resource;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function getResource()
51+
{
52+
return $this->resource;
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function isFresh($timestamp)
59+
{
60+
return file_exists($this->resource) === $this->exists;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function serialize()
67+
{
68+
return serialize(array($this->resource, $this->exists));
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function unserialize($serialized)
75+
{
76+
list($this->resource, $this->exists) = unserialize($serialized);
77+
}
78+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Tests\Resource;
13+
14+
use Symfony\Component\Config\Resource\FileExistenceResource;
15+
16+
class FileExistenceResourceTest extends \PHPUnit_Framework_TestCase
17+
{
18+
protected $resource;
19+
protected $file;
20+
protected $time;
21+
22+
protected function setUp()
23+
{
24+
$this->file = realpath(sys_get_temp_dir()).'/tmp.xml';
25+
$this->time = time();
26+
$this->resource = new FileExistenceResource($this->file);
27+
}
28+
29+
protected function tearDown()
30+
{
31+
if (file_exists($this->file)) {
32+
unlink($this->file);
33+
}
34+
}
35+
36+
public function testToString()
37+
{
38+
$this->assertSame($this->file, (string) $this->resource);
39+
}
40+
41+
public function testGetResource()
42+
{
43+
$this->assertSame($this->file, $this->resource->getResource(), '->getResource() returns the path to the resource');
44+
}
45+
46+
public function testIsFreshWithExistingResource()
47+
{
48+
touch($this->file, $this->time);
49+
$serialized = serialize(new FileExistenceResource($this->file));
50+
51+
$resource = unserialize($serialized);
52+
$this->assertTrue($resource->isFresh($this->time), '->isFresh() returns true if the resource is still present');
53+
54+
unlink($this->file);
55+
$resource = unserialize($serialized);
56+
$this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource has been deleted');
57+
}
58+
59+
public function testIsFreshWithAbsentResource()
60+
{
61+
$serialized = serialize(new FileExistenceResource($this->file));
62+
63+
$resource = unserialize($serialized);
64+
$this->assertTrue($resource->isFresh($this->time), '->isFresh() returns true if the resource is still absent');
65+
66+
touch($this->file, $this->time);
67+
$resource = unserialize($serialized);
68+
$this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource has been created');
69+
}
70+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy