1
0

RouteParser.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\AppFramework\Routing;
  24. use OC\Route\Route;
  25. use Symfony\Component\Routing\RouteCollection;
  26. class RouteParser {
  27. /** @var string[] */
  28. private $controllerNameCache = [];
  29. private const rootUrlApps = [
  30. 'cloud_federation_api',
  31. 'core',
  32. 'files_sharing',
  33. 'files',
  34. 'settings',
  35. 'spreed',
  36. ];
  37. public function parseDefaultRoutes(array $routes, string $appName): RouteCollection {
  38. $collection = $this->processIndexRoutes($routes, $appName);
  39. $collection->addCollection($this->processIndexResources($routes, $appName));
  40. return $collection;
  41. }
  42. public function parseOCSRoutes(array $routes, string $appName): RouteCollection {
  43. $collection = $this->processOCS($routes, $appName);
  44. $collection->addCollection($this->processOCSResources($routes, $appName));
  45. return $collection;
  46. }
  47. private function processOCS(array $routes, string $appName): RouteCollection {
  48. $collection = new RouteCollection();
  49. $ocsRoutes = $routes['ocs'] ?? [];
  50. foreach ($ocsRoutes as $ocsRoute) {
  51. $result = $this->processRoute($ocsRoute, $appName, 'ocs.');
  52. $collection->add($result[0], $result[1]);
  53. }
  54. return $collection;
  55. }
  56. /**
  57. * Creates one route base on the give configuration
  58. * @param array $routes
  59. * @throws \UnexpectedValueException
  60. */
  61. private function processIndexRoutes(array $routes, string $appName): RouteCollection {
  62. $collection = new RouteCollection();
  63. $simpleRoutes = $routes['routes'] ?? [];
  64. foreach ($simpleRoutes as $simpleRoute) {
  65. $result = $this->processRoute($simpleRoute, $appName);
  66. $collection->add($result[0], $result[1]);
  67. }
  68. return $collection;
  69. }
  70. private function processRoute(array $route, string $appName, string $routeNamePrefix = ''): array {
  71. $name = $route['name'];
  72. $postfix = $route['postfix'] ?? '';
  73. $root = $this->buildRootPrefix($route, $appName, $routeNamePrefix);
  74. $url = $root . '/' . ltrim($route['url'], '/');
  75. $verb = strtoupper($route['verb'] ?? 'GET');
  76. $split = explode('#', $name, 2);
  77. if (count($split) !== 2) {
  78. throw new \UnexpectedValueException('Invalid route name: use the format foo#bar to reference FooController::bar');
  79. }
  80. [$controller, $action] = $split;
  81. $controllerName = $this->buildControllerName($controller);
  82. $actionName = $this->buildActionName($action);
  83. $routeName = $routeNamePrefix . $appName . '.' . $controller . '.' . $action . $postfix;
  84. $routeObject = new Route($url);
  85. $routeObject->method($verb);
  86. // optionally register requirements for route. This is used to
  87. // tell the route parser how url parameters should be matched
  88. if (array_key_exists('requirements', $route)) {
  89. $routeObject->requirements($route['requirements']);
  90. }
  91. // optionally register defaults for route. This is used to
  92. // tell the route parser how url parameters should be default valued
  93. $defaults = [];
  94. if (array_key_exists('defaults', $route)) {
  95. $defaults = $route['defaults'];
  96. }
  97. $defaults['caller'] = [$appName, $controllerName, $actionName];
  98. $routeObject->defaults($defaults);
  99. return [$routeName, $routeObject];
  100. }
  101. /**
  102. * For a given name and url restful OCS routes are created:
  103. * - index
  104. * - show
  105. * - create
  106. * - update
  107. * - destroy
  108. *
  109. * @param array $routes
  110. */
  111. private function processOCSResources(array $routes, string $appName): RouteCollection {
  112. return $this->processResources($routes['ocs-resources'] ?? [], $appName, 'ocs.');
  113. }
  114. /**
  115. * For a given name and url restful routes are created:
  116. * - index
  117. * - show
  118. * - create
  119. * - update
  120. * - destroy
  121. *
  122. * @param array $routes
  123. */
  124. private function processIndexResources(array $routes, string $appName): RouteCollection {
  125. return $this->processResources($routes['resources'] ?? [], $appName);
  126. }
  127. /**
  128. * For a given name and url restful routes are created:
  129. * - index
  130. * - show
  131. * - create
  132. * - update
  133. * - destroy
  134. *
  135. * @param array $resources
  136. * @param string $routeNamePrefix
  137. */
  138. private function processResources(array $resources, string $appName, string $routeNamePrefix = ''): RouteCollection {
  139. // declaration of all restful actions
  140. $actions = [
  141. ['name' => 'index', 'verb' => 'GET', 'on-collection' => true],
  142. ['name' => 'show', 'verb' => 'GET'],
  143. ['name' => 'create', 'verb' => 'POST', 'on-collection' => true],
  144. ['name' => 'update', 'verb' => 'PUT'],
  145. ['name' => 'destroy', 'verb' => 'DELETE'],
  146. ];
  147. $collection = new RouteCollection();
  148. foreach ($resources as $resource => $config) {
  149. $root = $this->buildRootPrefix($config, $appName, $routeNamePrefix);
  150. // the url parameter used as id to the resource
  151. foreach ($actions as $action) {
  152. $url = $root . '/' . ltrim($config['url'], '/');
  153. $method = $action['name'];
  154. $verb = strtoupper($action['verb'] ?? 'GET');
  155. $collectionAction = $action['on-collection'] ?? false;
  156. if (!$collectionAction) {
  157. $url .= '/{id}';
  158. }
  159. $controller = $resource;
  160. $controllerName = $this->buildControllerName($controller);
  161. $actionName = $this->buildActionName($method);
  162. $routeName = $routeNamePrefix . $appName . '.' . strtolower($resource) . '.' . $method;
  163. $route = new Route($url);
  164. $route->method($verb);
  165. $route->defaults(['caller' => [$appName, $controllerName, $actionName]]);
  166. $collection->add($routeName, $route);
  167. }
  168. }
  169. return $collection;
  170. }
  171. private function buildRootPrefix(array $route, string $appName, string $routeNamePrefix): string {
  172. $defaultRoot = $appName === 'core' ? '' : '/apps/' . $appName;
  173. $root = $route['root'] ?? $defaultRoot;
  174. if ($routeNamePrefix !== '') {
  175. // In OCS all apps are whitelisted
  176. return $root;
  177. }
  178. if (!\in_array($appName, self::rootUrlApps, true)) {
  179. // Only allow root URLS for some apps
  180. return $defaultRoot;
  181. }
  182. return $root;
  183. }
  184. /**
  185. * Based on a given route name the controller name is generated
  186. * @param string $controller
  187. * @return string
  188. */
  189. private function buildControllerName(string $controller): string {
  190. if (!isset($this->controllerNameCache[$controller])) {
  191. $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller';
  192. }
  193. return $this->controllerNameCache[$controller];
  194. }
  195. /**
  196. * Based on the action part of the route name the controller method name is generated
  197. * @param string $action
  198. * @return string
  199. */
  200. private function buildActionName(string $action): string {
  201. return $this->underScoreToCamelCase($action);
  202. }
  203. /**
  204. * Underscored strings are converted to camel case strings
  205. * @param string $str
  206. * @return string
  207. */
  208. private function underScoreToCamelCase(string $str): string {
  209. $pattern = '/_[a-z]?/';
  210. return preg_replace_callback(
  211. $pattern,
  212. function ($matches) {
  213. return strtoupper(ltrim($matches[0], '_'));
  214. },
  215. $str);
  216. }
  217. }