Permalink
Browse files

merged branch vicb/finder-adapters (PR #6253)

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...
2 parents ff6fa82 + 466bf9a commit 5d92734481a141d8a5ea72ee2c2d05155fc9a5f5 @fabpot fabpot committed Dec 10, 2012
@@ -34,6 +34,22 @@
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}
*/
@@ -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();
}
@@ -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) {
@@ -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
*/
@@ -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)
{
@@ -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)
@@ -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');
}
}
@@ -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
@@ -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)
{
@@ -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)
@@ -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
*/
@@ -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
*/
@@ -124,7 +124,7 @@ public function searchInDirectory($dir);
/**
* Tests adapter support for current platform.
*
- * @return bool
+ * @return Boolean
*/
public function isSupported();
@@ -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-');
}
}
@@ -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();
}
/**
@@ -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;
}
}
Oops, something went wrong. Retry.

0 comments on commit 5d92734

Please sign in to comment.