Passed
Branch symfony-console (1f7bcb)
by Kacper
03:39
created

GenerateTableCommand::generate()   C

Complexity

Conditions 8
Paths 72

Size

Total Lines 26
Code Lines 16

Duplication

Lines 9
Ratio 34.62 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 16
c 1
b 0
f 1
nc 72
nop 0
dl 9
loc 26
rs 5.3846
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 View Code Duplication
        foreach(KeyLighter::get()->registeredLanguages('name', true) as $name => $class) {
58
            $result[$class]['name'][] = $name;
59
        }
60
61 View Code Duplication
        foreach(KeyLighter::get()->registeredLanguages('mime', true) as $name => $class) {
62
            $result[$class]['mime'][] = $name;
63
        }
64
65 View Code Duplication
        foreach(KeyLighter::get()->registeredLanguages('extension', true) as $name => $class) {
66
            $result[$class]['extension'][] = $name;
67
        }
68
69
        $return  =  'Class | Name | MIME | Extension'.PHP_EOL;
70
        $return .=  '------|------|------|----------'.PHP_EOL;
71
        foreach($result as $class => $aliases) {
72
            $return .= '`'.$class.'` | ';
73
            $return .= (isset($aliases['name']) ? '`'.implode('`, `', $aliases['name']).'`' : 'none').' | ';
74
            $return .= (isset($aliases['mime']) ? '`'.implode('`, `', $aliases['mime']).'`' : 'none').' | ';
75
            $return .= (isset($aliases['extension']) ? '`'.implode('`, `', $aliases['extension']).'`' : 'none'). PHP_EOL;
76
        }
77
78
        return $return;
79
    }
80
}
81