Test Failed
Push — master ( 68289f...b25dc9 )
by huang
03:32
created

RouteDump::execute()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 5
nop 2
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @link      https://www.github.com/janhuang
7
 * @link      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Console;
11
12
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class Route
20
 * @package FastD\Console
21
 */
22
class RouteDump extends Command
23
{
24
    public function configure()
25
    {
26
        $this
27
            ->setName('route:dump')
28
            ->setHelp('Show all route')
29
            ->setDescription('Show you defined routes.')
30
        ;
31
    }
32
33
    /**
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     * @return mixed
37
     */
38
    public function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $table = new Table($output);
41
        $rows = [];
42
        $table->setHeaders(array('Name', 'Path', 'Method', 'Callback', 'Middleware'));
43
        foreach (route()->aliasMap as $routes) {
44
            foreach ($routes as $route) {
45
                $m = [];
46
                $middleware = $route->getMiddleware();
47
                if (is_array($middleware)) {
48
                    foreach ($middleware as $value) {
49
                        if (is_object($value)) {
50
                            $m[] = get_class($value);
51
                        } else {
52
                            $m[] = $value;
53
                        }
54
                    }
55
                } else if(is_object($middleware)) {
56
                    $m[] = get_class($middleware);
57
                }
58
                $rows[] = [
59
                    $route->getName(),
60
                    $route->getPath(),
61
                    $route->getMethod(),
62
                    $route->getCallback(),
63
                    implode(',', $m),
64
                ];
65
            }
66
67
        }
68
69
        $table->setRows($rows);
70
71
        $table->render();
72
    }
73
}