Skip to content
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

[Finder] Fixed support for Streams (eg. Phar archives) #7128

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/Symfony/Component/Finder/Adapter/AbstractAdapter.php
Expand Up @@ -34,20 +34,38 @@ abstract class AbstractAdapter implements AdapterInterface
protected $paths = array();
protected $notPaths = array();

private static $areSupported = array();
private static $areSupportedAdapters = array();
private static $areSupportedPaths = array();

/**
* {@inheritDoc}
*
* This generic implementation should not need to be overridden in the derived
* class as it provides a cache layer. The 2 following methods should rather
* be implemented:
*
* @see canBeUsed
* @see canBeUsedOnPath
*/
public function isSupported()
public function isSupported($path)
{
$name = $this->getName();
$canonicalPath = realpath($path);
$canonicalPath = false === $canonicalPath ? (string) $path : $canonicalPath;

if (!isset(self::$areSupportedAdapters[$name])) {
self::$areSupportedAdapters[$name] = $this->canBeUsed();
}

if (!array_key_exists($name, self::$areSupported)) {
self::$areSupported[$name] = $this->canBeUsed();
if (!self::$areSupportedAdapters[$name]) {
return false;
}

return self::$areSupported[$name];
if (!isset(self::$areSupportedPaths[$canonicalPath][$name])) {
self::$areSupportedPaths[$canonicalPath][$name] = $this->canBeUsedOnPath($canonicalPath);
}

return self::$areSupportedPaths[$canonicalPath][$name];
}

/**
Expand Down Expand Up @@ -213,13 +231,24 @@ public function setNotPath(array $notPaths)
/**
* 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();

/**
* Returns whether the adapter supports a given path.
*
* Some adapters only support resolvable absolute paths like the GNU/BSD
* find binary adapter. Others adapters might support PHP streams like "ftp://"
* or "phar://" like the PHP adapter.
*
* @param string Path to check
*
* @see isSupported
*
* @return Boolean Whether the adapter supports the given path
*/
abstract protected function canBeUsedOnPath($path);
}
13 changes: 13 additions & 0 deletions src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Finder\Iterator\SortableIterator;
use Symfony\Component\Finder\Comparator\NumberComparator;
use Symfony\Component\Finder\Comparator\DateComparator;
use Symfony\Component\Finder\Exception\AdapterFailureException;

/**
* Shell engine implementation using GNU find command.
Expand Down Expand Up @@ -47,6 +48,10 @@ public function searchInDirectory($dir)
// having "/../" in path make find fail
$dir = realpath($dir);

if (false === $dir) {
throw new AdapterFailureException($this, sprintf('"%s" is not readable', $dir));
}

// searching directories containing or not containing strings leads to no result
if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode && ($this->contains || $this->notContains)) {
return new Iterator\FilePathsIterator(array(), $dir);
Expand Down Expand Up @@ -123,6 +128,14 @@ protected function canBeUsed()
return $this->shell->testCommand('find');
}

/**
* {@inheritdoc}
*/
protected function canBeUsedOnPath($path)
{
return false !== realpath($path);
}

/**
* @param Command $command
* @param string $dir
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Finder/Adapter/AdapterInterface.php
Expand Up @@ -122,14 +122,14 @@ public function setNotPath(array $notPaths);
public function searchInDirectory($dir);

/**
* Tests adapter support for current platform.
* @param string $path
*
* @return Boolean
* @return Boolean Whether the adapter can be used on the given path.
*/
public function isSupported();
public function isSupported($path);

/**
* Returns adapter name.
* Returns the adapter name.
*
* @return string
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Finder/Adapter/PhpAdapter.php
Expand Up @@ -95,4 +95,21 @@ protected function canBeUsed()
{
return true;
}

/**
* {@inheritdoc}
*/
protected function canBeUsedOnPath($path)
{
if (false !== realpath($path)) {
return true;
}

if (false !== ($handle = @opendir($path))) {
closedir($handle);
return true;
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Finder.php
Expand Up @@ -722,7 +722,7 @@ private function searchInDirectory($dir)
}

foreach ($this->adapters as $adapter) {
if ($adapter['adapter']->isSupported()) {
if ($adapter['adapter']->isSupported($dir)) {
try {
return $this
->buildAdapter($adapter['adapter'])
Expand Down
Expand Up @@ -54,4 +54,12 @@ protected function canBeUsed()
{
return true;
}

/**
* {@inheritdoc}
*/
protected function canBeUsedOnPath($path)
{
return true;
}
}
Expand Up @@ -42,4 +42,12 @@ protected function canBeUsed()
{
return true;
}

/**
* {@inheritdoc}
*/
protected function canBeUsedOnPath($path)
{
return true;
}
}
Expand Up @@ -54,4 +54,12 @@ protected function canBeUsed()
{
return true;
}

/**
* {@inheritdoc}
*/
protected function canBeUsedOnPath($path)
{
return true;
}
}
Expand Up @@ -41,4 +41,12 @@ protected function canBeUsed()
{
return false;
}

/**
* {@inheritdoc}
*/
protected function canBeUsedOnPath($path)
{
return false;
}
}