doc/DocIterator: Use `NonEmptyFileIterator'

Considering empty files makes no sense.

refs #4820
This commit is contained in:
Eric Lippmann 2014-07-28 18:59:42 +02:00
parent 4f8cbb99dd
commit c325c09293
1 changed files with 33 additions and 38 deletions

View File

@ -5,63 +5,58 @@
namespace Icinga\Module\Doc;
use ArrayIterator;
use RunetimeException;
class FileLockingIterator extends ArrayIterator
{
public function next()
{
$this->current()->flock(LOCK_UN);
parent::next();
}
public function valid()
{
if (!parent::valid()) {
return false;
}
$fileInfo = $this->current();
try {
$fileObject = $fileInfo->openFile();
} catch (RuntimeException $e) {
throw new DocException($e->getMessage());
}
if ($fileObject->flock(LOCK_SH) === false) {
throw new DocException('Couldn\'t get the lock');
}
$this[$this->key()] = $fileObject;
return true;
}
}
use Countable;
use IteratorAggregate;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
/**
* Iterator over non-empty Markdown files ordered by the case insensitive "natural order" of file names
*/
class DocIterator implements Countable, IteratorAggregate
{
protected $fileInfos;
/**
* Ordered files
*
* @var array
*/
protected $fileInfo;
/**
* Create a new DocIterator
*
* @param string $path Path to the documentation
*/
public function __construct($path)
{
$iter = new RecursiveIteratorIterator(
new MarkdownFileIterator(
new RecursiveDirectoryIterator($path)
$it = new RecursiveIteratorIterator(
new NonEmptyFileIterator(
new MarkdownFileIterator(
new RecursiveDirectoryIterator($path)
)
)
);
$fileInfos = iterator_to_array($iter);
natcasesort($fileInfos);
$this->fileInfos = $fileInfos;
// Unfortunately we have no chance to sort the iterator
$fileInfo = iterator_to_array($it);
natcasesort($fileInfo);
$this->fileInfo = $fileInfo;
}
/**
* (non-PHPDoc)
* @see Countable::count()
*/
public function count()
{
return count($this->fileInfos);
return count($this->fileInfo);
}
/**
* (non-PHPDoc)
* @see IteratorAggregate::getIterator()
*/
public function getIterator()
{
return new FileLockingIterator($this->fileInfos);
return new ArrayIterator($this->fileInfo);
}
}