[Config] updated resources API to be more explicit

This commit is contained in:
everzet 2011-11-19 08:58:54 +01:00
parent 092b5dde62
commit 45df2e681e
5 changed files with 102 additions and 8 deletions

View File

@ -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 integer
*
* @return Boolean true if the resource has not been updated, false otherwise
*/ */
public function isFresh($timestamp) public function getModificationTime()
{ {
clearstatcache(true, $this->resource);
if (!is_dir($this->resource)) { if (!is_dir($this->resource)) {
return false; return false;
} }
@ -84,10 +84,37 @@ class DirectoryResource implements ResourceInterface, \Serializable
continue; continue;
} }
clearstatcache(true, (string) $file);
$newestMTime = max($file->getMTime(), $newestMTime); $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() public function serialize()

View File

@ -52,6 +52,18 @@ class FileResource implements ResourceInterface, \Serializable
return $this->resource; 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. * 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) public function isFresh($timestamp)
{ {
if (!file_exists($this->resource)) { if (!$this->exists()) {
return false; 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() public function serialize()

View File

@ -34,6 +34,20 @@ interface ResourceInterface
*/ */
function isFresh($timestamp); 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. * Returns the resource tied to this Resource.
* *

View File

@ -118,8 +118,20 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed'); $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::isFresh
* @covers Symfony\Component\Config\Resource\DirectoryResource::getModificationTime
*/ */
public function testIsFreshCreateFileInSubdirectory() public function testIsFreshCreateFileInSubdirectory()
{ {

View File

@ -49,4 +49,23 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
$resource = new FileResource('/____foo/foobar'.rand(1, 999999)); $resource = new FileResource('/____foo/foobar'.rand(1, 999999));
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist'); $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());
}
} }