diff --git a/src/Symfony/Component/Config/Resource/FileExistenceResource.php b/src/Symfony/Component/Config/Resource/FileExistenceResource.php index 349402edf0494..be4ed134dd4dc 100644 --- a/src/Symfony/Component/Config/Resource/FileExistenceResource.php +++ b/src/Symfony/Component/Config/Resource/FileExistenceResource.php @@ -21,9 +21,9 @@ */ class FileExistenceResource implements SelfCheckingResourceInterface, \Serializable { - private $resource; + protected $resource; - private $exists; + protected $exists; /** * Constructor. diff --git a/src/Symfony/Component/Config/Resource/FileResource.php b/src/Symfony/Component/Config/Resource/FileResource.php index 6ad130518854c..1d5494c99c558 100644 --- a/src/Symfony/Component/Config/Resource/FileResource.php +++ b/src/Symfony/Component/Config/Resource/FileResource.php @@ -18,13 +18,8 @@ * * @author Fabien Potencier */ -class FileResource implements SelfCheckingResourceInterface, \Serializable +class FileResource extends FileExistenceResource { - /** - * @var string|false - */ - private $resource; - /** * Constructor. * @@ -34,44 +29,35 @@ class FileResource implements SelfCheckingResourceInterface, \Serializable */ public function __construct($resource) { - $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false); + $resource = realpath($resource) ?: $resource; - if (false === $this->resource) { - throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource)); - } + parent::__construct($resource); } /** * {@inheritdoc} */ - public function __toString() + public function isFresh($timestamp) { - return $this->resource; - } + if (!parent::isFresh($timestamp)) { + return false; + } - /** - * @return string The canonicalized, absolute path to the resource - */ - public function getResource() - { - return $this->resource; + return !file_exists($this->resource) || filemtime($this->resource) <= $timestamp; } /** * {@inheritdoc} */ - public function isFresh($timestamp) + public function unserialize($serialized) { - return file_exists($this->resource) && @filemtime($this->resource) <= $timestamp; - } + $unserialized = unserialize($serialized); - public function serialize() - { - return serialize($this->resource); - } + // compatibility with previously serialized resource + if (!is_array($unserialized)) { + $unserialized = array($unserialized, true); + } - public function unserialize($serialized) - { - $this->resource = unserialize($serialized); + list($this->resource, $this->exists) = $unserialized; } } diff --git a/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php index 6a168e6351d53..077de130bb45d 100644 --- a/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php @@ -52,15 +52,6 @@ public function testToString() $this->assertSame(realpath($this->file), (string) $this->resource); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessageRegExp /The file ".*" does not exist./ - */ - public function testResourceDoesNotExist() - { - $resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999)); - } - public function testIsFresh() { $this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second'); @@ -68,17 +59,27 @@ public function testIsFresh() $this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated'); } + public function testIsFreshForCreatedResources() + { + unlink($this->file); + + $this->resource = new FileResource($this->file); + touch($this->file, $this->time); + + $this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource is created'); + } + public function testIsFreshForDeletedResources() { unlink($this->file); - $this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist'); + $this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource is deleted'); } public function testSerializeUnserialize() { $unserialized = unserialize(serialize($this->resource)); - $this->assertSame(realpath($this->file), $this->resource->getResource()); + $this->assertSame(realpath($this->file), $unserialized->getResource()); } }