-
Notifications
You must be signed in to change notification settings - Fork 8
/
RoutesCommand.php
142 lines (126 loc) · 4.83 KB
/
RoutesCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Command;
use Ouzo\ApplicationPaths;
use Ouzo\Routing\Route;
use Ouzo\Routing\RouteRule;
use Ouzo\Uri\JsUriHelperGenerator;
use Ouzo\Uri\UriHelperGenerator;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Path;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class RoutesCommand extends Command
{
/**
* @var InputInterface
*/
private $_input;
/**
* @var OutputInterface
*/
private $_output;
public function configure()
{
$this->setName('ouzo:routes')
->addOption('controller', 'c', InputOption::VALUE_OPTIONAL)
->addOption('path', 'p', InputOption::VALUE_REQUIRED, 'Path for JS helper generated file', Path::join(ROOT_PATH, 'public'))
->addOption('generate-all', 'a', InputOption::VALUE_NONE)
->addOption('generate-php', 'g', InputOption::VALUE_NONE)
->addOption('generate-js', 'j', InputOption::VALUE_NONE);
}
public function execute(InputInterface $input, OutputInterface $output)
{
$this->_input = $input;
$this->_output = $output;
$generateOptionFunctionMap = [
"generate-php" => "generatePhpHelper",
"generate-js" => "generateJsHelper",
"generate-all" => "generateAllHelpers",
];
$selectedOptions = array_filter($input->getOptions());
$selectedGeneratedOptions = array_intersect(array_keys($selectedOptions), array_keys($generateOptionFunctionMap));
if (sizeof($selectedGeneratedOptions)) {
$this->runSelectedGenerators($selectedGeneratedOptions, $generateOptionFunctionMap);
} else {
if ($input->getOption('controller')) {
$this->controller();
} else {
$this->all();
}
}
}
private function runSelectedGenerators($selectedGeneratedOptions, $generateOptionFunctionMap)
{
foreach ($generateOptionFunctionMap as $optionName => $functionName) {
if (in_array($optionName, $selectedGeneratedOptions)) {
call_user_func([$this, $functionName]);
}
}
}
public function generateAllHelpers()
{
$this->generatePhpHelper();
$this->generateJsHelper();
}
private function generatePhpHelper()
{
$routesPhpHelperPath = Path::join(ROOT_PATH, ApplicationPaths::getHelperPath(), 'GeneratedUriHelper.php');
if (UriHelperGenerator::generate()->saveToFile($routesPhpHelperPath) !== false) {
$this->_output->writeln("File with PHP uri helpers is generated in <info>$routesPhpHelperPath</info>");
}
}
private function generateJsHelper()
{
$routesJSHelperPath = $this->_input->getOption('path');
$routesJSHelperPath = Path::join($routesJSHelperPath, 'generated_uri_helper.js');
if (JsUriHelperGenerator::generate()->saveToFile($routesJSHelperPath) !== false) {
$this->_output->writeln("File with JS uri helpers is generated in <info>$routesJSHelperPath</info>");
}
}
private function controller()
{
$controller = $this->_input->getOption('controller');
$this->_renderRoutes(Route::getRoutesForController($controller));
}
private function all()
{
$this->_renderRoutes(Route::getRoutes());
}
private function _renderRoutes($routes = [])
{
$table = $this->getHelper('table');
$table->setHeaders(['URL Helper', 'HTTP Verb', 'Path', 'Controller#Action']);
foreach ($routes as $route) {
$method = $this->_getRuleMethod($route);
$action = $route->getAction() ? '#' . $route->getAction() : $route->getAction();
$controllerAction = $route->getController() . $action;
$table->addRow([$route->getName(), $method, $route->getUri(), $controllerAction]);
$this->_printExceptIfExists($route, $table);
}
$table->render($this->_output);
}
private function _getRuleMethod(RouteRule $rule)
{
if (!$rule->isActionRequired()) {
return 'ALL';
}
return is_array($rule->getMethod()) ? 'ANY' : $rule->getMethod();
}
private function _printExceptIfExists(RouteRule $rule, $table)
{
$except = $rule->getExcept();
if ($except) {
$table->addRow(['', '', ' <info>except:</info>', '']);
Arrays::map($except, function ($except) use ($table) {
$table->addRow(['', '', ' ' . $except, '']);
return $except;
});
}
}
}