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

Skip to content

Commit 4e21bf2

Browse files
committed
[Finder] Added path & notPath support to gnu find adapter.
1 parent 6258d12 commit 4e21bf2

File tree

3 files changed

+82
-29
lines changed

3 files changed

+82
-29
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function setFollowLinks($followLinks)
4242
$this->followLinks = $followLinks;
4343

4444
return $this;
45-
}
45+
}
4646

4747
/**
4848
* {@inheritdoc}
@@ -52,7 +52,7 @@ public function setMode($mode)
5252
$this->mode = $mode;
5353

5454
return $this;
55-
}
55+
}
5656

5757
/**
5858
* {@inheritdoc}
@@ -82,7 +82,7 @@ public function setDepths(array $depths)
8282
}
8383

8484
return $this;
85-
}
85+
}
8686

8787
/**
8888
* {@inheritdoc}
@@ -92,7 +92,7 @@ public function setExclude(array $exclude)
9292
$this->exclude = $exclude;
9393

9494
return $this;
95-
}
95+
}
9696

9797
/**
9898
* {@inheritdoc}
@@ -102,7 +102,7 @@ public function setNames(array $names)
102102
$this->names = $names;
103103

104104
return $this;
105-
}
105+
}
106106

107107
/**
108108
* {@inheritdoc}
@@ -112,7 +112,7 @@ public function setNotNames(array $notNames)
112112
$this->notNames = $notNames;
113113

114114
return $this;
115-
}
115+
}
116116

117117
/**
118118
* {@inheritdoc}
@@ -122,7 +122,7 @@ public function setContains(array $contains)
122122
$this->contains = $contains;
123123

124124
return $this;
125-
}
125+
}
126126

127127
/**
128128
* {@inheritdoc}
@@ -132,7 +132,7 @@ public function setNotContains(array $notContains)
132132
$this->notContains = $notContains;
133133

134134
return $this;
135-
}
135+
}
136136

137137
/**
138138
* {@inheritdoc}
@@ -142,7 +142,7 @@ public function setSizes(array $sizes)
142142
$this->sizes = $sizes;
143143

144144
return $this;
145-
}
145+
}
146146

147147
/**
148148
* {@inheritdoc}
@@ -152,7 +152,7 @@ public function setDates(array $dates)
152152
$this->dates = $dates;
153153

154154
return $this;
155-
}
155+
}
156156

157157
/**
158158
* {@inheritdoc}
@@ -162,7 +162,7 @@ public function setFilters(array $filters)
162162
$this->filters = $filters;
163163

164164
return $this;
165-
}
165+
}
166166

167167
/**
168168
* {@inheritdoc}
@@ -172,7 +172,7 @@ public function setSort($sort)
172172
$this->sort = $sort;
173173

174174
return $this;
175-
}
175+
}
176176

177177
/**
178178
* {@inheritdoc}
@@ -193,5 +193,4 @@ public function setNotPath(array $notPaths)
193193

194194
return $this;
195195
}
196-
197196
}

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

0 commit comments

Comments
 (0)