Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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.
  • Loading branch information...
commit 3d90d9ac13ae4db2fd567672329fd1fc2c2a2894 1 parent ef0dba8
Manuele Menozzi mmenozzi authored
51 Adapter/AbstractFindAdapter.php
@@ -286,58 +286,25 @@ private function buildDatesFiltering(Command $command, array $dates)
286 286
287 287 /**
288 288 * @param Command $command
289   - * @param array $contains
290   - * @param Boolean $not
  289 + * @param string $sort
  290 + *
  291 + * @throws \InvalidArgumentException
291 292 */
292   - private function buildContentFiltering(Command $command, array $contains, $not = false)
  293 + private function buildSorting(Command $command, $sort)
293 294 {
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);
304 296 }
305 297
306 298 /**
307 299 * @param Command $command
308 300 * @param string $sort
309   - *
310   - * @throws \InvalidArgumentException
311 301 */
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);
337 303
338 304 /**
339 305 * @param Command $command
340   - * @param string $format
  306 + * @param array $contains
  307 + * @param Boolean $not
341 308 */
342   - abstract protected function buildFormatSorting(Command $command, $format);
  309 + abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);
343 310 }
62 Adapter/BsdFindAdapter.php
@@ -13,6 +13,8 @@
13 13
14 14 use Symfony\Component\Finder\Shell\Shell;
15 15 use Symfony\Component\Finder\Shell\Command;
  16 +use Symfony\Component\Finder\Iterator\SortableIterator;
  17 +use Symfony\Component\Finder\Expression\Expression;
16 18
17 19 /**
18 20 * Shell engine implementation using BSD find command.
@@ -34,21 +36,65 @@ public function getName()
34 36 */
35 37 protected function canBeUsed()
36 38 {
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();
39 40 }
40 41
41 42 /**
42 43 * {@inheritdoc}
43 44 */
44   - protected function buildFormatSorting(Command $command, $format)
  45 + protected function buildFormatSorting(Command $command, $sort)
45 46 {
  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 +
46 68 $command
47   - ->get('find')
48 69 ->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 + }
53 99 }
54 100 }
42 Adapter/GnuFindAdapter.php
@@ -13,6 +13,8 @@
13 13
14 14 use Symfony\Component\Finder\Shell\Shell;
15 15 use Symfony\Component\Finder\Shell\Command;
  16 +use Symfony\Component\Finder\Iterator\SortableIterator;
  17 +use Symfony\Component\Finder\Expression\Expression;
16 18
17 19 /**
18 20 * Shell engine implementation using GNU find command.
@@ -32,8 +34,29 @@ public function getName()
32 34 /**
33 35 * {@inheritdoc}
34 36 */
35   - protected function buildFormatSorting(Command $command, $format)
  37 + protected function buildFormatSorting(Command $command, $sort)
36 38 {
  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 +
37 60 $command
38 61 ->get('find')
39 62 ->add('-printf')
@@ -59,4 +82,21 @@ protected function buildFindCommand(Command $command, $dir)
59 82 {
60 83 return parent::buildFindCommand($command, $dir)->add('-regextype posix-extended');
61 84 }
  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 + }
62 102 }
13 Shell/Command.php
@@ -246,4 +246,17 @@ public function join()
246 246 function($bit) { return null !== $bit; }
247 247 ));
248 248 }
  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 + }
249 262 }
2  Tests/FinderTest.php
@@ -23,7 +23,7 @@ public static function setUpBeforeClass()
23 23 {
24 24 parent::setUpBeforeClass();
25 25
26   - self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
  26 + self::$tmpDir = realpath(sys_get_temp_dir().'/symfony2_finder');
27 27 }
28 28
29 29 public function testCreate()

0 comments on commit 3d90d9a

Please sign in to comment.
Something went wrong with that request. Please try again.