-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Finder] adds adapter selection/unselection capabilities #7212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ public function __construct() | |
->addAdapter(new GnuFindAdapter()) | ||
->addAdapter(new BsdFindAdapter()) | ||
->addAdapter(new PhpAdapter(), -50) | ||
->setAdapter('php') | ||
; | ||
} | ||
|
||
|
@@ -97,11 +98,45 @@ 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to call useBestAdapter() here, that seems counter-intuitive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, waht about creating a private method named |
||
|
||
return $this->sortAdapters(); | ||
} | ||
|
||
/** | ||
* Removes all adapters registered in the finder. | ||
* | ||
|
@@ -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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have something like
getSupportedAdapeters
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getAdapters
https://github.com/jfsimon/symfony/blob/44241bd27eaa357e2b77de810ad347963234af51/src/Symfony/Component/Finder/Finder.php#L157There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Supported" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adapters support is determined in
searchInDirectory
.There is no way to get them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true but only when
canBeUsed()
returns true, right ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I am not sure if my suggestion makes sense, just asking)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be useful?
I can't see any use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't know... I have to think more about it.
What triggers the idea is
setAdapter
(ie you would not want to set something that is not supported)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is not stupid...
I'll open an RFC tomorrow, could you keep your ideas in mind and feed comments with them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, please also link this comment. thanks.
chat to you tomorrow.
On 02/28/2013 05:49 PM, Jean-François Simon wrote: