Passed
Branch symfony-console (24af16)
by Kacper
03:39
created

GenerateTableCommand::generate()   B

Complexity

Conditions 7
Paths 27

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 7
eloc 13
c 2
b 0
f 1
nc 27
nop 0
dl 0
loc 20
rs 8.2222
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\KeyLighter;
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 GenerateTableCommand extends Command
26
{
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        if($input->getOption('dry')) {
30
            $output->writeln($this->generate());
31
        } else {
32
            $path = __DIR__ . '/../../../Docs/languages.md';
33
34
            $output->writeln("<info>Opening file ./Docs/languages.md ...</info>", OutputInterface::VERBOSITY_VERBOSE);
35
            $content = file_get_contents($path);
36
            file_put_contents($path, preg_replace(
37
                '/^<!-- aliasbegin -->\R.*?^<!-- aliasend -->\R/ms',
38
                "<!-- aliasbegin -->\n".$this->generate()."<!-- aliasend -->\n",
39
                $content
40
            ));
41
            $output->writeln("<info>Closing file ./Docs/languages.md ...</info>", OutputInterface::VERBOSITY_VERBOSE);
42
        }
43
    }
44
45
    protected function configure()
46
    {
47
        $this
48
            ->setName('dev:generate-table')
49
            ->setDescription('Generates language table for documentation')
50
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry run (output table to stdout)')
51
        ;
52
    }
53
54
    protected function generate()
55
    {
56
        $result = [];
57
        foreach(['name', 'mime', 'extension'] as $what) {
58
            foreach(KeyLighter::get()->registeredLanguages($what, true) as $name => $class) {
59
                $result[$class][$what][] = $name;
60
            }
61
        }
62
63
        $return  =  'Class | Name | MIME | Extension'.PHP_EOL;
64
        $return .=  '------|------|------|----------'.PHP_EOL;
65
        foreach($result as $class => $aliases) {
66
            $return .= '`'.$class.'` | ';
67
            $return .= (isset($aliases['name']) ? '`'.implode('`, `', $aliases['name']).'`' : 'none').' | ';
68
            $return .= (isset($aliases['mime']) ? '`'.implode('`, `', $aliases['mime']).'`' : 'none').' | ';
69
            $return .= (isset($aliases['extension']) ? '`'.implode('`, `', $aliases['extension']).'`' : 'none'). PHP_EOL;
70
        }
71
72
        return $return;
73
    }
74
}
75