Skip to content

Commit

Permalink
merged branch vicb/finder-adapters (PR #6253)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #6253).

Commits
-------

e62b5f7 [Finder] cleanup, fixes, improvements

Discussion
----------

[Finder] cleanup, fixes, improvements
  • Loading branch information
fabpot committed Dec 10, 2012
2 parents ff6fa82 + 466bf9a commit 5d92734
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 160 deletions.
29 changes: 29 additions & 0 deletions Adapter/AbstractAdapter.php
Expand Up @@ -34,6 +34,22 @@ abstract class AbstractAdapter implements AdapterInterface
protected $paths = array();
protected $notPaths = array();

private static $areSupported = array();

/**
* {@inheritDoc}
*/
public function isSupported()
{
$name = $this->getName();

if (!array_key_exists($name, self::$areSupported)) {
self::$areSupported[$name] = $this->canBeUsed();
}

return self::$areSupported[$name];
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -193,4 +209,17 @@ public function setNotPath(array $notPaths)

return $this;
}

/**
* Returns whether the adapter is supported in the current environment.
*
* This method should be implemented in all adapters. Do not implement
* isSupported in the adapters as the generic implementation provides a cache
* layer.
*
* @see isSupported
*
* @return Boolean Whether the adapter is supported
*/
abstract protected function canBeUsed();
}
104 changes: 44 additions & 60 deletions Adapter/AbstractFindAdapter.php
Expand Up @@ -59,11 +59,11 @@ public function searchInDirectory($dir)
$find->add('-follow');
}

$find->add('-mindepth')->add($this->minDepth+1);
$find->add('-mindepth')->add($this->minDepth + 1);
// warning! INF < INF => true ; INF == INF => false ; INF === INF => true
// https://bugs.php.net/bug.php?id=9118
if (INF !== $this->maxDepth) {
$find->add('-maxdepth')->add($this->maxDepth+1);
$find->add('-maxdepth')->add($this->maxDepth + 1);
}

