DebugRouter::renderRouter()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rostenkowski\Console\Command;
4
5
6
use Nette\Application\IRouter;
7
use Nette\Application\Routers\Route;
8
use Nette\Application\Routers\RouteList;
9
use Nette\Utils\Strings;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
12
use Symfony\Component\Console\Helper\Table;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class DebugRouter extends Command
17
{
18
19
	private $router;
20
21
22
	public function __construct(IRouter $router = NULL)
23
	{
24
		$this->setDescription('Display debug information about routes.');
25
		if ($router !== NULL) {
26
			$this->router = $router;
27
		}
28
29
		parent::__construct('nette:debug-router');
30
	}
31
32
33
	protected function execute(InputInterface $input, OutputInterface $output)
34
	{
35
		if ($this->router) {
36
			$style = new OutputFormatterStyle('white', 'default', ['bold']);
37
			$output->getFormatter()->setStyle('i', $style);
38
			$table = new Table($output);
39
			$table->setHeaders(['Type', 'Mask', 'Target', 'Defaults']);
40
			$this->renderRouter($table, $this->router);
41
			$table->render();
42
		} else {
43
			$output->writeln('No router service configured');
44
		}
45
	}
46
47
48
	private function renderRouter(Table $table, IRouter $router)
49
	{
50
		if ($router instanceof RouteList) {
51
			foreach ($router as $route) {
52
				$this->renderRouter($table, $route);
53
			}
54
		} else {
55
			$defaults = $router->getDefaults();
56
			$mask = $router instanceof Route ? $router->getMask() : '';
57
			$table->addRow([get_class($router), $mask, "$defaults[presenter]:$defaults[action]", $this->getDefaults($router)]);
58
		}
59
	}
60
61
62
	private function getDefaults(IRouter $router)
63
	{
64
		$defaults = array_filter($router->getDefaults(), function ($key) {
65
			return !in_array($key, ['presenter', 'action']);
66
		}, ARRAY_FILTER_USE_KEY);
67
68
		$result = '';
69
		foreach ($defaults as $key => $val) {
70
			$result .= "$key: $val" . PHP_EOL;
71
		}
72
73
		return Strings::substring($result, 0, -1);
74
	}
75
76
}
77