Completed
Branch symfony-console (0fa491)
by Kacper
03:12
created

FormattersCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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;
17
18
19
use Kadet\Highlighter\KeyLighter;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Helper\Table;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class FormattersCommand extends Command
27
{
28
    protected $formatters = [];
29
30
    protected function configure()
31
    {
32
        $this->setName('formatters')
33
            ->addOption('headerless', 'l', InputOption::VALUE_NONE, 'Output table without headers, useful for parsing')
34
            ->setDescription('Lists available formatters')
35
        ;
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $formatters = KeyLighter::get()->registeredFormatters();
41
        $table = new Table($output);
42
43
        if(!$input->getOption('headerless')) {
44
            $table->setHeaders(['Name', 'Formatter']);
45
        }
46
47
        $table->setRows(array_map(function($alias, $class) {
48
            return [
49
                "<comment>{$alias}</comment>",
50
                get_class($class)
51
            ];
52
        }, array_keys($formatters), array_values($formatters)));
53
54
        $table->setStyle($input->getOption('headerless') ? 'compact' : 'borderless');
55
        $table->render();
56
    }
57
}
58