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

Skip to content

Commit e5be0de

Browse files
committed
merged branch jfsimon/finder-access-denied-exception (PR #7256)
This PR was merged into the master branch. Discussion ---------- [Finder] Adds AccessDeniedException | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6981 Commits ------- 714ace8 [finder] Introduced AccessDeniedException
2 parents 2f7a9fd + 714ace8 commit e5be0de

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Finder\Adapter;
1313

14+
use Symfony\Component\Finder\Exception\AccessDeniedException;
1415
use Symfony\Component\Finder\Iterator;
1516
use Symfony\Component\Finder\Shell\Shell;
1617
use Symfony\Component\Finder\Expression\Expression;
@@ -91,6 +92,10 @@ public function searchInDirectory($dir)
9192
$this->buildSorting($command, $this->sort);
9293
}
9394

95+
$command->setErrorHandler(function ($stderr) {
96+
throw new AccessDeniedException($stderr);
97+
});
98+
9499
$paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
95100
$iterator = new Iterator\FilePathsIterator($paths, $dir);
96101

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Finder\Exception;
4+
5+
/**
6+
* @author Jean-François Simon <[email protected]>
7+
*/
8+
class AccessDeniedException extends \UnexpectedValueException
9+
{
10+
}

src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Finder\Iterator;
1313

14+
use Symfony\Component\Finder\Exception\AccessDeniedException;
1415
use Symfony\Component\Finder\SplFileInfo;
1516

1617
/**
@@ -38,4 +39,18 @@ public function current()
3839
{
3940
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
4041
}
42+
43+
/**
44+
* @return mixed object
45+
*
46+
* @throws AccessDeniedException
47+
*/
48+
public function getChildren()
49+
{
50+
try {
51+
return parent::getChildren();
52+
} catch (\UnexpectedValueException $e) {
53+
throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
54+
}
55+
}
4156
}

src/Symfony/Component/Finder/Shell/Command.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class Command
3131
*/
3232
private $labels;
3333

34+
/**
35+
* @var \Closure|null
36+
*/
37+
private $errorHandler;
38+
3439
/**
3540
* Constructor.
3641
*
@@ -214,6 +219,26 @@ public function length()
214219
return count($this->bits);
215220
}
216221

222+
/**
223+
* @param \Closure $errorHandler
224+
*
225+
* @return Command
226+
*/
227+
public function setErrorHandler(\Closure $errorHandler)
228+
{
229+
$this->errorHandler = $errorHandler;
230+
231+
return $this;
232+
}
233+
234+
/**
235+
* @return callable|null
236+
*/
237+
public function getErrorHandler()
238+
{
239+
return $this->errorHandler;
240+
}
241+
217242
/**
218243
* Executes current command.
219244
*
@@ -223,10 +248,17 @@ public function length()
223248
*/
224249
public function execute()
225250
{
226-
exec($this->join(), $output, $code);
251+
if (null === $this->errorHandler) {
252+
exec($this->join(), $output);
253+
} else {
254+
$process = proc_open($this->join(), array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
255+
$output = preg_split('~(\r\n|\r|\n)~', stream_get_contents($pipes[1]), -1, PREG_SPLIT_NO_EMPTY);
256+
257+
if ($error = stream_get_contents($pipes[2])) {
258+
call_user_func($this->errorHandler, $error);
259+
}
227260

228-
if (0 !== $code) {
229-
throw new \RuntimeException('Execution failed with return code: '.$code.'.');
261+
proc_close($process);
230262
}
231263

232264
return $output ?: array();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,27 @@ public function getTestPathData()
717717
return $this->buildTestData($tests);
718718
}
719719

720+
/**
721+
* @dataProvider getAdaptersTestData
722+
*/
723+
public function testAccessDeniedException(Adapter\AdapterInterface $adapter)
724+
{
725+
$finder = $this->buildFinder($adapter);
726+
$finder->files()->in(self::$tmpDir);
727+
728+
// make 'foo' directory non-openable
729+
chmod(self::$tmpDir.DIRECTORY_SEPARATOR.'foo', 0333);
730+
731+
try {
732+
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->getIterator());
733+
$this->fail('Finder should throw an exception when opening a non-readable directory.');
734+
} catch (\Exception $e) {
735+
$this->assertEquals('Symfony\\Component\\Finder\\Exception\\AccessDeniedException', get_class($e));
736+
}
737+
738+
chmod(self::$tmpDir.DIRECTORY_SEPARATOR.'foo', 0777);
739+
}
740+
720741
private function buildTestData(array $tests)
721742
{
722743
$data = array();

0 commit comments

Comments
 (0)