diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php index 5ccd204ef9..021523963e 100644 --- a/src/Symfony/Component/Config/Resource/DirectoryResource.php +++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php @@ -59,14 +59,14 @@ class DirectoryResource implements ResourceInterface, \Serializable } /** - * Returns true if the resource has not been updated since the given timestamp. + * Returns resource mtime. * - * @param integer $timestamp The last time the resource was loaded - * - * @return Boolean true if the resource has not been updated, false otherwise + * @return integer */ - public function isFresh($timestamp) + public function getModificationTime() { + clearstatcache(true, $this->resource); + if (!is_dir($this->resource)) { return false; } @@ -84,10 +84,37 @@ class DirectoryResource implements ResourceInterface, \Serializable continue; } + clearstatcache(true, (string) $file); $newestMTime = max($file->getMTime(), $newestMTime); } - return $newestMTime < $timestamp; + return $newestMTime; + } + + /** + * Returns true if the resource has not been updated since the given timestamp. + * + * @param integer $timestamp The last time the resource was loaded + * + * @return Boolean true if the resource has not been updated, false otherwise + */ + public function isFresh($timestamp) + { + if (!$this->exists()) { + return false; + } + + return $this->getModificationTime() <= $timestamp; + } + + /** + * Returns true if the resource exists in the filesystem. + * + * @return Boolean + */ + public function exists() + { + return file_exists($this->resource); } public function serialize() diff --git a/src/Symfony/Component/Config/Resource/FileResource.php b/src/Symfony/Component/Config/Resource/FileResource.php index 619f84bcef..2499de68e5 100644 --- a/src/Symfony/Component/Config/Resource/FileResource.php +++ b/src/Symfony/Component/Config/Resource/FileResource.php @@ -52,6 +52,18 @@ class FileResource implements ResourceInterface, \Serializable return $this->resource; } + /** + * Returns resource mtime. + * + * @return integer + */ + public function getModificationTime() + { + clearstatcache(true, $this->resource); + + return filemtime($this->resource); + } + /** * Returns true if the resource has not been updated since the given timestamp. * @@ -61,11 +73,21 @@ class FileResource implements ResourceInterface, \Serializable */ public function isFresh($timestamp) { - if (!file_exists($this->resource)) { + if (!$this->exists()) { return false; } - return filemtime($this->resource) < $timestamp; + return $this->getModificationTime() <= $timestamp; + } + + /** + * Returns true if the resource exists in the filesystem. + * + * @return Boolean + */ + public function exists() + { + return file_exists($this->resource); } public function serialize() diff --git a/src/Symfony/Component/Config/Resource/ResourceInterface.php b/src/Symfony/Component/Config/Resource/ResourceInterface.php index 024f2e95f9..e684fd6b09 100644 --- a/src/Symfony/Component/Config/Resource/ResourceInterface.php +++ b/src/Symfony/Component/Config/Resource/ResourceInterface.php @@ -34,6 +34,20 @@ interface ResourceInterface */ function isFresh($timestamp); + /** + * Returns resource mtime. + * + * @return integer + */ + function getModificationTime(); + + /** + * Returns true if the resource exists in the filesystem. + * + * @return Boolean + */ + function exists(); + /** * Returns the resource tied to this Resource. * diff --git a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php index c626ec62ad..98714289a1 100644 --- a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php @@ -118,8 +118,20 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed'); } + /** + * @covers Symfony\Component\Config\Resource\DirectoryResource::exists + */ + public function testExists() + { + $this->assertTrue($this->resource->exists(), '->exists() returns true if the directory still exist'); + + $this->removeDirectory($this->directory); + $this->assertFalse($this->resource->exists(), '->exists() returns false if the directory does not exist'); + } + /** * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh + * @covers Symfony\Component\Config\Resource\DirectoryResource::getModificationTime */ public function testIsFreshCreateFileInSubdirectory() { diff --git a/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php index 83c403bbe7..1c5fa4253b 100644 --- a/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php @@ -49,4 +49,23 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase $resource = new FileResource('/____foo/foobar'.rand(1, 999999)); $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist'); } + + /** + * @covers Symfony\Component\Config\Resource\FileResource::exists + */ + public function testExists() + { + $this->assertTrue($this->resource->exists(), '->exists() returns true if the resource does exist'); + + $resource = new FileResource('/____foo/foobar'.rand(1, 999999)); + $this->assertFalse($resource->exists(), '->exists() returns false if the resource does not exist'); + } + + /** + * @covers Symfony\Component\Config\Resource\FileResource::getModificationTime + */ + public function testGetModificationTime() + { + $this->assertSame(filemtime($this->resource->getResource()), $this->resource->getModificationTime()); + } }