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

Skip to content

[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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function __construct()
->addAdapter(new GnuFindAdapter())
->addAdapter(new BsdFindAdapter())
->addAdapter(new PhpAdapter(), -50)
->setAdapter('php')
;
}

Expand Down Expand Up @@ -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)
Copy link
Contributor

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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Supported" ?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

true but only when canBeUsed()returns true, right ?

Copy link
Contributor

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)

Copy link
Contributor Author

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.

Copy link
Contributor

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)

Copy link
Contributor Author

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?

Copy link
Contributor

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:

In src/Symfony/Component/Finder/Finder.php:

  • {
  •    $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)

Hmm, this is not stupid...
I'll open an RFC tomorrow, could you keep your ideas in mind and feed
comments with them?


Reply to this email directly or view it on GitHub
https://github.com/symfony/symfony/pull/7212/files#r3191594.

{
if (!isset($this->adapters[$name])) {
throw new \InvalidArgumentException(sprintf('Adapter "%s" does not exist.', $name));
}

$this->resetAdapterSelection();
$this->adapters[$name]['selected'] = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to call useBestAdapter() here, that seems counter-intuitive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, waht about creating a private method named resetAdapterSelection()?


return $this->sortAdapters();
}

/**
* Removes all adapters registered in the finder.
*
Expand Down Expand Up @@ -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;
});

Expand Down Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down