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