[Config] added new methods and their tests to File and Directory resources
This commit is contained in:
parent
9fe0d00735
commit
d7c24eb88a
@ -43,7 +43,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
|
|||||||
$childs = array();
|
$childs = array();
|
||||||
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
|
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
|
||||||
// if regex filtering is enabled only return matching files
|
// if regex filtering is enabled only return matching files
|
||||||
if ($file->isFile() && !$this->isFileMatchesPattern($file)) {
|
if ($file->isFile() && !$this->hasFile($file)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,14 +64,14 @@ class DirectoryResource implements ResourceInterface, \Serializable
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getFilteredChildResources()
|
public function getFilteredResources()
|
||||||
{
|
{
|
||||||
$iterator = new \DirectoryIterator($this->resource);
|
$iterator = new \DirectoryIterator($this->resource);
|
||||||
|
|
||||||
$resources = array();
|
$resources = array();
|
||||||
foreach ($iterator as $file) {
|
foreach ($iterator as $file) {
|
||||||
// if regex filtering is enabled only return matching files
|
// if regex filtering is enabled only return matching files
|
||||||
if ($file->isFile() && !$this->isFileMatchesPattern($file)) {
|
if ($file->isFile() && !$this->hasFile($file)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,9 +81,16 @@ class DirectoryResource implements ResourceInterface, \Serializable
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$resources[] = $file->isDir()
|
// if file is dot - continue
|
||||||
? new DirectoryResource((string) $file)
|
if ($file->isDot()) {
|
||||||
: new FileResource((string) $file);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($file->isFile()) {
|
||||||
|
$resources[] = new FileResource($file->getRealPath());
|
||||||
|
} elseif ($file->isDir()) {
|
||||||
|
$resources[] = new DirectoryResource($file->getRealPath());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $resources;
|
return $resources;
|
||||||
@ -119,6 +126,30 @@ class DirectoryResource implements ResourceInterface, \Serializable
|
|||||||
return $this->pattern;
|
return $this->pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that passed file exists in resource and matches resource filters.
|
||||||
|
*
|
||||||
|
* @param SplFileInfo|string $file
|
||||||
|
*
|
||||||
|
* @return Boolean
|
||||||
|
*/
|
||||||
|
public function hasFile($file)
|
||||||
|
{
|
||||||
|
if (!$file instanceof \SplFileInfo) {
|
||||||
|
$file = new \SplFileInfo($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 !== strpos($file->getRealPath(), realpath($this->resource))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->pattern) {
|
||||||
|
return (bool) preg_match($this->pattern, $file->getBasename());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns resource mtime.
|
* Returns resource mtime.
|
||||||
*
|
*
|
||||||
@ -163,6 +194,16 @@ class DirectoryResource implements ResourceInterface, \Serializable
|
|||||||
return is_dir($this->resource);
|
return is_dir($this->resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns unique resource ID.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return md5($this->resource.$this->pattern);
|
||||||
|
}
|
||||||
|
|
||||||
public function serialize()
|
public function serialize()
|
||||||
{
|
{
|
||||||
return serialize(array($this->resource, $this->pattern));
|
return serialize(array($this->resource, $this->pattern));
|
||||||
@ -172,20 +213,4 @@ class DirectoryResource implements ResourceInterface, \Serializable
|
|||||||
{
|
{
|
||||||
list($this->resource, $this->pattern) = unserialize($serialized);
|
list($this->resource, $this->pattern) = unserialize($serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks that passed file matches specified in resource filters.
|
|
||||||
*
|
|
||||||
* @param \SplFileInfo $file
|
|
||||||
*
|
|
||||||
* @return Boolean
|
|
||||||
*/
|
|
||||||
private function isFileMatchesPattern(\SplFileInfo $file)
|
|
||||||
{
|
|
||||||
if ($this->pattern) {
|
|
||||||
return preg_match($this->pattern, $file->getBasename());
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -90,6 +90,16 @@ class FileResource implements ResourceInterface, \Serializable
|
|||||||
return file_exists($this->resource);
|
return file_exists($this->resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns unique resource ID.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return md5($this->resource);
|
||||||
|
}
|
||||||
|
|
||||||
public function serialize()
|
public function serialize()
|
||||||
{
|
{
|
||||||
return serialize($this->resource);
|
return serialize($this->resource);
|
||||||
|
@ -54,4 +54,11 @@ interface ResourceInterface
|
|||||||
* @return mixed The resource
|
* @return mixed The resource
|
||||||
*/
|
*/
|
||||||
function getResource();
|
function getResource();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns unique resource ID.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function getId();
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,20 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
|
|||||||
rmdir($directory);
|
rmdir($directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::getId
|
||||||
|
*/
|
||||||
|
public function testGetId()
|
||||||
|
{
|
||||||
|
$resource1 = new DirectoryResource($this->directory);
|
||||||
|
$resource2 = new DirectoryResource($this->directory);
|
||||||
|
$resource3 = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
|
||||||
|
|
||||||
|
$this->assertNotNull($resource1->getId());
|
||||||
|
$this->assertEquals($resource1->getId(), $resource2->getId());
|
||||||
|
$this->assertNotEquals($resource1->getId(), $resource3->getId());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Symfony\Component\Config\Resource\DirectoryResource::getResource
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::getResource
|
||||||
*/
|
*/
|
||||||
@ -169,6 +183,72 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
|
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::hasFile
|
||||||
|
*/
|
||||||
|
public function testHasFile()
|
||||||
|
{
|
||||||
|
$resource = new DirectoryResource($this->directory, '/\.foo$/');
|
||||||
|
|
||||||
|
touch($this->directory.'/new.foo', time() + 20);
|
||||||
|
|
||||||
|
$this->assertFalse($resource->hasFile($this->directory.'/tmp.xml'));
|
||||||
|
$this->assertTrue($resource->hasFile($this->directory.'/new.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::getFilteredChilds
|
||||||
|
*/
|
||||||
|
public function testGetFilteredChilds()
|
||||||
|
{
|
||||||
|
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
|
||||||
|
|
||||||
|
touch($file1 = $this->directory.'/new.xml', time() + 20);
|
||||||
|
touch($file2 = $this->directory.'/old.foo', time() + 20);
|
||||||
|
touch($this->directory.'/old', time() + 20);
|
||||||
|
mkdir($dir = $this->directory.'/sub');
|
||||||
|
touch($file3 = $this->directory.'/sub/file.foo', time() + 20);
|
||||||
|
|
||||||
|
$childs = $resource->getFilteredChilds();
|
||||||
|
$this->assertSame(5, count($childs));
|
||||||
|
|
||||||
|
$childs = array_map(function($item) {
|
||||||
|
return (string) $item;
|
||||||
|
}, $childs);
|
||||||
|
|
||||||
|
$this->assertContains($file1, $childs);
|
||||||
|
$this->assertContains($file2, $childs);
|
||||||
|
$this->assertContains($dir, $childs);
|
||||||
|
$this->assertContains($this->directory.'/tmp.xml', $childs);
|
||||||
|
$this->assertContains($file3, $childs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::getFilteredResources
|
||||||
|
*/
|
||||||
|
public function testGetFilteredResources()
|
||||||
|
{
|
||||||
|
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
|
||||||
|
|
||||||
|
touch($file1 = $this->directory.'/new.xml', time() + 20);
|
||||||
|
touch($file2 = $this->directory.'/old.foo', time() + 20);
|
||||||
|
touch($this->directory.'/old', time() + 20);
|
||||||
|
mkdir($dir = $this->directory.'/sub');
|
||||||
|
touch($file3 = $this->directory.'/sub/file.foo', time() + 20);
|
||||||
|
|
||||||
|
$resources = $resource->getFilteredResources();
|
||||||
|
$this->assertSame(4, count($resources));
|
||||||
|
|
||||||
|
$childs = array_map(function($item) {
|
||||||
|
return realpath($item->getResource());
|
||||||
|
}, $resources);
|
||||||
|
|
||||||
|
$this->assertContains(realpath($file1), $childs);
|
||||||
|
$this->assertContains(realpath($file2), $childs);
|
||||||
|
$this->assertContains(realpath($dir), $childs);
|
||||||
|
$this->assertContains(realpath($this->directory.'/tmp.xml'), $childs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Symfony\Component\Config\Resource\DirectoryResource::exists
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::exists
|
||||||
*/
|
*/
|
||||||
|
@ -32,6 +32,18 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\Config\Resource\DirectoryResource::getId
|
||||||
|
*/
|
||||||
|
public function testGetId()
|
||||||
|
{
|
||||||
|
$resource1 = new FileResource($this->file);
|
||||||
|
$resource2 = new FileResource($this->file);
|
||||||
|
|
||||||
|
$this->assertNotNull($resource1->getId());
|
||||||
|
$this->assertEquals($resource1->getId(), $resource2->getId());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Symfony\Component\Config\Resource\FileResource::getResource
|
* @covers Symfony\Component\Config\Resource\FileResource::getResource
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user