-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
ListContent.php
executable file
·68 lines (63 loc) · 2.16 KB
/
ListContent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* This file is part of the PHPoole package.
*
* Copyright (c) Arnaud Ligny <arnaud@ligny.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPoole\Command;
use PHPoole\Command\ListContent\FilenameRecursiveTreeIterator;
use PHPoole\PHPoole;
use RecursiveDirectoryIterator;
use RecursiveTreeIterator;
class ListContent extends AbstractCommand
{
public function processCommand()
{
try {
$this->wlAnnonce('Content:');
$pages = $this->getPagesTree();
if ($this->getConsole()->isUtf8()) {
$unicodeTreePrefix = function (RecursiveTreeIterator $tree) {
$prefixParts = [
RecursiveTreeIterator::PREFIX_LEFT => ' ',
RecursiveTreeIterator::PREFIX_MID_HAS_NEXT => '│ ',
RecursiveTreeIterator::PREFIX_END_HAS_NEXT => '├ ',
RecursiveTreeIterator::PREFIX_END_LAST => '└ ',
];
foreach ($prefixParts as $part => $string) {
$tree->setPrefixPart($part, $string);
}
};
$unicodeTreePrefix($pages);
}
foreach ($pages as $page) {
$this->getConsole()->writeLine($page);
}
} catch (\Exception $e) {
$this->wlError($e->getMessage());
}
}
/**
* Return a console displayable tree of pages.
*
* @throws \Exception
*
* @return FilenameRecursiveTreeIterator
*/
public function getPagesTree()
{
$pagesPath = $this->path.'/'.$this->getPHPoole()->getOption('content.dir');
if (!is_dir($pagesPath)) {
throw new \Exception(sprintf('Invalid %s directory', 'content'));
}
$dirIterator = new RecursiveDirectoryIterator($pagesPath, RecursiveDirectoryIterator::SKIP_DOTS);
$pages = new FilenameRecursiveTreeIterator(
$dirIterator,
FilenameRecursiveTreeIterator::SELF_FIRST
);
return $pages;
}
}