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 092b5dd

Browse filesBrowse files
committed
merged branch gajdaw/finder_current_fix (PR #4335)
Commits ------- 3eb67fc [2.1][Component][Finder] $this->current() fix Discussion ---------- [2.1][Component][Finder] $this->current() fix Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/gajdaw/symfony.png?branch=master)](http://travis-ci.org/gajdaw/symfony) Fixes the following tickets: - Todo: - License of the code: MIT One method to resolve `->in("ftp://...")` problem is to create `RecursiveDirectoryFtpIterator`. (Details: [issue 3585](#3585)) I think that all filters should access the information about current item calling `current()` or `getInnerIterator()`. Otherwise it will not work if we replace `RecursiveDirectoryIterator` with ftp iterator inside `Finder`. I'm not sure if that should go to 2.0 or 2.1 branch. --------------------------------------------------------------------------- by travisbot at 2012-05-19T09:20:19Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1373361) (merged 9f247921 into 58b9245). --------------------------------------------------------------------------- by gajdaw at 2012-05-19T10:51:10Z Probably it should go to master branch, because it improves commit done to master: f2fea97 --------------------------------------------------------------------------- by travisbot at 2012-05-19T11:26:14Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1373982) (merged f9d1db8c into 58b9245). --------------------------------------------------------------------------- by travisbot at 2012-05-19T11:51:25Z This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1374031) (merged f1b4b4f7 into 58b9245). --------------------------------------------------------------------------- by travisbot at 2012-05-19T12:48:17Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1374303) (merged b6d073da into 58b9245). --------------------------------------------------------------------------- by travisbot at 2012-05-19T13:28:18Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1374568) (merged fd144c96 into 58b9245). --------------------------------------------------------------------------- by travisbot at 2012-05-19T13:35:38Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1374609) (merged 89a8d851 into 58b9245). --------------------------------------------------------------------------- by travisbot at 2012-05-21T04:31:46Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1385764) (merged 0d5b8322 into 58b9245). --------------------------------------------------------------------------- by travisbot at 2012-05-21T07:21:56Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1386545) (merged 3eb67fc into 1407f11). --------------------------------------------------------------------------- by stof at 2012-06-09T13:24:14Z seems good
2 parents 55f682c + 3eb67fc commit 092b5dd
Copy full SHA for 092b5dd
Expand file treeCollapse file tree

8 files changed

+161
-35
lines changed

‎src/Symfony/Component/Finder/Iterator/ExcludeDirectoryFilterIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Iterator/ExcludeDirectoryFilterIterator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(\Iterator $iterator, array $directories)
4343
*/
4444
public function accept()
4545
{
46-
$path = $this->isDir() ? $this->getSubPathname() : $this->getSubPath();
46+
$path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
4747
$path = strtr($path, '\\', '/');
4848
foreach ($this->patterns as $pattern) {
4949
if (preg_match($pattern, $path)) {

‎src/Symfony/Component/Finder/Iterator/FileTypeFilterIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Iterator/FileTypeFilterIterator.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public function __construct(\Iterator $iterator, $mode)
4343
*/
4444
public function accept()
4545
{
46-
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $this->isFile()) {
46+
$fileinfo = $this->current();
47+
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $fileinfo->isFile()) {
4748
return false;
48-
} elseif (self::ONLY_FILES === (self::ONLY_FILES & $this->mode) && $this->isDir()) {
49+
} elseif (self::ONLY_FILES === (self::ONLY_FILES & $this->mode) && $fileinfo->isDir()) {
4950
return false;
5051
}
5152

‎src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public function accept()
3030
return true;
3131
}
3232

33-
if ($this->isDir() || !$this->isReadable()) {
33+
$fileinfo = $this->current();
34+
35+
if ($fileinfo->isDir() || !$fileinfo->isReadable()) {
3436
return false;
3537
}
3638

37-
$content = @file_get_contents($filename = $this->getRealpath());
38-
if (false === $content) {
39-
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
39+
$content = $fileinfo->getContents();
40+
if (!$content) {
41+
return false;
4042
}
4143

4244
// should at least not match one rule to exclude

‎src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
2828
*/
2929
public function accept()
3030
{
31-
$filename = $this->getFilename();
31+
$filename = $this->current()->getFilename();
3232

3333
// should at least not match one rule to exclude
3434
foreach ($this->noMatchRegexps as $regex) {

‎src/Symfony/Component/Finder/Iterator/SizeRangeFilterIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Iterator/SizeRangeFilterIterator.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ public function __construct(\Iterator $iterator, array $comparators)
4040
*/
4141
public function accept()
4242
{
43-
if (!$this->isFile()) {
43+
$fileinfo = $this->current();
44+
if (!$fileinfo->isFile()) {
4445
return true;
4546
}
4647

47-
$filesize = $this->getSize();
48+
$filesize = $fileinfo->getSize();
4849
foreach ($this->comparators as $compare) {
4950
if (!$compare->test($filesize)) {
5051
return false;

‎src/Symfony/Component/Finder/SplFileInfo.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/SplFileInfo.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,22 @@ public function getRelativePathname()
5454
{
5555
return $this->relativePathname;
5656
}
57+
58+
/**
59+
* Returns the contents of the file
60+
*
61+
* @return string the contents of the file
62+
*/
63+
public function getContents()
64+
{
65+
$level = error_reporting(0);
66+
$content = file_get_contents($this->getRealpath());
67+
error_reporting($level);
68+
if (false === $content) {
69+
$error = error_get_last();
70+
throw new \RuntimeException($error['message']);
71+
}
72+
73+
return $content;
74+
}
5775
}

‎src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFileIteratorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFileIteratorTest.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Finder\Tests\Iterator;
1313

1414
use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
15+
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
1516

1617
class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
1718
{
@@ -20,7 +21,7 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
2021
*/
2122
public function testAccept($directories, $expected)
2223
{
23-
$inner = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/symfony2_finder', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
24+
$inner = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator(sys_get_temp_dir().'/symfony2_finder', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
2425

2526
$iterator = new ExcludeDirectoryFilterIterator($inner, $directories);
2627

‎src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php
+127-24Lines changed: 127 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,176 @@ class FilecontentFilterIteratorTest extends IteratorTestCase
1818

1919
public function testAccept()
2020
{
21-
$inner = new ContentInnerNameIterator(array('test.txt'));
21+
$inner = new MockFileListIterator(array('test.txt'));
2222
$iterator = new FilecontentFilterIterator($inner, array(), array());
2323
$this->assertIterator(array('test.txt'), $iterator);
2424
}
2525

2626
public function testDirectory()
2727
{
28-
$inner = new ContentInnerNameIterator(array('directory'));
28+
$inner = new MockFileListIterator(array('directory'));
2929
$iterator = new FilecontentFilterIterator($inner, array('directory'), array());
3030
$this->assertIterator(array(), $iterator);
3131
}
3232

3333
public function testUnreadableFile()
3434
{
35-
$inner = new ContentInnerNameIterator(array('file r-'));
35+
$inner = new MockFileListIterator(array('file r-'));
3636
$iterator = new FilecontentFilterIterator($inner, array('file r-'), array());
3737
$this->assertIterator(array(), $iterator);
3838
}
3939

4040
/**
41-
* @expectedException RuntimeException
41+
* @dataProvider getTestFilterData
4242
*/
43-
public function testFileGetContents()
43+
public function testFilter(\Iterator $inner, array $matchPatterns, array $noMatchPatterns, array $resultArray)
4444
{
45-
$inner = new ContentInnerNameIterator(array('file r+'));
46-
$iterator = new FilecontentFilterIterator($inner, array('file r+'), array());
47-
$array = iterator_to_array($iterator);
45+
$iterator = new FilecontentFilterIterator($inner, $matchPatterns, $noMatchPatterns);
46+
$this->assertIterator($resultArray, $iterator);
4847
}
4948

49+
public function getTestFilterData()
50+
{
51+
$inner = new MockFileListIterator();
52+
53+
$inner[] = new MockSplFileInfo(array(
54+
'name' => 'a.txt',
55+
'contents' => 'Lorem ipsum...',
56+
'type' => 'file',
57+
'mode' => 'r+')
58+
);
59+
60+
$inner[] = new MockSplFileInfo(array(
61+
'name' => 'b.yml',
62+
'contents' => 'dolor sit...',
63+
'type' => 'file',
64+
'mode' => 'r+')
65+
);
66+
67+
$inner[] = new MockSplFileInfo(array(
68+
'name' => 'some/other/dir/third.php',
69+
'contents' => 'amet...',
70+
'type' => 'file',
71+
'mode' => 'r+')
72+
);
73+
74+
$inner[] = new MockSplFileInfo(array(
75+
'name' => 'unreadable-file.txt',
76+
'contents' => false,
77+
'type' => 'file',
78+
'mode' => 'r+')
79+
);
80+
81+
return array(
82+
array($inner, array('.'), array(), array('a.txt', 'b.yml', 'some/other/dir/third.php')),
83+
array($inner, array('ipsum'), array(), array('a.txt')),
84+
array($inner, array('i', 'amet'), array('Lorem', 'amet'), array('b.yml')),
85+
);
86+
}
5087
}
5188

52-
class ContentInnerNameIterator extends \ArrayIterator
89+
class MockSplFileInfo extends \SplFileInfo
5390
{
54-
public function current()
55-
{
56-
return new \SplFileInfo(parent::current());
57-
}
91+
const TYPE_DIRECTORY = 1;
92+
const TYPE_FILE = 2;
93+
const TYPE_UNKNOWN = 3;
94+
95+
private $contents = null;
96+
private $mode = null;
97+
private $type = null;
5898

59-
public function getFilename()
99+
public function __construct($param)
60100
{
61-
return parent::current();
101+
if (is_string($param)) {
102+
parent::__construct($param);
103+
} elseif (is_array($param)) {
104+
105+
$defaults = array(
106+
'name' => 'file.txt',
107+
'contents' => null,
108+
'mode' => null,
109+
'type' => null
110+
);
111+
$defaults = array_merge($defaults, $param);
112+
parent::__construct($defaults['name']);
113+
$this->setContents($defaults['contents']);
114+
$this->setMode($defaults['mode']);
115+
$this->setType($defaults['type']);
116+
} else {
117+
throw new \RuntimeException(sprintf('Incorrect parameter "%s"', $param));
118+
}
62119
}
63120

64121
public function isFile()
65122
{
66-
$name = parent::current();
123+
if ($this->type === null) {
124+
return preg_match('/file/', $this->getFilename());
125+
};
67126

68-
return preg_match('/file/', $name);
127+
return self::TYPE_FILE === $this->type;
69128
}
70129

71130
public function isDir()
72131
{
73-
$name = parent::current();
132+
if ($this->type === null) {
133+
return preg_match('/directory/', $this->getFilename());
134+
}
74135

75-
return preg_match('/directory/', $name);
136+
return self::TYPE_DIRECTORY === $this->type;
76137
}
77138

78-
public function getRealpath()
139+
public function isReadable()
79140
{
80-
return parent::current();
141+
if ($this->mode === null) {
142+
return preg_match('/r\+/', $this->getFilename());
143+
}
144+
145+
return preg_match('/r\+/', $this->mode);
81146
}
82147

83-
public function isReadable()
148+
public function getContents()
149+
{
150+
return $this->contents;
151+
}
152+
153+
public function setContents($contents)
84154
{
85-
$name = parent::current();
155+
$this->contents = $contents;
156+
}
157+
158+
public function setMode($mode)
159+
{
160+
$this->mode = $mode;
161+
}
86162

87-
return preg_match('/r\+/', $name);
163+
public function setType($type)
164+
{
165+
if (is_string($type)) {
166+
switch ($type) {
167+
case 'directory':
168+
$this->type = self::TYPE_DIRECTORY;
169+
case 'd':
170+
$this->type = self::TYPE_DIRECTORY;
171+
break;
172+
case 'file':
173+
$this->type = self::TYPE_FILE;
174+
case 'f':
175+
$this->type = self::TYPE_FILE;
176+
break;
177+
default:
178+
$this->type = self::TYPE_UNKNOWN;
179+
}
180+
} else {
181+
$this->type = $type;
182+
}
88183
}
184+
}
89185

186+
class MockFileListIterator extends \ArrayIterator
187+
{
188+
public function __construct(array $filesArray = array())
189+
{
190+
$files = array_map(function($file){ return new MockSplFileInfo($file); }, $filesArray);
191+
parent::__construct($files);
192+
}
90193
}

0 commit comments

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