2011-02-15 23:18:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-15 23:18:23 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Finder\Iterator;
|
|
|
|
|
2013-02-12 15:28:26 +00:00
|
|
|
use Symfony\Component\Finder\Exception\AccessDeniedException;
|
2011-02-15 23:18:23 +00:00
|
|
|
use Symfony\Component\Finder\SplFileInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extends the \RecursiveDirectoryIterator to support relative paths
|
|
|
|
*
|
|
|
|
* @author Victor Berchet <victor@suumit.com>
|
|
|
|
*/
|
|
|
|
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
|
|
|
|
{
|
2013-03-29 14:47:08 +00:00
|
|
|
/**
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var bool
|
2013-03-29 14:47:08 +00:00
|
|
|
*/
|
|
|
|
private $ignoreUnreadableDirs;
|
|
|
|
|
2013-05-24 11:46:33 +01:00
|
|
|
/**
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var bool
|
2013-05-24 11:46:33 +01:00
|
|
|
*/
|
|
|
|
private $rewindable;
|
|
|
|
|
2013-03-29 14:47:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param int $flags
|
2014-04-12 18:54:57 +01:00
|
|
|
* @param bool $ignoreUnreadableDirs
|
2013-03-29 14:47:08 +00:00
|
|
|
*
|
|
|
|
* @throws \RuntimeException
|
|
|
|
*/
|
|
|
|
public function __construct($path, $flags, $ignoreUnreadableDirs = false)
|
2011-02-15 23:18:23 +00:00
|
|
|
{
|
|
|
|
if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
|
2011-03-21 09:37:49 +00:00
|
|
|
throw new \RuntimeException('This iterator only support returning current as fileinfo.');
|
2011-02-15 23:18:23 +00:00
|
|
|
}
|
2011-03-21 09:37:49 +00:00
|
|
|
|
2011-02-15 23:18:23 +00:00
|
|
|
parent::__construct($path, $flags);
|
2013-03-29 14:47:08 +00:00
|
|
|
$this->ignoreUnreadableDirs = $ignoreUnreadableDirs;
|
2011-02-15 23:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of SplFileInfo with support for relative paths
|
|
|
|
*
|
|
|
|
* @return SplFileInfo File information
|
|
|
|
*/
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
|
|
|
|
}
|
2013-02-12 15:28:26 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-29 14:47:08 +00:00
|
|
|
* @return \RecursiveIterator
|
2013-02-12 15:28:26 +00:00
|
|
|
*
|
|
|
|
* @throws AccessDeniedException
|
|
|
|
*/
|
|
|
|
public function getChildren()
|
|
|
|
{
|
|
|
|
try {
|
2014-04-23 21:22:53 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $children;
|
2013-02-12 15:28:26 +00:00
|
|
|
} catch (\UnexpectedValueException $e) {
|
2013-03-29 14:47:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-02-12 15:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-27 15:49:21 +01:00
|
|
|
|
2013-05-24 11:46:33 +01:00
|
|
|
/**
|
|
|
|
* Do nothing for non rewindable stream
|
|
|
|
*/
|
2013-05-24 09:59:59 +01:00
|
|
|
public function rewind()
|
|
|
|
{
|
2013-05-24 11:46:33 +01:00
|
|
|
if (false === $this->isRewindable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-24 09:59:59 +01:00
|
|
|
// @see https://bugs.php.net/bug.php?id=49104
|
|
|
|
parent::next();
|
|
|
|
|
|
|
|
parent::rewind();
|
|
|
|
}
|
2013-05-24 11:46:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the stream is rewindable.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool true when the stream is rewindable, false otherwise
|
2013-05-24 11:46:33 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|