doc: Use Util/DirectoryIterator

This commit is contained in:
Eric Lippmann 2015-11-24 16:10:45 +01:00
parent 076784f2c9
commit e3c1734d1a
6 changed files with 12 additions and 219 deletions

View File

@ -1,70 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\File;
use FilterIterator;
use Iterator;
/**
* Iterator over files having a specific file extension
*
* Usage example:
* <code>
* <?php
*
* namespace Icinga\Example;
*
* use RecursiveDirectoryIterator;
* use RecursiveIteratorIterator;
* use Icinga\File\FileExtensionFilterIterator;
*
* $markdownFiles = new FileExtensionFilterIterator(
* new RecursiveIteratorIterator(
* new RecursiveDirectoryIterator(__DIR__),
* RecursiveIteratorIterator::SELF_FIRST
* ),
* 'md'
* );
* </code>
*/
class FileExtensionFilterIterator extends FilterIterator
{
/**
* The extension to filter for
*
* @var string
*/
protected $extension;
/**
* Create a new FileExtensionFilterIterator
*
* @param Iterator $iterator Apply filter to this iterator
* @param string $extension The file extension to filter for. The file extension may not contain the leading dot
*/
public function __construct(Iterator $iterator, $extension)
{
$this->extension = '.' . ltrim(strtolower((string) $extension), '.');
parent::__construct($iterator);
}
/**
* Accept files which match the file extension to filter for
*
* @return bool Whether the current element of the iterator is acceptable
* through this filter
*/
public function accept()
{
$current = $this->current();
/** @var $current \SplFileInfo */
if (! $current->isFile()) {
return false;
}
// SplFileInfo::getExtension() is only available since PHP 5 >= 5.3.6
$filename = $current->getFilename();
$sfx = substr($filename, -strlen($this->extension));
return $sfx === false ? false : strtolower($sfx) === $this->extension;
}
}

View File

@ -1,48 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\File;
use FilterIterator;
/**
* Iterator over non-empty files
*
* Usage example:
* <code>
* <?php
*
* namespace Icinga\Example;
*
* use RecursiveDirectoryIterator;
* use RecursiveIteratorIterator;
* use Icinga\File\NonEmptyFilterIterator;
*
* $nonEmptyFiles = new NonEmptyFileIterator(
* new RecursiveIteratorIterator(
* new RecursiveDirectoryIterator(__DIR__),
* RecursiveIteratorIterator::SELF_FIRST
* )
* );
* </code>
*/
class NonEmptyFileIterator extends FilterIterator
{
/**
* Accept non-empty files
*
* @return bool Whether the current element of the iterator is acceptable
* through this filter
*/
public function accept()
{
$current = $this->current();
/** @var $current \SplFileInfo */
if (! $current->isFile()
|| $current->getSize() === 0
) {
return false;
}
return true;
}
}

View File

@ -1,63 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Doc;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Icinga\File\NonEmptyFileIterator;
use Icinga\File\FileExtensionFilterIterator;
/**
* Iterator over non-empty Markdown files ordered by the case insensitive "natural order" of file names
*/
class DocIterator implements Countable, IteratorAggregate
{
/**
* Ordered files
*
* @var array
*/
protected $fileInfo;
/**
* Create a new DocIterator
*
* @param string $path Path to the documentation
*/
public function __construct($path)
{
$it = new FileExtensionFilterIterator(
new NonEmptyFileIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST
)
),
'md'
);
// Unfortunately we have no chance to sort the iterator
$fileInfo = iterator_to_array($it);
natcasesort($fileInfo);
$this->fileInfo = $fileInfo;
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->fileInfo);
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->fileInfo);
}
}

View File

@ -4,11 +4,11 @@
namespace Icinga\Module\Doc; namespace Icinga\Module\Doc;
use CachingIterator; use CachingIterator;
use LogicException; use SplFileObject;
use SplStack; use SplStack;
use Icinga\Data\Tree\SimpleTree; use Icinga\Data\Tree\SimpleTree;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Module\Doc\Exception\DocEmptyException; use Icinga\Util\DirectoryIterator;
use Icinga\Module\Doc\Exception\DocException; use Icinga\Module\Doc\Exception\DocException;
/** /**
@ -40,7 +40,7 @@ class DocParser
/** /**
* Iterator over documentation files * Iterator over documentation files
* *
* @var DocIterator * @var DirectoryIterator
*/ */
protected $docIterator; protected $docIterator;
@ -51,34 +51,17 @@ class DocParser
* *
* @throws DocException If the documentation directory does not exist * @throws DocException If the documentation directory does not exist
* @throws NotReadableError If the documentation directory is not readable * @throws NotReadableError If the documentation directory is not readable
* @throws DocEmptyException If the documentation directory is empty
*/ */
public function __construct($path) public function __construct($path)
{ {
if (! is_dir($path)) { if (! DirectoryIterator::isReadable($path)) {
throw new DocException( throw new DocException(
sprintf(mt('doc', 'Documentation directory \'%s\' does not exist'), $path) mt('doc', 'Documentation directory \'%s\' is not readable'),
); $path
}
if (! is_readable($path)) {
throw new DocException(
sprintf(mt('doc', 'Documentation directory \'%s\' is not readable'), $path)
);
}
$docIterator = new DocIterator($path);
if ($docIterator->count() === 0) {
throw new DocEmptyException(
sprintf(
mt(
'doc',
'Documentation directory \'%s\' does not contain any non-empty Markdown file (\'.md\' suffix)'
),
$path
)
); );
} }
$this->path = $path; $this->path = $path;
$this->docIterator = $docIterator; $this->docIterator = new DirectoryIterator($path, 'md');
} }
/** /**
@ -145,9 +128,8 @@ class DocParser
public function getDocTree() public function getDocTree()
{ {
$tree = new SimpleTree(); $tree = new SimpleTree();
foreach ($this->docIterator as $fileInfo) { foreach ($this->docIterator as $filename) {
/** @var $fileInfo \SplFileInfo */ $file = new SplFileObject($filename);
$file = $fileInfo->openFile();
$lastLine = null; $lastLine = null;
$stack = new SplStack(); $stack = new SplStack();
$cachingIterator = new CachingIterator($file, CachingIterator::TOSTRING_USE_CURRENT); $cachingIterator = new CachingIterator($file, CachingIterator::TOSTRING_USE_CURRENT);

View File

@ -1,11 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Doc\Exception;
/**
* Exception thrown if a documentation directory is empty
*/
class DocEmptyException extends DocException
{
}

View File

@ -70,6 +70,9 @@ class DocTocRenderer extends DocRenderer
*/ */
public function render() public function render()
{ {
if (empty($this->content)) {
return '<p>' . mt('doc', 'Documentation is empty.') . '</p>';
}
$view = $this->getView(); $view = $this->getView();
$zendUrlHelper = $view->getHelper('Url'); $zendUrlHelper = $view->getHelper('Url');
foreach ($this as $section) { foreach ($this as $section) {