本文整理汇总了PHP中Iterator::setMaxDepth方法的典型用法代码示例。如果您正苦于以下问题:PHP Iterator::setMaxDepth方法的具体用法?PHP Iterator::setMaxDepth怎么用?PHP Iterator::setMaxDepth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Iterator
的用法示例。
在下文中一共展示了Iterator::setMaxDepth方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Constructs the iterator
* @param string $path The folder path
* @param int $depth The depth to recourse
* @param callable[] $filters An array of filters used to restrict the resulting list of files or folders
*/
public function __construct($path, $depth = null, array $filters = array())
{
//TODO: verify path exists and filters are callable
$this->path = (string) $path;
//create the iterator
$flags = \FilesystemIterator::SKIP_DOTS;
if ($depth === 0) {
$this->iterator = new \EmptyIterator();
} else {
if ($depth === 1) {
$this->iterator = new \FilesystemIterator($path, $flags);
} else {
$this->iterator = new \RecursiveDirectoryIterator($path, $flags);
$this->iterator = new \RecursiveIteratorIterator($this->iterator, \RecursiveIteratorIterator::SELF_FIRST);
if ($depth) {
$this->iterator->setMaxDepth($depth - 1);
}
}
}
//decorate iterator with filters
if (count($filters)) {
$this->iterator = new \CallbackFilterIterator($this->iterator, function ($path) use($filters) {
foreach ($filters as $filter) {
if (!$filter($path)) {
return false;
}
}
return true;
});
}
}