-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRoute_Controller.php
171 lines (146 loc) · 5.07 KB
/
Route_Controller.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace n0nag0n;
use CLI;
class Route_Controller extends Base_Controller {
protected $routes_file_path;
public function __construct(\Base $fw) {
parent::__construct($fw);
$this->routes_file_path = $this->fw->PROJECT_BASE_DIR.$this->fw->project_config->config.$this->fw->project_config->routes;
}
public function indexAction(\Base $fw): void {
$routes = $this->getRoutes();
$this->renderHtml('routes'.DIRECTORY_SEPARATOR.'index.htm', [ 'routes' => $routes ]);
}
public function addAction(\Base $fw): void {
$Controller_Controller = new Controller_Controller($fw);
$controllers = $Controller_Controller->getControllers();
foreach($controllers as &$controller) {
$controller['endpoints'] = $Controller_Controller->getControllerEndpointMethods($controller['base_name']);
}
$this->renderHtml('routes'.DIRECTORY_SEPARATOR.'add.htm', [
'controllers' => $controllers
]);
}
public function create(\Base $fw, array $args = []): void {
$post = $fw->clean($fw->POST);
$methods = $post['methods'];
$type = $post['type'];
$path = $post['path'];
$namespace = $post['namespace'] ?: '';
$alias = $post['alias'] ? '@'.$post['alias'].': ' : '';
$controller_endpoint = $post['controller_endpoint'];
if(empty($methods)) {
$this->fw->flash->addMessage('You must select at least one method before proceeding.', 'danger');
$this->fw->reroute('/routes/add', false);
}
if(empty($methods) || empty($type) || empty($path) || empty($controller_endpoint)) {
throw new \Exception('Missing data.');
}
$result = $this->addRoute($methods, $type, $path, $namespace.$controller_endpoint, $alias);
if($result === true) {
$message = 'Route successfully created';
if($this->fw->CLI) {
CLI::success($message);
} else {
$this->fw->flash->addMessage($message, 'success');
$this->fw->reroute('/routes', false);
}
} else {
$message = 'Route failed to be created';
if($this->fw->CLI) {
CLI::error($message);
} else {
$this->fw->flash->addMessage($message, 'danger');
$this->fw->reroute('/routes/add', false);
}
}
}
public function delete(\Base $fw, array $args = []): void {
$args = $fw->clean($args);
$token = $args['token'];
if(empty($token)) {
throw new \Exception('Missing token.');
}
$result = $this->deleteRoute($token);
if($result === true) {
$message = 'Route successfully deleted';
if($this->fw->CLI) {
CLI::success($message);
} else {
$this->fw->flash->addMessage($message, 'success');
$this->fw->reroute('/routes', false);
}
} else {
$message = 'Route failed to be deleted';
if($this->fw->CLI) {
CLI::error($message);
} else {
$this->fw->flash->addMessage($message, 'danger');
$this->fw->reroute('/routes', false);
}
}
}
public function getRoutes(): array {
$routes = [];
$routes_file_lines = $this->getRawTextRoutes();
foreach($routes_file_lines as $line) {
$line_token = $this->generateLineHash($line);
$exp = explode('=', $line);
$exp = array_map('trim', $exp);
$route_definition = $exp[0];
$controller = $exp[1];
$explode = explode(' ', preg_replace("/\s+/", ' ', trim($route_definition)));
$final_route = [];
$final_route['method'] = $explode[0];
$aliases_grep = preg_grep("/\@\w+\:/i", $explode);
$final_route['alias'] = end($aliases_grep);
$routes_grep = preg_grep("~/.+~", $explode);
$final_route['route'] = end($routes_grep);
$types_grep = preg_grep("/\[.+\]/", $explode);
$final_route['type'] = end($types_grep);
$final_route['controller'] = $controller;
$final_route['token'] = $line_token;
$routes[] = $final_route;
}
return $routes;
}
public function addRoute(array $methods, string $type, string $path, string $controller, string $alias = '') : bool {
$route_string = $this->createRouteConfigString($methods, $type, $path, $controller, $alias);
$routes_file = $this->routes_file_path;
$result = $this->fw->write($routes_file, $route_string, true);
return is_int($result);
}
public function deleteRoute(string $token): bool {
$raw_file_lines = file($this->routes_file_path);
foreach($raw_file_lines as $line_number => $text) {
if($this->generateLineHash($text) === $token) {
unset($raw_file_lines[$line_number]);
break;
}
}
$result = $this->fw->write($this->routes_file_path, join('', $raw_file_lines));
return is_int($result);
}
public function createRouteConfigString(array $methods, string $type, string $path, string $controller, string $alias = ''): string {
$path = preg_replace("/\-+/", '-', preg_replace("/[^\w\-\@\/\:]+/", '-', $path));
if(substr($path, 0, 1) !== '/') {
$path = '/'.$path;
}
return "\n".join('|', $methods).' '.(!empty($alias) ? ' '.$alias : '').$path.' '.$type.' = '.$controller;
}
public function generateLineHash(string $line): string {
return md5($line);
}
public function getRawTextRoutes(): array {
$routes = [];
$routes_file_lines = file($this->routes_file_path);
foreach($routes_file_lines as $line) {
if(strpos($line, '=') === false) {
continue;
}
$routes[] = $line;
}
return $routes;
}
}