@@ -35,235 +35,235 @@ |
||
35 | 35 | use Symfony\Component\Routing\RouteCollection; |
36 | 36 | |
37 | 37 | class RouteParser { |
38 | - /** @var string[] */ |
|
39 | - private $controllerNameCache = []; |
|
40 | - |
|
41 | - private const rootUrlApps = [ |
|
42 | - 'cloud_federation_api', |
|
43 | - 'core', |
|
44 | - 'files_sharing', |
|
45 | - 'files', |
|
46 | - 'settings', |
|
47 | - 'spreed', |
|
48 | - ]; |
|
49 | - |
|
50 | - public function parseDefaultRoutes(array $routes, string $appName): RouteCollection { |
|
51 | - $collection = $this->processIndexRoutes($routes, $appName); |
|
52 | - $collection->addCollection($this->processIndexResources($routes, $appName)); |
|
53 | - |
|
54 | - return $collection; |
|
55 | - } |
|
56 | - |
|
57 | - public function parseOCSRoutes(array $routes, string $appName): RouteCollection { |
|
58 | - $collection = $this->processOCS($routes, $appName); |
|
59 | - $collection->addCollection($this->processOCSResources($routes, $appName)); |
|
60 | - |
|
61 | - return $collection; |
|
62 | - } |
|
63 | - |
|
64 | - private function processOCS(array $routes, string $appName): RouteCollection { |
|
65 | - $collection = new RouteCollection(); |
|
66 | - $ocsRoutes = $routes['ocs'] ?? []; |
|
67 | - foreach ($ocsRoutes as $ocsRoute) { |
|
68 | - $result = $this->processRoute($ocsRoute, $appName, 'ocs.'); |
|
69 | - |
|
70 | - $collection->add($result[0], $result[1]); |
|
71 | - } |
|
72 | - |
|
73 | - return $collection; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Creates one route base on the give configuration |
|
78 | - * @param array $routes |
|
79 | - * @throws \UnexpectedValueException |
|
80 | - */ |
|
81 | - private function processIndexRoutes(array $routes, string $appName): RouteCollection { |
|
82 | - $collection = new RouteCollection(); |
|
83 | - $simpleRoutes = $routes['routes'] ?? []; |
|
84 | - foreach ($simpleRoutes as $simpleRoute) { |
|
85 | - $result = $this->processRoute($simpleRoute, $appName); |
|
86 | - |
|
87 | - $collection->add($result[0], $result[1]); |
|
88 | - } |
|
89 | - |
|
90 | - return $collection; |
|
91 | - } |
|
92 | - |
|
93 | - private function processRoute(array $route, string $appName, string $routeNamePrefix = ''): array { |
|
94 | - $name = $route['name']; |
|
95 | - $postfix = $route['postfix'] ?? ''; |
|
96 | - $root = $this->buildRootPrefix($route, $appName, $routeNamePrefix); |
|
97 | - |
|
98 | - $url = $root . '/' . ltrim($route['url'], '/'); |
|
99 | - $verb = strtoupper($route['verb'] ?? 'GET'); |
|
100 | - |
|
101 | - $split = explode('#', $name, 2); |
|
102 | - if (count($split) !== 2) { |
|
103 | - throw new \UnexpectedValueException('Invalid route name'); |
|
104 | - } |
|
105 | - list($controller, $action) = $split; |
|
106 | - |
|
107 | - $controllerName = $this->buildControllerName($controller); |
|
108 | - $actionName = $this->buildActionName($action); |
|
109 | - |
|
110 | - $routeName = $routeNamePrefix . $appName . '.' . $controller . '.' . $action . $postfix; |
|
111 | - |
|
112 | - $routeObject = new Route($url); |
|
113 | - $routeObject->method($verb); |
|
114 | - |
|
115 | - // optionally register requirements for route. This is used to |
|
116 | - // tell the route parser how url parameters should be matched |
|
117 | - if (array_key_exists('requirements', $route)) { |
|
118 | - $routeObject->requirements($route['requirements']); |
|
119 | - } |
|
120 | - |
|
121 | - // optionally register defaults for route. This is used to |
|
122 | - // tell the route parser how url parameters should be default valued |
|
123 | - $defaults = []; |
|
124 | - if (array_key_exists('defaults', $route)) { |
|
125 | - $defaults = $route['defaults']; |
|
126 | - } |
|
127 | - |
|
128 | - $defaults['caller'] = [$appName, $controllerName, $actionName]; |
|
129 | - $routeObject->defaults($defaults); |
|
130 | - |
|
131 | - return [$routeName, $routeObject]; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * For a given name and url restful OCS routes are created: |
|
136 | - * - index |
|
137 | - * - show |
|
138 | - * - create |
|
139 | - * - update |
|
140 | - * - destroy |
|
141 | - * |
|
142 | - * @param array $routes |
|
143 | - */ |
|
144 | - private function processOCSResources(array $routes, string $appName): RouteCollection { |
|
145 | - return $this->processResources($routes['ocs-resources'] ?? [], $appName, 'ocs.'); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * For a given name and url restful routes are created: |
|
150 | - * - index |
|
151 | - * - show |
|
152 | - * - create |
|
153 | - * - update |
|
154 | - * - destroy |
|
155 | - * |
|
156 | - * @param array $routes |
|
157 | - */ |
|
158 | - private function processIndexResources(array $routes, string $appName): RouteCollection { |
|
159 | - return $this->processResources($routes['resources'] ?? [], $appName); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * For a given name and url restful routes are created: |
|
164 | - * - index |
|
165 | - * - show |
|
166 | - * - create |
|
167 | - * - update |
|
168 | - * - destroy |
|
169 | - * |
|
170 | - * @param array $resources |
|
171 | - * @param string $routeNamePrefix |
|
172 | - */ |
|
173 | - private function processResources(array $resources, string $appName, string $routeNamePrefix = ''): RouteCollection { |
|
174 | - // declaration of all restful actions |
|
175 | - $actions = [ |
|
176 | - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
177 | - ['name' => 'show', 'verb' => 'GET'], |
|
178 | - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
179 | - ['name' => 'update', 'verb' => 'PUT'], |
|
180 | - ['name' => 'destroy', 'verb' => 'DELETE'], |
|
181 | - ]; |
|
182 | - |
|
183 | - $collection = new RouteCollection(); |
|
184 | - foreach ($resources as $resource => $config) { |
|
185 | - $root = $this->buildRootPrefix($config, $appName, $routeNamePrefix); |
|
186 | - |
|
187 | - // the url parameter used as id to the resource |
|
188 | - foreach ($actions as $action) { |
|
189 | - $url = $root . '/' . ltrim($config['url'], '/'); |
|
190 | - $method = $action['name']; |
|
191 | - |
|
192 | - $verb = strtoupper($action['verb'] ?? 'GET'); |
|
193 | - $collectionAction = $action['on-collection'] ?? false; |
|
194 | - if (!$collectionAction) { |
|
195 | - $url .= '/{id}'; |
|
196 | - } |
|
197 | - |
|
198 | - $controller = $resource; |
|
199 | - |
|
200 | - $controllerName = $this->buildControllerName($controller); |
|
201 | - $actionName = $this->buildActionName($method); |
|
202 | - |
|
203 | - $routeName = $routeNamePrefix . $appName . '.' . strtolower($resource) . '.' . $method; |
|
204 | - |
|
205 | - $route = new Route($url); |
|
206 | - $route->method($verb); |
|
207 | - |
|
208 | - $route->defaults(['caller' => [$appName, $controllerName, $actionName]]); |
|
209 | - |
|
210 | - $collection->add($routeName, $route); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - return $collection; |
|
215 | - } |
|
216 | - |
|
217 | - private function buildRootPrefix(array $route, string $appName, string $routeNamePrefix): string { |
|
218 | - $defaultRoot = $appName === 'core' ? '' : '/apps/' . $appName; |
|
219 | - $root = $route['root'] ?? $defaultRoot; |
|
220 | - |
|
221 | - if ($routeNamePrefix !== '') { |
|
222 | - // In OCS all apps are whitelisted |
|
223 | - return $root; |
|
224 | - } |
|
225 | - |
|
226 | - if (!\in_array($appName, self::rootUrlApps, true)) { |
|
227 | - // Only allow root URLS for some apps |
|
228 | - return $defaultRoot; |
|
229 | - } |
|
230 | - |
|
231 | - return $root; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Based on a given route name the controller name is generated |
|
236 | - * @param string $controller |
|
237 | - * @return string |
|
238 | - */ |
|
239 | - private function buildControllerName(string $controller): string { |
|
240 | - if (!isset($this->controllerNameCache[$controller])) { |
|
241 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
242 | - } |
|
243 | - return $this->controllerNameCache[$controller]; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Based on the action part of the route name the controller method name is generated |
|
248 | - * @param string $action |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - private function buildActionName(string $action): string { |
|
252 | - return $this->underScoreToCamelCase($action); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Underscored strings are converted to camel case strings |
|
257 | - * @param string $str |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - private function underScoreToCamelCase(string $str): string { |
|
261 | - $pattern = '/_[a-z]?/'; |
|
262 | - return preg_replace_callback( |
|
263 | - $pattern, |
|
264 | - function ($matches) { |
|
265 | - return strtoupper(ltrim($matches[0], '_')); |
|
266 | - }, |
|
267 | - $str); |
|
268 | - } |
|
38 | + /** @var string[] */ |
|
39 | + private $controllerNameCache = []; |
|
40 | + |
|
41 | + private const rootUrlApps = [ |
|
42 | + 'cloud_federation_api', |
|
43 | + 'core', |
|
44 | + 'files_sharing', |
|
45 | + 'files', |
|
46 | + 'settings', |
|
47 | + 'spreed', |
|
48 | + ]; |
|
49 | + |
|
50 | + public function parseDefaultRoutes(array $routes, string $appName): RouteCollection { |
|
51 | + $collection = $this->processIndexRoutes($routes, $appName); |
|
52 | + $collection->addCollection($this->processIndexResources($routes, $appName)); |
|
53 | + |
|
54 | + return $collection; |
|
55 | + } |
|
56 | + |
|
57 | + public function parseOCSRoutes(array $routes, string $appName): RouteCollection { |
|
58 | + $collection = $this->processOCS($routes, $appName); |
|
59 | + $collection->addCollection($this->processOCSResources($routes, $appName)); |
|
60 | + |
|
61 | + return $collection; |
|
62 | + } |
|
63 | + |
|
64 | + private function processOCS(array $routes, string $appName): RouteCollection { |
|
65 | + $collection = new RouteCollection(); |
|
66 | + $ocsRoutes = $routes['ocs'] ?? []; |
|
67 | + foreach ($ocsRoutes as $ocsRoute) { |
|
68 | + $result = $this->processRoute($ocsRoute, $appName, 'ocs.'); |
|
69 | + |
|
70 | + $collection->add($result[0], $result[1]); |
|
71 | + } |
|
72 | + |
|
73 | + return $collection; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Creates one route base on the give configuration |
|
78 | + * @param array $routes |
|
79 | + * @throws \UnexpectedValueException |
|
80 | + */ |
|
81 | + private function processIndexRoutes(array $routes, string $appName): RouteCollection { |
|
82 | + $collection = new RouteCollection(); |
|
83 | + $simpleRoutes = $routes['routes'] ?? []; |
|
84 | + foreach ($simpleRoutes as $simpleRoute) { |
|
85 | + $result = $this->processRoute($simpleRoute, $appName); |
|
86 | + |
|
87 | + $collection->add($result[0], $result[1]); |
|
88 | + } |
|
89 | + |
|
90 | + return $collection; |
|
91 | + } |
|
92 | + |
|
93 | + private function processRoute(array $route, string $appName, string $routeNamePrefix = ''): array { |
|
94 | + $name = $route['name']; |
|
95 | + $postfix = $route['postfix'] ?? ''; |
|
96 | + $root = $this->buildRootPrefix($route, $appName, $routeNamePrefix); |
|
97 | + |
|
98 | + $url = $root . '/' . ltrim($route['url'], '/'); |
|
99 | + $verb = strtoupper($route['verb'] ?? 'GET'); |
|
100 | + |
|
101 | + $split = explode('#', $name, 2); |
|
102 | + if (count($split) !== 2) { |
|
103 | + throw new \UnexpectedValueException('Invalid route name'); |
|
104 | + } |
|
105 | + list($controller, $action) = $split; |
|
106 | + |
|
107 | + $controllerName = $this->buildControllerName($controller); |
|
108 | + $actionName = $this->buildActionName($action); |
|
109 | + |
|
110 | + $routeName = $routeNamePrefix . $appName . '.' . $controller . '.' . $action . $postfix; |
|
111 | + |
|
112 | + $routeObject = new Route($url); |
|
113 | + $routeObject->method($verb); |
|
114 | + |
|
115 | + // optionally register requirements for route. This is used to |
|
116 | + // tell the route parser how url parameters should be matched |
|
117 | + if (array_key_exists('requirements', $route)) { |
|
118 | + $routeObject->requirements($route['requirements']); |
|
119 | + } |
|
120 | + |
|
121 | + // optionally register defaults for route. This is used to |
|
122 | + // tell the route parser how url parameters should be default valued |
|
123 | + $defaults = []; |
|
124 | + if (array_key_exists('defaults', $route)) { |
|
125 | + $defaults = $route['defaults']; |
|
126 | + } |
|
127 | + |
|
128 | + $defaults['caller'] = [$appName, $controllerName, $actionName]; |
|
129 | + $routeObject->defaults($defaults); |
|
130 | + |
|
131 | + return [$routeName, $routeObject]; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * For a given name and url restful OCS routes are created: |
|
136 | + * - index |
|
137 | + * - show |
|
138 | + * - create |
|
139 | + * - update |
|
140 | + * - destroy |
|
141 | + * |
|
142 | + * @param array $routes |
|
143 | + */ |
|
144 | + private function processOCSResources(array $routes, string $appName): RouteCollection { |
|
145 | + return $this->processResources($routes['ocs-resources'] ?? [], $appName, 'ocs.'); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * For a given name and url restful routes are created: |
|
150 | + * - index |
|
151 | + * - show |
|
152 | + * - create |
|
153 | + * - update |
|
154 | + * - destroy |
|
155 | + * |
|
156 | + * @param array $routes |
|
157 | + */ |
|
158 | + private function processIndexResources(array $routes, string $appName): RouteCollection { |
|
159 | + return $this->processResources($routes['resources'] ?? [], $appName); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * For a given name and url restful routes are created: |
|
164 | + * - index |
|
165 | + * - show |
|
166 | + * - create |
|
167 | + * - update |
|
168 | + * - destroy |
|
169 | + * |
|
170 | + * @param array $resources |
|
171 | + * @param string $routeNamePrefix |
|
172 | + */ |
|
173 | + private function processResources(array $resources, string $appName, string $routeNamePrefix = ''): RouteCollection { |
|
174 | + // declaration of all restful actions |
|
175 | + $actions = [ |
|
176 | + ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
177 | + ['name' => 'show', 'verb' => 'GET'], |
|
178 | + ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
179 | + ['name' => 'update', 'verb' => 'PUT'], |
|
180 | + ['name' => 'destroy', 'verb' => 'DELETE'], |
|
181 | + ]; |
|
182 | + |
|
183 | + $collection = new RouteCollection(); |
|
184 | + foreach ($resources as $resource => $config) { |
|
185 | + $root = $this->buildRootPrefix($config, $appName, $routeNamePrefix); |
|
186 | + |
|
187 | + // the url parameter used as id to the resource |
|
188 | + foreach ($actions as $action) { |
|
189 | + $url = $root . '/' . ltrim($config['url'], '/'); |
|
190 | + $method = $action['name']; |
|
191 | + |
|
192 | + $verb = strtoupper($action['verb'] ?? 'GET'); |
|
193 | + $collectionAction = $action['on-collection'] ?? false; |
|
194 | + if (!$collectionAction) { |
|
195 | + $url .= '/{id}'; |
|
196 | + } |
|
197 | + |
|
198 | + $controller = $resource; |
|
199 | + |
|
200 | + $controllerName = $this->buildControllerName($controller); |
|
201 | + $actionName = $this->buildActionName($method); |
|
202 | + |
|
203 | + $routeName = $routeNamePrefix . $appName . '.' . strtolower($resource) . '.' . $method; |
|
204 | + |
|
205 | + $route = new Route($url); |
|
206 | + $route->method($verb); |
|
207 | + |
|
208 | + $route->defaults(['caller' => [$appName, $controllerName, $actionName]]); |
|
209 | + |
|
210 | + $collection->add($routeName, $route); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + return $collection; |
|
215 | + } |
|
216 | + |
|
217 | + private function buildRootPrefix(array $route, string $appName, string $routeNamePrefix): string { |
|
218 | + $defaultRoot = $appName === 'core' ? '' : '/apps/' . $appName; |
|
219 | + $root = $route['root'] ?? $defaultRoot; |
|
220 | + |
|
221 | + if ($routeNamePrefix !== '') { |
|
222 | + // In OCS all apps are whitelisted |
|
223 | + return $root; |
|
224 | + } |
|
225 | + |
|
226 | + if (!\in_array($appName, self::rootUrlApps, true)) { |
|
227 | + // Only allow root URLS for some apps |
|
228 | + return $defaultRoot; |
|
229 | + } |
|
230 | + |
|
231 | + return $root; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Based on a given route name the controller name is generated |
|
236 | + * @param string $controller |
|
237 | + * @return string |
|
238 | + */ |
|
239 | + private function buildControllerName(string $controller): string { |
|
240 | + if (!isset($this->controllerNameCache[$controller])) { |
|
241 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
242 | + } |
|
243 | + return $this->controllerNameCache[$controller]; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Based on the action part of the route name the controller method name is generated |
|
248 | + * @param string $action |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + private function buildActionName(string $action): string { |
|
252 | + return $this->underScoreToCamelCase($action); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Underscored strings are converted to camel case strings |
|
257 | + * @param string $str |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + private function underScoreToCamelCase(string $str): string { |
|
261 | + $pattern = '/_[a-z]?/'; |
|
262 | + return preg_replace_callback( |
|
263 | + $pattern, |
|
264 | + function ($matches) { |
|
265 | + return strtoupper(ltrim($matches[0], '_')); |
|
266 | + }, |
|
267 | + $str); |
|
268 | + } |
|
269 | 269 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | $postfix = $route['postfix'] ?? ''; |
96 | 96 | $root = $this->buildRootPrefix($route, $appName, $routeNamePrefix); |
97 | 97 | |
98 | - $url = $root . '/' . ltrim($route['url'], '/'); |
|
98 | + $url = $root.'/'.ltrim($route['url'], '/'); |
|
99 | 99 | $verb = strtoupper($route['verb'] ?? 'GET'); |
100 | 100 | |
101 | 101 | $split = explode('#', $name, 2); |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | $controllerName = $this->buildControllerName($controller); |
108 | 108 | $actionName = $this->buildActionName($action); |
109 | 109 | |
110 | - $routeName = $routeNamePrefix . $appName . '.' . $controller . '.' . $action . $postfix; |
|
110 | + $routeName = $routeNamePrefix.$appName.'.'.$controller.'.'.$action.$postfix; |
|
111 | 111 | |
112 | 112 | $routeObject = new Route($url); |
113 | 113 | $routeObject->method($verb); |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | |
187 | 187 | // the url parameter used as id to the resource |
188 | 188 | foreach ($actions as $action) { |
189 | - $url = $root . '/' . ltrim($config['url'], '/'); |
|
189 | + $url = $root.'/'.ltrim($config['url'], '/'); |
|
190 | 190 | $method = $action['name']; |
191 | 191 | |
192 | 192 | $verb = strtoupper($action['verb'] ?? 'GET'); |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | $controllerName = $this->buildControllerName($controller); |
201 | 201 | $actionName = $this->buildActionName($method); |
202 | 202 | |
203 | - $routeName = $routeNamePrefix . $appName . '.' . strtolower($resource) . '.' . $method; |
|
203 | + $routeName = $routeNamePrefix.$appName.'.'.strtolower($resource).'.'.$method; |
|
204 | 204 | |
205 | 205 | $route = new Route($url); |
206 | 206 | $route->method($verb); |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | } |
216 | 216 | |
217 | 217 | private function buildRootPrefix(array $route, string $appName, string $routeNamePrefix): string { |
218 | - $defaultRoot = $appName === 'core' ? '' : '/apps/' . $appName; |
|
218 | + $defaultRoot = $appName === 'core' ? '' : '/apps/'.$appName; |
|
219 | 219 | $root = $route['root'] ?? $defaultRoot; |
220 | 220 | |
221 | 221 | if ($routeNamePrefix !== '') { |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | */ |
239 | 239 | private function buildControllerName(string $controller): string { |
240 | 240 | if (!isset($this->controllerNameCache[$controller])) { |
241 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
241 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)).'Controller'; |
|
242 | 242 | } |
243 | 243 | return $this->controllerNameCache[$controller]; |
244 | 244 | } |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | $pattern = '/_[a-z]?/'; |
262 | 262 | return preg_replace_callback( |
263 | 263 | $pattern, |
264 | - function ($matches) { |
|
264 | + function($matches) { |
|
265 | 265 | return strtoupper(ltrim($matches[0], '_')); |
266 | 266 | }, |
267 | 267 | $str); |
@@ -46,409 +46,409 @@ |
||
46 | 46 | use Symfony\Component\Routing\RouteCollection; |
47 | 47 | |
48 | 48 | class Router implements IRouter { |
49 | - /** @var RouteCollection[] */ |
|
50 | - protected $collections = []; |
|
51 | - /** @var null|RouteCollection */ |
|
52 | - protected $collection = null; |
|
53 | - /** @var null|string */ |
|
54 | - protected $collectionName = null; |
|
55 | - /** @var null|RouteCollection */ |
|
56 | - protected $root = null; |
|
57 | - /** @var null|UrlGenerator */ |
|
58 | - protected $generator = null; |
|
59 | - /** @var string[] */ |
|
60 | - protected $routingFiles; |
|
61 | - /** @var bool */ |
|
62 | - protected $loaded = false; |
|
63 | - /** @var array */ |
|
64 | - protected $loadedApps = []; |
|
65 | - /** @var ILogger */ |
|
66 | - protected $logger; |
|
67 | - /** @var RequestContext */ |
|
68 | - protected $context; |
|
69 | - |
|
70 | - /** |
|
71 | - * @param ILogger $logger |
|
72 | - */ |
|
73 | - public function __construct(ILogger $logger) { |
|
74 | - $this->logger = $logger; |
|
75 | - $baseUrl = \OC::$WEBROOT; |
|
76 | - if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
77 | - $baseUrl .= '/index.php'; |
|
78 | - } |
|
79 | - if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) { |
|
80 | - $method = $_SERVER['REQUEST_METHOD']; |
|
81 | - } else { |
|
82 | - $method = 'GET'; |
|
83 | - } |
|
84 | - $request = \OC::$server->getRequest(); |
|
85 | - $host = $request->getServerHost(); |
|
86 | - $schema = $request->getServerProtocol(); |
|
87 | - $this->context = new RequestContext($baseUrl, $method, $host, $schema); |
|
88 | - // TODO cache |
|
89 | - $this->root = $this->getCollection('root'); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Get the files to load the routes from |
|
94 | - * |
|
95 | - * @return string[] |
|
96 | - */ |
|
97 | - public function getRoutingFiles() { |
|
98 | - if (!isset($this->routingFiles)) { |
|
99 | - $this->routingFiles = []; |
|
100 | - foreach (\OC_APP::getEnabledApps() as $app) { |
|
101 | - $appPath = \OC_App::getAppPath($app); |
|
102 | - if ($appPath !== false) { |
|
103 | - $file = $appPath . '/appinfo/routes.php'; |
|
104 | - if (file_exists($file)) { |
|
105 | - $this->routingFiles[$app] = $file; |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - return $this->routingFiles; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Loads the routes |
|
115 | - * |
|
116 | - * @param null|string $app |
|
117 | - */ |
|
118 | - public function loadRoutes($app = null) { |
|
119 | - if (is_string($app)) { |
|
120 | - $app = \OC_App::cleanAppId($app); |
|
121 | - } |
|
122 | - |
|
123 | - $requestedApp = $app; |
|
124 | - if ($this->loaded) { |
|
125 | - return; |
|
126 | - } |
|
127 | - if (is_null($app)) { |
|
128 | - $this->loaded = true; |
|
129 | - $routingFiles = $this->getRoutingFiles(); |
|
130 | - } else { |
|
131 | - if (isset($this->loadedApps[$app])) { |
|
132 | - return; |
|
133 | - } |
|
134 | - $file = \OC_App::getAppPath($app) . '/appinfo/routes.php'; |
|
135 | - if ($file !== false && file_exists($file)) { |
|
136 | - $routingFiles = [$app => $file]; |
|
137 | - } else { |
|
138 | - $routingFiles = []; |
|
139 | - } |
|
140 | - } |
|
141 | - \OC::$server->getEventLogger()->start('loadroutes' . $requestedApp, 'Loading Routes'); |
|
142 | - foreach ($routingFiles as $app => $file) { |
|
143 | - if (!isset($this->loadedApps[$app])) { |
|
144 | - if (!\OC_App::isAppLoaded($app)) { |
|
145 | - // app MUST be loaded before app routes |
|
146 | - // try again next time loadRoutes() is called |
|
147 | - $this->loaded = false; |
|
148 | - continue; |
|
149 | - } |
|
150 | - $this->loadedApps[$app] = true; |
|
151 | - $this->useCollection($app); |
|
152 | - $this->requireRouteFile($file, $app); |
|
153 | - $collection = $this->getCollection($app); |
|
154 | - $this->root->addCollection($collection); |
|
155 | - |
|
156 | - // Also add the OCS collection |
|
157 | - $collection = $this->getCollection($app.'.ocs'); |
|
158 | - $collection->addPrefix('/ocsapp'); |
|
159 | - $this->root->addCollection($collection); |
|
160 | - } |
|
161 | - } |
|
162 | - if (!isset($this->loadedApps['core'])) { |
|
163 | - $this->loadedApps['core'] = true; |
|
164 | - $this->useCollection('root'); |
|
165 | - require_once __DIR__ . '/../../../core/routes.php'; |
|
166 | - |
|
167 | - // Also add the OCS collection |
|
168 | - $collection = $this->getCollection('root.ocs'); |
|
169 | - $collection->addPrefix('/ocsapp'); |
|
170 | - $this->root->addCollection($collection); |
|
171 | - } |
|
172 | - if ($this->loaded) { |
|
173 | - $collection = $this->getCollection('ocs'); |
|
174 | - $collection->addPrefix('/ocs'); |
|
175 | - $this->root->addCollection($collection); |
|
176 | - } |
|
177 | - \OC::$server->getEventLogger()->end('loadroutes' . $requestedApp); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return string |
|
182 | - * @deprecated |
|
183 | - */ |
|
184 | - public function getCacheKey() { |
|
185 | - return ''; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param string $name |
|
190 | - * @return \Symfony\Component\Routing\RouteCollection |
|
191 | - */ |
|
192 | - protected function getCollection($name) { |
|
193 | - if (!isset($this->collections[$name])) { |
|
194 | - $this->collections[$name] = new RouteCollection(); |
|
195 | - } |
|
196 | - return $this->collections[$name]; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Sets the collection to use for adding routes |
|
201 | - * |
|
202 | - * @param string $name Name of the collection to use. |
|
203 | - * @return void |
|
204 | - */ |
|
205 | - public function useCollection($name) { |
|
206 | - $this->collection = $this->getCollection($name); |
|
207 | - $this->collectionName = $name; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * returns the current collection name in use for adding routes |
|
212 | - * |
|
213 | - * @return string the collection name |
|
214 | - */ |
|
215 | - public function getCurrentCollection() { |
|
216 | - return $this->collectionName; |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * Create a \OC\Route\Route. |
|
222 | - * |
|
223 | - * @param string $name Name of the route to create. |
|
224 | - * @param string $pattern The pattern to match |
|
225 | - * @param array $defaults An array of default parameter values |
|
226 | - * @param array $requirements An array of requirements for parameters (regexes) |
|
227 | - * @return \OC\Route\Route |
|
228 | - */ |
|
229 | - public function create($name, |
|
230 | - $pattern, |
|
231 | - array $defaults = [], |
|
232 | - array $requirements = []) { |
|
233 | - $route = new Route($pattern, $defaults, $requirements); |
|
234 | - $this->collection->add($name, $route); |
|
235 | - return $route; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Find the route matching $url |
|
240 | - * |
|
241 | - * @param string $url The url to find |
|
242 | - * @throws \Exception |
|
243 | - * @return array |
|
244 | - */ |
|
245 | - public function findMatchingRoute(string $url): array { |
|
246 | - if (substr($url, 0, 6) === '/apps/') { |
|
247 | - // empty string / 'apps' / $app / rest of the route |
|
248 | - list(, , $app,) = explode('/', $url, 4); |
|
249 | - |
|
250 | - $app = \OC_App::cleanAppId($app); |
|
251 | - \OC::$REQUESTEDAPP = $app; |
|
252 | - $this->loadRoutes($app); |
|
253 | - } elseif (substr($url, 0, 13) === '/ocsapp/apps/') { |
|
254 | - // empty string / 'ocsapp' / 'apps' / $app / rest of the route |
|
255 | - list(, , , $app,) = explode('/', $url, 5); |
|
256 | - |
|
257 | - $app = \OC_App::cleanAppId($app); |
|
258 | - \OC::$REQUESTEDAPP = $app; |
|
259 | - $this->loadRoutes($app); |
|
260 | - } elseif (substr($url, 0, 10) === '/settings/') { |
|
261 | - $this->loadRoutes('settings'); |
|
262 | - } elseif (substr($url, 0, 6) === '/core/') { |
|
263 | - \OC::$REQUESTEDAPP = $url; |
|
264 | - if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) { |
|
265 | - \OC_App::loadApps(); |
|
266 | - } |
|
267 | - $this->loadRoutes('core'); |
|
268 | - } else { |
|
269 | - $this->loadRoutes(); |
|
270 | - } |
|
271 | - |
|
272 | - $matcher = new UrlMatcher($this->root, $this->context); |
|
273 | - try { |
|
274 | - $parameters = $matcher->match($url); |
|
275 | - } catch (ResourceNotFoundException $e) { |
|
276 | - if (substr($url, -1) !== '/') { |
|
277 | - // We allow links to apps/files? for backwards compatibility reasons |
|
278 | - // However, since Symfony does not allow empty route names, the route |
|
279 | - // we need to match is '/', so we need to append the '/' here. |
|
280 | - try { |
|
281 | - $parameters = $matcher->match($url . '/'); |
|
282 | - } catch (ResourceNotFoundException $newException) { |
|
283 | - // If we still didn't match a route, we throw the original exception |
|
284 | - throw $e; |
|
285 | - } |
|
286 | - } else { |
|
287 | - throw $e; |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - return $parameters; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Find and execute the route matching $url |
|
296 | - * |
|
297 | - * @param string $url The url to find |
|
298 | - * @throws \Exception |
|
299 | - * @return void |
|
300 | - */ |
|
301 | - public function match($url) { |
|
302 | - $parameters = $this->findMatchingRoute($url); |
|
303 | - |
|
304 | - \OC::$server->getEventLogger()->start('run_route', 'Run route'); |
|
305 | - if (isset($parameters['caller'])) { |
|
306 | - $caller = $parameters['caller']; |
|
307 | - unset($parameters['caller']); |
|
308 | - $application = $this->getApplicationClass($caller[0]); |
|
309 | - \OC\AppFramework\App::main($caller[1], $caller[2], $application->getContainer(), $parameters); |
|
310 | - } elseif (isset($parameters['action'])) { |
|
311 | - $action = $parameters['action']; |
|
312 | - if (!is_callable($action)) { |
|
313 | - throw new \Exception('not a callable action'); |
|
314 | - } |
|
315 | - unset($parameters['action']); |
|
316 | - call_user_func($action, $parameters); |
|
317 | - } elseif (isset($parameters['file'])) { |
|
318 | - include $parameters['file']; |
|
319 | - } else { |
|
320 | - throw new \Exception('no action available'); |
|
321 | - } |
|
322 | - \OC::$server->getEventLogger()->end('run_route'); |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Get the url generator |
|
327 | - * |
|
328 | - * @return \Symfony\Component\Routing\Generator\UrlGenerator |
|
329 | - * |
|
330 | - */ |
|
331 | - public function getGenerator() { |
|
332 | - if (null !== $this->generator) { |
|
333 | - return $this->generator; |
|
334 | - } |
|
335 | - |
|
336 | - return $this->generator = new UrlGenerator($this->root, $this->context); |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Generate url based on $name and $parameters |
|
341 | - * |
|
342 | - * @param string $name Name of the route to use. |
|
343 | - * @param array $parameters Parameters for the route |
|
344 | - * @param bool $absolute |
|
345 | - * @return string |
|
346 | - */ |
|
347 | - public function generate($name, |
|
348 | - $parameters = [], |
|
349 | - $absolute = false) { |
|
350 | - $referenceType = UrlGenerator::ABSOLUTE_URL; |
|
351 | - if ($absolute === false) { |
|
352 | - $referenceType = UrlGenerator::ABSOLUTE_PATH; |
|
353 | - } |
|
354 | - $name = $this->fixLegacyRootName($name); |
|
355 | - if (strpos($name, '.') !== false) { |
|
356 | - list($appName, $other) = explode('.', $name, 3); |
|
357 | - // OCS routes are prefixed with "ocs." |
|
358 | - if ($appName === 'ocs') { |
|
359 | - $appName = $other; |
|
360 | - } |
|
361 | - $this->loadRoutes($appName); |
|
362 | - try { |
|
363 | - return $this->getGenerator()->generate($name, $parameters, $referenceType); |
|
364 | - } catch (RouteNotFoundException $e) { |
|
365 | - } |
|
366 | - } |
|
367 | - |
|
368 | - // Fallback load all routes |
|
369 | - $this->loadRoutes(); |
|
370 | - try { |
|
371 | - return $this->getGenerator()->generate($name, $parameters, $referenceType); |
|
372 | - } catch (RouteNotFoundException $e) { |
|
373 | - $this->logger->logException($e, ['level' => ILogger::INFO]); |
|
374 | - return ''; |
|
375 | - } |
|
376 | - } |
|
377 | - |
|
378 | - protected function fixLegacyRootName(string $routeName): string { |
|
379 | - if ($routeName === 'files.viewcontroller.showFile') { |
|
380 | - return 'files.View.showFile'; |
|
381 | - } |
|
382 | - if ($routeName === 'files_sharing.sharecontroller.showShare') { |
|
383 | - return 'files_sharing.Share.showShare'; |
|
384 | - } |
|
385 | - if ($routeName === 'files_sharing.sharecontroller.showAuthenticate') { |
|
386 | - return 'files_sharing.Share.showAuthenticate'; |
|
387 | - } |
|
388 | - if ($routeName === 'files_sharing.sharecontroller.authenticate') { |
|
389 | - return 'files_sharing.Share.authenticate'; |
|
390 | - } |
|
391 | - if ($routeName === 'files_sharing.sharecontroller.downloadShare') { |
|
392 | - return 'files_sharing.Share.downloadShare'; |
|
393 | - } |
|
394 | - if ($routeName === 'files_sharing.publicpreview.directLink') { |
|
395 | - return 'files_sharing.PublicPreview.directLink'; |
|
396 | - } |
|
397 | - if ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') { |
|
398 | - return 'cloud_federation_api.RequestHandler.addShare'; |
|
399 | - } |
|
400 | - if ($routeName === 'cloud_federation_api.requesthandlercontroller.receiveNotification') { |
|
401 | - return 'cloud_federation_api.RequestHandler.receiveNotification'; |
|
402 | - } |
|
403 | - return $routeName; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * To isolate the variable scope used inside the $file it is required in it's own method |
|
408 | - * |
|
409 | - * @param string $file the route file location to include |
|
410 | - * @param string $appName |
|
411 | - */ |
|
412 | - private function requireRouteFile($file, $appName) { |
|
413 | - $this->setupRoutes(include_once $file, $appName); |
|
414 | - } |
|
415 | - |
|
416 | - |
|
417 | - /** |
|
418 | - * If a routes.php file returns an array, try to set up the application and |
|
419 | - * register the routes for the app. The application class will be chosen by |
|
420 | - * camelcasing the appname, e.g.: my_app will be turned into |
|
421 | - * \OCA\MyApp\AppInfo\Application. If that class does not exist, a default |
|
422 | - * App will be intialized. This makes it optional to ship an |
|
423 | - * appinfo/application.php by using the built in query resolver |
|
424 | - * |
|
425 | - * @param array $routes the application routes |
|
426 | - * @param string $appName the name of the app. |
|
427 | - */ |
|
428 | - private function setupRoutes($routes, $appName) { |
|
429 | - if (is_array($routes)) { |
|
430 | - $routeParser = new RouteParser(); |
|
431 | - |
|
432 | - $defaultRoutes = $routeParser->parseDefaultRoutes($routes, $appName); |
|
433 | - $ocsRoutes = $routeParser->parseOCSRoutes($routes, $appName); |
|
434 | - |
|
435 | - $this->root->addCollection($defaultRoutes); |
|
436 | - $ocsRoutes->addPrefix('/ocsapp'); |
|
437 | - $this->root->addCollection($ocsRoutes); |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - private function getApplicationClass(string $appName) { |
|
442 | - $appNameSpace = App::buildAppNamespace($appName); |
|
443 | - |
|
444 | - $applicationClassName = $appNameSpace . '\\AppInfo\\Application'; |
|
445 | - |
|
446 | - if (class_exists($applicationClassName)) { |
|
447 | - $application = \OC::$server->query($applicationClassName); |
|
448 | - } else { |
|
449 | - $application = new App($appName); |
|
450 | - } |
|
451 | - |
|
452 | - return $application; |
|
453 | - } |
|
49 | + /** @var RouteCollection[] */ |
|
50 | + protected $collections = []; |
|
51 | + /** @var null|RouteCollection */ |
|
52 | + protected $collection = null; |
|
53 | + /** @var null|string */ |
|
54 | + protected $collectionName = null; |
|
55 | + /** @var null|RouteCollection */ |
|
56 | + protected $root = null; |
|
57 | + /** @var null|UrlGenerator */ |
|
58 | + protected $generator = null; |
|
59 | + /** @var string[] */ |
|
60 | + protected $routingFiles; |
|
61 | + /** @var bool */ |
|
62 | + protected $loaded = false; |
|
63 | + /** @var array */ |
|
64 | + protected $loadedApps = []; |
|
65 | + /** @var ILogger */ |
|
66 | + protected $logger; |
|
67 | + /** @var RequestContext */ |
|
68 | + protected $context; |
|
69 | + |
|
70 | + /** |
|
71 | + * @param ILogger $logger |
|
72 | + */ |
|
73 | + public function __construct(ILogger $logger) { |
|
74 | + $this->logger = $logger; |
|
75 | + $baseUrl = \OC::$WEBROOT; |
|
76 | + if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
77 | + $baseUrl .= '/index.php'; |
|
78 | + } |
|
79 | + if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) { |
|
80 | + $method = $_SERVER['REQUEST_METHOD']; |
|
81 | + } else { |
|
82 | + $method = 'GET'; |
|
83 | + } |
|
84 | + $request = \OC::$server->getRequest(); |
|
85 | + $host = $request->getServerHost(); |
|
86 | + $schema = $request->getServerProtocol(); |
|
87 | + $this->context = new RequestContext($baseUrl, $method, $host, $schema); |
|
88 | + // TODO cache |
|
89 | + $this->root = $this->getCollection('root'); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Get the files to load the routes from |
|
94 | + * |
|
95 | + * @return string[] |
|
96 | + */ |
|
97 | + public function getRoutingFiles() { |
|
98 | + if (!isset($this->routingFiles)) { |
|
99 | + $this->routingFiles = []; |
|
100 | + foreach (\OC_APP::getEnabledApps() as $app) { |
|
101 | + $appPath = \OC_App::getAppPath($app); |
|
102 | + if ($appPath !== false) { |
|
103 | + $file = $appPath . '/appinfo/routes.php'; |
|
104 | + if (file_exists($file)) { |
|
105 | + $this->routingFiles[$app] = $file; |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + return $this->routingFiles; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Loads the routes |
|
115 | + * |
|
116 | + * @param null|string $app |
|
117 | + */ |
|
118 | + public function loadRoutes($app = null) { |
|
119 | + if (is_string($app)) { |
|
120 | + $app = \OC_App::cleanAppId($app); |
|
121 | + } |
|
122 | + |
|
123 | + $requestedApp = $app; |
|
124 | + if ($this->loaded) { |
|
125 | + return; |
|
126 | + } |
|
127 | + if (is_null($app)) { |
|
128 | + $this->loaded = true; |
|
129 | + $routingFiles = $this->getRoutingFiles(); |
|
130 | + } else { |
|
131 | + if (isset($this->loadedApps[$app])) { |
|
132 | + return; |
|
133 | + } |
|
134 | + $file = \OC_App::getAppPath($app) . '/appinfo/routes.php'; |
|
135 | + if ($file !== false && file_exists($file)) { |
|
136 | + $routingFiles = [$app => $file]; |
|
137 | + } else { |
|
138 | + $routingFiles = []; |
|
139 | + } |
|
140 | + } |
|
141 | + \OC::$server->getEventLogger()->start('loadroutes' . $requestedApp, 'Loading Routes'); |
|
142 | + foreach ($routingFiles as $app => $file) { |
|
143 | + if (!isset($this->loadedApps[$app])) { |
|
144 | + if (!\OC_App::isAppLoaded($app)) { |
|
145 | + // app MUST be loaded before app routes |
|
146 | + // try again next time loadRoutes() is called |
|
147 | + $this->loaded = false; |
|
148 | + continue; |
|
149 | + } |
|
150 | + $this->loadedApps[$app] = true; |
|
151 | + $this->useCollection($app); |
|
152 | + $this->requireRouteFile($file, $app); |
|
153 | + $collection = $this->getCollection($app); |
|
154 | + $this->root->addCollection($collection); |
|
155 | + |
|
156 | + // Also add the OCS collection |
|
157 | + $collection = $this->getCollection($app.'.ocs'); |
|
158 | + $collection->addPrefix('/ocsapp'); |
|
159 | + $this->root->addCollection($collection); |
|
160 | + } |
|
161 | + } |
|
162 | + if (!isset($this->loadedApps['core'])) { |
|
163 | + $this->loadedApps['core'] = true; |
|
164 | + $this->useCollection('root'); |
|
165 | + require_once __DIR__ . '/../../../core/routes.php'; |
|
166 | + |
|
167 | + // Also add the OCS collection |
|
168 | + $collection = $this->getCollection('root.ocs'); |
|
169 | + $collection->addPrefix('/ocsapp'); |
|
170 | + $this->root->addCollection($collection); |
|
171 | + } |
|
172 | + if ($this->loaded) { |
|
173 | + $collection = $this->getCollection('ocs'); |
|
174 | + $collection->addPrefix('/ocs'); |
|
175 | + $this->root->addCollection($collection); |
|
176 | + } |
|
177 | + \OC::$server->getEventLogger()->end('loadroutes' . $requestedApp); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return string |
|
182 | + * @deprecated |
|
183 | + */ |
|
184 | + public function getCacheKey() { |
|
185 | + return ''; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param string $name |
|
190 | + * @return \Symfony\Component\Routing\RouteCollection |
|
191 | + */ |
|
192 | + protected function getCollection($name) { |
|
193 | + if (!isset($this->collections[$name])) { |
|
194 | + $this->collections[$name] = new RouteCollection(); |
|
195 | + } |
|
196 | + return $this->collections[$name]; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Sets the collection to use for adding routes |
|
201 | + * |
|
202 | + * @param string $name Name of the collection to use. |
|
203 | + * @return void |
|
204 | + */ |
|
205 | + public function useCollection($name) { |
|
206 | + $this->collection = $this->getCollection($name); |
|
207 | + $this->collectionName = $name; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * returns the current collection name in use for adding routes |
|
212 | + * |
|
213 | + * @return string the collection name |
|
214 | + */ |
|
215 | + public function getCurrentCollection() { |
|
216 | + return $this->collectionName; |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * Create a \OC\Route\Route. |
|
222 | + * |
|
223 | + * @param string $name Name of the route to create. |
|
224 | + * @param string $pattern The pattern to match |
|
225 | + * @param array $defaults An array of default parameter values |
|
226 | + * @param array $requirements An array of requirements for parameters (regexes) |
|
227 | + * @return \OC\Route\Route |
|
228 | + */ |
|
229 | + public function create($name, |
|
230 | + $pattern, |
|
231 | + array $defaults = [], |
|
232 | + array $requirements = []) { |
|
233 | + $route = new Route($pattern, $defaults, $requirements); |
|
234 | + $this->collection->add($name, $route); |
|
235 | + return $route; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Find the route matching $url |
|
240 | + * |
|
241 | + * @param string $url The url to find |
|
242 | + * @throws \Exception |
|
243 | + * @return array |
|
244 | + */ |
|
245 | + public function findMatchingRoute(string $url): array { |
|
246 | + if (substr($url, 0, 6) === '/apps/') { |
|
247 | + // empty string / 'apps' / $app / rest of the route |
|
248 | + list(, , $app,) = explode('/', $url, 4); |
|
249 | + |
|
250 | + $app = \OC_App::cleanAppId($app); |
|
251 | + \OC::$REQUESTEDAPP = $app; |
|
252 | + $this->loadRoutes($app); |
|
253 | + } elseif (substr($url, 0, 13) === '/ocsapp/apps/') { |
|
254 | + // empty string / 'ocsapp' / 'apps' / $app / rest of the route |
|
255 | + list(, , , $app,) = explode('/', $url, 5); |
|
256 | + |
|
257 | + $app = \OC_App::cleanAppId($app); |
|
258 | + \OC::$REQUESTEDAPP = $app; |
|
259 | + $this->loadRoutes($app); |
|
260 | + } elseif (substr($url, 0, 10) === '/settings/') { |
|
261 | + $this->loadRoutes('settings'); |
|
262 | + } elseif (substr($url, 0, 6) === '/core/') { |
|
263 | + \OC::$REQUESTEDAPP = $url; |
|
264 | + if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) { |
|
265 | + \OC_App::loadApps(); |
|
266 | + } |
|
267 | + $this->loadRoutes('core'); |
|
268 | + } else { |
|
269 | + $this->loadRoutes(); |
|
270 | + } |
|
271 | + |
|
272 | + $matcher = new UrlMatcher($this->root, $this->context); |
|
273 | + try { |
|
274 | + $parameters = $matcher->match($url); |
|
275 | + } catch (ResourceNotFoundException $e) { |
|
276 | + if (substr($url, -1) !== '/') { |
|
277 | + // We allow links to apps/files? for backwards compatibility reasons |
|
278 | + // However, since Symfony does not allow empty route names, the route |
|
279 | + // we need to match is '/', so we need to append the '/' here. |
|
280 | + try { |
|
281 | + $parameters = $matcher->match($url . '/'); |
|
282 | + } catch (ResourceNotFoundException $newException) { |
|
283 | + // If we still didn't match a route, we throw the original exception |
|
284 | + throw $e; |
|
285 | + } |
|
286 | + } else { |
|
287 | + throw $e; |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + return $parameters; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Find and execute the route matching $url |
|
296 | + * |
|
297 | + * @param string $url The url to find |
|
298 | + * @throws \Exception |
|
299 | + * @return void |
|
300 | + */ |
|
301 | + public function match($url) { |
|
302 | + $parameters = $this->findMatchingRoute($url); |
|
303 | + |
|
304 | + \OC::$server->getEventLogger()->start('run_route', 'Run route'); |
|
305 | + if (isset($parameters['caller'])) { |
|
306 | + $caller = $parameters['caller']; |
|
307 | + unset($parameters['caller']); |
|
308 | + $application = $this->getApplicationClass($caller[0]); |
|
309 | + \OC\AppFramework\App::main($caller[1], $caller[2], $application->getContainer(), $parameters); |
|
310 | + } elseif (isset($parameters['action'])) { |
|
311 | + $action = $parameters['action']; |
|
312 | + if (!is_callable($action)) { |
|
313 | + throw new \Exception('not a callable action'); |
|
314 | + } |
|
315 | + unset($parameters['action']); |
|
316 | + call_user_func($action, $parameters); |
|
317 | + } elseif (isset($parameters['file'])) { |
|
318 | + include $parameters['file']; |
|
319 | + } else { |
|
320 | + throw new \Exception('no action available'); |
|
321 | + } |
|
322 | + \OC::$server->getEventLogger()->end('run_route'); |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Get the url generator |
|
327 | + * |
|
328 | + * @return \Symfony\Component\Routing\Generator\UrlGenerator |
|
329 | + * |
|
330 | + */ |
|
331 | + public function getGenerator() { |
|
332 | + if (null !== $this->generator) { |
|
333 | + return $this->generator; |
|
334 | + } |
|
335 | + |
|
336 | + return $this->generator = new UrlGenerator($this->root, $this->context); |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Generate url based on $name and $parameters |
|
341 | + * |
|
342 | + * @param string $name Name of the route to use. |
|
343 | + * @param array $parameters Parameters for the route |
|
344 | + * @param bool $absolute |
|
345 | + * @return string |
|
346 | + */ |
|
347 | + public function generate($name, |
|
348 | + $parameters = [], |
|
349 | + $absolute = false) { |
|
350 | + $referenceType = UrlGenerator::ABSOLUTE_URL; |
|
351 | + if ($absolute === false) { |
|
352 | + $referenceType = UrlGenerator::ABSOLUTE_PATH; |
|
353 | + } |
|
354 | + $name = $this->fixLegacyRootName($name); |
|
355 | + if (strpos($name, '.') !== false) { |
|
356 | + list($appName, $other) = explode('.', $name, 3); |
|
357 | + // OCS routes are prefixed with "ocs." |
|
358 | + if ($appName === 'ocs') { |
|
359 | + $appName = $other; |
|
360 | + } |
|
361 | + $this->loadRoutes($appName); |
|
362 | + try { |
|
363 | + return $this->getGenerator()->generate($name, $parameters, $referenceType); |
|
364 | + } catch (RouteNotFoundException $e) { |
|
365 | + } |
|
366 | + } |
|
367 | + |
|
368 | + // Fallback load all routes |
|
369 | + $this->loadRoutes(); |
|
370 | + try { |
|
371 | + return $this->getGenerator()->generate($name, $parameters, $referenceType); |
|
372 | + } catch (RouteNotFoundException $e) { |
|
373 | + $this->logger->logException($e, ['level' => ILogger::INFO]); |
|
374 | + return ''; |
|
375 | + } |
|
376 | + } |
|
377 | + |
|
378 | + protected function fixLegacyRootName(string $routeName): string { |
|
379 | + if ($routeName === 'files.viewcontroller.showFile') { |
|
380 | + return 'files.View.showFile'; |
|
381 | + } |
|
382 | + if ($routeName === 'files_sharing.sharecontroller.showShare') { |
|
383 | + return 'files_sharing.Share.showShare'; |
|
384 | + } |
|
385 | + if ($routeName === 'files_sharing.sharecontroller.showAuthenticate') { |
|
386 | + return 'files_sharing.Share.showAuthenticate'; |
|
387 | + } |
|
388 | + if ($routeName === 'files_sharing.sharecontroller.authenticate') { |
|
389 | + return 'files_sharing.Share.authenticate'; |
|
390 | + } |
|
391 | + if ($routeName === 'files_sharing.sharecontroller.downloadShare') { |
|
392 | + return 'files_sharing.Share.downloadShare'; |
|
393 | + } |
|
394 | + if ($routeName === 'files_sharing.publicpreview.directLink') { |
|
395 | + return 'files_sharing.PublicPreview.directLink'; |
|
396 | + } |
|
397 | + if ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') { |
|
398 | + return 'cloud_federation_api.RequestHandler.addShare'; |
|
399 | + } |
|
400 | + if ($routeName === 'cloud_federation_api.requesthandlercontroller.receiveNotification') { |
|
401 | + return 'cloud_federation_api.RequestHandler.receiveNotification'; |
|
402 | + } |
|
403 | + return $routeName; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * To isolate the variable scope used inside the $file it is required in it's own method |
|
408 | + * |
|
409 | + * @param string $file the route file location to include |
|
410 | + * @param string $appName |
|
411 | + */ |
|
412 | + private function requireRouteFile($file, $appName) { |
|
413 | + $this->setupRoutes(include_once $file, $appName); |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * If a routes.php file returns an array, try to set up the application and |
|
419 | + * register the routes for the app. The application class will be chosen by |
|
420 | + * camelcasing the appname, e.g.: my_app will be turned into |
|
421 | + * \OCA\MyApp\AppInfo\Application. If that class does not exist, a default |
|
422 | + * App will be intialized. This makes it optional to ship an |
|
423 | + * appinfo/application.php by using the built in query resolver |
|
424 | + * |
|
425 | + * @param array $routes the application routes |
|
426 | + * @param string $appName the name of the app. |
|
427 | + */ |
|
428 | + private function setupRoutes($routes, $appName) { |
|
429 | + if (is_array($routes)) { |
|
430 | + $routeParser = new RouteParser(); |
|
431 | + |
|
432 | + $defaultRoutes = $routeParser->parseDefaultRoutes($routes, $appName); |
|
433 | + $ocsRoutes = $routeParser->parseOCSRoutes($routes, $appName); |
|
434 | + |
|
435 | + $this->root->addCollection($defaultRoutes); |
|
436 | + $ocsRoutes->addPrefix('/ocsapp'); |
|
437 | + $this->root->addCollection($ocsRoutes); |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + private function getApplicationClass(string $appName) { |
|
442 | + $appNameSpace = App::buildAppNamespace($appName); |
|
443 | + |
|
444 | + $applicationClassName = $appNameSpace . '\\AppInfo\\Application'; |
|
445 | + |
|
446 | + if (class_exists($applicationClassName)) { |
|
447 | + $application = \OC::$server->query($applicationClassName); |
|
448 | + } else { |
|
449 | + $application = new App($appName); |
|
450 | + } |
|
451 | + |
|
452 | + return $application; |
|
453 | + } |
|
454 | 454 | } |