doc: Use Util/DirectoryIterator
This commit is contained in:
parent
076784f2c9
commit
e3c1734d1a
library/Icinga/File
modules/doc/library/Doc
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -4,11 +4,11 @@
|
|||
namespace Icinga\Module\Doc;
|
||||
|
||||
use CachingIterator;
|
||||
use LogicException;
|
||||
use SplFileObject;
|
||||
use SplStack;
|
||||
use Icinga\Data\Tree\SimpleTree;
|
||||
use Icinga\Exception\NotReadableError;
|
||||
use Icinga\Module\Doc\Exception\DocEmptyException;
|
||||
use Icinga\Util\DirectoryIterator;
|
||||
use Icinga\Module\Doc\Exception\DocException;
|
||||
|
||||
/**
|
||||
|
@ -40,7 +40,7 @@ class DocParser
|
|||
/**
|
||||
* Iterator over documentation files
|
||||
*
|
||||
* @var DocIterator
|
||||
* @var DirectoryIterator
|
||||
*/
|
||||
protected $docIterator;
|
||||
|
||||
|
@ -51,34 +51,17 @@ class DocParser
|
|||
*
|
||||
* @throws DocException If the documentation directory does not exist
|
||||
* @throws NotReadableError If the documentation directory is not readable
|
||||
* @throws DocEmptyException If the documentation directory is empty
|
||||
*/
|
||||
public function __construct($path)
|
||||
{
|
||||
if (! is_dir($path)) {
|
||||
if (! DirectoryIterator::isReadable($path)) {
|
||||
throw new DocException(
|
||||
sprintf(mt('doc', 'Documentation directory \'%s\' does not exist'), $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
|
||||
)
|
||||
mt('doc', 'Documentation directory \'%s\' is not readable'),
|
||||
$path
|
||||
);
|
||||
}
|
||||
$this->path = $path;
|
||||
$this->docIterator = $docIterator;
|
||||
$this->docIterator = new DirectoryIterator($path, 'md');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,9 +128,8 @@ class DocParser
|
|||
public function getDocTree()
|
||||
{
|
||||
$tree = new SimpleTree();
|
||||
foreach ($this->docIterator as $fileInfo) {
|
||||
/** @var $fileInfo \SplFileInfo */
|
||||
$file = $fileInfo->openFile();
|
||||
foreach ($this->docIterator as $filename) {
|
||||
$file = new SplFileObject($filename);
|
||||
$lastLine = null;
|
||||
$stack = new SplStack();
|
||||
$cachingIterator = new CachingIterator($file, CachingIterator::TOSTRING_USE_CURRENT);
|
||||
|
|
|
@ -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
|
||||
{
|
||||
}
|
|
@ -70,6 +70,9 @@ class DocTocRenderer extends DocRenderer
|
|||
*/
|
||||
public function render()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
return '<p>' . mt('doc', 'Documentation is empty.') . '</p>';
|
||||
}
|
||||
$view = $this->getView();
|
||||
$zendUrlHelper = $view->getHelper('Url');
|
||||
foreach ($this as $section) {
|
||||
|
|
Loading…
Reference in New Issue