Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Finder\Iterator;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Finder\Exception\AccessDeniedException;
|
Chris@0
|
15 use Symfony\Component\Finder\SplFileInfo;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Extends the \RecursiveDirectoryIterator to support relative paths.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Victor Berchet <victor@suumit.com>
|
Chris@0
|
21 */
|
Chris@0
|
22 class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
|
Chris@0
|
23 {
|
Chris@0
|
24 /**
|
Chris@0
|
25 * @var bool
|
Chris@0
|
26 */
|
Chris@0
|
27 private $ignoreUnreadableDirs;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * @var bool
|
Chris@0
|
31 */
|
Chris@0
|
32 private $rewindable;
|
Chris@0
|
33
|
Chris@0
|
34 // these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
|
Chris@0
|
35 private $rootPath;
|
Chris@0
|
36 private $subPath;
|
Chris@0
|
37 private $directorySeparator = '/';
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * @param string $path
|
Chris@0
|
41 * @param int $flags
|
Chris@0
|
42 * @param bool $ignoreUnreadableDirs
|
Chris@0
|
43 *
|
Chris@0
|
44 * @throws \RuntimeException
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct($path, $flags, $ignoreUnreadableDirs = false)
|
Chris@0
|
47 {
|
Chris@0
|
48 if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
|
Chris@0
|
49 throw new \RuntimeException('This iterator only support returning current as fileinfo.');
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 parent::__construct($path, $flags);
|
Chris@0
|
53 $this->ignoreUnreadableDirs = $ignoreUnreadableDirs;
|
Chris@0
|
54 $this->rootPath = $path;
|
Chris@17
|
55 if ('/' !== \DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) {
|
Chris@17
|
56 $this->directorySeparator = \DIRECTORY_SEPARATOR;
|
Chris@0
|
57 }
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Return an instance of SplFileInfo with support for relative paths.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return SplFileInfo File information
|
Chris@0
|
64 */
|
Chris@0
|
65 public function current()
|
Chris@0
|
66 {
|
Chris@0
|
67 // the logic here avoids redoing the same work in all iterations
|
Chris@0
|
68
|
Chris@0
|
69 if (null === $subPathname = $this->subPath) {
|
Chris@0
|
70 $subPathname = $this->subPath = (string) $this->getSubPath();
|
Chris@0
|
71 }
|
Chris@0
|
72 if ('' !== $subPathname) {
|
Chris@0
|
73 $subPathname .= $this->directorySeparator;
|
Chris@0
|
74 }
|
Chris@0
|
75 $subPathname .= $this->getFilename();
|
Chris@0
|
76
|
Chris@0
|
77 return new SplFileInfo($this->rootPath.$this->directorySeparator.$subPathname, $this->subPath, $subPathname);
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * @return \RecursiveIterator
|
Chris@0
|
82 *
|
Chris@0
|
83 * @throws AccessDeniedException
|
Chris@0
|
84 */
|
Chris@0
|
85 public function getChildren()
|
Chris@0
|
86 {
|
Chris@0
|
87 try {
|
Chris@0
|
88 $children = parent::getChildren();
|
Chris@0
|
89
|
Chris@0
|
90 if ($children instanceof self) {
|
Chris@0
|
91 // parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore
|
Chris@0
|
92 $children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;
|
Chris@0
|
93
|
Chris@0
|
94 // performance optimization to avoid redoing the same work in all children
|
Chris@0
|
95 $children->rewindable = &$this->rewindable;
|
Chris@0
|
96 $children->rootPath = $this->rootPath;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 return $children;
|
Chris@0
|
100 } catch (\UnexpectedValueException $e) {
|
Chris@0
|
101 if ($this->ignoreUnreadableDirs) {
|
Chris@0
|
102 // If directory is unreadable and finder is set to ignore it, a fake empty content is returned.
|
Chris@17
|
103 return new \RecursiveArrayIterator([]);
|
Chris@0
|
104 } else {
|
Chris@0
|
105 throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Do nothing for non rewindable stream.
|
Chris@0
|
112 */
|
Chris@0
|
113 public function rewind()
|
Chris@0
|
114 {
|
Chris@0
|
115 if (false === $this->isRewindable()) {
|
Chris@0
|
116 return;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 // @see https://bugs.php.net/68557
|
Chris@0
|
120 if (\PHP_VERSION_ID < 50523 || \PHP_VERSION_ID >= 50600 && \PHP_VERSION_ID < 50607) {
|
Chris@0
|
121 parent::next();
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 parent::rewind();
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Checks if the stream is rewindable.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @return bool true when the stream is rewindable, false otherwise
|
Chris@0
|
131 */
|
Chris@0
|
132 public function isRewindable()
|
Chris@0
|
133 {
|
Chris@0
|
134 if (null !== $this->rewindable) {
|
Chris@0
|
135 return $this->rewindable;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 // workaround for an HHVM bug, should be removed when https://github.com/facebook/hhvm/issues/7281 is fixed
|
Chris@0
|
139 if ('' === $this->getPath()) {
|
Chris@0
|
140 return $this->rewindable = false;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 if (false !== $stream = @opendir($this->getPath())) {
|
Chris@0
|
144 $infos = stream_get_meta_data($stream);
|
Chris@0
|
145 closedir($stream);
|
Chris@0
|
146
|
Chris@0
|
147 if ($infos['seekable']) {
|
Chris@0
|
148 return $this->rewindable = true;
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 return $this->rewindable = false;
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|