3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Finder; /** * Extends \SplFileInfo to support relative paths. * * @author Fabien Potencier <fabien@symfony.com> */ class SplFileInfo extends \SplFileInfo { private $relativePath; private $relativePathname; /** * Constructor. * * @param string $file The file name * @param string $relativePath The relative path * @param string $relativePathname The relative path name */ public function __construct($file, $relativePath, $relativePathname) { parent::__construct($file); $this->relativePath = $relativePath; $this->relativePathname = $relativePathname; } /** * Returns the relative path. * * @return string the relative path */ public function getRelativePath() { return $this->relativePath; } /** * Returns the relative path name. * * @return string the relative path name */ public function getRelativePathname() { return $this->relativePathname; } /** * Returns the contents of the file. * * @return string the contents of the file * * @throws \RuntimeException */ public function getContents() { $level = error_reporting(0); $content = file_get_contents($this->getPathname()); error_reporting($level); if (false === $content) { $error = error_get_last(); throw new \RuntimeException($error['message']); } return $content; } } /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Finder\Iterator; use Symfony\Component\Finder\Exception\AccessDeniedException; use Symfony\Component\Finder\SplFileInfo; /** * Extends the \RecursiveDirectoryIterator to support relative paths. * * @author Victor Berchet <victor@suumit.com> */ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator { /** * @var bool */ private $ignoreUnreadableDirs; /** * @var bool */ private $rewindable; // these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations private $rootPath; private $subPath = false; private $directorySeparator = '/'; /** * Constructor. * * @param string $path * @param int $flags * @param bool $ignoreUnreadableDirs * * @throws \RuntimeException */ public function __construct($path, $flags, $ignoreUnreadableDirs = false) { if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) { throw new \RuntimeException('This iterator only support returning current as fileinfo.'); } parent::__construct($path, $flags); $this->ignoreUnreadableDirs = $ignoreUnreadableDirs; if ('/' !== DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) { $this->directorySeparator = DIRECTORY_SEPARATOR; } } /** * Return an instance of SplFileInfo with support for relative paths. * * @return SplFileInfo File information */ public function current() { // the logic here avoids redoing the same work in all iterations if (null === $this->rootPath) { $this->rootPath = $this->getPath(); } if (false === $subPathname = $this->subPath) { $subPathname = $this->subPath = $this->getSubPath(); } if ('' !== $subPathname) { $subPathname .= $this->directorySeparator; } $subPathname .= $this->getFilename(); var_dump($this); return new SplFileInfo($this->rootPath.$this->directorySeparator.$subPathname, $this->subPath, $subPathname); } /** * @return \RecursiveIterator * * @throws AccessDeniedException */ public function getChildren() { try { $children = parent::getChildren(); if ($children instanceof self) { // parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore $children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs; // performance optimization to avoid redoing the same work in all children $children->rewindable = &$this->rewindable; $children->rootPath = $this->rootPath; } return $children; } catch (\UnexpectedValueException $e) { if ($this->ignoreUnreadableDirs) { // If directory is unreadable and finder is set to ignore it, a fake empty content is returned. return new \RecursiveArrayIterator(array()); } else { throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e); } } } /** * Do nothing for non rewindable stream. */ public function rewind() { if (false === $this->isRewindable()) { return; } // @see https://bugs.php.net/bug.php?id=49104 parent::next(); parent::rewind(); } /** * Checks if the stream is rewindable. * * @return bool true when the stream is rewindable, false otherwise */ public function isRewindable() { if (null !== $this->rewindable) { return $this->rewindable; } if (false !== $stream = @opendir($this->getPath())) { $infos = stream_get_meta_data($stream); closedir($stream); if ($infos['seekable']) { return $this->rewindable = true; } } return $this->rewindable = false; } } $r = new RecursiveDirectoryIterator('./', RecursiveDirectoryIterator::SKIP_DOTS, true); $r = new \RecursiveIteratorIterator($r); foreach ($r as $f) { echo $f, "\n"; exit; }
based on oKD7c
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(./): failed to open dir: Permission denied in /in/IoBvW:130 Stack trace: #0 /in/IoBvW(130): RecursiveDirectoryIterator->__construct('./', 4096) #1 /in/IoBvW(230): Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator->__construct('./', 4096, true) #2 {main} thrown in /in/IoBvW on line 130
Process exited with code 255.
Output for 5.5.24 - 5.6.28
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(./): failed to open dir: Permission denied' in /in/IoBvW:130 Stack trace: #0 /in/IoBvW(130): RecursiveDirectoryIterator->__construct('./', 4096) #1 /in/IoBvW(230): Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator->__construct('./', 4096, true) #2 {main} thrown in /in/IoBvW on line 130
Process exited with code 255.
Output for 5.3.18 - 5.4.45
object(Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator)#1 (9) { ["ignoreUnreadableDirs":"Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator":private]=> bool(true) ["rewindable":"Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator":private]=> bool(true) ["rootPath":"Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator":private]=> string(1) "." ["subPath":"Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator":private]=> string(0) "" ["directorySeparator":"Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator":private]=> string(1) "/" ["pathName":"SplFileInfo":private]=> string(5) "./bin" ["fileName":"SplFileInfo":private]=> string(3) "bin" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } ./bin