[finder] Introduced AccessDeniedException
This exception is thrown when trying to open a non-readable directory. Squashed commits: [Finder] Fixed small errors [Finder] small changes about AccessDeniedException [Finder] removed unnecessary comment [Finder] added access denied exception test [Finder] Updated AccessDeniedException base class [Finder] fixed access denied exception test [Finder] native adapters now throw AccessDeniedException on stderr
This commit is contained in:
parent
9568768fd9
commit
714ace8250
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Finder\Adapter;
|
namespace Symfony\Component\Finder\Adapter;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Exception\AccessDeniedException;
|
||||||
use Symfony\Component\Finder\Iterator;
|
use Symfony\Component\Finder\Iterator;
|
||||||
use Symfony\Component\Finder\Shell\Shell;
|
use Symfony\Component\Finder\Shell\Shell;
|
||||||
use Symfony\Component\Finder\Expression\Expression;
|
use Symfony\Component\Finder\Expression\Expression;
|
||||||
@ -91,6 +92,10 @@ abstract class AbstractFindAdapter extends AbstractAdapter
|
|||||||
$this->buildSorting($command, $this->sort);
|
$this->buildSorting($command, $this->sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$command->setErrorHandler(function ($stderr) {
|
||||||
|
throw new AccessDeniedException($stderr);
|
||||||
|
});
|
||||||
|
|
||||||
$paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
|
$paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
|
||||||
$iterator = new Iterator\FilePathsIterator($paths, $dir);
|
$iterator = new Iterator\FilePathsIterator($paths, $dir);
|
||||||
|
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
|
||||||
|
*/
|
||||||
|
class AccessDeniedException extends \UnexpectedValueException
|
||||||
|
{
|
||||||
|
}
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Finder\Iterator;
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Exception\AccessDeniedException;
|
||||||
use Symfony\Component\Finder\SplFileInfo;
|
use Symfony\Component\Finder\SplFileInfo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,4 +39,18 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
|
|||||||
{
|
{
|
||||||
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
|
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed object
|
||||||
|
*
|
||||||
|
* @throws AccessDeniedException
|
||||||
|
*/
|
||||||
|
public function getChildren()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::getChildren();
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,11 @@ class Command
|
|||||||
*/
|
*/
|
||||||
private $labels;
|
private $labels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Closure|null
|
||||||
|
*/
|
||||||
|
private $errorHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
@ -214,6 +219,26 @@ class Command
|
|||||||
return count($this->bits);
|
return count($this->bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Closure $errorHandler
|
||||||
|
*
|
||||||
|
* @return Command
|
||||||
|
*/
|
||||||
|
public function setErrorHandler(\Closure $errorHandler)
|
||||||
|
{
|
||||||
|
$this->errorHandler = $errorHandler;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return callable|null
|
||||||
|
*/
|
||||||
|
public function getErrorHandler()
|
||||||
|
{
|
||||||
|
return $this->errorHandler;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes current command.
|
* Executes current command.
|
||||||
*
|
*
|
||||||
@ -223,10 +248,17 @@ class Command
|
|||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
exec($this->join(), $output, $code);
|
if (null === $this->errorHandler) {
|
||||||
|
exec($this->join(), $output);
|
||||||
|
} else {
|
||||||
|
$process = proc_open($this->join(), array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
|
||||||
|
$output = preg_split('~(\r\n|\r|\n)~', stream_get_contents($pipes[1]), -1, PREG_SPLIT_NO_EMPTY);
|
||||||
|
|
||||||
if (0 !== $code) {
|
if ($error = stream_get_contents($pipes[2])) {
|
||||||
throw new \RuntimeException('Execution failed with return code: '.$code.'.');
|
call_user_func($this->errorHandler, $error);
|
||||||
|
}
|
||||||
|
|
||||||
|
proc_close($process);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $output ?: array();
|
return $output ?: array();
|
||||||
|
@ -717,6 +717,27 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
|||||||
return $this->buildTestData($tests);
|
return $this->buildTestData($tests);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getAdaptersTestData
|
||||||
|
*/
|
||||||
|
public function testAccessDeniedException(Adapter\AdapterInterface $adapter)
|
||||||
|
{
|
||||||
|
$finder = $this->buildFinder($adapter);
|
||||||
|
$finder->files()->in(self::$tmpDir);
|
||||||
|
|
||||||
|
// make 'foo' directory non-openable
|
||||||
|
chmod(self::$tmpDir.DIRECTORY_SEPARATOR.'foo', 0333);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->getIterator());
|
||||||
|
$this->fail('Finder should throw an exception when opening a non-readable directory.');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->assertEquals('Symfony\\Component\\Finder\\Exception\\AccessDeniedException', get_class($e));
|
||||||
|
}
|
||||||
|
|
||||||
|
chmod(self::$tmpDir.DIRECTORY_SEPARATOR.'foo', 0777);
|
||||||
|
}
|
||||||
|
|
||||||
private function buildTestData(array $tests)
|
private function buildTestData(array $tests)
|
||||||
{
|
{
|
||||||
$data = array();
|
$data = array();
|
||||||
|
Reference in New Issue
Block a user