Passed
Push — master ( c4f4b2...52e931 )
by Kacper
03:28
created

GenerateAliasesCommand::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 23
c 2
b 0
f 1
nc 4
nop 2
dl 0
loc 32
rs 8.5806
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\bin\Commands\Dev;
17
18
19
use Kadet\Highlighter\Language\Language;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class GenerateAliasesCommand extends Command
26
{
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        if($input->getOption('dry')) {
30
            $output->writeln($this->generate($output));
31
        } else {
32
            file_put_contents(
33
                __DIR__ . '/../../../Config/aliases.php',
34
                '<?php return '.$this->generate($output).';'
35
            );
36
        }
37
    }
38
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('dev:aliases')
43
            ->setDescription('Generates Config/aliases.php file')
44
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry run (output to stdout instead of file)')
45
        ;
46
    }
47
48
    protected function generate(OutputInterface $output)
49
    {
50
        $dir = __DIR__ . '/../../../Language'.DIRECTORY_SEPARATOR;
51
        $iterator = new \RecursiveIteratorIterator(
52
            new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
53
            \RecursiveIteratorIterator::LEAVES_ONLY
54
        );
55
56
        $result = [];
57
58
        /** @var \SplFileInfo $file */
59
        foreach ($iterator as $file) {
60
            $class = '\Kadet\Highlighter\Language\\'.str_replace([$dir, '.php'], '', $file->getPathname());
61
62
            $output->writeln(sprintf(
63
                'Found <info>%s</info>, assuming class <language>%s</language>',
64
                $file->getBasename(), $class
65
            ), OutputInterface::VERBOSITY_VERBOSE);
66
67
68
            if($metadata = $this->process($output, $class)) {
69
                $result[] = $metadata;
70
            }
71
        }
72
73
        return var_export($result, true);
74
    }
75
76
    /**
77
     * @param OutputInterface $output
78
     * @param                 $class
79
     *
80
     * @return array|false
81
     */
82
    protected function process(OutputInterface $output, $class)
83
    {
84
        $reflection = new \ReflectionClass($class);
85
        if ($reflection->isAbstract()) {
86
            $output->writeln(sprintf(
87
                '<language>%s</language> is abstract, skipping...',
88
                $reflection->name
89
            ), OutputInterface::VERBOSITY_VERBOSE);
90
            return false;
91
        }
92
93
        if (!$reflection->isSubclassOf(Language::class)) {
94
            $output->writeln(sprintf(
95
                '<language>%s</language> is not Language, skipping...',
96
                $reflection->name
97
            ), OutputInterface::VERBOSITY_VERBOSE);
98
            return false;
99
        }
100
101
        if ($reflection->getMethod('getAliases')->getDeclaringClass()->name !== $reflection->name) {
102
            $output->writeln(sprintf(
103
                '<language>%s</language>::<info>getAliases</info> is not declared, skipping...',
104
                $reflection->name
105
            ), OutputInterface::VERBOSITY_VERBOSE);
106
            return false;
107
        }
108
109
        $result = array_merge([$reflection->name], call_user_func([$reflection->name, 'getAliases']));
110
        $output->writeln(var_export($result, true), OutputInterface::VERBOSITY_VERBOSE);
111
112
        return $result;
113
    }
114
}
115