Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d574e23

Browse files
committed
merged branch gajdaw/component_finder_path_notPath (PR #4739)
This PR was merged into the master branch. Commits ------- 4e21bf2 [Finder] Added path & notPath support to gnu find adapter. 6258d12 [Finder] Fixed expression classes. 5c6dbeb [Finder] Fixed tests. c36dfc1 [Component][Finder] ->path(), ->notPath() methods (with basic tests) Discussion ---------- [2.2][Finder] ->pathContains(), ->pathNotContains() methods (with basic tests) Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/gajdaw/symfony.png?branch=component_finder_path_notPath)](http://travis-ci.org/gajdaw/symfony) Fixes the following tickets: #4581 Todo: - License of the code: MIT Documentation PR: - Two additional methods: `Finder->path()` and `Finder->notPath()`. They allow filtering with paths, e.g. `->path('some/special/dir')`. --------------------------------------------------------------------------- by fabpot at 2012-09-21T05:52:01Z Can you submit a PR on symfony/symfony-docs to update the documentation and reference it here before I merge? Thanks. --------------------------------------------------------------------------- by gajdaw at 2012-09-21T10:54:50Z I've already done it in PR1527. symfony/symfony-docs#1527 --------------------------------------------------------------------------- by stof at 2012-09-21T22:15:46Z Actually, to be BC in PHPUnit when switching to Finder (this feature is the reason why the switch was reverted in 3.7), they will need the support of the glob syntax. Should it be supported directly in the Finder or should we consider that PHPUnit will have to put the code converting globs to regexes themselves before calling the finder ? --------------------------------------------------------------------------- by fabpot at 2012-09-23T13:15:08Z @stof what about supporting globs/patterns for the `in()` method? --------------------------------------------------------------------------- by stof at 2012-09-23T13:38:00Z yeah, this could be a good idea too --------------------------------------------------------------------------- by gajdaw at 2012-09-26T06:11:56Z Supporting globs in `path(), notPath()` methods is trivial: we have `Glob::toRegex()`. The only thing to do is to convert (when necessary) parameter sent to `path(), notPath()`. --------------------------------------------------------------------------- by stof at 2012-10-13T17:19:08Z @gajdaw can you update this PR with the glob support and rebase it ? --------------------------------------------------------------------------- by fabpot at 2012-10-29T11:20:55Z @gajdaw You also need to rebase and update the new adapters accordingly. Maybe @jfsimon can help. --------------------------------------------------------------------------- by jfsimon at 2012-10-29T16:22:25Z With pleasure! @gajdaw let me know if I can do anything. --------------------------------------------------------------------------- by gajdaw at 2012-10-29T16:48:10Z I have moved `->path()` and `->notPath()` methods to `PhpAdapter`. This implementation passes all the tests on Windows, but Travis reports failures. I think that similar methods should be implemented for `GnuFindAdapter`. @jfsimon What do you think? --------------------------------------------------------------------------- by jfsimon at 2012-10-29T17:03:36Z @gajdaw Travis says class 'Symfony\Component\HttpKernel\Exception\InternalServerErrorHttpException' not found in /home/travis/builds/symfony/symfony/src/Symfony/Component/HttpKernel/Tests/Exception/FlattenExceptionTest.php on line 83. This is weird. And yes, `AdapterInterface` and `GnuFindAdapter` should be updated too. I can work on it if you like. --------------------------------------------------------------------------- by fabpot at 2012-10-29T17:07:46Z I've just fixed the unit tests --------------------------------------------------------------------------- by gajdaw at 2012-10-29T17:25:43Z @jfsimon Can implement `path(), notPath()` for `GnuFindAdapter`? I have no time to analyse Gnu's `find` command at the moment. Thanks! --------------------------------------------------------------------------- by jfsimon at 2012-10-29T17:30:13Z @gajdaw okay. --------------------------------------------------------------------------- by jfsimon at 2012-10-29T19:05:26Z @gajdaw work complete! I cant make a PR on your repos :-/ Could you merge my repos https://github.com/jfsimon/symfony/tree/component_finder_path_notPath please? Or maybe could I post a new PR on symfony/master. --------------------------------------------------------------------------- by gajdaw at 2012-10-30T05:34:17Z @jfsimon I have pulled your changes. I don't know wheather you should post a new PR.
2 parents 2a23dbd + 4e21bf2 commit d574e23

21 files changed

+617
-161
lines changed

src/Symfony/Component/Finder/Adapter/AbstractAdapter.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ abstract class AbstractAdapter implements AdapterInterface
3131
protected $dates = array();
3232
protected $filters = array();
3333
protected $sort = false;
34+
protected $paths = array();
35+
protected $notPaths = array();
3436

3537
/**
3638
* {@inheritdoc}
@@ -40,7 +42,7 @@ public function setFollowLinks($followLinks)
4042
$this->followLinks = $followLinks;
4143

4244
return $this;
43-
}
45+
}
4446

4547
/**
4648
* {@inheritdoc}
@@ -50,7 +52,7 @@ public function setMode($mode)
5052
$this->mode = $mode;
5153

5254
return $this;
53-
}
55+
}
5456

5557
/**
5658
* {@inheritdoc}
@@ -80,7 +82,7 @@ public function setDepths(array $depths)
8082
}
8183

8284
return $this;
83-
}
85+
}
8486

8587
/**
8688
* {@inheritdoc}
@@ -90,7 +92,7 @@ public function setExclude(array $exclude)
9092
$this->exclude = $exclude;
9193

9294
return $this;
93-
}
95+
}
9496

9597
/**
9698
* {@inheritdoc}
@@ -100,7 +102,7 @@ public function setNames(array $names)
100102
$this->names = $names;
101103

102104
return $this;
103-
}
105+
}
104106

105107
/**
106108
* {@inheritdoc}
@@ -110,7 +112,7 @@ public function setNotNames(array $notNames)
110112
$this->notNames = $notNames;
111113

112114
return $this;
113-
}
115+
}
114116

115117
/**
116118
* {@inheritdoc}
@@ -120,7 +122,7 @@ public function setContains(array $contains)
120122
$this->contains = $contains;
121123

122124
return $this;
123-
}
125+
}
124126

125127
/**
126128
* {@inheritdoc}
@@ -130,7 +132,7 @@ public function setNotContains(array $notContains)
130132
$this->notContains = $notContains;
131133

132134
return $this;
133-
}
135+
}
134136

135137
/**
136138
* {@inheritdoc}
@@ -140,7 +142,7 @@ public function setSizes(array $sizes)
140142
$this->sizes = $sizes;
141143

142144
return $this;
143-
}
145+
}
144146

145147
/**
146148
* {@inheritdoc}
@@ -150,7 +152,7 @@ public function setDates(array $dates)
150152
$this->dates = $dates;
151153

152154
return $this;
153-
}
155+
}
154156

155157
/**
156158
* {@inheritdoc}
@@ -160,7 +162,7 @@ public function setFilters(array $filters)
160162
$this->filters = $filters;
161163

162164
return $this;
163-
}
165+
}
164166

165167
/**
166168
* {@inheritdoc}
@@ -170,5 +172,25 @@ public function setSort($sort)
170172
$this->sort = $sort;
171173

172174
return $this;
173-
}
175+
}
176+
177+
/**
178+
* {@inheritdoc}
179+
*/
180+
public function setPath(array $paths)
181+
{
182+
$this->paths = $paths;
183+
184+
return $this;
185+
}
186+
187+
/**
188+
* {@inheritdoc}
189+
*/
190+
public function setNotPath(array $notPaths)
191+
{
192+
$this->notPaths = $notPaths;
193+
194+
return $this;
195+
}
174196
}

