Skip to content
Browse files

[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...
1 parent ef0dba8 commit 3d90d9ac13ae4db2fd567672329fd1fc2c2a2894 @mmenozzi mmenozzi committed Dec 19, 2012
Showing with 118 additions and 52 deletions.
  1. +9 −42 Adapter/AbstractFindAdapter.php
  2. +54 −8 Adapter/BsdFindAdapter.php
  3. +41 −1 Adapter/GnuFindAdapter.php
  4. +13 −0 Shell/Command.php
  5. +1 −1 Tests/FinderTest.php
View
51 Adapter/AbstractFindAdapter.php
@@ -286,58 +286,25 @@ private function buildDatesFiltering(Command $command, array $dates)
/**
* @param Command $command
- * @param array $contains
- * @param Boolean $not
+ * @param string $sort
+ *
+ * @throws \InvalidArgumentException
*/
- private function buildContentFiltering(Command $command, array $contains, $not = false)
+ private function buildSorting(Command $command, $sort)
{
- foreach ($contains as $contain) {
- $expr = Expression::create($contain);
-
- // todo: avoid forking process for each $pattern by using multiple -e options
- $command
- ->add('| xargs -r grep -I')
- ->add($expr->isCaseSensitive() ? null : '-i')
- ->add($not ? '-L' : '-l')
- ->add('-Ee')->arg($expr->renderPattern());
- }
+ $this->buildFormatSorting($command, $sort);
}
/**
* @param Command $command
* @param string $sort
- *
- * @throws \InvalidArgumentException
*/
- private function buildSorting(Command $command, $sort)
- {
- switch ($sort) {
- case SortableIterator::SORT_BY_NAME:
- $command->ins('sort')->add('| sort');
-
- return;
- case SortableIterator::SORT_BY_TYPE:
- $format = '%y';
- break;
- case SortableIterator::SORT_BY_ACCESSED_TIME:
- $format = '%A@';
- break;
- case SortableIterator::SORT_BY_CHANGED_TIME:
- $format = '%C@';
- break;
- case SortableIterator::SORT_BY_MODIFIED_TIME:
- $format = '%T@';
- break;
- default:
- throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
- }
-
- $this->buildFormatSorting($command, $format);
- }
+ abstract protected function buildFormatSorting(Command $command, $sort);
/**
* @param Command $command
- * @param string $format
+ * @param array $contains
+ * @param Boolean $not
*/
- abstract protected function buildFormatSorting(Command $command, $format);
+ abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);
}
View
62 Adapter/BsdFindAdapter.php
@@ -13,6 +13,8 @@
use Symfony\Component\Finder\Shell\Shell;
use Symfony\Component\Finder\Shell\Command;
+use Symfony\Component\Finder\Iterator\SortableIterator;
+use Symfony\Component\Finder\Expression\Expression;
/**
* Shell engine implementation using BSD find command.
@@ -34,21 +36,65 @@ public function getName()
*/
protected function canBeUsed()
{
- // FIXME: this adapter does not work yet with Shell::TYPE_DARWIN
- return in_array($this->shell->getType(), array(Shell::TYPE_BSD)) && parent::canBeUsed();
+ return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
}
/**
* {@inheritdoc}
*/
- protected function buildFormatSorting(Command $command, $format)
+ protected function buildFormatSorting(Command $command, $sort)
{
+ switch ($sort) {
+ case SortableIterator::SORT_BY_NAME:
+ $command->ins('sort')->add('| sort');
+
+ return;
+ case SortableIterator::SORT_BY_TYPE:
+ $format = '%HT';
+ break;
+ case SortableIterator::SORT_BY_ACCESSED_TIME:
+ $format = '%a';
+ break;
+ case SortableIterator::SORT_BY_CHANGED_TIME:
+ $format = '%c';
+ break;
+ case SortableIterator::SORT_BY_MODIFIED_TIME:
+ $format = '%m';
+ break;
+ default:
+ throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
+ }
+
$command
- ->get('find')
->add('-print0 | xargs -0 stat -f')
- ->arg($format.' %h/%f\\n')
- ->add('| sort | cut')
- ->arg('-d ')
- ->arg('-f2-');
+ ->arg($format.'%t%N')
+ ->add('| sort | cut -f 2');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function buildFindCommand(Command $command, $dir)
+ {
+ parent::buildFindCommand($command, $dir)->addAtIndex('-E', 1);
+ return $command;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function buildContentFiltering(Command $command, array $contains, $not = false)
+ {
+ foreach ($contains as $contain) {
+ $expr = Expression::create($contain);
+
+ // todo: avoid forking process for each $pattern by using multiple -e options
+ $command
+ ->add('| grep -v \'^$\'')
+ ->add('| xargs grep -I')
+ ->add($expr->isCaseSensitive() ? null : '-i')
+ ->add($not ? '-L' : '-l')
+ ->add('-Ee')->arg($expr->renderPattern());
+ }
}
}
View
42 Adapter/GnuFindAdapter.php
@@ -13,6 +13,8 @@
use Symfony\Component\Finder\Shell\Shell;
use Symfony\Component\Finder\Shell\Command;
+use Symfony\Component\Finder\Iterator\SortableIterator;
+use Symfony\Component\Finder\Expression\Expression;
/**
* Shell engine implementation using GNU find command.
@@ -32,8 +34,29 @@ public function getName()
/**
* {@inheritdoc}
*/
- protected function buildFormatSorting(Command $command, $format)
+ protected function buildFormatSorting(Command $command, $sort)
{
+ switch ($sort) {
+ case SortableIterator::SORT_BY_NAME:
+ $command->ins('sort')->add('| sort');
+
+ return;
+ case SortableIterator::SORT_BY_TYPE:
+ $format = '%y';
+ break;
+ case SortableIterator::SORT_BY_ACCESSED_TIME:
+ $format = '%A@';
+ break;
+ case SortableIterator::SORT_BY_CHANGED_TIME:
+ $format = '%C@';
+ break;
+ case SortableIterator::SORT_BY_MODIFIED_TIME:
+ $format = '%T@';
+ break;
+ default:
+ throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
+ }
+
$command
->get('find')
->add('-printf')
@@ -59,4 +82,21 @@ protected function buildFindCommand(Command $command, $dir)
{
return parent::buildFindCommand($command, $dir)->add('-regextype posix-extended');
}
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function buildContentFiltering(Command $command, array $contains, $not = false)
+ {
+ foreach ($contains as $contain) {
+ $expr = Expression::create($contain);
+
+ // todo: avoid forking process for each $pattern by using multiple -e options
+ $command
+ ->add('| xargs -r grep -I')
+ ->add($expr->isCaseSensitive() ? null : '-i')
+ ->add($not ? '-L' : '-l')
+ ->add('-Ee')->arg($expr->renderPattern());
+ }
+ }
}
View
13 Shell/Command.php
@@ -246,4 +246,17 @@ public function join()
function($bit) { return null !== $bit; }
));
}
+
+ /**
+ * Insert a string or a Command instance before the bit at given position $index (index starts from 0).
+ *
+ * @param string|Command $bit
+ *
+ * @return Command The current Command instance
+ */
+ public function addAtIndex($bit, $index)
+ {
+ array_splice($this->bits, $index, 0, $bit);
+ return $this;
+ }
}
View
2 Tests/FinderTest.php
@@ -23,7 +23,7 @@ public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
- self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
+ self::$tmpDir = realpath(sys_get_temp_dir().'/symfony2_finder');
}
public function testCreate()

0 comments on commit 3d90d9a

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