@@ -45,376 +45,376 @@ |
||
| 45 | 45 | use Symfony\Component\Routing\RouteCollection; |
| 46 | 46 | |
| 47 | 47 | class Router implements IRouter { |
| 48 | - /** @var RouteCollection[] */ |
|
| 49 | - protected $collections = []; |
|
| 50 | - /** @var null|RouteCollection */ |
|
| 51 | - protected $collection = null; |
|
| 52 | - /** @var null|string */ |
|
| 53 | - protected $collectionName = null; |
|
| 54 | - /** @var null|RouteCollection */ |
|
| 55 | - protected $root = null; |
|
| 56 | - /** @var null|UrlGenerator */ |
|
| 57 | - protected $generator = null; |
|
| 58 | - /** @var string[] */ |
|
| 59 | - protected $routingFiles; |
|
| 60 | - /** @var bool */ |
|
| 61 | - protected $loaded = false; |
|
| 62 | - /** @var array */ |
|
| 63 | - protected $loadedApps = []; |
|
| 64 | - /** @var ILogger */ |
|
| 65 | - protected $logger; |
|
| 66 | - /** @var RequestContext */ |
|
| 67 | - protected $context; |
|
| 48 | + /** @var RouteCollection[] */ |
|
| 49 | + protected $collections = []; |
|
| 50 | + /** @var null|RouteCollection */ |
|
| 51 | + protected $collection = null; |
|
| 52 | + /** @var null|string */ |
|
| 53 | + protected $collectionName = null; |
|
| 54 | + /** @var null|RouteCollection */ |
|
| 55 | + protected $root = null; |
|
| 56 | + /** @var null|UrlGenerator */ |
|
| 57 | + protected $generator = null; |
|
| 58 | + /** @var string[] */ |
|
| 59 | + protected $routingFiles; |
|
| 60 | + /** @var bool */ |
|
| 61 | + protected $loaded = false; |
|
| 62 | + /** @var array */ |
|
| 63 | + protected $loadedApps = []; |
|
| 64 | + /** @var ILogger */ |
|
| 65 | + protected $logger; |
|
| 66 | + /** @var RequestContext */ |
|
| 67 | + protected $context; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @param ILogger $logger |
|
| 71 | - */ |
|
| 72 | - public function __construct(ILogger $logger) { |
|
| 73 | - $this->logger = $logger; |
|
| 74 | - $baseUrl = \OC::$WEBROOT; |
|
| 75 | - if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
| 76 | - $baseUrl = \OC::$server->getURLGenerator()->linkTo('', 'index.php'); |
|
| 77 | - } |
|
| 78 | - if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) { |
|
| 79 | - $method = $_SERVER['REQUEST_METHOD']; |
|
| 80 | - } else { |
|
| 81 | - $method = 'GET'; |
|
| 82 | - } |
|
| 83 | - $request = \OC::$server->getRequest(); |
|
| 84 | - $host = $request->getServerHost(); |
|
| 85 | - $schema = $request->getServerProtocol(); |
|
| 86 | - $this->context = new RequestContext($baseUrl, $method, $host, $schema); |
|
| 87 | - // TODO cache |
|
| 88 | - $this->root = $this->getCollection('root'); |
|
| 89 | - } |
|
| 69 | + /** |
|
| 70 | + * @param ILogger $logger |
|
| 71 | + */ |
|
| 72 | + public function __construct(ILogger $logger) { |
|
| 73 | + $this->logger = $logger; |
|
| 74 | + $baseUrl = \OC::$WEBROOT; |
|
| 75 | + if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
| 76 | + $baseUrl = \OC::$server->getURLGenerator()->linkTo('', 'index.php'); |
|
| 77 | + } |
|
| 78 | + if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) { |
|
| 79 | + $method = $_SERVER['REQUEST_METHOD']; |
|
| 80 | + } else { |
|
| 81 | + $method = 'GET'; |
|
| 82 | + } |
|
| 83 | + $request = \OC::$server->getRequest(); |
|
| 84 | + $host = $request->getServerHost(); |
|
| 85 | + $schema = $request->getServerProtocol(); |
|
| 86 | + $this->context = new RequestContext($baseUrl, $method, $host, $schema); |
|
| 87 | + // TODO cache |
|
| 88 | + $this->root = $this->getCollection('root'); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Get the files to load the routes from |
|
| 93 | - * |
|
| 94 | - * @return string[] |
|
| 95 | - */ |
|
| 96 | - public function getRoutingFiles() { |
|
| 97 | - if (!isset($this->routingFiles)) { |
|
| 98 | - $this->routingFiles = []; |
|
| 99 | - foreach (\OC_APP::getEnabledApps() as $app) { |
|
| 100 | - $appPath = \OC_App::getAppPath($app); |
|
| 101 | - if ($appPath !== false) { |
|
| 102 | - $file = $appPath . '/appinfo/routes.php'; |
|
| 103 | - if (file_exists($file)) { |
|
| 104 | - $this->routingFiles[$app] = $file; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - return $this->routingFiles; |
|
| 110 | - } |
|
| 91 | + /** |
|
| 92 | + * Get the files to load the routes from |
|
| 93 | + * |
|
| 94 | + * @return string[] |
|
| 95 | + */ |
|
| 96 | + public function getRoutingFiles() { |
|
| 97 | + if (!isset($this->routingFiles)) { |
|
| 98 | + $this->routingFiles = []; |
|
| 99 | + foreach (\OC_APP::getEnabledApps() as $app) { |
|
| 100 | + $appPath = \OC_App::getAppPath($app); |
|
| 101 | + if ($appPath !== false) { |
|
| 102 | + $file = $appPath . '/appinfo/routes.php'; |
|
| 103 | + if (file_exists($file)) { |
|
| 104 | + $this->routingFiles[$app] = $file; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + return $this->routingFiles; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Loads the routes |
|
| 114 | - * |
|
| 115 | - * @param null|string $app |
|
| 116 | - */ |
|
| 117 | - public function loadRoutes($app = null) { |
|
| 118 | - if (is_string($app)) { |
|
| 119 | - $app = \OC_App::cleanAppId($app); |
|
| 120 | - } |
|
| 112 | + /** |
|
| 113 | + * Loads the routes |
|
| 114 | + * |
|
| 115 | + * @param null|string $app |
|
| 116 | + */ |
|
| 117 | + public function loadRoutes($app = null) { |
|
| 118 | + if (is_string($app)) { |
|
| 119 | + $app = \OC_App::cleanAppId($app); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - $requestedApp = $app; |
|
| 123 | - if ($this->loaded) { |
|
| 124 | - return; |
|
| 125 | - } |
|
| 126 | - if (is_null($app)) { |
|
| 127 | - $this->loaded = true; |
|
| 128 | - $routingFiles = $this->getRoutingFiles(); |
|
| 129 | - } else { |
|
| 130 | - if (isset($this->loadedApps[$app])) { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - $file = \OC_App::getAppPath($app) . '/appinfo/routes.php'; |
|
| 134 | - if ($file !== false && file_exists($file)) { |
|
| 135 | - $routingFiles = [$app => $file]; |
|
| 136 | - } else { |
|
| 137 | - $routingFiles = []; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - \OC::$server->getEventLogger()->start('loadroutes' . $requestedApp, 'Loading Routes'); |
|
| 141 | - foreach ($routingFiles as $app => $file) { |
|
| 142 | - if (!isset($this->loadedApps[$app])) { |
|
| 143 | - if (!\OC_App::isAppLoaded($app)) { |
|
| 144 | - // app MUST be loaded before app routes |
|
| 145 | - // try again next time loadRoutes() is called |
|
| 146 | - $this->loaded = false; |
|
| 147 | - continue; |
|
| 148 | - } |
|
| 149 | - $this->loadedApps[$app] = true; |
|
| 150 | - $this->useCollection($app); |
|
| 151 | - $this->requireRouteFile($file, $app); |
|
| 152 | - $collection = $this->getCollection($app); |
|
| 153 | - $this->root->addCollection($collection); |
|
| 122 | + $requestedApp = $app; |
|
| 123 | + if ($this->loaded) { |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | + if (is_null($app)) { |
|
| 127 | + $this->loaded = true; |
|
| 128 | + $routingFiles = $this->getRoutingFiles(); |
|
| 129 | + } else { |
|
| 130 | + if (isset($this->loadedApps[$app])) { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + $file = \OC_App::getAppPath($app) . '/appinfo/routes.php'; |
|
| 134 | + if ($file !== false && file_exists($file)) { |
|
| 135 | + $routingFiles = [$app => $file]; |
|
| 136 | + } else { |
|
| 137 | + $routingFiles = []; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + \OC::$server->getEventLogger()->start('loadroutes' . $requestedApp, 'Loading Routes'); |
|
| 141 | + foreach ($routingFiles as $app => $file) { |
|
| 142 | + if (!isset($this->loadedApps[$app])) { |
|
| 143 | + if (!\OC_App::isAppLoaded($app)) { |
|
| 144 | + // app MUST be loaded before app routes |
|
| 145 | + // try again next time loadRoutes() is called |
|
| 146 | + $this->loaded = false; |
|
| 147 | + continue; |
|
| 148 | + } |
|
| 149 | + $this->loadedApps[$app] = true; |
|
| 150 | + $this->useCollection($app); |
|
| 151 | + $this->requireRouteFile($file, $app); |
|
| 152 | + $collection = $this->getCollection($app); |
|
| 153 | + $this->root->addCollection($collection); |
|
| 154 | 154 | |
| 155 | - // Also add the OCS collection |
|
| 156 | - $collection = $this->getCollection($app.'.ocs'); |
|
| 157 | - $collection->addPrefix('/ocsapp'); |
|
| 158 | - $this->root->addCollection($collection); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - if (!isset($this->loadedApps['core'])) { |
|
| 162 | - $this->loadedApps['core'] = true; |
|
| 163 | - $this->useCollection('root'); |
|
| 164 | - require_once __DIR__ . '/../../../core/routes.php'; |
|
| 155 | + // Also add the OCS collection |
|
| 156 | + $collection = $this->getCollection($app.'.ocs'); |
|
| 157 | + $collection->addPrefix('/ocsapp'); |
|
| 158 | + $this->root->addCollection($collection); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + if (!isset($this->loadedApps['core'])) { |
|
| 162 | + $this->loadedApps['core'] = true; |
|
| 163 | + $this->useCollection('root'); |
|
| 164 | + require_once __DIR__ . '/../../../core/routes.php'; |
|
| 165 | 165 | |
| 166 | - // Also add the OCS collection |
|
| 167 | - $collection = $this->getCollection('root.ocs'); |
|
| 168 | - $collection->addPrefix('/ocsapp'); |
|
| 169 | - $this->root->addCollection($collection); |
|
| 170 | - } |
|
| 171 | - if ($this->loaded) { |
|
| 172 | - $collection = $this->getCollection('ocs'); |
|
| 173 | - $collection->addPrefix('/ocs'); |
|
| 174 | - $this->root->addCollection($collection); |
|
| 175 | - } |
|
| 176 | - \OC::$server->getEventLogger()->end('loadroutes' . $requestedApp); |
|
| 177 | - } |
|
| 166 | + // Also add the OCS collection |
|
| 167 | + $collection = $this->getCollection('root.ocs'); |
|
| 168 | + $collection->addPrefix('/ocsapp'); |
|
| 169 | + $this->root->addCollection($collection); |
|
| 170 | + } |
|
| 171 | + if ($this->loaded) { |
|
| 172 | + $collection = $this->getCollection('ocs'); |
|
| 173 | + $collection->addPrefix('/ocs'); |
|
| 174 | + $this->root->addCollection($collection); |
|
| 175 | + } |
|
| 176 | + \OC::$server->getEventLogger()->end('loadroutes' . $requestedApp); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * @return string |
|
| 181 | - * @deprecated |
|
| 182 | - */ |
|
| 183 | - public function getCacheKey() { |
|
| 184 | - return ''; |
|
| 185 | - } |
|
| 179 | + /** |
|
| 180 | + * @return string |
|
| 181 | + * @deprecated |
|
| 182 | + */ |
|
| 183 | + public function getCacheKey() { |
|
| 184 | + return ''; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * @param string $name |
|
| 189 | - * @return \Symfony\Component\Routing\RouteCollection |
|
| 190 | - */ |
|
| 191 | - protected function getCollection($name) { |
|
| 192 | - if (!isset($this->collections[$name])) { |
|
| 193 | - $this->collections[$name] = new RouteCollection(); |
|
| 194 | - } |
|
| 195 | - return $this->collections[$name]; |
|
| 196 | - } |
|
| 187 | + /** |
|
| 188 | + * @param string $name |
|
| 189 | + * @return \Symfony\Component\Routing\RouteCollection |
|
| 190 | + */ |
|
| 191 | + protected function getCollection($name) { |
|
| 192 | + if (!isset($this->collections[$name])) { |
|
| 193 | + $this->collections[$name] = new RouteCollection(); |
|
| 194 | + } |
|
| 195 | + return $this->collections[$name]; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Sets the collection to use for adding routes |
|
| 200 | - * |
|
| 201 | - * @param string $name Name of the collection to use. |
|
| 202 | - * @return void |
|
| 203 | - */ |
|
| 204 | - public function useCollection($name) { |
|
| 205 | - $this->collection = $this->getCollection($name); |
|
| 206 | - $this->collectionName = $name; |
|
| 207 | - } |
|
| 198 | + /** |
|
| 199 | + * Sets the collection to use for adding routes |
|
| 200 | + * |
|
| 201 | + * @param string $name Name of the collection to use. |
|
| 202 | + * @return void |
|
| 203 | + */ |
|
| 204 | + public function useCollection($name) { |
|
| 205 | + $this->collection = $this->getCollection($name); |
|
| 206 | + $this->collectionName = $name; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * returns the current collection name in use for adding routes |
|
| 211 | - * |
|
| 212 | - * @return string the collection name |
|
| 213 | - */ |
|
| 214 | - public function getCurrentCollection() { |
|
| 215 | - return $this->collectionName; |
|
| 216 | - } |
|
| 209 | + /** |
|
| 210 | + * returns the current collection name in use for adding routes |
|
| 211 | + * |
|
| 212 | + * @return string the collection name |
|
| 213 | + */ |
|
| 214 | + public function getCurrentCollection() { |
|
| 215 | + return $this->collectionName; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * Create a \OC\Route\Route. |
|
| 221 | - * |
|
| 222 | - * @param string $name Name of the route to create. |
|
| 223 | - * @param string $pattern The pattern to match |
|
| 224 | - * @param array $defaults An array of default parameter values |
|
| 225 | - * @param array $requirements An array of requirements for parameters (regexes) |
|
| 226 | - * @return \OC\Route\Route |
|
| 227 | - */ |
|
| 228 | - public function create($name, |
|
| 229 | - $pattern, |
|
| 230 | - array $defaults = [], |
|
| 231 | - array $requirements = []) { |
|
| 232 | - $route = new Route($pattern, $defaults, $requirements); |
|
| 233 | - $this->collection->add($name, $route); |
|
| 234 | - return $route; |
|
| 235 | - } |
|
| 219 | + /** |
|
| 220 | + * Create a \OC\Route\Route. |
|
| 221 | + * |
|
| 222 | + * @param string $name Name of the route to create. |
|
| 223 | + * @param string $pattern The pattern to match |
|
| 224 | + * @param array $defaults An array of default parameter values |
|
| 225 | + * @param array $requirements An array of requirements for parameters (regexes) |
|
| 226 | + * @return \OC\Route\Route |
|
| 227 | + */ |
|
| 228 | + public function create($name, |
|
| 229 | + $pattern, |
|
| 230 | + array $defaults = [], |
|
| 231 | + array $requirements = []) { |
|
| 232 | + $route = new Route($pattern, $defaults, $requirements); |
|
| 233 | + $this->collection->add($name, $route); |
|
| 234 | + return $route; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Find the route matching $url |
|
| 239 | - * |
|
| 240 | - * @param string $url The url to find |
|
| 241 | - * @throws \Exception |
|
| 242 | - * @return void |
|
| 243 | - */ |
|
| 244 | - public function match($url) { |
|
| 245 | - if (substr($url, 0, 6) === '/apps/') { |
|
| 246 | - // empty string / 'apps' / $app / rest of the route |
|
| 247 | - list(, , $app,) = explode('/', $url, 4); |
|
| 237 | + /** |
|
| 238 | + * Find the route matching $url |
|
| 239 | + * |
|
| 240 | + * @param string $url The url to find |
|
| 241 | + * @throws \Exception |
|
| 242 | + * @return void |
|
| 243 | + */ |
|
| 244 | + public function match($url) { |
|
| 245 | + if (substr($url, 0, 6) === '/apps/') { |
|
| 246 | + // empty string / 'apps' / $app / rest of the route |
|
| 247 | + list(, , $app,) = explode('/', $url, 4); |
|
| 248 | 248 | |
| 249 | - $app = \OC_App::cleanAppId($app); |
|
| 250 | - \OC::$REQUESTEDAPP = $app; |
|
| 251 | - $this->loadRoutes($app); |
|
| 252 | - } elseif (substr($url, 0, 13) === '/ocsapp/apps/') { |
|
| 253 | - // empty string / 'ocsapp' / 'apps' / $app / rest of the route |
|
| 254 | - list(, , , $app,) = explode('/', $url, 5); |
|
| 249 | + $app = \OC_App::cleanAppId($app); |
|
| 250 | + \OC::$REQUESTEDAPP = $app; |
|
| 251 | + $this->loadRoutes($app); |
|
| 252 | + } elseif (substr($url, 0, 13) === '/ocsapp/apps/') { |
|
| 253 | + // empty string / 'ocsapp' / 'apps' / $app / rest of the route |
|
| 254 | + list(, , , $app,) = explode('/', $url, 5); |
|
| 255 | 255 | |
| 256 | - $app = \OC_App::cleanAppId($app); |
|
| 257 | - \OC::$REQUESTEDAPP = $app; |
|
| 258 | - $this->loadRoutes($app); |
|
| 259 | - } elseif (substr($url, 0, 10) === '/settings/') { |
|
| 260 | - $this->loadRoutes('settings'); |
|
| 261 | - } elseif (substr($url, 0, 6) === '/core/') { |
|
| 262 | - \OC::$REQUESTEDAPP = $url; |
|
| 263 | - if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) { |
|
| 264 | - \OC_App::loadApps(); |
|
| 265 | - } |
|
| 266 | - $this->loadRoutes('core'); |
|
| 267 | - } else { |
|
| 268 | - $this->loadRoutes(); |
|
| 269 | - } |
|
| 256 | + $app = \OC_App::cleanAppId($app); |
|
| 257 | + \OC::$REQUESTEDAPP = $app; |
|
| 258 | + $this->loadRoutes($app); |
|
| 259 | + } elseif (substr($url, 0, 10) === '/settings/') { |
|
| 260 | + $this->loadRoutes('settings'); |
|
| 261 | + } elseif (substr($url, 0, 6) === '/core/') { |
|
| 262 | + \OC::$REQUESTEDAPP = $url; |
|
| 263 | + if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) { |
|
| 264 | + \OC_App::loadApps(); |
|
| 265 | + } |
|
| 266 | + $this->loadRoutes('core'); |
|
| 267 | + } else { |
|
| 268 | + $this->loadRoutes(); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - $matcher = new UrlMatcher($this->root, $this->context); |
|
| 272 | - try { |
|
| 273 | - $parameters = $matcher->match($url); |
|
| 274 | - } catch (ResourceNotFoundException $e) { |
|
| 275 | - if (substr($url, -1) !== '/') { |
|
| 276 | - // We allow links to apps/files? for backwards compatibility reasons |
|
| 277 | - // However, since Symfony does not allow empty route names, the route |
|
| 278 | - // we need to match is '/', so we need to append the '/' here. |
|
| 279 | - try { |
|
| 280 | - $parameters = $matcher->match($url . '/'); |
|
| 281 | - } catch (ResourceNotFoundException $newException) { |
|
| 282 | - // If we still didn't match a route, we throw the original exception |
|
| 283 | - throw $e; |
|
| 284 | - } |
|
| 285 | - } else { |
|
| 286 | - throw $e; |
|
| 287 | - } |
|
| 288 | - } |
|
| 271 | + $matcher = new UrlMatcher($this->root, $this->context); |
|
| 272 | + try { |
|
| 273 | + $parameters = $matcher->match($url); |
|
| 274 | + } catch (ResourceNotFoundException $e) { |
|
| 275 | + if (substr($url, -1) !== '/') { |
|
| 276 | + // We allow links to apps/files? for backwards compatibility reasons |
|
| 277 | + // However, since Symfony does not allow empty route names, the route |
|
| 278 | + // we need to match is '/', so we need to append the '/' here. |
|
| 279 | + try { |
|
| 280 | + $parameters = $matcher->match($url . '/'); |
|
| 281 | + } catch (ResourceNotFoundException $newException) { |
|
| 282 | + // If we still didn't match a route, we throw the original exception |
|
| 283 | + throw $e; |
|
| 284 | + } |
|
| 285 | + } else { |
|
| 286 | + throw $e; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - \OC::$server->getEventLogger()->start('run_route', 'Run route'); |
|
| 291 | - if (isset($parameters['caller'])) { |
|
| 292 | - $caller = $parameters['caller']; |
|
| 293 | - unset($parameters['caller']); |
|
| 294 | - $application = $this->getApplicationClass($caller[0]); |
|
| 295 | - \OC\AppFramework\App::main($caller[1], $caller[2], $application->getContainer(), $parameters); |
|
| 296 | - } elseif (isset($parameters['action'])) { |
|
| 297 | - $action = $parameters['action']; |
|
| 298 | - if (!is_callable($action)) { |
|
| 299 | - throw new \Exception('not a callable action'); |
|
| 300 | - } |
|
| 301 | - unset($parameters['action']); |
|
| 302 | - call_user_func($action, $parameters); |
|
| 303 | - } elseif (isset($parameters['file'])) { |
|
| 304 | - include $parameters['file']; |
|
| 305 | - } else { |
|
| 306 | - throw new \Exception('no action available'); |
|
| 307 | - } |
|
| 308 | - \OC::$server->getEventLogger()->end('run_route'); |
|
| 309 | - } |
|
| 290 | + \OC::$server->getEventLogger()->start('run_route', 'Run route'); |
|
| 291 | + if (isset($parameters['caller'])) { |
|
| 292 | + $caller = $parameters['caller']; |
|
| 293 | + unset($parameters['caller']); |
|
| 294 | + $application = $this->getApplicationClass($caller[0]); |
|
| 295 | + \OC\AppFramework\App::main($caller[1], $caller[2], $application->getContainer(), $parameters); |
|
| 296 | + } elseif (isset($parameters['action'])) { |
|
| 297 | + $action = $parameters['action']; |
|
| 298 | + if (!is_callable($action)) { |
|
| 299 | + throw new \Exception('not a callable action'); |
|
| 300 | + } |
|
| 301 | + unset($parameters['action']); |
|
| 302 | + call_user_func($action, $parameters); |
|
| 303 | + } elseif (isset($parameters['file'])) { |
|
| 304 | + include $parameters['file']; |
|
| 305 | + } else { |
|
| 306 | + throw new \Exception('no action available'); |
|
| 307 | + } |
|
| 308 | + \OC::$server->getEventLogger()->end('run_route'); |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * Get the url generator |
|
| 313 | - * |
|
| 314 | - * @return \Symfony\Component\Routing\Generator\UrlGenerator |
|
| 315 | - * |
|
| 316 | - */ |
|
| 317 | - public function getGenerator() { |
|
| 318 | - if (null !== $this->generator) { |
|
| 319 | - return $this->generator; |
|
| 320 | - } |
|
| 311 | + /** |
|
| 312 | + * Get the url generator |
|
| 313 | + * |
|
| 314 | + * @return \Symfony\Component\Routing\Generator\UrlGenerator |
|
| 315 | + * |
|
| 316 | + */ |
|
| 317 | + public function getGenerator() { |
|
| 318 | + if (null !== $this->generator) { |
|
| 319 | + return $this->generator; |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - return $this->generator = new UrlGenerator($this->root, $this->context); |
|
| 323 | - } |
|
| 322 | + return $this->generator = new UrlGenerator($this->root, $this->context); |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** |
|
| 326 | - * Generate url based on $name and $parameters |
|
| 327 | - * |
|
| 328 | - * @param string $name Name of the route to use. |
|
| 329 | - * @param array $parameters Parameters for the route |
|
| 330 | - * @param bool $absolute |
|
| 331 | - * @return string |
|
| 332 | - */ |
|
| 333 | - public function generate($name, |
|
| 334 | - $parameters = [], |
|
| 335 | - $absolute = false) { |
|
| 336 | - $this->loadRoutes(); |
|
| 337 | - try { |
|
| 338 | - $referenceType = UrlGenerator::ABSOLUTE_URL; |
|
| 339 | - if ($absolute === false) { |
|
| 340 | - $referenceType = UrlGenerator::ABSOLUTE_PATH; |
|
| 341 | - } |
|
| 342 | - $name = $this->fixLegacyRootName($name); |
|
| 343 | - return $this->getGenerator()->generate($name, $parameters, $referenceType); |
|
| 344 | - } catch (RouteNotFoundException $e) { |
|
| 345 | - $this->logger->logException($e); |
|
| 346 | - return ''; |
|
| 347 | - } |
|
| 348 | - } |
|
| 325 | + /** |
|
| 326 | + * Generate url based on $name and $parameters |
|
| 327 | + * |
|
| 328 | + * @param string $name Name of the route to use. |
|
| 329 | + * @param array $parameters Parameters for the route |
|
| 330 | + * @param bool $absolute |
|
| 331 | + * @return string |
|
| 332 | + */ |
|
| 333 | + public function generate($name, |
|
| 334 | + $parameters = [], |
|
| 335 | + $absolute = false) { |
|
| 336 | + $this->loadRoutes(); |
|
| 337 | + try { |
|
| 338 | + $referenceType = UrlGenerator::ABSOLUTE_URL; |
|
| 339 | + if ($absolute === false) { |
|
| 340 | + $referenceType = UrlGenerator::ABSOLUTE_PATH; |
|
| 341 | + } |
|
| 342 | + $name = $this->fixLegacyRootName($name); |
|
| 343 | + return $this->getGenerator()->generate($name, $parameters, $referenceType); |
|
| 344 | + } catch (RouteNotFoundException $e) { |
|
| 345 | + $this->logger->logException($e); |
|
| 346 | + return ''; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | 349 | |
| 350 | - protected function fixLegacyRootName(string $routeName): string { |
|
| 351 | - if ($routeName === 'files.viewcontroller.showFile') { |
|
| 352 | - return 'files.View.showFile'; |
|
| 353 | - } |
|
| 354 | - if ($routeName === 'files_sharing.sharecontroller.showShare') { |
|
| 355 | - return 'files_sharing.Share.showShare'; |
|
| 356 | - } |
|
| 357 | - if ($routeName === 'files_sharing.sharecontroller.showAuthenticate') { |
|
| 358 | - return 'files_sharing.Share.showAuthenticate'; |
|
| 359 | - } |
|
| 360 | - if ($routeName === 'files_sharing.sharecontroller.authenticate') { |
|
| 361 | - return 'files_sharing.Share.authenticate'; |
|
| 362 | - } |
|
| 363 | - if ($routeName === 'files_sharing.sharecontroller.downloadShare') { |
|
| 364 | - return 'files_sharing.Share.downloadShare'; |
|
| 365 | - } |
|
| 366 | - if ($routeName === 'files_sharing.publicpreview.directLink') { |
|
| 367 | - return 'files_sharing.PublicPreview.directLink'; |
|
| 368 | - } |
|
| 369 | - if ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') { |
|
| 370 | - return 'cloud_federation_api.RequestHandler.addShare'; |
|
| 371 | - } |
|
| 372 | - if ($routeName === 'cloud_federation_api.requesthandlercontroller.receiveNotification') { |
|
| 373 | - return 'cloud_federation_api.RequestHandler.receiveNotification'; |
|
| 374 | - } |
|
| 375 | - return $routeName; |
|
| 376 | - } |
|
| 350 | + protected function fixLegacyRootName(string $routeName): string { |
|
| 351 | + if ($routeName === 'files.viewcontroller.showFile') { |
|
| 352 | + return 'files.View.showFile'; |
|
| 353 | + } |
|
| 354 | + if ($routeName === 'files_sharing.sharecontroller.showShare') { |
|
| 355 | + return 'files_sharing.Share.showShare'; |
|
| 356 | + } |
|
| 357 | + if ($routeName === 'files_sharing.sharecontroller.showAuthenticate') { |
|
| 358 | + return 'files_sharing.Share.showAuthenticate'; |
|
| 359 | + } |
|
| 360 | + if ($routeName === 'files_sharing.sharecontroller.authenticate') { |
|
| 361 | + return 'files_sharing.Share.authenticate'; |
|
| 362 | + } |
|
| 363 | + if ($routeName === 'files_sharing.sharecontroller.downloadShare') { |
|
| 364 | + return 'files_sharing.Share.downloadShare'; |
|
| 365 | + } |
|
| 366 | + if ($routeName === 'files_sharing.publicpreview.directLink') { |
|
| 367 | + return 'files_sharing.PublicPreview.directLink'; |
|
| 368 | + } |
|
| 369 | + if ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') { |
|
| 370 | + return 'cloud_federation_api.RequestHandler.addShare'; |
|
| 371 | + } |
|
| 372 | + if ($routeName === 'cloud_federation_api.requesthandlercontroller.receiveNotification') { |
|
| 373 | + return 'cloud_federation_api.RequestHandler.receiveNotification'; |
|
| 374 | + } |
|
| 375 | + return $routeName; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * To isolate the variable scope used inside the $file it is required in it's own method |
|
| 380 | - * |
|
| 381 | - * @param string $file the route file location to include |
|
| 382 | - * @param string $appName |
|
| 383 | - */ |
|
| 384 | - private function requireRouteFile($file, $appName) { |
|
| 385 | - $this->setupRoutes(include_once $file, $appName); |
|
| 386 | - } |
|
| 378 | + /** |
|
| 379 | + * To isolate the variable scope used inside the $file it is required in it's own method |
|
| 380 | + * |
|
| 381 | + * @param string $file the route file location to include |
|
| 382 | + * @param string $appName |
|
| 383 | + */ |
|
| 384 | + private function requireRouteFile($file, $appName) { |
|
| 385 | + $this->setupRoutes(include_once $file, $appName); |
|
| 386 | + } |
|
| 387 | 387 | |
| 388 | 388 | |
| 389 | - /** |
|
| 390 | - * If a routes.php file returns an array, try to set up the application and |
|
| 391 | - * register the routes for the app. The application class will be chosen by |
|
| 392 | - * camelcasing the appname, e.g.: my_app will be turned into |
|
| 393 | - * \OCA\MyApp\AppInfo\Application. If that class does not exist, a default |
|
| 394 | - * App will be intialized. This makes it optional to ship an |
|
| 395 | - * appinfo/application.php by using the built in query resolver |
|
| 396 | - * |
|
| 397 | - * @param array $routes the application routes |
|
| 398 | - * @param string $appName the name of the app. |
|
| 399 | - */ |
|
| 400 | - private function setupRoutes($routes, $appName) { |
|
| 401 | - if (is_array($routes)) { |
|
| 402 | - $application = $this->getApplicationClass($appName); |
|
| 403 | - $application->registerRoutes($this, $routes); |
|
| 404 | - } |
|
| 405 | - } |
|
| 389 | + /** |
|
| 390 | + * If a routes.php file returns an array, try to set up the application and |
|
| 391 | + * register the routes for the app. The application class will be chosen by |
|
| 392 | + * camelcasing the appname, e.g.: my_app will be turned into |
|
| 393 | + * \OCA\MyApp\AppInfo\Application. If that class does not exist, a default |
|
| 394 | + * App will be intialized. This makes it optional to ship an |
|
| 395 | + * appinfo/application.php by using the built in query resolver |
|
| 396 | + * |
|
| 397 | + * @param array $routes the application routes |
|
| 398 | + * @param string $appName the name of the app. |
|
| 399 | + */ |
|
| 400 | + private function setupRoutes($routes, $appName) { |
|
| 401 | + if (is_array($routes)) { |
|
| 402 | + $application = $this->getApplicationClass($appName); |
|
| 403 | + $application->registerRoutes($this, $routes); |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | - private function getApplicationClass(string $appName) { |
|
| 408 | - $appNameSpace = App::buildAppNamespace($appName); |
|
| 407 | + private function getApplicationClass(string $appName) { |
|
| 408 | + $appNameSpace = App::buildAppNamespace($appName); |
|
| 409 | 409 | |
| 410 | - $applicationClassName = $appNameSpace . '\\AppInfo\\Application'; |
|
| 410 | + $applicationClassName = $appNameSpace . '\\AppInfo\\Application'; |
|
| 411 | 411 | |
| 412 | - if (class_exists($applicationClassName)) { |
|
| 413 | - $application = \OC::$server->query($applicationClassName); |
|
| 414 | - } else { |
|
| 415 | - $application = new App($appName); |
|
| 416 | - } |
|
| 412 | + if (class_exists($applicationClassName)) { |
|
| 413 | + $application = \OC::$server->query($applicationClassName); |
|
| 414 | + } else { |
|
| 415 | + $application = new App($appName); |
|
| 416 | + } |
|
| 417 | 417 | |
| 418 | - return $application; |
|
| 419 | - } |
|
| 418 | + return $application; |
|
| 419 | + } |
|
| 420 | 420 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | foreach (\OC_APP::getEnabledApps() as $app) { |
| 100 | 100 | $appPath = \OC_App::getAppPath($app); |
| 101 | 101 | if ($appPath !== false) { |
| 102 | - $file = $appPath . '/appinfo/routes.php'; |
|
| 102 | + $file = $appPath.'/appinfo/routes.php'; |
|
| 103 | 103 | if (file_exists($file)) { |
| 104 | 104 | $this->routingFiles[$app] = $file; |
| 105 | 105 | } |
@@ -130,14 +130,14 @@ discard block |
||
| 130 | 130 | if (isset($this->loadedApps[$app])) { |
| 131 | 131 | return; |
| 132 | 132 | } |
| 133 | - $file = \OC_App::getAppPath($app) . '/appinfo/routes.php'; |
|
| 133 | + $file = \OC_App::getAppPath($app).'/appinfo/routes.php'; |
|
| 134 | 134 | if ($file !== false && file_exists($file)) { |
| 135 | 135 | $routingFiles = [$app => $file]; |
| 136 | 136 | } else { |
| 137 | 137 | $routingFiles = []; |
| 138 | 138 | } |
| 139 | 139 | } |
| 140 | - \OC::$server->getEventLogger()->start('loadroutes' . $requestedApp, 'Loading Routes'); |
|
| 140 | + \OC::$server->getEventLogger()->start('loadroutes'.$requestedApp, 'Loading Routes'); |
|
| 141 | 141 | foreach ($routingFiles as $app => $file) { |
| 142 | 142 | if (!isset($this->loadedApps[$app])) { |
| 143 | 143 | if (!\OC_App::isAppLoaded($app)) { |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | if (!isset($this->loadedApps['core'])) { |
| 162 | 162 | $this->loadedApps['core'] = true; |
| 163 | 163 | $this->useCollection('root'); |
| 164 | - require_once __DIR__ . '/../../../core/routes.php'; |
|
| 164 | + require_once __DIR__.'/../../../core/routes.php'; |
|
| 165 | 165 | |
| 166 | 166 | // Also add the OCS collection |
| 167 | 167 | $collection = $this->getCollection('root.ocs'); |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | $collection->addPrefix('/ocs'); |
| 174 | 174 | $this->root->addCollection($collection); |
| 175 | 175 | } |
| 176 | - \OC::$server->getEventLogger()->end('loadroutes' . $requestedApp); |
|
| 176 | + \OC::$server->getEventLogger()->end('loadroutes'.$requestedApp); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -244,14 +244,14 @@ discard block |
||
| 244 | 244 | public function match($url) { |
| 245 | 245 | if (substr($url, 0, 6) === '/apps/') { |
| 246 | 246 | // empty string / 'apps' / $app / rest of the route |
| 247 | - list(, , $app,) = explode('/', $url, 4); |
|
| 247 | + list(,, $app,) = explode('/', $url, 4); |
|
| 248 | 248 | |
| 249 | 249 | $app = \OC_App::cleanAppId($app); |
| 250 | 250 | \OC::$REQUESTEDAPP = $app; |
| 251 | 251 | $this->loadRoutes($app); |
| 252 | 252 | } elseif (substr($url, 0, 13) === '/ocsapp/apps/') { |
| 253 | 253 | // empty string / 'ocsapp' / 'apps' / $app / rest of the route |
| 254 | - list(, , , $app,) = explode('/', $url, 5); |
|
| 254 | + list(,,, $app,) = explode('/', $url, 5); |
|
| 255 | 255 | |
| 256 | 256 | $app = \OC_App::cleanAppId($app); |
| 257 | 257 | \OC::$REQUESTEDAPP = $app; |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | // However, since Symfony does not allow empty route names, the route |
| 278 | 278 | // we need to match is '/', so we need to append the '/' here. |
| 279 | 279 | try { |
| 280 | - $parameters = $matcher->match($url . '/'); |
|
| 280 | + $parameters = $matcher->match($url.'/'); |
|
| 281 | 281 | } catch (ResourceNotFoundException $newException) { |
| 282 | 282 | // If we still didn't match a route, we throw the original exception |
| 283 | 283 | throw $e; |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | private function getApplicationClass(string $appName) { |
| 408 | 408 | $appNameSpace = App::buildAppNamespace($appName); |
| 409 | 409 | |
| 410 | - $applicationClassName = $appNameSpace . '\\AppInfo\\Application'; |
|
| 410 | + $applicationClassName = $appNameSpace.'\\AppInfo\\Application'; |
|
| 411 | 411 | |
| 412 | 412 | if (class_exists($applicationClassName)) { |
| 413 | 413 | $application = \OC::$server->query($applicationClassName); |
@@ -39,254 +39,254 @@ |
||
| 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 | - ->setDefault('caller', [$this->appName, $controllerName, $actionName]); |
|
| 146 | - |
|
| 147 | - // optionally register requirements for route. This is used to |
|
| 148 | - // tell the route parser how url parameters should be matched |
|
| 149 | - if (array_key_exists('requirements', $route)) { |
|
| 150 | - $router->requirements($route['requirements']); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // optionally register defaults for route. This is used to |
|
| 154 | - // tell the route parser how url parameters should be default valued |
|
| 155 | - if (array_key_exists('defaults', $route)) { |
|
| 156 | - $router->defaults($route['defaults']); |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * For a given name and url restful OCS routes are created: |
|
| 162 | - * - index |
|
| 163 | - * - show |
|
| 164 | - * - create |
|
| 165 | - * - update |
|
| 166 | - * - destroy |
|
| 167 | - * |
|
| 168 | - * @param array $routes |
|
| 169 | - */ |
|
| 170 | - private function processOCSResources(array $routes): void { |
|
| 171 | - $this->processResources($routes['ocs-resources'] ?? [], 'ocs.'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * For a given name and url restful routes are created: |
|
| 176 | - * - index |
|
| 177 | - * - show |
|
| 178 | - * - create |
|
| 179 | - * - update |
|
| 180 | - * - destroy |
|
| 181 | - * |
|
| 182 | - * @param array $routes |
|
| 183 | - */ |
|
| 184 | - private function processIndexResources(array $routes): void { |
|
| 185 | - $this->processResources($routes['resources'] ?? []); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * For a given name and url restful routes are created: |
|
| 190 | - * - index |
|
| 191 | - * - show |
|
| 192 | - * - create |
|
| 193 | - * - update |
|
| 194 | - * - destroy |
|
| 195 | - * |
|
| 196 | - * @param array $resources |
|
| 197 | - * @param string $routeNamePrefix |
|
| 198 | - */ |
|
| 199 | - protected function processResources(array $resources, string $routeNamePrefix = ''): void { |
|
| 200 | - // declaration of all restful actions |
|
| 201 | - $actions = [ |
|
| 202 | - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 203 | - ['name' => 'show', 'verb' => 'GET'], |
|
| 204 | - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 205 | - ['name' => 'update', 'verb' => 'PUT'], |
|
| 206 | - ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 207 | - ]; |
|
| 208 | - |
|
| 209 | - foreach ($resources as $resource => $config) { |
|
| 210 | - $root = $this->buildRootPrefix($config, $routeNamePrefix); |
|
| 211 | - |
|
| 212 | - // the url parameter used as id to the resource |
|
| 213 | - foreach ($actions as $action) { |
|
| 214 | - $url = $root . '/' . ltrim($config['url'], '/'); |
|
| 215 | - $method = $action['name']; |
|
| 216 | - |
|
| 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 | - |
|
| 226 | - $controller = $resource; |
|
| 227 | - |
|
| 228 | - $controllerName = $this->buildControllerName($controller); |
|
| 229 | - $actionName = $this->buildActionName($method); |
|
| 230 | - |
|
| 231 | - $routeName = $routeNamePrefix . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 232 | - |
|
| 233 | - $this->router->create($routeName, $url) |
|
| 234 | - ->method($verb) |
|
| 235 | - ->setDefault('caller', [$this->appName, $controllerName, $actionName]); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - private function buildRootPrefix(array $route, string $routeNamePrefix): string { |
|
| 241 | - $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; |
|
| 242 | - $root = $route['root'] ?? $defaultRoot; |
|
| 243 | - |
|
| 244 | - if ($routeNamePrefix !== '') { |
|
| 245 | - // In OCS all apps are whitelisted |
|
| 246 | - return $root; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if (!\in_array($this->appName, $this->rootUrlApps, true)) { |
|
| 250 | - // Only allow root URLS for some apps |
|
| 251 | - return $defaultRoot; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $root; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Based on a given route name the controller name is generated |
|
| 259 | - * @param string $controller |
|
| 260 | - * @return string |
|
| 261 | - */ |
|
| 262 | - private function buildControllerName(string $controller): string { |
|
| 263 | - if (!isset($this->controllerNameCache[$controller])) { |
|
| 264 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 265 | - } |
|
| 266 | - return $this->controllerNameCache[$controller]; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Based on the action part of the route name the controller method name is generated |
|
| 271 | - * @param string $action |
|
| 272 | - * @return string |
|
| 273 | - */ |
|
| 274 | - private function buildActionName(string $action): string { |
|
| 275 | - return $this->underScoreToCamelCase($action); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Underscored strings are converted to camel case strings |
|
| 280 | - * @param string $str |
|
| 281 | - * @return string |
|
| 282 | - */ |
|
| 283 | - private function underScoreToCamelCase(string $str): string { |
|
| 284 | - $pattern = '/_[a-z]?/'; |
|
| 285 | - return preg_replace_callback( |
|
| 286 | - $pattern, |
|
| 287 | - function ($matches) { |
|
| 288 | - return strtoupper(ltrim($matches[0], '_')); |
|
| 289 | - }, |
|
| 290 | - $str); |
|
| 291 | - } |
|
| 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 | + ->setDefault('caller', [$this->appName, $controllerName, $actionName]); |
|
| 146 | + |
|
| 147 | + // optionally register requirements for route. This is used to |
|
| 148 | + // tell the route parser how url parameters should be matched |
|
| 149 | + if (array_key_exists('requirements', $route)) { |
|
| 150 | + $router->requirements($route['requirements']); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // optionally register defaults for route. This is used to |
|
| 154 | + // tell the route parser how url parameters should be default valued |
|
| 155 | + if (array_key_exists('defaults', $route)) { |
|
| 156 | + $router->defaults($route['defaults']); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * For a given name and url restful OCS routes are created: |
|
| 162 | + * - index |
|
| 163 | + * - show |
|
| 164 | + * - create |
|
| 165 | + * - update |
|
| 166 | + * - destroy |
|
| 167 | + * |
|
| 168 | + * @param array $routes |
|
| 169 | + */ |
|
| 170 | + private function processOCSResources(array $routes): void { |
|
| 171 | + $this->processResources($routes['ocs-resources'] ?? [], 'ocs.'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * For a given name and url restful routes are created: |
|
| 176 | + * - index |
|
| 177 | + * - show |
|
| 178 | + * - create |
|
| 179 | + * - update |
|
| 180 | + * - destroy |
|
| 181 | + * |
|
| 182 | + * @param array $routes |
|
| 183 | + */ |
|
| 184 | + private function processIndexResources(array $routes): void { |
|
| 185 | + $this->processResources($routes['resources'] ?? []); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * For a given name and url restful routes are created: |
|
| 190 | + * - index |
|
| 191 | + * - show |
|
| 192 | + * - create |
|
| 193 | + * - update |
|
| 194 | + * - destroy |
|
| 195 | + * |
|
| 196 | + * @param array $resources |
|
| 197 | + * @param string $routeNamePrefix |
|
| 198 | + */ |
|
| 199 | + protected function processResources(array $resources, string $routeNamePrefix = ''): void { |
|
| 200 | + // declaration of all restful actions |
|
| 201 | + $actions = [ |
|
| 202 | + ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 203 | + ['name' => 'show', 'verb' => 'GET'], |
|
| 204 | + ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 205 | + ['name' => 'update', 'verb' => 'PUT'], |
|
| 206 | + ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 207 | + ]; |
|
| 208 | + |
|
| 209 | + foreach ($resources as $resource => $config) { |
|
| 210 | + $root = $this->buildRootPrefix($config, $routeNamePrefix); |
|
| 211 | + |
|
| 212 | + // the url parameter used as id to the resource |
|
| 213 | + foreach ($actions as $action) { |
|
| 214 | + $url = $root . '/' . ltrim($config['url'], '/'); |
|
| 215 | + $method = $action['name']; |
|
| 216 | + |
|
| 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 | + |
|
| 226 | + $controller = $resource; |
|
| 227 | + |
|
| 228 | + $controllerName = $this->buildControllerName($controller); |
|
| 229 | + $actionName = $this->buildActionName($method); |
|
| 230 | + |
|
| 231 | + $routeName = $routeNamePrefix . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 232 | + |
|
| 233 | + $this->router->create($routeName, $url) |
|
| 234 | + ->method($verb) |
|
| 235 | + ->setDefault('caller', [$this->appName, $controllerName, $actionName]); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + private function buildRootPrefix(array $route, string $routeNamePrefix): string { |
|
| 241 | + $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; |
|
| 242 | + $root = $route['root'] ?? $defaultRoot; |
|
| 243 | + |
|
| 244 | + if ($routeNamePrefix !== '') { |
|
| 245 | + // In OCS all apps are whitelisted |
|
| 246 | + return $root; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if (!\in_array($this->appName, $this->rootUrlApps, true)) { |
|
| 250 | + // Only allow root URLS for some apps |
|
| 251 | + return $defaultRoot; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $root; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Based on a given route name the controller name is generated |
|
| 259 | + * @param string $controller |
|
| 260 | + * @return string |
|
| 261 | + */ |
|
| 262 | + private function buildControllerName(string $controller): string { |
|
| 263 | + if (!isset($this->controllerNameCache[$controller])) { |
|
| 264 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 265 | + } |
|
| 266 | + return $this->controllerNameCache[$controller]; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Based on the action part of the route name the controller method name is generated |
|
| 271 | + * @param string $action |
|
| 272 | + * @return string |
|
| 273 | + */ |
|
| 274 | + private function buildActionName(string $action): string { |
|
| 275 | + return $this->underScoreToCamelCase($action); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Underscored strings are converted to camel case strings |
|
| 280 | + * @param string $str |
|
| 281 | + * @return string |
|
| 282 | + */ |
|
| 283 | + private function underScoreToCamelCase(string $str): string { |
|
| 284 | + $pattern = '/_[a-z]?/'; |
|
| 285 | + return preg_replace_callback( |
|
| 286 | + $pattern, |
|
| 287 | + function ($matches) { |
|
| 288 | + return strtoupper(ltrim($matches[0], '_')); |
|
| 289 | + }, |
|
| 290 | + $str); |
|
| 291 | + } |
|
| 292 | 292 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * OCS routes go into a different collection |
| 92 | 92 | */ |
| 93 | 93 | $oldCollection = $this->router->getCurrentCollection(); |
| 94 | - $this->router->useCollection($oldCollection . '.ocs'); |
|
| 94 | + $this->router->useCollection($oldCollection.'.ocs'); |
|
| 95 | 95 | |
| 96 | 96 | // parse ocs simple routes |
| 97 | 97 | $this->processOCS($this->routes); |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | $postfix = $route['postfix'] ?? ''; |
| 127 | 127 | $root = $this->buildRootPrefix($route, $routeNamePrefix); |
| 128 | 128 | |
| 129 | - $url = $root . '/' . ltrim($route['url'], '/'); |
|
| 129 | + $url = $root.'/'.ltrim($route['url'], '/'); |
|
| 130 | 130 | $verb = strtoupper($route['verb'] ?? 'GET'); |
| 131 | 131 | |
| 132 | 132 | $split = explode('#', $name, 2); |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | $controllerName = $this->buildControllerName($controller); |
| 139 | 139 | $actionName = $this->buildActionName($action); |
| 140 | 140 | |
| 141 | - $routeName = $routeNamePrefix . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 141 | + $routeName = $routeNamePrefix.$this->appName.'.'.$controller.'.'.$action.$postfix; |
|
| 142 | 142 | |
| 143 | 143 | $router = $this->router->create($routeName, $url) |
| 144 | 144 | ->method($verb) |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | |
| 212 | 212 | // the url parameter used as id to the resource |
| 213 | 213 | foreach ($actions as $action) { |
| 214 | - $url = $root . '/' . ltrim($config['url'], '/'); |
|
| 214 | + $url = $root.'/'.ltrim($config['url'], '/'); |
|
| 215 | 215 | $method = $action['name']; |
| 216 | 216 | |
| 217 | 217 | $verb = strtoupper($action['verb'] ?? 'GET'); |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | $url .= '/{id}'; |
| 221 | 221 | } |
| 222 | 222 | if (isset($action['url-postfix'])) { |
| 223 | - $url .= '/' . $action['url-postfix']; |
|
| 223 | + $url .= '/'.$action['url-postfix']; |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | $controller = $resource; |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | $controllerName = $this->buildControllerName($controller); |
| 229 | 229 | $actionName = $this->buildActionName($method); |
| 230 | 230 | |
| 231 | - $routeName = $routeNamePrefix . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 231 | + $routeName = $routeNamePrefix.$this->appName.'.'.strtolower($resource).'.'.strtolower($method); |
|
| 232 | 232 | |
| 233 | 233 | $this->router->create($routeName, $url) |
| 234 | 234 | ->method($verb) |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | private function buildRootPrefix(array $route, string $routeNamePrefix): string { |
| 241 | - $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; |
|
| 241 | + $defaultRoot = $this->appName === 'core' ? '' : '/apps/'.$this->appName; |
|
| 242 | 242 | $root = $route['root'] ?? $defaultRoot; |
| 243 | 243 | |
| 244 | 244 | if ($routeNamePrefix !== '') { |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | private function buildControllerName(string $controller): string { |
| 263 | 263 | if (!isset($this->controllerNameCache[$controller])) { |
| 264 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 264 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)).'Controller'; |
|
| 265 | 265 | } |
| 266 | 266 | return $this->controllerNameCache[$controller]; |
| 267 | 267 | } |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | $pattern = '/_[a-z]?/'; |
| 285 | 285 | return preg_replace_callback( |
| 286 | 286 | $pattern, |
| 287 | - function ($matches) { |
|
| 287 | + function($matches) { |
|
| 288 | 288 | return strtoupper(ltrim($matches[0], '_')); |
| 289 | 289 | }, |
| 290 | 290 | $str); |