RouteConfig.php 9.2 KB

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