1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Command; |
10
|
|
|
|
11
|
|
|
use Cecil\Command\ListContent\FileExtensionFilter; |
12
|
|
|
use Cecil\Command\ListContent\FilenameRecursiveTreeIterator; |
13
|
|
|
use RecursiveDirectoryIterator; |
14
|
|
|
use RecursiveTreeIterator; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class CommandShowContent extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $contentDir; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->setName('show:content') |
34
|
|
|
->setDescription('Show content') |
35
|
|
|
->setDefinition( |
36
|
|
|
new InputDefinition([ |
37
|
|
|
new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'), |
38
|
|
|
]) |
39
|
|
|
) |
40
|
|
|
->setHelp('Show content as tree.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
$this->contentDir = $this->getBuilder($output)->getConfig()->get('content.dir'); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$output->writeln(sprintf('<info>%s/</info>', $this->contentDir)); |
52
|
|
|
$pages = $this->getPagesTree($output); |
53
|
|
|
//if ($this->getConsole()->isUtf8()) { |
54
|
|
View Code Duplication |
$unicodeTreePrefix = function (RecursiveTreeIterator $tree) { |
|
|
|
|
55
|
|
|
$prefixParts = [ |
56
|
|
|
RecursiveTreeIterator::PREFIX_LEFT => ' ', |
57
|
|
|
RecursiveTreeIterator::PREFIX_MID_HAS_NEXT => '│ ', |
58
|
|
|
RecursiveTreeIterator::PREFIX_END_HAS_NEXT => '├ ', |
59
|
|
|
RecursiveTreeIterator::PREFIX_END_LAST => '└ ', |
60
|
|
|
]; |
61
|
|
|
foreach ($prefixParts as $part => $string) { |
62
|
|
|
$tree->setPrefixPart($part, $string); |
63
|
|
|
} |
64
|
|
|
}; |
65
|
|
|
$unicodeTreePrefix($pages); |
66
|
|
|
//} |
67
|
|
|
foreach ($pages as $page) { |
68
|
|
|
$output->writeln($page); |
69
|
|
|
} |
70
|
|
|
$output->writeln(''); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return a console displayable tree of pages. |
80
|
|
|
* |
81
|
|
|
* @param OutputInterface $output |
82
|
|
|
* |
83
|
|
|
* @throws \Exception |
84
|
|
|
* |
85
|
|
|
* @return FilenameRecursiveTreeIterator |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function getPagesTree(OutputInterface $output) |
88
|
|
|
{ |
89
|
|
|
$pagesPath = $this->getPath().'/'.$this->contentDir; |
90
|
|
|
if (!is_dir($pagesPath)) { |
91
|
|
|
throw new \Exception(sprintf('Invalid directory: %s.', $pagesPath)); |
92
|
|
|
} |
93
|
|
|
$dirIterator = new RecursiveDirectoryIterator($pagesPath, RecursiveDirectoryIterator::SKIP_DOTS); |
94
|
|
|
$dirIterator = new FileExtensionFilter($dirIterator, $this->getBuilder($output)->getConfig()->get('content.ext')); |
95
|
|
|
$pages = new FilenameRecursiveTreeIterator( |
96
|
|
|
$dirIterator, |
97
|
|
|
FilenameRecursiveTreeIterator::SELF_FIRST |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $pages; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.