Make RouteConfig strict

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2019-01-21 12:14:01 +01:00
parent f8b74cf0a5
commit 92edd40e51
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
1 changed files with 48 additions and 62 deletions

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -79,7 +80,7 @@ class RouteConfig {
* OCS routes go into a different collection
*/
$oldCollection = $this->router->getCurrentCollection();
$this->router->useCollection($oldCollection.'.ocs');
$this->router->useCollection($oldCollection . '.ocs');
// parse ocs simple routes
$this->processOCS($this->routes);
@ -90,39 +91,31 @@ class RouteConfig {
$this->router->useCollection($oldCollection);
}
private function processOCS(array $routes) {
$ocsRoutes = isset($routes['ocs']) ? $routes['ocs'] : [];
private function processOCS(array $routes): void {
$ocsRoutes = $routes['ocs'] ?? [];
foreach ($ocsRoutes as $ocsRoute) {
$name = $ocsRoute['name'];
$postfix = '';
if (isset($ocsRoute['postfix'])) {
$postfix = $ocsRoute['postfix'];
}
if (isset($ocsRoute['root'])) {
$root = $ocsRoute['root'];
} else {
$root = '/apps/'.$this->appName;
}
$postfix = $ocsRoute['postfix'] ?? '';
$root = $ocsRoute['root'] ?? '/apps/' . $this->appName;
$url = $root . $ocsRoute['url'];
$verb = isset($ocsRoute['verb']) ? strtoupper($ocsRoute['verb']) : 'GET';
$verb = strtoupper($ocsRoute['verb'] ?? 'GET');
$split = explode('#', $name, 2);
if (count($split) != 2) {
if (count($split) !== 2) {
throw new \UnexpectedValueException('Invalid route name');
}
$controller = $split[0];
$action = $split[1];
list($controller, $action) = $split;
$controllerName = $this->buildControllerName($controller);
$actionName = $this->buildActionName($action);
$routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix;
// register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
$router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action . $postfix, $url)
$router = $this->router->create($routeName, $url)
->method($verb)
->action($handler);
@ -145,33 +138,29 @@ class RouteConfig {
* @param array $routes
* @throws \UnexpectedValueException
*/
private function processSimpleRoutes($routes)
{
$simpleRoutes = isset($routes['routes']) ? $routes['routes'] : array();
private function processSimpleRoutes(array $routes): void {
$simpleRoutes = $routes['routes'] ?? [];
foreach ($simpleRoutes as $simpleRoute) {
$name = $simpleRoute['name'];
$postfix = '';
if (isset($simpleRoute['postfix'])) {
$postfix = $simpleRoute['postfix'];
}
$postfix = $simpleRoute['postfix'] ?? '';
$url = $simpleRoute['url'];
$verb = isset($simpleRoute['verb']) ? strtoupper($simpleRoute['verb']) : 'GET';
$verb = strtoupper($simpleRoute['verb'] ?? 'GET');
$split = explode('#', $name, 2);
if (count($split) != 2) {
if (count($split) !== 2) {
throw new \UnexpectedValueException('Invalid route name');
}
$controller = $split[0];
$action = $split[1];
list($controller, $action) = $split;
$controllerName = $this->buildControllerName($controller);
$actionName = $this->buildActionName($action);
$routeName = $this->appName . '.' . $controller . '.' . $action . $postfix;
// register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
$router = $this->router->create($this->appName.'.'.$controller.'.'.$action . $postfix, $url)
$router = $this->router->create($routeName, $url)
->method($verb)
->action($handler);
@ -190,7 +179,7 @@ class RouteConfig {
}
/**
* For a given name and url restful routes are created:
* For a given name and url restful OCS routes are created:
* - index
* - show
* - create
@ -199,16 +188,15 @@ class RouteConfig {
*
* @param array $routes
*/
private function processOCSResources($routes)
{
private function processOCSResources(array $routes): void {
// declaration of all restful actions
$actions = array(
array('name' => 'index', 'verb' => 'GET', 'on-collection' => true),
array('name' => 'show', 'verb' => 'GET'),
array('name' => 'create', 'verb' => 'POST', 'on-collection' => true),
array('name' => 'update', 'verb' => 'PUT'),
array('name' => 'destroy', 'verb' => 'DELETE'),
);
$actions = [
['name' => 'index', 'verb' => 'GET', 'on-collection' => true],
['name' => 'show', 'verb' => 'GET'],
['name' => 'create', 'verb' => 'POST', 'on-collection' => true],
['name' => 'update', 'verb' => 'PUT'],
['name' => 'destroy', 'verb' => 'DELETE'],
];
$resources = $routes['ocs-resources'] ?? [];
foreach ($resources as $resource => $config) {
@ -251,31 +239,30 @@ class RouteConfig {
*
* @param array $routes
*/
private function processResources($routes)
{
private function processResources(array $routes): void {
// declaration of all restful actions
$actions = array(
array('name' => 'index', 'verb' => 'GET', 'on-collection' => true),
array('name' => 'show', 'verb' => 'GET'),
array('name' => 'create', 'verb' => 'POST', 'on-collection' => true),
array('name' => 'update', 'verb' => 'PUT'),
array('name' => 'destroy', 'verb' => 'DELETE'),
);
$actions = [
['name' => 'index', 'verb' => 'GET', 'on-collection' => true],
['name' => 'show', 'verb' => 'GET'],
['name' => 'create', 'verb' => 'POST', 'on-collection' => true],
['name' => 'update', 'verb' => 'PUT'],
['name' => 'destroy', 'verb' => 'DELETE'],
];
$resources = isset($routes['resources']) ? $routes['resources'] : array();
$resources = $routes['resources'] ?? [];
foreach ($resources as $resource => $config) {
// the url parameter used as id to the resource
foreach($actions as $action) {
$url = $config['url'];
$method = $action['name'];
$verb = isset($action['verb']) ? strtoupper($action['verb']) : 'GET';
$collectionAction = isset($action['on-collection']) ? $action['on-collection'] : false;
$verb = strtoupper($action['verb'] ?? 'GET');
$collectionAction = $action['on-collection'] ?? false;
if (!$collectionAction) {
$url = $url . '/{id}';
$url .= '/{id}';
}
if (isset($action['url-postfix'])) {
$url = $url . '/' . $action['url-postfix'];
$url .= '/' . $action['url-postfix'];
}
$controller = $resource;
@ -297,8 +284,7 @@ class RouteConfig {
* @param string $controller
* @return string
*/
private function buildControllerName($controller)
{
private function buildControllerName(string $controller): string {
if (!isset($this->controllerNameCache[$controller])) {
$this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller';
}
@ -310,7 +296,7 @@ class RouteConfig {
* @param string $action
* @return string
*/
private function buildActionName($action) {
private function buildActionName(string $action): string {
return $this->underScoreToCamelCase($action);
}
@ -319,12 +305,12 @@ class RouteConfig {
* @param string $str
* @return string
*/
private function underScoreToCamelCase($str) {
$pattern = "/_[a-z]?/";
private function underScoreToCamelCase(string $str): string {
$pattern = '/_[a-z]?/';
return preg_replace_callback(
$pattern,
function ($matches) {
return strtoupper(ltrim($matches[0], "_"));
return strtoupper(ltrim($matches[0], '_'));
},
$str);
}