src/Symfony/Component/Finder/Adapter/AdapterInterface.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,103 +21,117 @@ interface AdapterInterface
2121
*
2222
* @return AdapterInterface Current instance
2323
*/
24-
function setFollowLinks($followLinks);
24+
public function setFollowLinks($followLinks);
2525

2626
/**
2727
* @param int $mode
2828
*
2929
* @return AdapterInterface Current instance
3030
*/
31-
function setMode($mode);
31+
public function setMode($mode);
3232

3333
/**
3434
* @param array $exclude
3535
*
3636
* @return AdapterInterface Current instance
3737
*/
38-
function setExclude(array $exclude);
38+
public function setExclude(array $exclude);
3939

4040
/**
4141
* @param array $depths
4242
*
4343
* @return AdapterInterface Current instance
4444
*/
45-
function setDepths(array $depths);
45+
public function setDepths(array $depths);
4646

4747
/**
4848
* @param array $names
4949
*
5050
* @return AdapterInterface Current instance
5151
*/
52-
function setNames(array $names);
52+
public function setNames(array $names);
5353

5454
/**
5555
* @param array $notNames
5656
*
5757
* @return AdapterInterface Current instance
5858
*/
59-
function setNotNames(array $notNames);
59+
public function setNotNames(array $notNames);
6060

6161
/**
6262
* @param array $contains
6363
*
6464
* @return AdapterInterface Current instance
6565
*/
66-
function setContains(array $contains);
66+
public function setContains(array $contains);
6767

6868
/**
6969
* @param array $notContains
7070
*
7171
* @return AdapterInterface Current instance
7272
*/
73-
function setNotContains(array $notContains);
73+
public function setNotContains(array $notContains);
7474

7575
/**
7676
* @param array $sizes
7777
*
7878
* @return AdapterInterface Current instance
7979
*/
80-
function setSizes(array $sizes);
80+
public function setSizes(array $sizes);
8181

8282
/**
8383
* @param array $dates
8484
*
8585
* @return AdapterInterface Current instance
8686
*/
87-
function setDates(array $dates);
87+
public function setDates(array $dates);
8888

