Skip to content

Commit ad6a6c8

Browse files
committed
[Finder] Re-enable BsdFindAdapter for Darwin shells and fix it
BSD find command needs "-E" switch to evaluate POSIX regex. Added the ability to Command to insert bits at a given index. On some systems (Mac OS X for example) php's function sys_get_temp_dir() returns a directory that is a symlink. This causes tests failures because expected paths are different from path returned by the adapter. So, has been added a realpath. The building of sorting command has been totally moved on the adapter so the sorting command for BsdFindAdapter has been fixed. The building of content filtering command has been moved on the adapter. In BsdFindAdapter version, -r switch has been replaced with initial grep that srips out blank lines, this way is compatible with BSD shell.
1 parent 06c3222 commit ad6a6c8

File tree

5 files changed

+118
-52
lines changed

5 files changed

+118
-52
lines changed

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

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -286,58 +286,25 @@ private function buildDatesFiltering(Command $command, array $dates)
286286

287287
/**
288288
* @param Command $command
289-
* @param array $contains
290-
* @param Boolean $not
289+
* @param string $sort
290+
*
291+
* @throws \InvalidArgumentException
291292
*/
292-
private function buildContentFiltering(Command $command, array $contains, $not = false)
293+
private function buildSorting(Command $command, $sort)
293294
{
294-
foreach ($contains as $contain) {
295-
$expr = Expression::create($contain);
296-
297-
// todo: avoid forking process for each $pattern by using multiple -e options
298-
$command
299-
->add('| xargs -r grep -I')
300-
->add($expr->isCaseSensitive() ? null : '-i')
301-
->add($not ? '-L' : '-l')
302-
->add('-Ee')->arg($expr->renderPattern());
303-
}
295+
$this->buildFormatSorting($command, $sort);
304296
}
305297

306298
/**
307299
* @param Command $command
308300
* @param string $sort
309-
*
310-
* @throws \InvalidArgumentException
311301
*/
312-
private function buildSorting(Command $command, $sort)
313-
{
314-
switch ($sort) {
315-
case SortableIterator::SORT_BY_NAME:
316-
$command->ins('sort')->add('| sort');
317-
318-
return;
319-
case SortableIterator::SORT_BY_TYPE:
320-
$format = '%y';
321-
break;
322-
case SortableIterator::SORT_BY_ACCESSED_TIME:
323-
$format = '%A@';
324-
break;
325-
case SortableIterator::SORT_BY_CHANGED_TIME:
326-
$format = '%C@';
327-
break;
328-
case SortableIterator::SORT_BY_MODIFIED_TIME:
329-
$format = '%T@';
330-
break;
331-
default:
332-
throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
333-
}
334-
335-
$this->buildFormatSorting($command, $format);
336-
}
302+
abstract protected function buildFormatSorting(Command $command, $sort);
337303

338304
/**
339305
* @param Command $command
340-
* @param string $format
306+
* @param array $contains
307+
* @param Boolean $not
341308
*/
342-
abstract protected function buildFormatSorting(Command $command, $format);
309+
abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);
343310
}

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

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Finder\Shell\Shell;
1515
use Symfony\Component\Finder\Shell\Command;
16+
use Symfony\Component\Finder\Iterator\SortableIterator;
17+
use Symfony\Component\Finder\Expression\Expression;
1618

1719
/**
1820
* Shell engine implementation using BSD find command.
@@ -34,21 +36,65 @@ public function getName()
3436
*/
3537
protected function canBeUsed()
3638
{
37-
// FIXME: this adapter does not work yet with Shell::TYPE_DARWIN
38-
return in_array($this->shell->getType(), array(Shell::TYPE_BSD)) && parent::canBeUsed();
39+
return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
3940
}
4041

