Passed
Push — master ( 6ce97f...1fe2f9 )
by huang
05:13
created

Routing::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 9.4285
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\Http\ServerRequest;
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\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class Route.
22
 */
23
class Routing extends Command
24
{
25
    public function configure()
26
    {
27
        $this
28
            ->setName('route')
29
            ->setHelp('Show all route')
30
            ->setDescription('Show you defined routes.')
31
            ->addArgument('method', InputArgument::OPTIONAL, 'Route request method.')
32
            ->addArgument('path', InputArgument::OPTIONAL, 'Route path.')
33
            ->addOption('data', 'd', InputOption::VALUE_OPTIONAL, 'Path request data.')
34
            ->addOption('all', 'a', InputOption::VALUE_OPTIONAL, 'Test all routes')
35
        ;
36
    }
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     *
42
     * @return mixed
43
     */
44
    public function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        if (empty($path = $input->getArgument('path'))) {
47
            $this->render($input, $output);
48
        } else {
49
            $this->test($input->getArgument('method'), $path, $input, $output);
50
        }
51
52
        return 0;
53
    }
54
55
    /**
56
     * @param $method
57
     * @param $path
58
     * @param InputInterface  $input
59
     * @param OutputInterface $output
60
     */
61
    protected function test($method, $path, InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $request = new ServerRequest($method, $path, [
64
            'User-Agent' => 'FastD Console/'.version(),
65
        ]);
66
67
        $response = app()->handleRequest($request);
68
69
        $output->writeln(sprintf('Method: <info>%s</info>', $method));
70
        $output->writeln(sprintf('Path: <info>%s</info>', $path).PHP_EOL);
71
72
        $headersLIne = '';
73
74
        foreach ($response->getHeaders() as $name => $header) {
75
            $headersLIne .= $name.': '.$response->getHeaderLine($name).PHP_EOL;
76
        }
77
78
        $body = (string) $response->getBody();
79
        $body = json_encode(json_decode($body, true), JSON_PRETTY_PRINT);
80
        $output->writeln($headersLIne.PHP_EOL.$body);
81
    }
82
83
    /**
84
     * @param InputInterface  $input
85
     * @param OutputInterface $output
86
     */
87
    protected function render(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        $table = new Table($output);
90
        $rows = [];
91
        $table->setHeaders(array('Path', 'Method', 'Callback', 'Middleware'));
92
        foreach (route()->aliasMap as $routes) {
93
            foreach ($routes as $route) {
94
                $m = [];
95
                $middleware = $route->getMiddleware();
96
                if (is_array($middleware)) {
97
                    foreach ($middleware as $value) {
98
                        if (is_object($value)) {
99
                            $m[] = get_class($value);
100
                        } else {
101
                            $m[] = $value;
102
                        }
103
                    }
104
                } elseif (is_object($middleware)) {
105
                    $m[] = get_class($middleware);
106
                }
107
                $rows[] = [
108
                    $route->getPath(),
109
                    $route->getMethod(),
110
                    $route->getCallback(),
111
                    implode(',', $m),
112
                ];
113
            }
114
        }
115
116
        $table->setRows($rows);
117
118
        $table->render();
119
    }
120
}
121