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; namespace Icinga\Module\Doc;
use ArrayIterator; 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 Countable;
use IteratorAggregate; use IteratorAggregate;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
/**
* Iterator over non-empty Markdown files ordered by the case insensitive "natural order" of file names
*/
class DocIterator implements Countable, IteratorAggregate 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) public function __construct($path)
{ {
$iter = new RecursiveIteratorIterator( $it = new RecursiveIteratorIterator(
new MarkdownFileIterator( new NonEmptyFileIterator(
new RecursiveDirectoryIterator($path) new MarkdownFileIterator(
new RecursiveDirectoryIterator($path)
)
) )
); );
$fileInfos = iterator_to_array($iter); // Unfortunately we have no chance to sort the iterator
natcasesort($fileInfos); $fileInfo = iterator_to_array($it);
$this->fileInfos = $fileInfos; natcasesort($fileInfo);
$this->fileInfo = $fileInfo;
} }
/**
* (non-PHPDoc)
* @see Countable::count()
*/
public function count() public function count()
{ {
return count($this->fileInfos); return count($this->fileInfo);
} }
/**
* (non-PHPDoc)
* @see IteratorAggregate::getIterator()
*/
public function getIterator() public function getIterator()
{ {
return new FileLockingIterator($this->fileInfos); return new ArrayIterator($this->fileInfo);
} }
} }