4142
/**
4243
* {@inheritdoc}
4344
*/
44-
protected function buildFormatSorting(Command $command, $format)
45+
protected function buildFormatSorting(Command $command, $sort)
4546
{
47+
switch ($sort) {
48+
case SortableIterator::SORT_BY_NAME:
49+
$command->ins('sort')->add('| sort');
50+
51+
return;
52+
case SortableIterator::SORT_BY_TYPE:
53+
$format = '%HT';
54+
break;
55+
case SortableIterator::SORT_BY_ACCESSED_TIME:
56+
$format = '%a';
57+
break;
58+
case SortableIterator::SORT_BY_CHANGED_TIME:
59+
$format = '%c';
60+
break;
61+
case SortableIterator::SORT_BY_MODIFIED_TIME:
62+
$format = '%m';
63+
break;
64+
default:
65+
throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
66+
}
67+
4668
$command
47-
->get('find')
4869
->add('-print0 | xargs -0 stat -f')
49-
->arg($format.' %h/%f\\n')
50-
->add('| sort | cut')
51-
->arg('-d ')
52-
->arg('-f2-');
70+
->arg($format.'%t%N')
71+
->add('| sort | cut -f 2');
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
protected function buildFindCommand(Command $command, $dir)
78+
{
79+
parent::buildFindCommand($command, $dir)->addAtIndex('-E', 1);
80+
return $command;
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
protected function buildContentFiltering(Command $command, array $contains, $not = false)
87+
{
88+
foreach ($contains as $contain) {
89+
$expr = Expression::create($contain);
90+
91+
// todo: avoid forking process for each $pattern by using multiple -e options
92+
$command
93+
->add('| grep -v \'^$\'')
94+
->add('| xargs grep -I')
95+
->add($expr->isCaseSensitive() ? null : '-i')
96+
->add($not ? '-L' : '-l')
97+
->add('-Ee')->arg($expr->renderPattern());
98+
}
5399
}
54100
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Finder\Shell\Shell;
1515
use Symfony\Component\Finder\Shell\Command;
16+
use Symfony\Component\Finder\Iterator\SortableIterator;
17+
use Symfony\Component\Finder\Expression\Expression;
1618

1719
/**
1820
* Shell engine implementation using GNU find command.
@@ -32,8 +34,29 @@ public function getName()
3234
/**
3335
* {@inheritdoc}
3436
*/
35-
protected function buildFormatSorting(Command $command, $format)
37+
protected function buildFormatSorting(Command $command, $sort)
3638
{
39+
switch ($sort) {
40+
case SortableIterator::SORT_BY_NAME:
41+
$command->ins('sort')->add('| sort');
42+
43+
return;
44+
case SortableIterator::SORT_BY_TYPE:
45+
$format = '%y';
46+
break;
47+
case SortableIterator::SORT_BY_ACCESSED_TIME:
48+
$format = '%A@';
49+
break;
50+
case SortableIterator::SORT_BY_CHANGED_TIME:
51+
$format = '%C@';
52+
break;
53+
case SortableIterator::SORT_BY_MODIFIED_TIME:
54+
$format = '%T@';
55+
break;
56+
default:
57+
throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
58+
}
59+
3760
$command
3861
->get('find')
3962
->add('-printf')
@@ -59,4 +82,21 @@ protected function buildFindCommand(Command $command, $dir)
5982
{
6083
return parent::buildFindCommand($command, $dir)->add('-regextype posix-extended');
6184
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
protected function buildContentFiltering(Command $command, array $contains, $not = false)
90+
{
91+
foreach ($contains as $contain) {
92+
$expr = Expression::create($contain);
93+
94+
// todo: avoid forking process for each $pattern by using multiple -e options
95+
$command
96+
->add('| xargs -r grep -I')
97+
->add($expr->isCaseSensitive() ? null : '-i')
98+
->add($not ? '-L' : '-l')
99+
->add('-Ee')->arg($expr->renderPattern());
100+
}
101+
}
62102
}

src/Symfony/Component/Finder/Shell/Command.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,17 @@ public function join()
246246
function($bit) { return null !== $bit; }
247247
));
248248
}
249+
250+
/**
251+
* Insert a string or a Command instance before the bit at given position $index (index starts from 0).
252+
*
253+
* @param string|Command $bit
254+
*
255+
* @return Command The current Command instance
256+
*/
257+
public function addAtIndex($bit, $index)
258+
{
259+
array_splice($this->bits, $index, 0, $bit);
260+
return $this;
261+
}
249262
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function setUpBeforeClass()
2323
{
2424
parent::setUpBeforeClass();
2525

26-
self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
26+
self::$tmpDir = realpath(sys_get_temp_dir().'/symfony2_finder');
2727
}
2828

2929
public function testCreate()

0 commit comments

Comments
 (0)