Skip to content

[TwigBundle] Reconfigure twig paths when they are updated #14778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -67,27 +67,30 @@ public function load(array $configs, ContainerBuilder $container)
} else {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
}
$container->addResource(new DirectoryResource($path));
$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 DirectoryResource($dir));
}
$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 DirectoryResource($dir));
}
$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 DirectoryResource($dir));
}
$container->addResource(new FileExistenceResource($dir));

if (!empty($config['globals'])) {
$def = $container->getDefinition('twig');
Expand Down
78 changes: 78 additions & 0 deletions src/Symfony/Component/Config/Resource/FileExistenceResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <charleshenri.bruyand@gmail.com>
*/
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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');
}
}