merged branch aerialls/fs_exists (PR #4586)

Commits
-------

38cad9d [Filesystem] added exists method

Discussion
----------

[Filesystem] added exists method

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets:
Todo:
License of the code: MIT
Documentation PR:

---------------------------------------------------------------------------

by travisbot at 2012-06-15T16:29:20Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1629204) (merged ebd1a4c6 into f881d282).

---------------------------------------------------------------------------

by sstok at 2012-06-16T09:05:48Z

Shouldn't it be better to stop on the first failure? as all the others files will be false automatically.

---------------------------------------------------------------------------

by stof at 2012-06-16T10:21:49Z

indeed. We should avoid unnecessary filesystem IO by returning false as soon as it is known

---------------------------------------------------------------------------

by aerialls at 2012-06-16T11:55:24Z

Indeed it's better this way. fixed!

---------------------------------------------------------------------------

by travisbot at 2012-06-16T12:01:16Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1634615) (merged 8d98f417 into 76b2ed46).
This commit is contained in:
Fabien Potencier 2012-06-16 18:35:22 +02:00
commit bc147d3492
2 changed files with 73 additions and 5 deletions

View File

@ -66,6 +66,24 @@ class Filesystem
return $ret;
}
/**
* Checks the existence of files or directories.
*
* @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check
*
* @return Boolean true if the file exists, false otherwise
*/
public function exists($files)
{
foreach ($this->toIterator($files) as $file) {
if (!file_exists($file)) {
return false;
}
}
return true;
}
/**
* Creates empty files.
*

View File

@ -313,6 +313,56 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(!is_dir($basePath));
}
public function testFilesExists()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
mkdir($basePath);
touch($basePath.'file1');
mkdir($basePath.'folder');
$this->assertTrue($this->filesystem->exists($basePath.'file1'));
$this->assertTrue($this->filesystem->exists($basePath.'folder'));
}
public function testFilesExistsTraversableObjectOfFilesAndDirectories()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
mkdir($basePath.'dir');
touch($basePath.'file');
$files = new \ArrayObject(array(
$basePath.'dir', $basePath.'file'
));
$this->assertTrue($this->filesystem->exists($files));
}
public function testFilesNotExistsTraversableObjectOfFilesAndDirectories()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
mkdir($basePath.'dir');
touch($basePath.'file');
touch($basePath.'file2');
$files = new \ArrayObject(array(
$basePath.'dir', $basePath.'file', $basePath.'file2'
));
unlink($basePath.'file');
$this->assertFalse($this->filesystem->exists($files));
}
public function testInvalidFileNotExists()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
$this->assertFalse($this->filesystem->exists($basePath.time()));
}
public function testChmodChangesFileMode()
{
$this->markAsSkippedIfChmodIsMissing();
@ -421,18 +471,18 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(is_link($link));
$this->assertEquals($file, readlink($link));
}
/**
* @depends testSymlink
* @depends testSymlink
*/
public function testRemoveSymlink()
{
$this->markAsSkippedIfSymlinkIsMissing();
$link = $this->workspace.DIRECTORY_SEPARATOR.'link';
$this->filesystem->remove($link);
$this->assertTrue(!is_link($link));
}