4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Iterator;
14 use Symfony\Component\Finder\Exception\AccessDeniedException;
15 use Symfony\Component\Finder\SplFileInfo;
18 * Extends the \RecursiveDirectoryIterator to support relative paths.
20 * @author Victor Berchet <victor@suumit.com>
22 class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
27 private $ignoreUnreadableDirs;
34 // these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
37 private $directorySeparator = '/';
42 * @param bool $ignoreUnreadableDirs
44 * @throws \RuntimeException
46 public function __construct($path, $flags, $ignoreUnreadableDirs = false)
48 if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
49 throw new \RuntimeException('This iterator only support returning current as fileinfo.');
52 parent::__construct($path, $flags);
53 $this->ignoreUnreadableDirs = $ignoreUnreadableDirs;
54 $this->rootPath = (string) $path;
55 if ('/' !== DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) {
56 $this->directorySeparator = DIRECTORY_SEPARATOR;
61 * Return an instance of SplFileInfo with support for relative paths.
63 * @return SplFileInfo File information
65 public function current()
67 // the logic here avoids redoing the same work in all iterations
69 if (null === $subPathname = $this->subPath) {
70 $subPathname = $this->subPath = (string) $this->getSubPath();
72 if ('' !== $subPathname) {
73 $subPathname .= $this->directorySeparator;
75 $subPathname .= $this->getFilename();
77 return new SplFileInfo($this->rootPath.$this->directorySeparator.$subPathname, $this->subPath, $subPathname);
81 * @return \RecursiveIterator
83 * @throws AccessDeniedException
85 public function getChildren()
88 $children = parent::getChildren();
90 if ($children instanceof self) {
91 // parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore
92 $children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;
94 // performance optimization to avoid redoing the same work in all children
95 $children->rewindable = &$this->rewindable;
96 $children->rootPath = $this->rootPath;
100 } catch (\UnexpectedValueException $e) {
101 if ($this->ignoreUnreadableDirs) {
102 // If directory is unreadable and finder is set to ignore it, a fake empty content is returned.
103 return new \RecursiveArrayIterator(array());
105 throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
111 * Do nothing for non rewindable stream.
113 public function rewind()
115 if (false === $this->isRewindable()) {
119 // @see https://bugs.php.net/68557
120 if (\PHP_VERSION_ID < 50523 || \PHP_VERSION_ID >= 50600 && \PHP_VERSION_ID < 50607) {
128 * Checks if the stream is rewindable.
130 * @return bool true when the stream is rewindable, false otherwise
132 public function isRewindable()
134 if (null !== $this->rewindable) {
135 return $this->rewindable;
138 // workaround for an HHVM bug, should be removed when https://github.com/facebook/hhvm/issues/7281 is fixed
139 if ('' === $this->getPath()) {
140 return $this->rewindable = false;
143 if (false !== $stream = @opendir($this->getPath())) {
144 $infos = stream_get_meta_data($stream);
147 if ($infos['seekable']) {
148 return $this->rewindable = true;
152 return $this->rewindable = false;