Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Finder] adds adapter selection/unselection capabilities

  • Loading branch information...
commit 2feb8d33876f64801bd1b14bdce829b314e2c418 1 parent de61913
@jfsimon jfsimon authored fabpot committed
Showing with 67 additions and 1 deletion.
  1. +52 −1 Finder.php
  2. +15 −0 Tests/FinderTest.php
View
53 Finder.php
@@ -69,6 +69,7 @@ public function __construct()
->addAdapter(new GnuFindAdapter())
->addAdapter(new BsdFindAdapter())
->addAdapter(new PhpAdapter(), -50)
+ ->setAdapter('php')
;
}
@@ -97,12 +98,46 @@ public function addAdapter(Adapter\AdapterInterface $adapter, $priority = 0)
$this->adapters[$adapter->getName()] = array(
'adapter' => $adapter,
'priority' => $priority,
+ 'selected' => false,
);
return $this->sortAdapters();
}
/**
+ * Sets the selected adapter to the best one according to the current platform the code is run on.
+ *
+ * @return Finder The current Finder instance
+ */
+ public function useBestAdapter()
+ {
+ $this->resetAdapterSelection();
+
+ return $this->sortAdapters();
+ }
+
+ /**
+ * Selects the adapter to use.
+ *
+ * @param string $name
+ *
+ * @throws \InvalidArgumentException
+ *
+ * @return Finder The current Finder instance
+ */
+ public function setAdapter($name)
+ {
+ if (!isset($this->adapters[$name])) {
+ throw new \InvalidArgumentException(sprintf('Adapter "%s" does not exist.', $name));
+ }
+
+ $this->resetAdapterSelection();
+ $this->adapters[$name]['selected'] = true;
+
+ return $this->sortAdapters();
+ }
+
+ /**
* Removes all adapters registered in the finder.
*
* @return Finder The current Finder instance
@@ -692,12 +727,16 @@ public function count()
return iterator_count($this->getIterator());
}
- /*
+ /**
* @return Finder The current Finder instance
*/
private function sortAdapters()
{
uasort($this->adapters, function (array $a, array $b) {
+ if ($a['selected'] || $b['selected']) {
+ return $a['selected'] ? -1 : 1;
+ }
+
return $a['priority'] > $b['priority'] ? -1 : 1;
});
@@ -757,4 +796,16 @@ private function buildAdapter(AdapterInterface $adapter)
->setPath($this->paths)
->setNotPath($this->notPaths);
}
+
+ /**
+ * Unselects all adapters.
+ */
+ private function resetAdapterSelection()
+ {
+ $this->adapters = array_map(function (array $properties) {
+ $properties['selected'] = false;
+
+ return $properties;
+ }, $this->adapters);
+ }
}
View
15 Tests/FinderTest.php
@@ -683,6 +683,21 @@ public function testPath(Adapter\AdapterInterface $adapter, $matchPatterns, $noM
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
}
+ public function testAdapterSelection()
+ {
+ // test that by default, PhpAdapter is selected
+ $adapters = Finder::create()->getAdapters();
+ $this->assertTrue($adapters[0] instanceof Adapter\PhpAdapter);
+
+ // test another adapter selection
+ $adapters = Finder::create()->setAdapter('gnu_find')->getAdapters();
+ $this->assertTrue($adapters[0] instanceof Adapter\GnuFindAdapter);
+
+ // test that useBestAdapter method removes selection
+ $adapters = Finder::create()->useBestAdapter()->getAdapters();
+ $this->assertFalse($adapters[0] instanceof Adapter\PhpAdapter);
+ }
+
public function getTestPathData()
{
$tests = array(
Please sign in to comment.
Something went wrong with that request. Please try again.