[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.
This commit is contained in:
parent
06c3222d44
commit
ad6a6c8651
@ -284,25 +284,6 @@ abstract class AbstractFindAdapter extends AbstractAdapter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Command $command
|
|
||||||
* @param array $contains
|
|
||||||
* @param Boolean $not
|
|
||||||
*/
|
|
||||||
private 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Command $command
|
* @param Command $command
|
||||||
* @param string $sort
|
* @param string $sort
|
||||||
@ -311,33 +292,19 @@ abstract class AbstractFindAdapter extends AbstractAdapter
|
|||||||
*/
|
*/
|
||||||
private function buildSorting(Command $command, $sort)
|
private function buildSorting(Command $command, $sort)
|
||||||
{
|
{
|
||||||
switch ($sort) {
|
$this->buildFormatSorting($command, $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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Command $command
|
* @param Command $command
|
||||||
* @param string $format
|
* @param string $sort
|
||||||
*/
|
*/
|
||||||
abstract protected function buildFormatSorting(Command $command, $format);
|
abstract protected function buildFormatSorting(Command $command, $sort);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Command $command
|
||||||
|
* @param array $contains
|
||||||
|
* @param Boolean $not
|
||||||
|
*/
|
||||||
|
abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@ namespace Symfony\Component\Finder\Adapter;
|
|||||||
|
|
||||||
use Symfony\Component\Finder\Shell\Shell;
|
use Symfony\Component\Finder\Shell\Shell;
|
||||||
use Symfony\Component\Finder\Shell\Command;
|
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.
|
* Shell engine implementation using BSD find command.
|
||||||
@ -34,21 +36,65 @@ class BsdFindAdapter extends AbstractFindAdapter
|
|||||||
*/
|
*/
|
||||||
protected function canBeUsed()
|
protected function canBeUsed()
|
||||||
{
|
{
|
||||||
// FIXME: this adapter does not work yet with Shell::TYPE_DARWIN
|
return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
|
||||||
return in_array($this->shell->getType(), array(Shell::TYPE_BSD)) && parent::canBeUsed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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
|
$command
|
||||||
->get('find')
|
|
||||||
->add('-print0 | xargs -0 stat -f')
|
->add('-print0 | xargs -0 stat -f')
|
||||||
->arg($format.' %h/%f\\n')
|
->arg($format.'%t%N')
|
||||||
->add('| sort | cut')
|
->add('| sort | cut -f 2');
|
||||||
->arg('-d ')
|
}
|
||||||
->arg('-f2-');
|
|
||||||
|
/**
|
||||||
|
* {@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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@ namespace Symfony\Component\Finder\Adapter;
|
|||||||
|
|
||||||
use Symfony\Component\Finder\Shell\Shell;
|
use Symfony\Component\Finder\Shell\Shell;
|
||||||
use Symfony\Component\Finder\Shell\Command;
|
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.
|
* Shell engine implementation using GNU find command.
|
||||||
@ -32,8 +34,29 @@ class GnuFindAdapter extends AbstractFindAdapter
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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
|
$command
|
||||||
->get('find')
|
->get('find')
|
||||||
->add('-printf')
|
->add('-printf')
|
||||||
@ -59,4 +82,21 @@ class GnuFindAdapter extends AbstractFindAdapter
|
|||||||
{
|
{
|
||||||
return parent::buildFindCommand($command, $dir)->add('-regextype posix-extended');
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,4 +246,17 @@ class Command
|
|||||||
function($bit) { return null !== $bit; }
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
|||||||
{
|
{
|
||||||
parent::setUpBeforeClass();
|
parent::setUpBeforeClass();
|
||||||
|
|
||||||
self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
|
self::$tmpDir = realpath(sys_get_temp_dir().'/symfony2_finder');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreate()
|
public function testCreate()
|
||||||
|
Reference in New Issue
Block a user