Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[TwigBundle] Reconfigure twig paths when they are updated
  • Loading branch information
chbruyand committed May 29, 2015
commit dd399677a96444664afa646df2e9f09e37c7c210
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,23 +75,30 @@ public function load(array $configs, ContainerBuilder $container)
} else {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
}
$container->addResource(new FileExistenceResource($path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed here as we don't change the container configuration based on the existence of this folder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint

}

// 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');
Expand Down
68 changes: 68 additions & 0 deletions src/Symfony/Component/Config/Resource/FileExistenceResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do the string casting in the constructor already.

}
/**
* {@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 <[email protected]>
*
* 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');
}
}