Passed
Push — master ( 322a58...f1cf97 )
by Robbie
03:11
created

DumpCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace SilverLeague\Console\Command\Config;
4
5
use SilverLeague\Console\Command\Config\AbstractConfigCommand;
6
use SilverStripe\Core\Convert;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Outputs a combined representation of all SilverStripe configuration
14
 *
15
 * @package silverstripe-console
16
 * @author  Robbie Averill <[email protected]>
17
 */
18
class DumpCommand extends AbstractConfigCommand
19
{
20
    /**
21
     * @var string|null
22
     */
23
    protected $filter;
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('config:dump')
32
            ->setDescription('Dumps all of the processed configuration properties and their values')
33
            ->addOption('filter', null, InputOption::VALUE_REQUIRED, 'Filter the results (search)');
34
35
        $this->setHelp(<<<HELP
36
Dumps all of the processed configuration properties and their values. You can optionally add the --filter option
37
with a search value to narrow the results.
38
HELP
39
        );
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     * @param InputInterface $input
45
     * @param OutputInterface $output
46
     * @throws InvalidArgumentException If an invalid configuration type is provided
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $this->filter = $input->getOption('filter');
51
52
        $data = $this->getParsedOutput($this->getConfig()->getAll());
53
        if ($this->filter) {
54
            $data = $this->filterOutput($data, $this->filter);
55
        }
56
57
        $table = new Table($output);
58
        $table
59
            ->setHeaders(['Class name', 'Property', 'Key', 'Value'])
60
            ->setRows($data)
61
            ->render();
62
    }
63
64
    /**
65
     * Creates a table-friendly output array from the input configuration source
66
     *
67
     * @param  array $data
68
     * @return array
69
     */
70
    protected function getParsedOutput($data)
71
    {
72
        $output = [];
73
        foreach ($data as $className => $classInfo) {
74
            foreach ($classInfo as $property => $values) {
75
                $row = [$className, $property];
76
                if (is_array($values) || is_object($values)) {
77
                    foreach ($values as $key => $value) {
78
                        $row[] = is_numeric($key) ? '' : $key;
79
                        if (is_array($value)) {
80
                            $value = Convert::raw2json($value, JSON_PRETTY_PRINT);
81
                        } elseif (is_object($value)) {
82
                            $value = get_class($value);
83
                        } else {
84
                            $value = (string) $value;
85
                        }
86
                        $row[] = $value;
87
                        if (array_filter($row) != []) {
88
                            $output[] = $row;
89
                        }
90
                        // We need the class and property data for second level values if we're filtering.
91
                        if ($this->filter !== null) {
92
                            $row = [$className, $property];
93
                        } else {
94
                            $row = ['', ''];
95
                        }
96
                    }
97
                } else {
98
                    $row[] = '';
99
                    $row[] = $values;
100
                }
101
102
                if (array_filter($row) != []) {
103
                    $output[] = $row;
104
                }
105
            }
106
        }
107
        return $output;
108
    }
109
110
    /**
111
     * Apply a search filter to the results
112
     *
113
     * @param  array  $data   The pre-parsed output data
114
     * @param  string $filter The string to search on
115
     * @return array          Rows that have a string match on any of their fields
116
     */
117
    protected function filterOutput($data, $filter)
118
    {
119
        $output = [];
120
        foreach ($data as $values) {
121
            foreach ($values as $value) {
122
                if (is_string($value) && stripos($value, $filter) !== false) {
123
                    $output[] = $values;
124
                }
125
            }
126
        }
127
        return $output;
128
    }
129
}
130