Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bc147d3

Browse filesBrowse files
committed
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 f881d28). --------------------------------------------------------------------------- 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 76b2ed4).
2 parents 1ac2e9c + 38cad9d commit bc147d3
Copy full SHA for bc147d3

File tree

Expand file treeCollapse file tree

2 files changed

+73
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+73
-5
lines changed

‎src/Symfony/Component/Filesystem/Filesystem.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Filesystem.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ public function mkdir($dirs, $mode = 0777)
6666
return $ret;
6767
}
6868

69+
/**
70+
* Checks the existence of files or directories.
71+
*
72+
* @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check
73+
*
74+
* @return Boolean true if the file exists, false otherwise
75+
*/
76+
public function exists($files)
77+
{
78+
foreach ($this->toIterator($files) as $file) {
79+
if (!file_exists($file)) {
80+
return false;
81+
}
82+
}
83+
84+
return true;
85+
}
86+
6987
/**
7088
* Creates empty files.
7189
*

‎src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
+55-5Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,56 @@ public function testRemoveCleansInvalidLinks()
313313
$this->assertTrue(!is_dir($basePath));
314314
}
315315

316+
public function testFilesExists()
317+
{
318+
$basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
319+
320+
mkdir($basePath);
321+
touch($basePath.'file1');
322+
mkdir($basePath.'folder');
323+
324+
$this->assertTrue($this->filesystem->exists($basePath.'file1'));
325+
$this->assertTrue($this->filesystem->exists($basePath.'folder'));
326+
}
327+
328+
public function testFilesExistsTraversableObjectOfFilesAndDirectories()
329+
{
330+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
331+
332+
mkdir($basePath.'dir');
333+
touch($basePath.'file');
334+
335+
$files = new \ArrayObject(array(
336+
$basePath.'dir', $basePath.'file'
337+
));
338+
339+
$this->assertTrue($this->filesystem->exists($files));
340+
}
341+
342+
public function testFilesNotExistsTraversableObjectOfFilesAndDirectories()
343+
{
344+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
345+
346+
mkdir($basePath.'dir');
347+
touch($basePath.'file');
348+
touch($basePath.'file2');
349+
350+
$files = new \ArrayObject(array(
351+
$basePath.'dir', $basePath.'file', $basePath.'file2'
352+
));
353+
354+
unlink($basePath.'file');
355+
356+
$this->assertFalse($this->filesystem->exists($files));
357+
}
358+
359+
public function testInvalidFileNotExists()
360+
{
361+
$basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
362+
363+
$this->assertFalse($this->filesystem->exists($basePath.time()));
364+
}
365+
316366
public function testChmodChangesFileMode()
317367
{
318368
$this->markAsSkippedIfChmodIsMissing();
@@ -421,18 +471,18 @@ public function testSymlink()
421471
$this->assertTrue(is_link($link));
422472
$this->assertEquals($file, readlink($link));
423473
}
424-
474+
425475
/**
426-
* @depends testSymlink
476+
* @depends testSymlink
427477
*/
428478
public function testRemoveSymlink()
429479
{
430480
$this->markAsSkippedIfSymlinkIsMissing();
431-
481+
432482
$link = $this->workspace.DIRECTORY_SEPARATOR.'link';
433-
483+
434484
$this->filesystem->remove($link);
435-
485+
436486
$this->assertTrue(!is_link($link));
437487
}
438488

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.