Config::execute()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 6
nop 2
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Console;
11
12
use FastD\Utils\FileObject;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class Config.
21
 */
22
class Config extends Command
23
{
24
    public function configure()
25
    {
26
        $this->setName('config');
27
        $this->addArgument('name', InputArgument::OPTIONAL, 'file name');
28
        $this->setDescription('Dump application config information.');
29
    }
30
31
    /**
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     *
35
     * @return mixed
36
     */
37
    public function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        if (($name = $input->getArgument('name'))) {
40
            if ('php' === pathinfo($name, PATHINFO_EXTENSION)) {
41
                $file = app()->getPath().'/config/'.$name;
42
                $config = load($file);
43
                $config = json_encode($config, JSON_PRETTY_PRINT);
44
            } else {
45
                $config = config()->get($name, null);
46
                if (null === $config) {
47
                    throw new \LogicException(sprintf('Config "%s" is not configure.', $name));
48
                }
49
50
                $config = [$name => $config];
51
                $config = json_encode($config, JSON_PRETTY_PRINT);
52
            }
53
54
            $output->write('<comment>'.$config.'</comment>');
55
56
            return 0;
57
        }
58
        $table = new Table($output);
59
        $rows = [];
60
        $table->setHeaders(array('File', 'Config', 'Owner', 'Modify At'));
61
62
        foreach (glob(app()->getPath().'/config/*') as $file) {
63
            $file = new FileObject($file);
64
            $config = load($file->getPathname());
65
            $count = 0;
66
            if (is_array($config)) {
67
                $count = count(array_keys($config));
68
            }
69
            $rows[] = [
70
                $file->getFilename(),
71
                $count.' Keys',
72
                posix_getpwuid($file->getOwner())['name'],
73
                date('Y-m-d H:i:s', $file->getMTime()),
74
            ];
75
        }
76
77
        $table->setRows($rows);
78
        $table->render();
79
    }
80
}
81