if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode) {
Expand Down Expand Up @@ -118,13 +118,14 @@ public function searchInDirectory($dir)
/**
* {@inheritdoc}
*/
public function isSupported()
protected function canBeUsed()
{
return $this->shell->testCommand('find');
}

/**
* @param Command $command
* @param string $dir
*
* @return Command
*/
Expand All @@ -140,7 +141,7 @@ protected function buildFindCommand(Command $command, $dir)
/**
* @param Command $command
* @param string[] $names
* @param bool $not
* @param Boolean $not
*/
private function buildNamesFiltering(Command $command, array $names, $not = false)
{
Expand Down Expand Up @@ -183,7 +184,8 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
* @param Command $command
* @param string $dir
* @param string[] $paths
* @param bool $not
* @param Boolean $not
*
* @return void
*/
private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
Expand Down Expand Up @@ -226,33 +228,23 @@ private function buildSizesFiltering(Command $command, array $sizes)
foreach ($sizes as $i => $size) {
$command->add($i > 0 ? '-and' : null);

if ('<=' === $size->getOperator()) {
$command->add('-size -'.($size->getTarget()+1).'c');
continue;
}

if ('<' === $size->getOperator()) {
$command->add('-size -'.$size->getTarget().'c');
continue;
switch ($size->getOperator()) {
case '<=':
$command->add('-size -' . ($size->getTarget() + 1) . 'c');
break;
case '>=':
$command->add('-size +'. ($size->getTarget() - 1) . 'c');
break;
case '>':
$command->add('-size +' . $size->getTarget() . 'c');
break;
case '!=':
$command->add('-size -' . $size->getTarget() . 'c');
$command->add('-size +' . $size->getTarget() . 'c');
case '<':
default:
$command->add('-size -' . $size->getTarget() . 'c');
}

if ('>=' === $size->getOperator()) {
$command->add('-size +'.($size->getTarget()-1).'c');
continue;
}

if ('>' === $size->getOperator()) {
$command->add('-size +'.$size->getTarget().'c');
continue;
}

if ('!=' === $size->getOperator()) {
$command->add('-size -'.$size->getTarget().'c');
$command->add('-size +'.$size->getTarget().'c');
continue;
}

$command->add('-size '.$size->getTarget().'c');
}
}

Expand All @@ -265,7 +257,7 @@ private function buildDatesFiltering(Command $command, array $dates)
foreach ($dates as $i => $date) {
$command->add($i > 0 ? '-and' : null);

$mins = (int) round((time()-$date->getTarget())/60);
$mins = (int) round((time()-$date->getTarget()) / 60);

if (0 > $mins) {
// mtime is in the future
Expand All @@ -274,39 +266,30 @@ private function buildDatesFiltering(Command $command, array $dates)
return;
}

if ('<=' === $date->getOperator()) {
$command->add('-mmin +'.($mins-1));
continue;
switch ($date->getOperator()) {
case '<=':
$command->add('-mmin +' . ($mins - 1));
break;
case '>=':
$command->add('-mmin -' . ($mins + 1));
break;
case '>':
$command->add('-mmin -' . $mins);
break;
case '!=':
$command->add('-mmin +' . $mins.' -or -mmin -' . $mins);
break;
case '<':
default:
$command->add('-mmin +' . $mins);
}

if ('<' === $date->getOperator()) {
$command->add('-mmin +'.$mins);
continue;
}

if ('>=' === $date->getOperator()) {
$command->add('-mmin -'.($mins+1));
continue;
}

if ('>' === $date->getOperator()) {
$command->add('-mmin -'.$mins);
continue;
}

if ('!=' === $date->getOperator()) {
$command->add('-mmin +'.$mins.' -or -mmin -'.$mins);
continue;
}

$command->add('-mmin '.$mins);
}
}

/**
* @param Command $command
* @param array $contains
* @param bool $not
* @param Boolean $not
*/
private function buildContentFiltering(Command $command, array $contains, $not = false)
{
Expand All @@ -323,8 +306,9 @@ private function buildContentFiltering(Command $command, array $contains, $not =
}

/**
* @param \Symfony\Component\Finder\Shell\Command $command
* @param string $sort
* @param Command $command
* @param string $sort
*
* @throws \InvalidArgumentException
*/
private function buildSorting(Command $command, $sort)
Expand Down
10 changes: 5 additions & 5 deletions Adapter/AdapterInterface.php
Expand Up @@ -17,14 +17,14 @@
interface AdapterInterface
{
/**
* @param bool $followLinks
* @param Boolean $followLinks
*
* @return AdapterInterface Current instance
*/
public function setFollowLinks($followLinks);

/**
* @param int $mode
* @param integer $mode
*
* @return AdapterInterface Current instance
*/
Expand Down Expand Up @@ -94,14 +94,14 @@ public function setDates(array $dates);
public function setFilters(array $filters);

/**
* @param \Closure|int $sort
* @param \Closure|integer $sort
*
* @return AdapterInterface Current instance
*/
public function setSort($sort);

/**
* @param array $path
* @param array $paths
*
* @return AdapterInterface Current instance
*/
Expand All @@ -124,7 +124,7 @@ public function searchInDirectory($dir);
/**
* Tests adapter support for current platform.
*
* @return bool
* @return Boolean
*/
public function isSupported();

Expand Down
17 changes: 11 additions & 6 deletions Adapter/BsdFindAdapter.php
Expand Up @@ -25,25 +25,30 @@ class BsdFindAdapter extends AbstractFindAdapter
/**
* {@inheritdoc}
*/
public function isSupported()
public function getName()
{
return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::isSupported();
return 'bsd_find';
}

/**
* {@inheritdoc}
*/
public function getName()
protected function canBeUsed()
{
return 'bsd_find';
return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
}

/**
* {@inheritdoc}
*/
protected function buildFormatSorting(Command $command, $format)
{
$command->get('find')->add('-print0 | xargs -0 stat -f')->arg($format.' %h/%f\\n')
->add('| sort | cut')->arg('-d ')->arg('-f2-');
$command
->get('find')
->add('-print0 | xargs -0 stat -f')
->arg($format.' %h/%f\\n')
->add('| sort | cut')
->arg('-d ')
->arg('-f2-');
}
}
20 changes: 13 additions & 7 deletions Adapter/GnuFindAdapter.php
Expand Up @@ -25,26 +25,32 @@ class GnuFindAdapter extends AbstractFindAdapter
/**
* {@inheritdoc}
*/
public function isSupported()
public function getName()
{
return $this->shell->getType() === Shell::TYPE_UNIX && parent::isSupported();
return 'gnu_find';
}

/**
* {@inheritdoc}
*/
public function getName()
protected function buildFormatSorting(Command $command, $format)
{
return 'gnu_find';
$command
->get('find')
->add('-printf')
->arg($format.' %h/%f\\n')
->add('| sort | cut')
->arg('-d ')
->arg('-f2-')
;
}

/**
* {@inheritdoc}
*/
protected function buildFormatSorting(Command $command, $format)
protected function canBeUsed()
{
$command->get('find')->add('-printf')->arg($format.' %h/%f\\n')
->add('| sort | cut')->arg('-d ')->arg('-f2-');
return $this->shell->getType() === Shell::TYPE_UNIX && parent::canBeUsed();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Adapter/PhpAdapter.php
Expand Up @@ -83,16 +83,16 @@ public function searchInDirectory($dir)
/**
* {@inheritdoc}
*/
public function isSupported()
public function getName()
{
return true;
return 'php';
}

/**
* {@inheritdoc}
*/
public function getName()
protected function canBeUsed()
{
return 'php';
return true;
}
}

0 comments on commit 5d92734

Please sign in to comment.