Completed
Push — master ( 2820f4...076502 )
by Vítor
16:50
created

RoutesController::listAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 20
cts 20
cp 1
rs 9.2
cc 2
eloc 18
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * ZfDebugModule. WebUI and Console commands for debugging ZF2 apps.
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
6
 * @copyright 2016 Vítor Brandão <[email protected]>
7
 */
8
9
namespace Noiselabs\ZfDebugModule\Controller\Console;
10
11
use Noiselabs\ZfDebugModule\Util\Routing\CsvExporter;
12
use Noiselabs\ZfDebugModule\Util\Routing\RouteCollection;
13
use Noiselabs\ZfDebugModule\Util\Routing\RouteMatcher;
14
use Zend\Console\Adapter\AdapterInterface as Console;
15
use Zend\Console\ColorInterface;
16
use Zend\Console\Request;
17
use Zend\Mvc\Controller\AbstractActionController;
18
19
class RoutesController extends AbstractActionController
20
{
21
    /**
22
     * @var Console
23
     */
24
    private $console;
25
26
    /**
27
     * @var RouteCollection
28
     */
29
    private $routeCollection;
30
31
    /**
32
     * @var RouteMatcher
33
     */
34
    private $routeMatcher;
35
36
    /**
37
     * RoutesController constructor.
38
     *
39
     * @param RouteCollection $routeCollection
40
     * @param RouteMatcher    $routeMatcher
41
     * @param Console         $console
42
     */
43 5
    public function __construct(RouteCollection $routeCollection, RouteMatcher $routeMatcher, Console $console)
44
    {
45 5
        $this->routeCollection = $routeCollection;
46 5
        $this->routeMatcher = $routeMatcher;
47 5
        $this->console = $console;
48 5
    }
49
50 1
    public function exportAction()
51
    {
52 1
        $csvExporter = new CsvExporter($this->routeCollection, sys_get_temp_dir());
53 1
        $this->console->write('Exporting all routes...');
54 1
        $fileName = $csvExporter->export();
55 1
        $this->console->writeLine(' done.');
56
57 1
        $this->console->write('CSV file now available at ');
58 1
        $this->console->writeLine($fileName, ColorInterface::LIGHT_BLUE);
59 1
    }
60
61 1
    public function listAction()
62
    {
63 1
        $this->console->write('{ ', ColorInterface::GRAY);
64 1
        $this->console->write('ROUTE', ColorInterface::GREEN);
65 1
        $this->console->write(', ', ColorInterface::GRAY);
66 1
        $this->console->write('URL ', ColorInterface::BLUE);
67 1
        $this->console->write(', ', ColorInterface::GRAY);
68 1
        $this->console->write('CONTROLLER::ACTION', ColorInterface::RED);
69 1
        $this->console->writeLine(' }', ColorInterface::GRAY);
70 1
        $this->console->writeLine('');
71
72 1
        foreach ($this->routeCollection->getRoutes() as $route) {
73 1
            $this->console->write('{ ', ColorInterface::GRAY);
74 1
            $this->console->write($route->getName(), ColorInterface::GREEN);
75 1
            $this->console->write(', ', ColorInterface::GRAY);
76 1
            $this->console->write($route->getUrl(), ColorInterface::BLUE);
77 1
            $this->console->write(', ', ColorInterface::GRAY);
78 1
            $this->console->write(sprintf('%s::%s', $route->getController(), $route->getAction()),
79 1
                ColorInterface::RED);
80 1
            $this->console->writeLine(' }', ColorInterface::GRAY);
81 1
        }
82 1
    }
83
84 2
    public function matchAction()
85
    {
86
        /** @var Request $request */
87 2
        $request = $this->getRequest();
88 2
        $method = strtoupper($request->getParam('method'));
89 2
        $url = $request->getParam('url');
90
91 2
        $match = $this->routeMatcher->match($method, $url);
92 2
        if (null !== $match) {
93 1
            $route = $this->routeCollection->getRoute($match);
94
95 1
            $this->console->writeLine(sprintf('A match was found for %s "%s"', $method, $url), ColorInterface::GREEN);
96 1
            $this->console->write('        Name:  ');
97 1
            $this->console->writeLine($route->getName(), ColorInterface::LIGHT_WHITE);
98 1
            $this->console->write('         URL:  ');
99 1
            $this->console->writeLine($route->getUrl(), ColorInterface::LIGHT_WHITE);
100 1
            $this->console->write('  Controller:  ');
101 1
            $this->console->writeLine($route->getController(), ColorInterface::LIGHT_WHITE);
102 1
            $this->console->write('      Action:  ');
103 1
            $this->console->writeLine($route->getAction(), ColorInterface::LIGHT_WHITE);
104 1
        } else {
105 1
            $this->console->writeLine(sprintf('No match found for %s "%s"', $method, $url), ColorInterface::RED);
106
        }
107 2
    }
108
}
109