You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RouteConfig.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Patrick Paysant <ppaysant@linagora.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\AppFramework\Routing;
  30. use OC\AppFramework\DependencyInjection\DIContainer;
  31. use OCP\AppFramework\App;
  32. use OCP\Route\IRouter;
  33. /**
  34. * Class RouteConfig
  35. * @package OC\AppFramework\routing
  36. */
  37. class RouteConfig {
  38. /** @var DIContainer */
  39. private $container;
  40. /** @var IRouter */
  41. private $router;
  42. /** @var array */
  43. private $routes;
  44. /** @var string */
  45. private $appName;
  46. /** @var string[] */
  47. private $controllerNameCache = [];
  48. /**
  49. * @param \OC\AppFramework\DependencyInjection\DIContainer $container
  50. * @param \OCP\Route\IRouter $router
  51. * @param array $routes
  52. * @internal param $appName
  53. */
  54. public function __construct(DIContainer $container, IRouter $router, $routes) {
  55. $this->routes = $routes;
  56. $this->container = $container;
  57. $this->router = $router;
  58. $this->appName = $container['AppName'];
  59. }
  60. /**
  61. * The routes and resource will be registered to the \OCP\Route\IRouter
  62. */
  63. public function register() {
  64. // parse simple
  65. $this->processSimpleRoutes($this->routes);
  66. // parse resources
  67. $this->processResources($this->routes);
  68. /*
  69. * OCS routes go into a different collection
  70. */
  71. $oldCollection = $this->router->getCurrentCollection();
  72. $this->router->useCollection($oldCollection . '.ocs');
  73. // parse ocs simple routes
  74. $this->processOCS($this->routes);
  75. // parse ocs simple routes
  76. $this->processOCSResources($this->routes);
  77. $this->router->useCollection($oldCollection);
  78. }
  79. private function processOCS(array $routes): void {
  80. $ocsRoutes = $routes['ocs'] ?? [];
  81. foreach ($ocsRoutes as $ocsRoute) {
  82. $name = $ocsRoute['name'];
  83. $postfix = $ocsRoute['postfix'] ?? '';
  84. $root = $ocsRoute['root'] ?? '/apps/' . $this->appName;
  85. $url = $root . $ocsRoute['url'];
  86. $verb = strtoupper($ocsRoute['verb'] ?? 'GET');
  87. $split = explode('#', $name, 2);
  88. if (count($split) !== 2) {
  89. throw new \UnexpectedValueException('Invalid route name');
  90. }
  91. list($controller, $action) = $split;
  92. $controllerName = $this->buildControllerName($controller);
  93. $actionName = $this->buildActionName($action);
  94. $routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix;
  95. // register the route
  96. $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
  97. $router = $this->router->create($routeName, $url)
  98. ->method($verb)
  99. ->action($handler);
  100. // optionally register requirements for route. This is used to
  101. // tell the route parser how url parameters should be matched
  102. if(array_key_exists('requirements', $ocsRoute)) {
  103. $router->requirements($ocsRoute['requirements']);
  104. }
  105. // optionally register defaults for route. This is used to
  106. // tell the route parser how url parameters should be default valued
  107. if(array_key_exists('defaults', $ocsRoute)) {
  108. $router->defaults($ocsRoute['defaults']);
  109. }
  110. }
  111. }
  112. /**
  113. * Creates one route base on the give configuration
  114. * @param array $routes
  115. * @throws \UnexpectedValueException
  116. */
  117. private function processSimpleRoutes(array $routes): void {
  118. $simpleRoutes = $routes['routes'] ?? [];
  119. foreach ($simpleRoutes as $simpleRoute) {
  120. $name = $simpleRoute['name'];
  121. $postfix = $simpleRoute['postfix'] ?? '';
  122. $url = $simpleRoute['url'];
  123. $verb = strtoupper($simpleRoute['verb'] ?? 'GET');
  124. $split = explode('#', $name, 2);
  125. if (count($split) !== 2) {
  126. throw new \UnexpectedValueException('Invalid route name');
  127. }
  128. list($controller, $action) = $split;
  129. $controllerName = $this->buildControllerName($controller);
  130. $actionName = $this->buildActionName($action);
  131. $appName = $simpleRoute['app'] ?? $this->appName;
  132. if (isset($simpleRoute['app'])) {
  133. // Legacy routes that need to be globally available while they are handled by an app
  134. // E.g. '/f/{id}', '/s/{token}', '/call/{token}', …
  135. $controllerName = str_replace('controllerController', 'Controller', $controllerName);
  136. if ($controllerName === 'PublicpreviewController') {
  137. $controllerName = 'PublicPreviewController';
  138. } else if ($controllerName === 'RequesthandlerController') {
  139. $controllerName = 'RequestHandlerController';
  140. }
  141. $controllerName = App::buildAppNamespace($appName) . '\\Controller\\' . $controllerName;
  142. }
  143. $routeName = $appName . '.' . $controller . '.' . $action . $postfix;
  144. // register the route
  145. $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
  146. $router = $this->router->create($routeName, $url)
  147. ->method($verb)
  148. ->action($handler);
  149. // optionally register requirements for route. This is used to
  150. // tell the route parser how url parameters should be matched
  151. if(array_key_exists('requirements', $simpleRoute)) {
  152. $router->requirements($simpleRoute['requirements']);
  153. }
  154. // optionally register defaults for route. This is used to
  155. // tell the route parser how url parameters should be default valued
  156. if(array_key_exists('defaults', $simpleRoute)) {
  157. $router->defaults($simpleRoute['defaults']);
  158. }
  159. }
  160. }
  161. /**
  162. * For a given name and url restful OCS routes are created:
  163. * - index
  164. * - show
  165. * - create
  166. * - update
  167. * - destroy
  168. *
  169. * @param array $routes
  170. */
  171. private function processOCSResources(array $routes): void {
  172. // declaration of all restful actions
  173. $actions = [
  174. ['name' => 'index', 'verb' => 'GET', 'on-collection' => true],
  175. ['name' => 'show', 'verb' => 'GET'],
  176. ['name' => 'create', 'verb' => 'POST', 'on-collection' => true],
  177. ['name' => 'update', 'verb' => 'PUT'],
  178. ['name' => 'destroy', 'verb' => 'DELETE'],
  179. ];
  180. $resources = $routes['ocs-resources'] ?? [];
  181. foreach ($resources as $resource => $config) {
  182. $root = $config['root'] ?? '/apps/' . $this->appName;
  183. // the url parameter used as id to the resource
  184. foreach($actions as $action) {
  185. $url = $root . $config['url'];
  186. $method = $action['name'];
  187. $verb = strtoupper($action['verb'] ?? 'GET');
  188. $collectionAction = $action['on-collection'] ?? false;
  189. if (!$collectionAction) {
  190. $url .= '/{id}';
  191. }
  192. if (isset($action['url-postfix'])) {
  193. $url .= '/' . $action['url-postfix'];
  194. }
  195. $controller = $resource;
  196. $controllerName = $this->buildControllerName($controller);
  197. $actionName = $this->buildActionName($method);
  198. $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method);
  199. $this->router->create($routeName, $url)->method($verb)->action(
  200. new RouteActionHandler($this->container, $controllerName, $actionName)
  201. );
  202. }
  203. }
  204. }
  205. /**
  206. * For a given name and url restful routes are created:
  207. * - index
  208. * - show
  209. * - create
  210. * - update
  211. * - destroy
  212. *
  213. * @param array $routes
  214. */
  215. private function processResources(array $routes): void {
  216. // declaration of all restful actions
  217. $actions = [
  218. ['name' => 'index', 'verb' => 'GET', 'on-collection' => true],
  219. ['name' => 'show', 'verb' => 'GET'],
  220. ['name' => 'create', 'verb' => 'POST', 'on-collection' => true],
  221. ['name' => 'update', 'verb' => 'PUT'],
  222. ['name' => 'destroy', 'verb' => 'DELETE'],
  223. ];
  224. $resources = $routes['resources'] ?? [];
  225. foreach ($resources as $resource => $config) {
  226. // the url parameter used as id to the resource
  227. foreach($actions as $action) {
  228. $url = $config['url'];
  229. $method = $action['name'];
  230. $verb = strtoupper($action['verb'] ?? 'GET');
  231. $collectionAction = $action['on-collection'] ?? false;
  232. if (!$collectionAction) {
  233. $url .= '/{id}';
  234. }
  235. if (isset($action['url-postfix'])) {
  236. $url .= '/' . $action['url-postfix'];
  237. }
  238. $controller = $resource;
  239. $controllerName = $this->buildControllerName($controller);
  240. $actionName = $this->buildActionName($method);
  241. $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method);
  242. $this->router->create($routeName, $url)->method($verb)->action(
  243. new RouteActionHandler($this->container, $controllerName, $actionName)
  244. );
  245. }
  246. }
  247. }
  248. /**
  249. * Based on a given route name the controller name is generated
  250. * @param string $controller
  251. * @return string
  252. */
  253. private function buildControllerName(string $controller): string {
  254. if (!isset($this->controllerNameCache[$controller])) {
  255. $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller';
  256. }
  257. return $this->controllerNameCache[$controller];
  258. }
  259. /**
  260. * Based on the action part of the route name the controller method name is generated
  261. * @param string $action
  262. * @return string
  263. */
  264. private function buildActionName(string $action): string {
  265. return $this->underScoreToCamelCase($action);
  266. }
  267. /**
  268. * Underscored strings are converted to camel case strings
  269. * @param string $str
  270. * @return string
  271. */
  272. private function underScoreToCamelCase(string $str): string {
  273. $pattern = '/_[a-z]?/';
  274. return preg_replace_callback(
  275. $pattern,
  276. function ($matches) {
  277. return strtoupper(ltrim($matches[0], '_'));
  278. },
  279. $str);
  280. }
  281. }