Completed
Push — symfony-console ( 505007...6e7652 )
by Arnaud
02:41 queued 10s
created

ShowContent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 6
dl 0
loc 83
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 31 4
A getPagesTree() 0 15 2
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\ShowContent\FileExtensionFilter;
12
use Cecil\Command\ShowContent\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 ShowContent 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
            $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
    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