8989
/**
9090
* @param array $filters
9191
*
9292
* @return AdapterInterface Current instance
9393
*/
94-
function setFilters(array $filters);
94+
public function setFilters(array $filters);
9595

9696
/**
9797
* @param \Closure|int $sort
9898
*
9999
* @return AdapterInterface Current instance
100100
*/
101-
function setSort($sort);
101+
public function setSort($sort);
102+
103+
/**
104+
* @param array $path
105+
*
106+
* @return AdapterInterface Current instance
107+
*/
108+
public function setPath(array $paths);
109+
110+
/**
111+
* @param array $notPaths
112+
*
113+
* @return AdapterInterface Current instance
114+
*/
115+
public function setNotPath(array $notPaths);
102116

103117
/**
104118
* @param string $dir
105119
*
106120
* @return \Iterator Result iterator
107121
*/
108-
function searchInDirectory($dir);
122+
public function searchInDirectory($dir);
109123

110124
/**
111125
* Tests adapter support for current platform.
112126
*
113127
* @return bool
114128
*/
115-
function isSupported();
129+
public function isSupported();
116130

117131
/**
118132
* Returns adapter name.
119133
*
120134
* @return string
121135
*/
122-
function getName();
136+
public function getName();
123137
}

src/Symfony/Component/Finder/Adapter/GnuFindAdapter.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function searchInDirectory($dir)
8080

8181
$this->buildNamesFiltering($find, $this->names);
8282
$this->buildNamesFiltering($find, $this->notNames, true);
83+
$this->buildPathsFiltering($find, $dir, $this->paths);
84+
$this->buildPathsFiltering($find, $dir, $this->notPaths, true);
8385
$this->buildSizesFiltering($find, $this->sizes);
8486
$this->buildDatesFiltering($find, $this->dates);
8587

@@ -151,7 +153,7 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
151153
foreach ($names as $i => $name) {
152154
$expr = Expression::create($name);
153155

154-
// Fixes 'not search' and 'fuls path matching' regex problems.
156+
// Fixes 'not search' and 'full path matching' regex problems.
155157
// - Jokers '.' are replaced by [^/].
156158
// - We add '[^/]*' before and after regex (if no ^|$ flags are present).
157159
if ($expr->isRegex()) {
@@ -177,6 +179,44 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
177179
$command->cmd(')');
178180
}
179181

182+
/**
183+
* @param Command $command
184+
* @param string $dir
185+
* @param string[] $paths
186+
* @param bool $not
187+
* @return void
188+
*/
189+
private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
190+
{
191+
if (0 === count($paths)) {
192+
return;
193+
}
194+
195+
$command->add($not ? '-not' : null)->cmd('(');
196+
197+
foreach ($paths as $i => $path) {
198+
$expr = Expression::create($path);
199+
200+
// Fixes 'not search' regex problems.
201+
if ($expr->isRegex()) {
202+
$regex = $expr->getRegex();
203+
$regex->prepend($regex->hasStartFlag() ? '' : '.*')->setEndJoker(!$regex->hasEndFlag());
204+
} else {
205+
$expr->prepend('*')->append('*');
206+
}
207+
208+
$command
209+
->add($i > 0 ? '-or' : null)
210+
->add($expr->isRegex()
211+
? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
212+
: ($expr->isCaseSensitive() ? '-path' : '-ipath')
213+
)
214+
->arg($expr->prepend($dir.DIRECTORY_SEPARATOR)->renderPattern());
215+
}
216+
217+
$command->cmd(')');
218+
}
219+
180220
/**
181221
* @param Command $command
182222
* @param NumberComparator[] $sizes

src/Symfony/Component/Finder/Adapter/PhpAdapter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function searchInDirectory($dir)
7373
$iterator = $iteratorAggregate->getIterator();
7474
}
7575

76+
if ($this->paths || $this->notPaths) {
77+
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $this->notPaths);
78+
}
79+
7680
return $iterator;
7781
}
7882

src/Symfony/Component/Finder/Expression/Expression.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ public function getType()
8686
return $this->value->getType();
8787
}
8888

89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function prepend($expr)
93+
{
94+
$this->value->prepend($expr);
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function append($expr)
103+
{
104+
$this->value->append($expr);
105+
106+
return $this;
107+
}
108+
89109
/**
90110
* @return bool
91111
*/

src/Symfony/Component/Finder/Expression/Glob.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ public function isCaseSensitive()
6161
return true;
6262
}
6363

64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function prepend($expr)
68+
{
69+
$this->pattern = $expr.$this->pattern;
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function append($expr)
78+
{
79+
$this->pattern .= $expr;
80+
81+
return $this;
82+
}
83+
6484
/**
6585
* @param bool $strictLeadingDot
6686
* @param bool $strictWildcardSlash

0 commit comments

Comments
 (0)