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 38cad9d

Browse filesBrowse files
author
Julien Brochet
committed
[Filesystem] added exists method
1 parent 7c91ee5 commit 38cad9d
Copy full SHA for 38cad9d

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.