Completed
Pull Request — master (#46)
by Jitendra
01:49
created

DocsCommand::prepare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Console;
13
14
use Ahc\Phint\Generator\TwigGenerator;
15
use CrazyFactory\DocBlocks\DocBlock;
16
17
class DocsCommand extends BaseCommand
18
{
19
    /** @var string Command name */
20
    protected $_name = 'docs';
21
22
    /** @var string Command description */
23
    protected $_desc = 'Generate basic readme docs from docblocks';
24
25
    /**
26
     * Configure the command options/arguments.
27
     *
28
     * @return void
29
     */
30
    protected function onConstruct()
31
    {
32
        $this
33
            ->option('-o --output', 'Output file (default README.md). For old project you should use something else'
34
                . "\n(OR mark region with <!-- DOCS START --> and <!-- DOCS END --> to inject docs)",
35
                null, 'README.md'
36
            )
37
            ->option('-a --with-abstract', 'Create docs for abstract/interface class')
38
            ->option('-x --template', "User supplied template path\nIt has higher precedence than inbuilt templates")
39
            ->usage(
40
                '<bold>  phint docs</end>               Appends to readme.md<eol/>' .
41
                '<bold>  phint d</end> <comment>-o docs/api.md</end>   Writes to docs/api.md<eol/>'
42
            );
43
    }
44
45
    /**
46
     * Generate test stubs.
47
     *
48
     * @return void
49
     */
50
    public function execute()
51
    {
52
        $io = $this->app()->io();
53
54
        $io->comment('Preparing metadata ...', true);
55
        $docsMetadata = $this->getClassesMetadata();
56
57
        if (empty($docsMetadata)) {
58
            $io->bgGreen('Looks like nothing to do here', true);
59
60
            return;
61
        }
62
63
        $io->comment('Generating docs ...', true);
64
        $generated = $this->generate($docsMetadata, $this->values());
65
66
        if ($generated) {
67
            $io->cyan("$generated doc(s) generated", true);
68
        }
69
70
        $io->ok('Done', true);
71
    }
72
73
    protected function generate(array $docsMetadata, array $parameters): int
74
    {
75
        $generator = new TwigGenerator($this->getTemplatePaths($parameters), $this->getCachePath());
76
77
        $parameters['output'] = $this->_pathUtil->expand($parameters['output'], $this->_workDir);
78
79
        return $generator->generateDocs($docsMetadata, $parameters);
80
    }
81
}
82