Completed
Pull Request — master (#9272)
by Joas
26:02 queued 06:59
created
AppFramework/Middleware/Security/Exceptions/NotConfirmedException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@
 block discarded – undo
31 31
  * @package OC\AppFramework\Middleware\Security\Exceptions
32 32
  */
33 33
 class NotConfirmedException extends SecurityException {
34
-	public function __construct() {
35
-		parent::__construct('Password confirmation is required', Http::STATUS_FORBIDDEN);
36
-	}
34
+    public function __construct() {
35
+        parent::__construct('Password confirmation is required', Http::STATUS_FORBIDDEN);
36
+    }
37 37
 }
Please login to merge, or discard this patch.
Middleware/Security/Exceptions/CrossSiteRequestForgeryException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class CrossSiteRequestForgeryException extends SecurityException {
37
-	public function __construct() {
38
-		parent::__construct('CSRF check failed', Http::STATUS_PRECONDITION_FAILED);
39
-	}
37
+    public function __construct() {
38
+        parent::__construct('CSRF check failed', Http::STATUS_PRECONDITION_FAILED);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
AppFramework/Middleware/Security/Exceptions/AppNotEnabledException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class AppNotEnabledException extends SecurityException {
37
-	public function __construct() {
38
-		parent::__construct('App is not enabled', Http::STATUS_PRECONDITION_FAILED);
39
-	}
37
+    public function __construct() {
38
+        parent::__construct('App is not enabled', Http::STATUS_PRECONDITION_FAILED);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
AppFramework/Middleware/Security/Exceptions/NotLoggedInException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class NotLoggedInException extends SecurityException {
37
-	public function __construct() {
38
-		parent::__construct('Current user is not logged in', Http::STATUS_UNAUTHORIZED);
39
-	}
37
+    public function __construct() {
38
+        parent::__construct('Current user is not logged in', Http::STATUS_UNAUTHORIZED);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/OCS/BaseResponse.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -27,70 +27,70 @@
 block discarded – undo
27 27
 use OCP\AppFramework\Http\Response;
28 28
 
29 29
 abstract class BaseResponse extends Response   {
30
-	/** @var array */
31
-	protected $data;
30
+    /** @var array */
31
+    protected $data;
32 32
 
33
-	/** @var string */
34
-	protected $format;
33
+    /** @var string */
34
+    protected $format;
35 35
 
36
-	/** @var string */
37
-	protected $statusMessage;
36
+    /** @var string */
37
+    protected $statusMessage;
38 38
 
39
-	/** @var int */
40
-	protected $itemsCount;
39
+    /** @var int */
40
+    protected $itemsCount;
41 41
 
42
-	/** @var int */
43
-	protected $itemsPerPage;
42
+    /** @var int */
43
+    protected $itemsPerPage;
44 44
 
45
-	/**
46
-	 * BaseResponse constructor.
47
-	 *
48
-	 * @param DataResponse|null $dataResponse
49
-	 * @param string $format
50
-	 * @param string|null $statusMessage
51
-	 * @param int|null $itemsCount
52
-	 * @param int|null $itemsPerPage
53
-	 */
54
-	public function __construct(DataResponse $dataResponse,
55
-								$format = 'xml',
56
-								$statusMessage = null,
57
-								$itemsCount = null,
58
-								$itemsPerPage = null) {
59
-		$this->format = $format;
60
-		$this->statusMessage = $statusMessage;
61
-		$this->itemsCount = $itemsCount;
62
-		$this->itemsPerPage = $itemsPerPage;
45
+    /**
46
+     * BaseResponse constructor.
47
+     *
48
+     * @param DataResponse|null $dataResponse
49
+     * @param string $format
50
+     * @param string|null $statusMessage
51
+     * @param int|null $itemsCount
52
+     * @param int|null $itemsPerPage
53
+     */
54
+    public function __construct(DataResponse $dataResponse,
55
+                                $format = 'xml',
56
+                                $statusMessage = null,
57
+                                $itemsCount = null,
58
+                                $itemsPerPage = null) {
59
+        $this->format = $format;
60
+        $this->statusMessage = $statusMessage;
61
+        $this->itemsCount = $itemsCount;
62
+        $this->itemsPerPage = $itemsPerPage;
63 63
 
64
-		$this->data = $dataResponse->getData();
64
+        $this->data = $dataResponse->getData();
65 65
 
66
-		$this->setHeaders($dataResponse->getHeaders());
67
-		$this->setStatus($dataResponse->getStatus());
68
-		$this->setETag($dataResponse->getETag());
69
-		$this->setLastModified($dataResponse->getLastModified());
70
-		$this->setCookies($dataResponse->getCookies());
71
-		$this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
66
+        $this->setHeaders($dataResponse->getHeaders());
67
+        $this->setStatus($dataResponse->getStatus());
68
+        $this->setETag($dataResponse->getETag());
69
+        $this->setLastModified($dataResponse->getLastModified());
70
+        $this->setCookies($dataResponse->getCookies());
71
+        $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
72 72
 
73
-		if ($format === 'json') {
74
-			$this->addHeader(
75
-				'Content-Type', 'application/json; charset=utf-8'
76
-			);
77
-		} else {
78
-			$this->addHeader(
79
-				'Content-Type', 'application/xml; charset=utf-8'
80
-			);
81
-		}
82
-	}
73
+        if ($format === 'json') {
74
+            $this->addHeader(
75
+                'Content-Type', 'application/json; charset=utf-8'
76
+            );
77
+        } else {
78
+            $this->addHeader(
79
+                'Content-Type', 'application/xml; charset=utf-8'
80
+            );
81
+        }
82
+    }
83 83
 
84
-	/**
85
-	 * @param string[] $meta
86
-	 * @return string
87
-	 */
88
-	protected function renderResult($meta) {
89
-		// TODO rewrite functions
90
-		return \OC_API::renderResult($this->format, $meta, $this->data);
91
-	}
84
+    /**
85
+     * @param string[] $meta
86
+     * @return string
87
+     */
88
+    protected function renderResult($meta) {
89
+        // TODO rewrite functions
90
+        return \OC_API::renderResult($this->format, $meta, $this->data);
91
+    }
92 92
 
93
-	public function getOCSStatus() {
94
-		return parent::getStatus();
95
-	}
93
+    public function getOCSStatus() {
94
+        return parent::getStatus();
95
+    }
96 96
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/OCS/V2Response.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -27,50 +27,50 @@
 block discarded – undo
27 27
 
28 28
 class V2Response extends BaseResponse {
29 29
 
30
-	/**
31
-	 * The V2 endpoint just passes on status codes.
32
-	 * Of course we have to map the OCS specific codes to proper HTTP status codes
33
-	 *
34
-	 * @return int
35
-	 */
36
-	public function getStatus() {
30
+    /**
31
+     * The V2 endpoint just passes on status codes.
32
+     * Of course we have to map the OCS specific codes to proper HTTP status codes
33
+     *
34
+     * @return int
35
+     */
36
+    public function getStatus() {
37 37
 
38
-		$status  = parent::getStatus();
39
-		if ($status === API::RESPOND_UNAUTHORISED) {
40
-			return Http::STATUS_UNAUTHORIZED;
41
-		} else if ($status === API::RESPOND_NOT_FOUND) {
42
-			return Http::STATUS_NOT_FOUND;
43
-		} else if ($status === API::RESPOND_SERVER_ERROR || $status === API::RESPOND_UNKNOWN_ERROR) {
44
-			return Http::STATUS_INTERNAL_SERVER_ERROR;
45
-		} else if ($status < 200 || $status > 600) {
46
-			return Http::STATUS_BAD_REQUEST;
47
-		}
38
+        $status  = parent::getStatus();
39
+        if ($status === API::RESPOND_UNAUTHORISED) {
40
+            return Http::STATUS_UNAUTHORIZED;
41
+        } else if ($status === API::RESPOND_NOT_FOUND) {
42
+            return Http::STATUS_NOT_FOUND;
43
+        } else if ($status === API::RESPOND_SERVER_ERROR || $status === API::RESPOND_UNKNOWN_ERROR) {
44
+            return Http::STATUS_INTERNAL_SERVER_ERROR;
45
+        } else if ($status < 200 || $status > 600) {
46
+            return Http::STATUS_BAD_REQUEST;
47
+        }
48 48
 
49
-		return $status;
50
-	}
49
+        return $status;
50
+    }
51 51
 
52
-	/**
53
-	 * Construct the meta part of the response
54
-	 * And then late the base class render
55
-	 *
56
-	 * @return string
57
-	 */
58
-	public function render() {
59
-		$status = parent::getStatus();
52
+    /**
53
+     * Construct the meta part of the response
54
+     * And then late the base class render
55
+     *
56
+     * @return string
57
+     */
58
+    public function render() {
59
+        $status = parent::getStatus();
60 60
 
61
-		$meta = [
62
-			'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure',
63
-			'statuscode' => $this->getOCSStatus(),
64
-			'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage,
65
-		];
61
+        $meta = [
62
+            'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure',
63
+            'statuscode' => $this->getOCSStatus(),
64
+            'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage,
65
+        ];
66 66
 
67
-		if ($this->itemsCount !== null) {
68
-			$meta['totalitems'] = $this->itemsCount;
69
-		}
70
-		if ($this->itemsPerPage !== null) {
71
-			$meta['itemsperpage'] = $this->itemsPerPage;
72
-		}
67
+        if ($this->itemsCount !== null) {
68
+            $meta['totalitems'] = $this->itemsCount;
69
+        }
70
+        if ($this->itemsPerPage !== null) {
71
+            $meta['itemsperpage'] = $this->itemsPerPage;
72
+        }
73 73
 
74
-		return $this->renderResult($meta);
75
-	}
74
+        return $this->renderResult($meta);
75
+    }
76 76
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/OCS/V1Response.php 1 patch
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -27,52 +27,52 @@
 block discarded – undo
27 27
 
28 28
 class V1Response extends BaseResponse {
29 29
 
30
-	/**
31
-	 * The V1 endpoint has very limited http status codes basically everything
32
-	 * is status 200 except 401
33
-	 *
34
-	 * @return int
35
-	 */
36
-	public function getStatus() {
37
-		$status  = parent::getStatus();
38
-		if ($status === Http::STATUS_FORBIDDEN || $status === API::RESPOND_UNAUTHORISED) {
39
-			return Http::STATUS_UNAUTHORIZED;
40
-		}
30
+    /**
31
+     * The V1 endpoint has very limited http status codes basically everything
32
+     * is status 200 except 401
33
+     *
34
+     * @return int
35
+     */
36
+    public function getStatus() {
37
+        $status  = parent::getStatus();
38
+        if ($status === Http::STATUS_FORBIDDEN || $status === API::RESPOND_UNAUTHORISED) {
39
+            return Http::STATUS_UNAUTHORIZED;
40
+        }
41 41
 
42
-		return Http::STATUS_OK;
43
-	}
42
+        return Http::STATUS_OK;
43
+    }
44 44
 
45
-	/**
46
-	 * In v1 all OK is 100
47
-	 *
48
-	 * @return int
49
-	 */
50
-	public function getOCSStatus() {
51
-		$status = parent::getOCSStatus();
45
+    /**
46
+     * In v1 all OK is 100
47
+     *
48
+     * @return int
49
+     */
50
+    public function getOCSStatus() {
51
+        $status = parent::getOCSStatus();
52 52
 
53
-		if ($status === Http::STATUS_OK) {
54
-			return 100;
55
-		}
53
+        if ($status === Http::STATUS_OK) {
54
+            return 100;
55
+        }
56 56
 
57
-		return $status;
58
-	}
57
+        return $status;
58
+    }
59 59
 
60
-	/**
61
-	 * Construct the meta part of the response
62
-	 * And then late the base class render
63
-	 *
64
-	 * @return string
65
-	 */
66
-	public function render() {
67
-		$meta = [
68
-			'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure',
69
-			'statuscode' => $this->getOCSStatus(),
70
-			'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage,
71
-		];
60
+    /**
61
+     * Construct the meta part of the response
62
+     * And then late the base class render
63
+     *
64
+     * @return string
65
+     */
66
+    public function render() {
67
+        $meta = [
68
+            'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure',
69
+            'statuscode' => $this->getOCSStatus(),
70
+            'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage,
71
+        ];
72 72
 
73
-		$meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : '';
74
-		$meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: '';
73
+        $meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : '';
74
+        $meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: '';
75 75
 
76
-		return $this->renderResult($meta);
77
-	}
76
+        return $this->renderResult($meta);
77
+    }
78 78
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/Routing/RouteConfig.php 1 patch
Indentation   +236 added lines, -236 removed lines patch added patch discarded remove patch
@@ -36,242 +36,242 @@
 block discarded – undo
36 36
  * @package OC\AppFramework\routing
37 37
  */
38 38
 class RouteConfig {
39
-	/** @var DIContainer */
40
-	private $container;
41
-
42
-	/** @var IRouter */
43
-	private $router;
44
-
45
-	/** @var array */
46
-	private $routes;
47
-
48
-	/** @var string */
49
-	private $appName;
50
-
51
-	/** @var string[] */
52
-	private $controllerNameCache = [];
53
-
54
-	/**
55
-	 * @param \OC\AppFramework\DependencyInjection\DIContainer $container
56
-	 * @param \OCP\Route\IRouter $router
57
-	 * @param array $routes
58
-	 * @internal param $appName
59
-	 */
60
-	public function __construct(DIContainer $container, IRouter $router, $routes) {
61
-		$this->routes = $routes;
62
-		$this->container = $container;
63
-		$this->router = $router;
64
-		$this->appName = $container['AppName'];
65
-	}
66
-
67
-	/**
68
-	 * The routes and resource will be registered to the \OCP\Route\IRouter
69
-	 */
70
-	public function register() {
71
-
72
-		// parse simple
73
-		$this->processSimpleRoutes($this->routes);
74
-
75
-		// parse resources
76
-		$this->processResources($this->routes);
77
-
78
-		/*
39
+    /** @var DIContainer */
40
+    private $container;
41
+
42
+    /** @var IRouter */
43
+    private $router;
44
+
45
+    /** @var array */
46
+    private $routes;
47
+
48
+    /** @var string */
49
+    private $appName;
50
+
51
+    /** @var string[] */
52
+    private $controllerNameCache = [];
53
+
54
+    /**
55
+     * @param \OC\AppFramework\DependencyInjection\DIContainer $container
56
+     * @param \OCP\Route\IRouter $router
57
+     * @param array $routes
58
+     * @internal param $appName
59
+     */
60
+    public function __construct(DIContainer $container, IRouter $router, $routes) {
61
+        $this->routes = $routes;
62
+        $this->container = $container;
63
+        $this->router = $router;
64
+        $this->appName = $container['AppName'];
65
+    }
66
+
67
+    /**
68
+     * The routes and resource will be registered to the \OCP\Route\IRouter
69
+     */
70
+    public function register() {
71
+
72
+        // parse simple
73
+        $this->processSimpleRoutes($this->routes);
74
+
75
+        // parse resources
76
+        $this->processResources($this->routes);
77
+
78
+        /*
79 79
 		 * OCS routes go into a different collection
80 80
 		 */
81
-		$oldCollection = $this->router->getCurrentCollection();
82
-		$this->router->useCollection($oldCollection.'.ocs');
83
-
84
-		// parse ocs simple routes
85
-		$this->processOCS($this->routes);
86
-
87
-		$this->router->useCollection($oldCollection);
88
-	}
89
-
90
-	private function processOCS(array $routes) {
91
-		$ocsRoutes = isset($routes['ocs']) ? $routes['ocs'] : [];
92
-		foreach ($ocsRoutes as $ocsRoute) {
93
-			$name = $ocsRoute['name'];
94
-			$postfix = '';
95
-
96
-			if (isset($ocsRoute['postfix'])) {
97
-				$postfix = $ocsRoute['postfix'];
98
-			}
99
-
100
-			if (isset($ocsRoute['root'])) {
101
-				$root = $ocsRoute['root'];
102
-			} else {
103
-				$root = '/apps/'.$this->appName;
104
-			}
105
-
106
-			$url = $root . $ocsRoute['url'];
107
-			$verb = isset($ocsRoute['verb']) ? strtoupper($ocsRoute['verb']) : 'GET';
108
-
109
-			$split = explode('#', $name, 2);
110
-			if (count($split) != 2) {
111
-				throw new \UnexpectedValueException('Invalid route name');
112
-			}
113
-			$controller = $split[0];
114
-			$action = $split[1];
115
-
116
-			$controllerName = $this->buildControllerName($controller);
117
-			$actionName = $this->buildActionName($action);
118
-
119
-			// register the route
120
-			$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
121
-
122
-			$router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action . $postfix, $url)
123
-				->method($verb)
124
-				->action($handler);
125
-
126
-			// optionally register requirements for route. This is used to
127
-			// tell the route parser how url parameters should be matched
128
-			if(array_key_exists('requirements', $ocsRoute)) {
129
-				$router->requirements($ocsRoute['requirements']);
130
-			}
131
-
132
-			// optionally register defaults for route. This is used to
133
-			// tell the route parser how url parameters should be default valued
134
-			if(array_key_exists('defaults', $ocsRoute)) {
135
-				$router->defaults($ocsRoute['defaults']);
136
-			}
137
-		}
138
-	}
139
-
140
-	/**
141
-	 * Creates one route base on the give configuration
142
-	 * @param array $routes
143
-	 * @throws \UnexpectedValueException
144
-	 */
145
-	private function processSimpleRoutes($routes)
146
-	{
147
-		$simpleRoutes = isset($routes['routes']) ? $routes['routes'] : array();
148
-		foreach ($simpleRoutes as $simpleRoute) {
149
-			$name = $simpleRoute['name'];
150
-			$postfix = '';
151
-
152
-			if (isset($simpleRoute['postfix'])) {
153
-				$postfix = $simpleRoute['postfix'];
154
-			}
155
-
156
-			$url = $simpleRoute['url'];
157
-			$verb = isset($simpleRoute['verb']) ? strtoupper($simpleRoute['verb']) : 'GET';
158
-
159
-			$split = explode('#', $name, 2);
160
-			if (count($split) != 2) {
161
-				throw new \UnexpectedValueException('Invalid route name');
162
-			}
163
-			$controller = $split[0];
164
-			$action = $split[1];
165
-
166
-			$controllerName = $this->buildControllerName($controller);
167
-			$actionName = $this->buildActionName($action);
168
-
169
-			// register the route
170
-			$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
171
-			$router = $this->router->create($this->appName.'.'.$controller.'.'.$action . $postfix, $url)
172
-							->method($verb)
173
-							->action($handler);
174
-
175
-			// optionally register requirements for route. This is used to
176
-			// tell the route parser how url parameters should be matched
177
-			if(array_key_exists('requirements', $simpleRoute)) {
178
-				$router->requirements($simpleRoute['requirements']);
179
-			}
180
-
181
-			// optionally register defaults for route. This is used to
182
-			// tell the route parser how url parameters should be default valued
183
-			if(array_key_exists('defaults', $simpleRoute)) {
184
-				$router->defaults($simpleRoute['defaults']);
185
-			}
186
-		}
187
-	}
188
-
189
-	/**
190
-	 * For a given name and url restful routes are created:
191
-	 *  - index
192
-	 *  - show
193
-	 *  - new
194
-	 *  - create
195
-	 *  - update
196
-	 *  - destroy
197
-	 *
198
-	 * @param array $routes
199
-	 */
200
-	private function processResources($routes)
201
-	{
202
-		// declaration of all restful actions
203
-		$actions = array(
204
-			array('name' => 'index', 'verb' => 'GET', 'on-collection' => true),
205
-			array('name' => 'show', 'verb' => 'GET'),
206
-			array('name' => 'create', 'verb' => 'POST', 'on-collection' => true),
207
-			array('name' => 'update', 'verb' => 'PUT'),
208
-			array('name' => 'destroy', 'verb' => 'DELETE'),
209
-		);
210
-
211
-		$resources = isset($routes['resources']) ? $routes['resources'] : array();
212
-		foreach ($resources as $resource => $config) {
213
-
214
-			// the url parameter used as id to the resource
215
-			foreach($actions as $action) {
216
-				$url = $config['url'];
217
-				$method = $action['name'];
218
-				$verb = isset($action['verb']) ? strtoupper($action['verb']) : 'GET';
219
-				$collectionAction = isset($action['on-collection']) ? $action['on-collection'] : false;
220
-				if (!$collectionAction) {
221
-					$url = $url . '/{id}';
222
-				}
223
-				if (isset($action['url-postfix'])) {
224
-					$url = $url . '/' . $action['url-postfix'];
225
-				}
226
-
227
-				$controller = $resource;
228
-
229
-				$controllerName = $this->buildControllerName($controller);
230
-				$actionName = $this->buildActionName($method);
231
-
232
-				$routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method);
233
-
234
-				$this->router->create($routeName, $url)->method($verb)->action(
235
-					new RouteActionHandler($this->container, $controllerName, $actionName)
236
-				);
237
-			}
238
-		}
239
-	}
240
-
241
-	/**
242
-	 * Based on a given route name the controller name is generated
243
-	 * @param string $controller
244
-	 * @return string
245
-	 */
246
-	private function buildControllerName($controller)
247
-	{
248
-		if (!isset($this->controllerNameCache[$controller])) {
249
-			$this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller';
250
-		}
251
-		return $this->controllerNameCache[$controller];
252
-	}
253
-
254
-	/**
255
-	 * Based on the action part of the route name the controller method name is generated
256
-	 * @param string $action
257
-	 * @return string
258
-	 */
259
-	private function buildActionName($action) {
260
-		return $this->underScoreToCamelCase($action);
261
-	}
262
-
263
-	/**
264
-	 * Underscored strings are converted to camel case strings
265
-	 * @param string $str
266
-	 * @return string
267
-	 */
268
-	private function underScoreToCamelCase($str) {
269
-		$pattern = "/_[a-z]?/";
270
-		return preg_replace_callback(
271
-			$pattern,
272
-			function ($matches) {
273
-				return strtoupper(ltrim($matches[0], "_"));
274
-			},
275
-			$str);
276
-	}
81
+        $oldCollection = $this->router->getCurrentCollection();
82
+        $this->router->useCollection($oldCollection.'.ocs');
83
+
84
+        // parse ocs simple routes
85
+        $this->processOCS($this->routes);
86
+
87
+        $this->router->useCollection($oldCollection);
88
+    }
89
+
90
+    private function processOCS(array $routes) {
91
+        $ocsRoutes = isset($routes['ocs']) ? $routes['ocs'] : [];
92
+        foreach ($ocsRoutes as $ocsRoute) {
93
+            $name = $ocsRoute['name'];
94
+            $postfix = '';
95
+
96
+            if (isset($ocsRoute['postfix'])) {
97
+                $postfix = $ocsRoute['postfix'];
98
+            }
99
+
100
+            if (isset($ocsRoute['root'])) {
101
+                $root = $ocsRoute['root'];
102
+            } else {
103
+                $root = '/apps/'.$this->appName;
104
+            }
105
+
106
+            $url = $root . $ocsRoute['url'];
107
+            $verb = isset($ocsRoute['verb']) ? strtoupper($ocsRoute['verb']) : 'GET';
108
+
109
+            $split = explode('#', $name, 2);
110
+            if (count($split) != 2) {
111
+                throw new \UnexpectedValueException('Invalid route name');
112
+            }
113
+            $controller = $split[0];
114
+            $action = $split[1];
115
+
116
+            $controllerName = $this->buildControllerName($controller);
117
+            $actionName = $this->buildActionName($action);
118
+
119
+            // register the route
120
+            $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
121
+
122
+            $router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action . $postfix, $url)
123
+                ->method($verb)
124
+                ->action($handler);
125
+
126
+            // optionally register requirements for route. This is used to
127
+            // tell the route parser how url parameters should be matched
128
+            if(array_key_exists('requirements', $ocsRoute)) {
129
+                $router->requirements($ocsRoute['requirements']);
130
+            }
131
+
132
+            // optionally register defaults for route. This is used to
133
+            // tell the route parser how url parameters should be default valued
134
+            if(array_key_exists('defaults', $ocsRoute)) {
135
+                $router->defaults($ocsRoute['defaults']);
136
+            }
137
+        }
138
+    }
139
+
140
+    /**
141
+     * Creates one route base on the give configuration
142
+     * @param array $routes
143
+     * @throws \UnexpectedValueException
144
+     */
145
+    private function processSimpleRoutes($routes)
146
+    {
147
+        $simpleRoutes = isset($routes['routes']) ? $routes['routes'] : array();
148
+        foreach ($simpleRoutes as $simpleRoute) {
149
+            $name = $simpleRoute['name'];
150
+            $postfix = '';
151
+
152
+            if (isset($simpleRoute['postfix'])) {
153
+                $postfix = $simpleRoute['postfix'];
154
+            }
155
+
156
+            $url = $simpleRoute['url'];
157
+            $verb = isset($simpleRoute['verb']) ? strtoupper($simpleRoute['verb']) : 'GET';
158
+
159
+            $split = explode('#', $name, 2);
160
+            if (count($split) != 2) {
161
+                throw new \UnexpectedValueException('Invalid route name');
162
+            }
163
+            $controller = $split[0];
164
+            $action = $split[1];
165
+
166
+            $controllerName = $this->buildControllerName($controller);
167
+            $actionName = $this->buildActionName($action);
168
+
169
+            // register the route
170
+            $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
171
+            $router = $this->router->create($this->appName.'.'.$controller.'.'.$action . $postfix, $url)
172
+                            ->method($verb)
173
+                            ->action($handler);
174
+
175
+            // optionally register requirements for route. This is used to
176
+            // tell the route parser how url parameters should be matched
177
+            if(array_key_exists('requirements', $simpleRoute)) {
178
+                $router->requirements($simpleRoute['requirements']);
179
+            }
180
+
181
+            // optionally register defaults for route. This is used to
182
+            // tell the route parser how url parameters should be default valued
183
+            if(array_key_exists('defaults', $simpleRoute)) {
184
+                $router->defaults($simpleRoute['defaults']);
185
+            }
186
+        }
187
+    }
188
+
189
+    /**
190
+     * For a given name and url restful routes are created:
191
+     *  - index
192
+     *  - show
193
+     *  - new
194
+     *  - create
195
+     *  - update
196
+     *  - destroy
197
+     *
198
+     * @param array $routes
199
+     */
200
+    private function processResources($routes)
201
+    {
202
+        // declaration of all restful actions
203
+        $actions = array(
204
+            array('name' => 'index', 'verb' => 'GET', 'on-collection' => true),
205
+            array('name' => 'show', 'verb' => 'GET'),
206
+            array('name' => 'create', 'verb' => 'POST', 'on-collection' => true),
207
+            array('name' => 'update', 'verb' => 'PUT'),
208
+            array('name' => 'destroy', 'verb' => 'DELETE'),
209
+        );
210
+
211
+        $resources = isset($routes['resources']) ? $routes['resources'] : array();
212
+        foreach ($resources as $resource => $config) {
213
+
214
+            // the url parameter used as id to the resource
215
+            foreach($actions as $action) {
216
+                $url = $config['url'];
217
+                $method = $action['name'];
218
+                $verb = isset($action['verb']) ? strtoupper($action['verb']) : 'GET';
219
+                $collectionAction = isset($action['on-collection']) ? $action['on-collection'] : false;
220
+                if (!$collectionAction) {
221
+                    $url = $url . '/{id}';
222
+                }
223
+                if (isset($action['url-postfix'])) {
224
+                    $url = $url . '/' . $action['url-postfix'];
225
+                }
226
+
227
+                $controller = $resource;
228
+
229
+                $controllerName = $this->buildControllerName($controller);
230
+                $actionName = $this->buildActionName($method);
231
+
232
+                $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method);
233
+
234
+                $this->router->create($routeName, $url)->method($verb)->action(
235
+                    new RouteActionHandler($this->container, $controllerName, $actionName)
236
+                );
237
+            }
238
+        }
239
+    }
240
+
241
+    /**
242
+     * Based on a given route name the controller name is generated
243
+     * @param string $controller
244
+     * @return string
245
+     */
246
+    private function buildControllerName($controller)
247
+    {
248
+        if (!isset($this->controllerNameCache[$controller])) {
249
+            $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller';
250
+        }
251
+        return $this->controllerNameCache[$controller];
252
+    }
253
+
254
+    /**
255
+     * Based on the action part of the route name the controller method name is generated
256
+     * @param string $action
257
+     * @return string
258
+     */
259
+    private function buildActionName($action) {
260
+        return $this->underScoreToCamelCase($action);
261
+    }
262
+
263
+    /**
264
+     * Underscored strings are converted to camel case strings
265
+     * @param string $str
266
+     * @return string
267
+     */
268
+    private function underScoreToCamelCase($str) {
269
+        $pattern = "/_[a-z]?/";
270
+        return preg_replace_callback(
271
+            $pattern,
272
+            function ($matches) {
273
+                return strtoupper(ltrim($matches[0], "_"));
274
+            },
275
+            $str);
276
+    }
277 277
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/Routing/RouteActionHandler.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -29,21 +29,21 @@
 block discarded – undo
29 29
 use \OC\AppFramework\DependencyInjection\DIContainer;
30 30
 
31 31
 class RouteActionHandler {
32
-	private $controllerName;
33
-	private $actionName;
34
-	private $container;
32
+    private $controllerName;
33
+    private $actionName;
34
+    private $container;
35 35
 
36
-	/**
37
-	 * @param string $controllerName
38
-	 * @param string $actionName
39
-	 */
40
-	public function __construct(DIContainer $container, $controllerName, $actionName) {
41
-		$this->controllerName = $controllerName;
42
-		$this->actionName = $actionName;
43
-		$this->container = $container;
44
-	}
36
+    /**
37
+     * @param string $controllerName
38
+     * @param string $actionName
39
+     */
40
+    public function __construct(DIContainer $container, $controllerName, $actionName) {
41
+        $this->controllerName = $controllerName;
42
+        $this->actionName = $actionName;
43
+        $this->container = $container;
44
+    }
45 45
 
46
-	public function __invoke($params) {
47
-		App::main($this->controllerName, $this->actionName, $this->container, $params);
48
-	}
46
+    public function __invoke($params) {
47
+        App::main($this->controllerName, $this->actionName, $this->container, $params);
48
+    }
49 49
 }
Please login to merge, or discard this patch.