@@ -37,281 +37,281 @@ |
||
| 37 | 37 | * @package OC\AppFramework\routing |
| 38 | 38 | */ |
| 39 | 39 | class RouteConfig { |
| 40 | - /** @var DIContainer */ |
|
| 41 | - private $container; |
|
| 42 | - |
|
| 43 | - /** @var IRouter */ |
|
| 44 | - private $router; |
|
| 45 | - |
|
| 46 | - /** @var array */ |
|
| 47 | - private $routes; |
|
| 48 | - |
|
| 49 | - /** @var string */ |
|
| 50 | - private $appName; |
|
| 51 | - |
|
| 52 | - /** @var string[] */ |
|
| 53 | - private $controllerNameCache = []; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param \OC\AppFramework\DependencyInjection\DIContainer $container |
|
| 57 | - * @param \OCP\Route\IRouter $router |
|
| 58 | - * @param array $routes |
|
| 59 | - * @internal param $appName |
|
| 60 | - */ |
|
| 61 | - public function __construct(DIContainer $container, IRouter $router, $routes) { |
|
| 62 | - $this->routes = $routes; |
|
| 63 | - $this->container = $container; |
|
| 64 | - $this->router = $router; |
|
| 65 | - $this->appName = $container['AppName']; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * The routes and resource will be registered to the \OCP\Route\IRouter |
|
| 70 | - */ |
|
| 71 | - public function register() { |
|
| 72 | - |
|
| 73 | - // parse simple |
|
| 74 | - $this->processSimpleRoutes($this->routes); |
|
| 75 | - |
|
| 76 | - // parse resources |
|
| 77 | - $this->processResources($this->routes); |
|
| 78 | - |
|
| 79 | - /* |
|
| 40 | + /** @var DIContainer */ |
|
| 41 | + private $container; |
|
| 42 | + |
|
| 43 | + /** @var IRouter */ |
|
| 44 | + private $router; |
|
| 45 | + |
|
| 46 | + /** @var array */ |
|
| 47 | + private $routes; |
|
| 48 | + |
|
| 49 | + /** @var string */ |
|
| 50 | + private $appName; |
|
| 51 | + |
|
| 52 | + /** @var string[] */ |
|
| 53 | + private $controllerNameCache = []; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param \OC\AppFramework\DependencyInjection\DIContainer $container |
|
| 57 | + * @param \OCP\Route\IRouter $router |
|
| 58 | + * @param array $routes |
|
| 59 | + * @internal param $appName |
|
| 60 | + */ |
|
| 61 | + public function __construct(DIContainer $container, IRouter $router, $routes) { |
|
| 62 | + $this->routes = $routes; |
|
| 63 | + $this->container = $container; |
|
| 64 | + $this->router = $router; |
|
| 65 | + $this->appName = $container['AppName']; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * The routes and resource will be registered to the \OCP\Route\IRouter |
|
| 70 | + */ |
|
| 71 | + public function register() { |
|
| 72 | + |
|
| 73 | + // parse simple |
|
| 74 | + $this->processSimpleRoutes($this->routes); |
|
| 75 | + |
|
| 76 | + // parse resources |
|
| 77 | + $this->processResources($this->routes); |
|
| 78 | + |
|
| 79 | + /* |
|
| 80 | 80 | * OCS routes go into a different collection |
| 81 | 81 | */ |
| 82 | - $oldCollection = $this->router->getCurrentCollection(); |
|
| 83 | - $this->router->useCollection($oldCollection . '.ocs'); |
|
| 84 | - |
|
| 85 | - // parse ocs simple routes |
|
| 86 | - $this->processOCS($this->routes); |
|
| 87 | - |
|
| 88 | - // parse ocs simple routes |
|
| 89 | - $this->processOCSResources($this->routes); |
|
| 90 | - |
|
| 91 | - $this->router->useCollection($oldCollection); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - private function processOCS(array $routes): void { |
|
| 95 | - $ocsRoutes = $routes['ocs'] ?? []; |
|
| 96 | - foreach ($ocsRoutes as $ocsRoute) { |
|
| 97 | - $name = $ocsRoute['name']; |
|
| 98 | - $postfix = $ocsRoute['postfix'] ?? ''; |
|
| 99 | - $root = $ocsRoute['root'] ?? '/apps/' . $this->appName; |
|
| 100 | - |
|
| 101 | - $url = $root . $ocsRoute['url']; |
|
| 102 | - $verb = strtoupper($ocsRoute['verb'] ?? 'GET'); |
|
| 103 | - |
|
| 104 | - $split = explode('#', $name, 2); |
|
| 105 | - if (count($split) !== 2) { |
|
| 106 | - throw new \UnexpectedValueException('Invalid route name'); |
|
| 107 | - } |
|
| 108 | - list($controller, $action) = $split; |
|
| 109 | - |
|
| 110 | - $controllerName = $this->buildControllerName($controller); |
|
| 111 | - $actionName = $this->buildActionName($action); |
|
| 112 | - |
|
| 113 | - $routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 114 | - |
|
| 115 | - // register the route |
|
| 116 | - $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 117 | - |
|
| 118 | - $router = $this->router->create($routeName, $url) |
|
| 119 | - ->method($verb) |
|
| 120 | - ->action($handler); |
|
| 121 | - |
|
| 122 | - // optionally register requirements for route. This is used to |
|
| 123 | - // tell the route parser how url parameters should be matched |
|
| 124 | - if(array_key_exists('requirements', $ocsRoute)) { |
|
| 125 | - $router->requirements($ocsRoute['requirements']); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // optionally register defaults for route. This is used to |
|
| 129 | - // tell the route parser how url parameters should be default valued |
|
| 130 | - if(array_key_exists('defaults', $ocsRoute)) { |
|
| 131 | - $router->defaults($ocsRoute['defaults']); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Creates one route base on the give configuration |
|
| 138 | - * @param array $routes |
|
| 139 | - * @throws \UnexpectedValueException |
|
| 140 | - */ |
|
| 141 | - private function processSimpleRoutes(array $routes): void { |
|
| 142 | - $simpleRoutes = $routes['routes'] ?? []; |
|
| 143 | - foreach ($simpleRoutes as $simpleRoute) { |
|
| 144 | - $name = $simpleRoute['name']; |
|
| 145 | - $postfix = $simpleRoute['postfix'] ?? ''; |
|
| 146 | - |
|
| 147 | - $url = $simpleRoute['url']; |
|
| 148 | - $verb = strtoupper($simpleRoute['verb'] ?? 'GET'); |
|
| 149 | - |
|
| 150 | - $split = explode('#', $name, 2); |
|
| 151 | - if (count($split) !== 2) { |
|
| 152 | - throw new \UnexpectedValueException('Invalid route name'); |
|
| 153 | - } |
|
| 154 | - list($controller, $action) = $split; |
|
| 155 | - |
|
| 156 | - $controllerName = $this->buildControllerName($controller); |
|
| 157 | - $actionName = $this->buildActionName($action); |
|
| 158 | - |
|
| 159 | - $routeName = $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 160 | - |
|
| 161 | - // register the route |
|
| 162 | - $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 163 | - $router = $this->router->create($routeName, $url) |
|
| 164 | - ->method($verb) |
|
| 165 | - ->action($handler); |
|
| 166 | - |
|
| 167 | - // optionally register requirements for route. This is used to |
|
| 168 | - // tell the route parser how url parameters should be matched |
|
| 169 | - if(array_key_exists('requirements', $simpleRoute)) { |
|
| 170 | - $router->requirements($simpleRoute['requirements']); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // optionally register defaults for route. This is used to |
|
| 174 | - // tell the route parser how url parameters should be default valued |
|
| 175 | - if(array_key_exists('defaults', $simpleRoute)) { |
|
| 176 | - $router->defaults($simpleRoute['defaults']); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * For a given name and url restful OCS routes are created: |
|
| 183 | - * - index |
|
| 184 | - * - show |
|
| 185 | - * - create |
|
| 186 | - * - update |
|
| 187 | - * - destroy |
|
| 188 | - * |
|
| 189 | - * @param array $routes |
|
| 190 | - */ |
|
| 191 | - private function processOCSResources(array $routes): void { |
|
| 192 | - // declaration of all restful actions |
|
| 193 | - $actions = [ |
|
| 194 | - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 195 | - ['name' => 'show', 'verb' => 'GET'], |
|
| 196 | - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 197 | - ['name' => 'update', 'verb' => 'PUT'], |
|
| 198 | - ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 199 | - ]; |
|
| 200 | - |
|
| 201 | - $resources = $routes['ocs-resources'] ?? []; |
|
| 202 | - foreach ($resources as $resource => $config) { |
|
| 203 | - $root = $config['root'] ?? '/apps/' . $this->appName; |
|
| 204 | - |
|
| 205 | - // the url parameter used as id to the resource |
|
| 206 | - foreach($actions as $action) { |
|
| 207 | - $url = $root . $config['url']; |
|
| 208 | - $method = $action['name']; |
|
| 209 | - $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 210 | - $collectionAction = $action['on-collection'] ?? false; |
|
| 211 | - if (!$collectionAction) { |
|
| 212 | - $url .= '/{id}'; |
|
| 213 | - } |
|
| 214 | - if (isset($action['url-postfix'])) { |
|
| 215 | - $url .= '/' . $action['url-postfix']; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $controller = $resource; |
|
| 219 | - |
|
| 220 | - $controllerName = $this->buildControllerName($controller); |
|
| 221 | - $actionName = $this->buildActionName($method); |
|
| 222 | - |
|
| 223 | - $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 224 | - |
|
| 225 | - $this->router->create($routeName, $url)->method($verb)->action( |
|
| 226 | - new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 227 | - ); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * For a given name and url restful routes are created: |
|
| 234 | - * - index |
|
| 235 | - * - show |
|
| 236 | - * - create |
|
| 237 | - * - update |
|
| 238 | - * - destroy |
|
| 239 | - * |
|
| 240 | - * @param array $routes |
|
| 241 | - */ |
|
| 242 | - private function processResources(array $routes): void { |
|
| 243 | - // declaration of all restful actions |
|
| 244 | - $actions = [ |
|
| 245 | - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 246 | - ['name' => 'show', 'verb' => 'GET'], |
|
| 247 | - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 248 | - ['name' => 'update', 'verb' => 'PUT'], |
|
| 249 | - ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 250 | - ]; |
|
| 251 | - |
|
| 252 | - $resources = $routes['resources'] ?? []; |
|
| 253 | - foreach ($resources as $resource => $config) { |
|
| 254 | - |
|
| 255 | - // the url parameter used as id to the resource |
|
| 256 | - foreach($actions as $action) { |
|
| 257 | - $url = $config['url']; |
|
| 258 | - $method = $action['name']; |
|
| 259 | - $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 260 | - $collectionAction = $action['on-collection'] ?? false; |
|
| 261 | - if (!$collectionAction) { |
|
| 262 | - $url .= '/{id}'; |
|
| 263 | - } |
|
| 264 | - if (isset($action['url-postfix'])) { |
|
| 265 | - $url .= '/' . $action['url-postfix']; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $controller = $resource; |
|
| 269 | - |
|
| 270 | - $controllerName = $this->buildControllerName($controller); |
|
| 271 | - $actionName = $this->buildActionName($method); |
|
| 272 | - |
|
| 273 | - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 274 | - |
|
| 275 | - $this->router->create($routeName, $url)->method($verb)->action( |
|
| 276 | - new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 277 | - ); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Based on a given route name the controller name is generated |
|
| 284 | - * @param string $controller |
|
| 285 | - * @return string |
|
| 286 | - */ |
|
| 287 | - private function buildControllerName(string $controller): string { |
|
| 288 | - if (!isset($this->controllerNameCache[$controller])) { |
|
| 289 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 290 | - } |
|
| 291 | - return $this->controllerNameCache[$controller]; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Based on the action part of the route name the controller method name is generated |
|
| 296 | - * @param string $action |
|
| 297 | - * @return string |
|
| 298 | - */ |
|
| 299 | - private function buildActionName(string $action): string { |
|
| 300 | - return $this->underScoreToCamelCase($action); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Underscored strings are converted to camel case strings |
|
| 305 | - * @param string $str |
|
| 306 | - * @return string |
|
| 307 | - */ |
|
| 308 | - private function underScoreToCamelCase(string $str): string { |
|
| 309 | - $pattern = '/_[a-z]?/'; |
|
| 310 | - return preg_replace_callback( |
|
| 311 | - $pattern, |
|
| 312 | - function ($matches) { |
|
| 313 | - return strtoupper(ltrim($matches[0], '_')); |
|
| 314 | - }, |
|
| 315 | - $str); |
|
| 316 | - } |
|
| 82 | + $oldCollection = $this->router->getCurrentCollection(); |
|
| 83 | + $this->router->useCollection($oldCollection . '.ocs'); |
|
| 84 | + |
|
| 85 | + // parse ocs simple routes |
|
| 86 | + $this->processOCS($this->routes); |
|
| 87 | + |
|
| 88 | + // parse ocs simple routes |
|
| 89 | + $this->processOCSResources($this->routes); |
|
| 90 | + |
|
| 91 | + $this->router->useCollection($oldCollection); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + private function processOCS(array $routes): void { |
|
| 95 | + $ocsRoutes = $routes['ocs'] ?? []; |
|
| 96 | + foreach ($ocsRoutes as $ocsRoute) { |
|
| 97 | + $name = $ocsRoute['name']; |
|
| 98 | + $postfix = $ocsRoute['postfix'] ?? ''; |
|
| 99 | + $root = $ocsRoute['root'] ?? '/apps/' . $this->appName; |
|
| 100 | + |
|
| 101 | + $url = $root . $ocsRoute['url']; |
|
| 102 | + $verb = strtoupper($ocsRoute['verb'] ?? 'GET'); |
|
| 103 | + |
|
| 104 | + $split = explode('#', $name, 2); |
|
| 105 | + if (count($split) !== 2) { |
|
| 106 | + throw new \UnexpectedValueException('Invalid route name'); |
|
| 107 | + } |
|
| 108 | + list($controller, $action) = $split; |
|
| 109 | + |
|
| 110 | + $controllerName = $this->buildControllerName($controller); |
|
| 111 | + $actionName = $this->buildActionName($action); |
|
| 112 | + |
|
| 113 | + $routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 114 | + |
|
| 115 | + // register the route |
|
| 116 | + $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 117 | + |
|
| 118 | + $router = $this->router->create($routeName, $url) |
|
| 119 | + ->method($verb) |
|
| 120 | + ->action($handler); |
|
| 121 | + |
|
| 122 | + // optionally register requirements for route. This is used to |
|
| 123 | + // tell the route parser how url parameters should be matched |
|
| 124 | + if(array_key_exists('requirements', $ocsRoute)) { |
|
| 125 | + $router->requirements($ocsRoute['requirements']); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // optionally register defaults for route. This is used to |
|
| 129 | + // tell the route parser how url parameters should be default valued |
|
| 130 | + if(array_key_exists('defaults', $ocsRoute)) { |
|
| 131 | + $router->defaults($ocsRoute['defaults']); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Creates one route base on the give configuration |
|
| 138 | + * @param array $routes |
|
| 139 | + * @throws \UnexpectedValueException |
|
| 140 | + */ |
|
| 141 | + private function processSimpleRoutes(array $routes): void { |
|
| 142 | + $simpleRoutes = $routes['routes'] ?? []; |
|
| 143 | + foreach ($simpleRoutes as $simpleRoute) { |
|
| 144 | + $name = $simpleRoute['name']; |
|
| 145 | + $postfix = $simpleRoute['postfix'] ?? ''; |
|
| 146 | + |
|
| 147 | + $url = $simpleRoute['url']; |
|
| 148 | + $verb = strtoupper($simpleRoute['verb'] ?? 'GET'); |
|
| 149 | + |
|
| 150 | + $split = explode('#', $name, 2); |
|
| 151 | + if (count($split) !== 2) { |
|
| 152 | + throw new \UnexpectedValueException('Invalid route name'); |
|
| 153 | + } |
|
| 154 | + list($controller, $action) = $split; |
|
| 155 | + |
|
| 156 | + $controllerName = $this->buildControllerName($controller); |
|
| 157 | + $actionName = $this->buildActionName($action); |
|
| 158 | + |
|
| 159 | + $routeName = $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 160 | + |
|
| 161 | + // register the route |
|
| 162 | + $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 163 | + $router = $this->router->create($routeName, $url) |
|
| 164 | + ->method($verb) |
|
| 165 | + ->action($handler); |
|
| 166 | + |
|
| 167 | + // optionally register requirements for route. This is used to |
|
| 168 | + // tell the route parser how url parameters should be matched |
|
| 169 | + if(array_key_exists('requirements', $simpleRoute)) { |
|
| 170 | + $router->requirements($simpleRoute['requirements']); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // optionally register defaults for route. This is used to |
|
| 174 | + // tell the route parser how url parameters should be default valued |
|
| 175 | + if(array_key_exists('defaults', $simpleRoute)) { |
|
| 176 | + $router->defaults($simpleRoute['defaults']); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * For a given name and url restful OCS routes are created: |
|
| 183 | + * - index |
|
| 184 | + * - show |
|
| 185 | + * - create |
|
| 186 | + * - update |
|
| 187 | + * - destroy |
|
| 188 | + * |
|
| 189 | + * @param array $routes |
|
| 190 | + */ |
|
| 191 | + private function processOCSResources(array $routes): void { |
|
| 192 | + // declaration of all restful actions |
|
| 193 | + $actions = [ |
|
| 194 | + ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 195 | + ['name' => 'show', 'verb' => 'GET'], |
|
| 196 | + ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 197 | + ['name' => 'update', 'verb' => 'PUT'], |
|
| 198 | + ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 199 | + ]; |
|
| 200 | + |
|
| 201 | + $resources = $routes['ocs-resources'] ?? []; |
|
| 202 | + foreach ($resources as $resource => $config) { |
|
| 203 | + $root = $config['root'] ?? '/apps/' . $this->appName; |
|
| 204 | + |
|
| 205 | + // the url parameter used as id to the resource |
|
| 206 | + foreach($actions as $action) { |
|
| 207 | + $url = $root . $config['url']; |
|
| 208 | + $method = $action['name']; |
|
| 209 | + $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 210 | + $collectionAction = $action['on-collection'] ?? false; |
|
| 211 | + if (!$collectionAction) { |
|
| 212 | + $url .= '/{id}'; |
|
| 213 | + } |
|
| 214 | + if (isset($action['url-postfix'])) { |
|
| 215 | + $url .= '/' . $action['url-postfix']; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $controller = $resource; |
|
| 219 | + |
|
| 220 | + $controllerName = $this->buildControllerName($controller); |
|
| 221 | + $actionName = $this->buildActionName($method); |
|
| 222 | + |
|
| 223 | + $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 224 | + |
|
| 225 | + $this->router->create($routeName, $url)->method($verb)->action( |
|
| 226 | + new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 227 | + ); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * For a given name and url restful routes are created: |
|
| 234 | + * - index |
|
| 235 | + * - show |
|
| 236 | + * - create |
|
| 237 | + * - update |
|
| 238 | + * - destroy |
|
| 239 | + * |
|
| 240 | + * @param array $routes |
|
| 241 | + */ |
|
| 242 | + private function processResources(array $routes): void { |
|
| 243 | + // declaration of all restful actions |
|
| 244 | + $actions = [ |
|
| 245 | + ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 246 | + ['name' => 'show', 'verb' => 'GET'], |
|
| 247 | + ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 248 | + ['name' => 'update', 'verb' => 'PUT'], |
|
| 249 | + ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 250 | + ]; |
|
| 251 | + |
|
| 252 | + $resources = $routes['resources'] ?? []; |
|
| 253 | + foreach ($resources as $resource => $config) { |
|
| 254 | + |
|
| 255 | + // the url parameter used as id to the resource |
|
| 256 | + foreach($actions as $action) { |
|
| 257 | + $url = $config['url']; |
|
| 258 | + $method = $action['name']; |
|
| 259 | + $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 260 | + $collectionAction = $action['on-collection'] ?? false; |
|
| 261 | + if (!$collectionAction) { |
|
| 262 | + $url .= '/{id}'; |
|
| 263 | + } |
|
| 264 | + if (isset($action['url-postfix'])) { |
|
| 265 | + $url .= '/' . $action['url-postfix']; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $controller = $resource; |
|
| 269 | + |
|
| 270 | + $controllerName = $this->buildControllerName($controller); |
|
| 271 | + $actionName = $this->buildActionName($method); |
|
| 272 | + |
|
| 273 | + $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 274 | + |
|
| 275 | + $this->router->create($routeName, $url)->method($verb)->action( |
|
| 276 | + new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 277 | + ); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Based on a given route name the controller name is generated |
|
| 284 | + * @param string $controller |
|
| 285 | + * @return string |
|
| 286 | + */ |
|
| 287 | + private function buildControllerName(string $controller): string { |
|
| 288 | + if (!isset($this->controllerNameCache[$controller])) { |
|
| 289 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 290 | + } |
|
| 291 | + return $this->controllerNameCache[$controller]; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Based on the action part of the route name the controller method name is generated |
|
| 296 | + * @param string $action |
|
| 297 | + * @return string |
|
| 298 | + */ |
|
| 299 | + private function buildActionName(string $action): string { |
|
| 300 | + return $this->underScoreToCamelCase($action); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Underscored strings are converted to camel case strings |
|
| 305 | + * @param string $str |
|
| 306 | + * @return string |
|
| 307 | + */ |
|
| 308 | + private function underScoreToCamelCase(string $str): string { |
|
| 309 | + $pattern = '/_[a-z]?/'; |
|
| 310 | + return preg_replace_callback( |
|
| 311 | + $pattern, |
|
| 312 | + function ($matches) { |
|
| 313 | + return strtoupper(ltrim($matches[0], '_')); |
|
| 314 | + }, |
|
| 315 | + $str); |
|
| 316 | + } |
|
| 317 | 317 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * OCS routes go into a different collection |
| 81 | 81 | */ |
| 82 | 82 | $oldCollection = $this->router->getCurrentCollection(); |
| 83 | - $this->router->useCollection($oldCollection . '.ocs'); |
|
| 83 | + $this->router->useCollection($oldCollection.'.ocs'); |
|
| 84 | 84 | |
| 85 | 85 | // parse ocs simple routes |
| 86 | 86 | $this->processOCS($this->routes); |
@@ -96,9 +96,9 @@ discard block |
||
| 96 | 96 | foreach ($ocsRoutes as $ocsRoute) { |
| 97 | 97 | $name = $ocsRoute['name']; |
| 98 | 98 | $postfix = $ocsRoute['postfix'] ?? ''; |
| 99 | - $root = $ocsRoute['root'] ?? '/apps/' . $this->appName; |
|
| 99 | + $root = $ocsRoute['root'] ?? '/apps/'.$this->appName; |
|
| 100 | 100 | |
| 101 | - $url = $root . $ocsRoute['url']; |
|
| 101 | + $url = $root.$ocsRoute['url']; |
|
| 102 | 102 | $verb = strtoupper($ocsRoute['verb'] ?? 'GET'); |
| 103 | 103 | |
| 104 | 104 | $split = explode('#', $name, 2); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $controllerName = $this->buildControllerName($controller); |
| 111 | 111 | $actionName = $this->buildActionName($action); |
| 112 | 112 | |
| 113 | - $routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 113 | + $routeName = 'ocs.'.$this->appName.'.'.$controller.'.'.$action.$postfix; |
|
| 114 | 114 | |
| 115 | 115 | // register the route |
| 116 | 116 | $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
@@ -121,13 +121,13 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | // optionally register requirements for route. This is used to |
| 123 | 123 | // tell the route parser how url parameters should be matched |
| 124 | - if(array_key_exists('requirements', $ocsRoute)) { |
|
| 124 | + if (array_key_exists('requirements', $ocsRoute)) { |
|
| 125 | 125 | $router->requirements($ocsRoute['requirements']); |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // optionally register defaults for route. This is used to |
| 129 | 129 | // tell the route parser how url parameters should be default valued |
| 130 | - if(array_key_exists('defaults', $ocsRoute)) { |
|
| 130 | + if (array_key_exists('defaults', $ocsRoute)) { |
|
| 131 | 131 | $router->defaults($ocsRoute['defaults']); |
| 132 | 132 | } |
| 133 | 133 | } |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | $controllerName = $this->buildControllerName($controller); |
| 157 | 157 | $actionName = $this->buildActionName($action); |
| 158 | 158 | |
| 159 | - $routeName = $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 159 | + $routeName = $this->appName.'.'.$controller.'.'.$action.$postfix; |
|
| 160 | 160 | |
| 161 | 161 | // register the route |
| 162 | 162 | $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
@@ -166,13 +166,13 @@ discard block |
||
| 166 | 166 | |
| 167 | 167 | // optionally register requirements for route. This is used to |
| 168 | 168 | // tell the route parser how url parameters should be matched |
| 169 | - if(array_key_exists('requirements', $simpleRoute)) { |
|
| 169 | + if (array_key_exists('requirements', $simpleRoute)) { |
|
| 170 | 170 | $router->requirements($simpleRoute['requirements']); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // optionally register defaults for route. This is used to |
| 174 | 174 | // tell the route parser how url parameters should be default valued |
| 175 | - if(array_key_exists('defaults', $simpleRoute)) { |
|
| 175 | + if (array_key_exists('defaults', $simpleRoute)) { |
|
| 176 | 176 | $router->defaults($simpleRoute['defaults']); |
| 177 | 177 | } |
| 178 | 178 | } |
@@ -200,11 +200,11 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | $resources = $routes['ocs-resources'] ?? []; |
| 202 | 202 | foreach ($resources as $resource => $config) { |
| 203 | - $root = $config['root'] ?? '/apps/' . $this->appName; |
|
| 203 | + $root = $config['root'] ?? '/apps/'.$this->appName; |
|
| 204 | 204 | |
| 205 | 205 | // the url parameter used as id to the resource |
| 206 | - foreach($actions as $action) { |
|
| 207 | - $url = $root . $config['url']; |
|
| 206 | + foreach ($actions as $action) { |
|
| 207 | + $url = $root.$config['url']; |
|
| 208 | 208 | $method = $action['name']; |
| 209 | 209 | $verb = strtoupper($action['verb'] ?? 'GET'); |
| 210 | 210 | $collectionAction = $action['on-collection'] ?? false; |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | $url .= '/{id}'; |
| 213 | 213 | } |
| 214 | 214 | if (isset($action['url-postfix'])) { |
| 215 | - $url .= '/' . $action['url-postfix']; |
|
| 215 | + $url .= '/'.$action['url-postfix']; |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | $controller = $resource; |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | $controllerName = $this->buildControllerName($controller); |
| 221 | 221 | $actionName = $this->buildActionName($method); |
| 222 | 222 | |
| 223 | - $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 223 | + $routeName = 'ocs.'.$this->appName.'.'.strtolower($resource).'.'.strtolower($method); |
|
| 224 | 224 | |
| 225 | 225 | $this->router->create($routeName, $url)->method($verb)->action( |
| 226 | 226 | new RouteActionHandler($this->container, $controllerName, $actionName) |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | foreach ($resources as $resource => $config) { |
| 254 | 254 | |
| 255 | 255 | // the url parameter used as id to the resource |
| 256 | - foreach($actions as $action) { |
|
| 256 | + foreach ($actions as $action) { |
|
| 257 | 257 | $url = $config['url']; |
| 258 | 258 | $method = $action['name']; |
| 259 | 259 | $verb = strtoupper($action['verb'] ?? 'GET'); |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | $url .= '/{id}'; |
| 263 | 263 | } |
| 264 | 264 | if (isset($action['url-postfix'])) { |
| 265 | - $url .= '/' . $action['url-postfix']; |
|
| 265 | + $url .= '/'.$action['url-postfix']; |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | $controller = $resource; |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | $controllerName = $this->buildControllerName($controller); |
| 271 | 271 | $actionName = $this->buildActionName($method); |
| 272 | 272 | |
| 273 | - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 273 | + $routeName = $this->appName.'.'.strtolower($resource).'.'.strtolower($method); |
|
| 274 | 274 | |
| 275 | 275 | $this->router->create($routeName, $url)->method($verb)->action( |
| 276 | 276 | new RouteActionHandler($this->container, $controllerName, $actionName) |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | */ |
| 287 | 287 | private function buildControllerName(string $controller): string { |
| 288 | 288 | if (!isset($this->controllerNameCache[$controller])) { |
| 289 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 289 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)).'Controller'; |
|
| 290 | 290 | } |
| 291 | 291 | return $this->controllerNameCache[$controller]; |
| 292 | 292 | } |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | $pattern = '/_[a-z]?/'; |
| 310 | 310 | return preg_replace_callback( |
| 311 | 311 | $pattern, |
| 312 | - function ($matches) { |
|
| 312 | + function($matches) { |
|
| 313 | 313 | return strtoupper(ltrim($matches[0], '_')); |
| 314 | 314 | }, |
| 315 | 315 | $str); |