Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06560df

Browse files
committed
merged branch jfsimon/finder-adapter-selection (PR #7212)
This PR was squashed before being merged into the 2.2 branch (closes #7212). Commits ------- f40adbc [Finder] adds adapter selection/unselection capabilities Discussion ---------- [Finder] adds adapter selection/unselection capabilities As we have many issues with the native finder adapter, it would be good to: * permit selection of a particular adapter which will be choosed over the others until its unselection * permit unselection of adapters to get the finder select trhe best on (by priority) * have `PhpAdapter` selected by default This PR adds 2 methods to the `Finder`: * `setAdapter($adapter)`: selects an adapter by its name * `useBestAdapter()`: tells the finder to select best adapter by their priority | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes --------------------------------------------------------------------------- by jfsimon at 2013-02-28T14:05:25Z @fabpot I just applied all your good advices, there was no bad one :)
2 parents 9c46043 + f40adbc commit 06560df

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Symfony/Component/Finder/Finder.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function __construct()
6969
->addAdapter(new GnuFindAdapter())
7070
->addAdapter(new BsdFindAdapter())
7171
->addAdapter(new PhpAdapter(), -50)
72+
->setAdapter('php')
7273
;
7374
}
7475

@@ -97,11 +98,45 @@ public function addAdapter(Adapter\AdapterInterface $adapter, $priority = 0)
9798
$this->adapters[$adapter->getName()] = array(
9899
'adapter' => $adapter,
99100
'priority' => $priority,
101+
'selected' => false,
100102
);
101103

102104
return $this->sortAdapters();
103105
}
104106

107+
/**
108+
* Sets the selected adapter to the best one according to the current platform the code is run on.
109+
*
110+
* @return Finder The current Finder instance
111+
*/
112+
public function useBestAdapter()
113+
{
114+
$this->resetAdapterSelection();
115+
116+
return $this->sortAdapters();
117+
}
118+
119+
/**
120+
* Selects the adapter to use.
121+
*
122+
* @param string $name
123+
*
124+
* @throws \InvalidArgumentException
125+
*
126+
* @return Finder The current Finder instance
127+
*/
128+
public function setAdapter($name)
129+
{
130+
if (!isset($this->adapters[$name])) {
131+
throw new \InvalidArgumentException(sprintf('Adapter "%s" does not exist.', $name));
132+
}
133+
134+
$this->resetAdapterSelection();
135+
$this->adapters[$name]['selected'] = true;
136+
137+
return $this->sortAdapters();
138+
}
139+
105140
/**
106141
* Removes all adapters registered in the finder.
107142
*
@@ -692,12 +727,16 @@ public function count()
692727
return iterator_count($this->getIterator());
693728
}
694729

695-
/*
730+
/**
696731
* @return Finder The current Finder instance
697732
*/
698733
private function sortAdapters()
699734
{
700735
uasort($this->adapters, function (array $a, array $b) {
736+
if ($a['selected'] || $b['selected']) {
737+
return $a['selected'] ? -1 : 1;
738+
}
739+
701740
return $a['priority'] > $b['priority'] ? -1 : 1;
702741
});
703742

@@ -757,4 +796,16 @@ private function buildAdapter(AdapterInterface $adapter)
757796
->setPath($this->paths)
758797
->setNotPath($this->notPaths);
759798
}
799+
800+
/**
801+
* Unselects all adapters.
802+
*/
803+
private function resetAdapterSelection()
804+
{
805+
$this->adapters = array_map(function (array $properties) {
806+
$properties['selected'] = false;
807+
808+
return $properties;
809+
}, $this->adapters);
810+
}
760811
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,21 @@ public function testPath(Adapter\AdapterInterface $adapter, $matchPatterns, $noM
683683
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
684684
}
685685

686+
public function testAdapterSelection()
687+
{
688+
// test that by default, PhpAdapter is selected
689+
$adapters = Finder::create()->getAdapters();
690+
$this->assertTrue($adapters[0] instanceof Adapter\PhpAdapter);
691+
692+
// test another adapter selection
693+
$adapters = Finder::create()->setAdapter('gnu_find')->getAdapters();
694+
$this->assertTrue($adapters[0] instanceof Adapter\GnuFindAdapter);
695+
696+
// test that useBestAdapter method removes selection
697+
$adapters = Finder::create()->useBestAdapter()->getAdapters();
698+
$this->assertFalse($adapters[0] instanceof Adapter\PhpAdapter);
699+
}
700+
686701
public function getTestPathData()
687702
{
688703
$tests = array(

0 commit comments

Comments
 (0)