TYPO3  7.6
RecursiveDirectoryIterator.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Finder\Iterator;
13 
16 
23 {
28 
32  private $rewindable;
33 
43  public function __construct($path, $flags, $ignoreUnreadableDirs = false)
44  {
45  if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
46  throw new \RuntimeException('This iterator only support returning current as fileinfo.');
47  }
48 
49  parent::__construct($path, $flags);
50  $this->ignoreUnreadableDirs = $ignoreUnreadableDirs;
51  }
52 
58  public function current()
59  {
60  return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
61  }
62 
68  public function getChildren()
69  {
70  try {
71  $children = parent::getChildren();
72 
73  if ($children instanceof self) {
74  // parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore
75  $children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;
76  }
77 
78  return $children;
79  } catch (\UnexpectedValueException $e) {
80  if ($this->ignoreUnreadableDirs) {
81  // If directory is unreadable and finder is set to ignore it, a fake empty content is returned.
82  return new \RecursiveArrayIterator(array());
83  } else {
84  throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
85  }
86  }
87  }
88 
92  public function rewind()
93  {
94  if (false === $this->isRewindable()) {
95  return;
96  }
97 
98  // @see https://bugs.php.net/bug.php?id=49104
99  parent::next();
100 
101  parent::rewind();
102  }
103 
109  public function isRewindable()
110  {
111  if (null !== $this->rewindable) {
112  return $this->rewindable;
113  }
114 
115  if (false !== $stream = @opendir($this->getPath())) {
116  $infos = stream_get_meta_data($stream);
117  closedir($stream);
118 
119  if ($infos['seekable']) {
120  return $this->rewindable = true;
121  }
122  }
123 
124  return $this->rewindable = false;
125  }
126 }