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 = '/';
44 * @param bool $ignoreUnreadableDirs
46 * @throws \RuntimeException
48 public function __construct($path, $flags, $ignoreUnreadableDirs = false)
50 if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
51 throw new \RuntimeException('This iterator only support returning current as fileinfo.');
54 parent::__construct($path, $flags);
55 $this->ignoreUnreadableDirs = $ignoreUnreadableDirs;
56 $this->rootPath = (string) $path;
57 if ('/' !== DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) {
58 $this->directorySeparator = DIRECTORY_SEPARATOR;
63 * Return an instance of SplFileInfo with support for relative paths.
65 * @return SplFileInfo File information
67 public function current()
69 // the logic here avoids redoing the same work in all iterations
71 if (null === $subPathname = $this->subPath) {
72 $subPathname = $this->subPath = (string) $this->getSubPath();
74 if ('' !== $subPathname) {
75 $subPathname .= $this->directorySeparator;
77 $subPathname .= $this->getFilename();
79 return new SplFileInfo($this->rootPath.$this->directorySeparator.$subPathname, $this->subPath, $subPathname);
83 * @return \RecursiveIterator
85 * @throws AccessDeniedException
87 public function getChildren()
90 $children = parent::getChildren();
92 if ($children instanceof self) {
93 // parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore
94 $children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;
96 // performance optimization to avoid redoing the same work in all children
97 $children->rewindable = &$this->rewindable;
98 $children->rootPath = $this->rootPath;
102 } catch (\UnexpectedValueException $e) {
103 if ($this->ignoreUnreadableDirs) {
104 // If directory is unreadable and finder is set to ignore it, a fake empty content is returned.
105 return new \RecursiveArrayIterator(array());
107 throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
113 * Do nothing for non rewindable stream.
115 public function rewind()
117 if (false === $this->isRewindable()) {
121 // @see https://bugs.php.net/68557
122 if (\PHP_VERSION_ID < 50523 || \PHP_VERSION_ID >= 50600 && \PHP_VERSION_ID < 50607) {
130 * Checks if the stream is rewindable.
132 * @return bool true when the stream is rewindable, false otherwise
134 public function isRewindable()
136 if (null !== $this->rewindable) {
137 return $this->rewindable;
140 // workaround for an HHVM bug, should be removed when https://github.com/facebook/hhvm/issues/7281 is fixed
141 if ('' === $this->getPath()) {
142 return $this->rewindable = false;
145 if (false !== $stream = @opendir($this->getPath())) {
146 $infos = stream_get_meta_data($stream);
149 if ($infos['seekable']) {
150 return $this->rewindable = true;
154 return $this->rewindable = false;