@@ -21,139 +21,139 @@ |
||
| 21 | 21 | use Symfony\Component\Routing\RouteCollection; |
| 22 | 22 | |
| 23 | 23 | class CachingRouter extends Router { |
| 24 | - protected ICache $cache; |
|
| 24 | + protected ICache $cache; |
|
| 25 | 25 | |
| 26 | - protected array $legacyCreatedRoutes = []; |
|
| 26 | + protected array $legacyCreatedRoutes = []; |
|
| 27 | 27 | |
| 28 | - public function __construct( |
|
| 29 | - ICacheFactory $cacheFactory, |
|
| 30 | - LoggerInterface $logger, |
|
| 31 | - IRequest $request, |
|
| 32 | - IConfig $config, |
|
| 33 | - IEventLogger $eventLogger, |
|
| 34 | - ContainerInterface $container, |
|
| 35 | - IAppManager $appManager, |
|
| 36 | - ) { |
|
| 37 | - $this->cache = $cacheFactory->createLocal('route'); |
|
| 38 | - parent::__construct($logger, $request, $config, $eventLogger, $container, $appManager); |
|
| 39 | - } |
|
| 28 | + public function __construct( |
|
| 29 | + ICacheFactory $cacheFactory, |
|
| 30 | + LoggerInterface $logger, |
|
| 31 | + IRequest $request, |
|
| 32 | + IConfig $config, |
|
| 33 | + IEventLogger $eventLogger, |
|
| 34 | + ContainerInterface $container, |
|
| 35 | + IAppManager $appManager, |
|
| 36 | + ) { |
|
| 37 | + $this->cache = $cacheFactory->createLocal('route'); |
|
| 38 | + parent::__construct($logger, $request, $config, $eventLogger, $container, $appManager); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Generate url based on $name and $parameters |
|
| 43 | - * |
|
| 44 | - * @param string $name Name of the route to use. |
|
| 45 | - * @param array $parameters Parameters for the route |
|
| 46 | - * @param bool $absolute |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public function generate($name, $parameters = [], $absolute = false) { |
|
| 50 | - asort($parameters); |
|
| 51 | - $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . sha1(json_encode($parameters)) . (int)$absolute; |
|
| 52 | - $cachedKey = $this->cache->get($key); |
|
| 53 | - if ($cachedKey) { |
|
| 54 | - return $cachedKey; |
|
| 55 | - } else { |
|
| 56 | - $url = parent::generate($name, $parameters, $absolute); |
|
| 57 | - if ($url) { |
|
| 58 | - $this->cache->set($key, $url, 3600); |
|
| 59 | - } |
|
| 60 | - return $url; |
|
| 61 | - } |
|
| 62 | - } |
|
| 41 | + /** |
|
| 42 | + * Generate url based on $name and $parameters |
|
| 43 | + * |
|
| 44 | + * @param string $name Name of the route to use. |
|
| 45 | + * @param array $parameters Parameters for the route |
|
| 46 | + * @param bool $absolute |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public function generate($name, $parameters = [], $absolute = false) { |
|
| 50 | + asort($parameters); |
|
| 51 | + $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . sha1(json_encode($parameters)) . (int)$absolute; |
|
| 52 | + $cachedKey = $this->cache->get($key); |
|
| 53 | + if ($cachedKey) { |
|
| 54 | + return $cachedKey; |
|
| 55 | + } else { |
|
| 56 | + $url = parent::generate($name, $parameters, $absolute); |
|
| 57 | + if ($url) { |
|
| 58 | + $this->cache->set($key, $url, 3600); |
|
| 59 | + } |
|
| 60 | + return $url; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - private function serializeRouteCollection(RouteCollection $collection): array { |
|
| 65 | - $dumper = new CompiledUrlMatcherDumper($collection); |
|
| 66 | - return $dumper->getCompiledRoutes(); |
|
| 67 | - } |
|
| 64 | + private function serializeRouteCollection(RouteCollection $collection): array { |
|
| 65 | + $dumper = new CompiledUrlMatcherDumper($collection); |
|
| 66 | + return $dumper->getCompiledRoutes(); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Find the route matching $url |
|
| 71 | - * |
|
| 72 | - * @param string $url The url to find |
|
| 73 | - * @throws \Exception |
|
| 74 | - * @return array |
|
| 75 | - */ |
|
| 76 | - public function findMatchingRoute(string $url): array { |
|
| 77 | - return parent::findMatchingRoute($url); |
|
| 69 | + /** |
|
| 70 | + * Find the route matching $url |
|
| 71 | + * |
|
| 72 | + * @param string $url The url to find |
|
| 73 | + * @throws \Exception |
|
| 74 | + * @return array |
|
| 75 | + */ |
|
| 76 | + public function findMatchingRoute(string $url): array { |
|
| 77 | + return parent::findMatchingRoute($url); |
|
| 78 | 78 | |
| 79 | - $this->eventLogger->start('cacheroute:match', 'Match route'); |
|
| 80 | - $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . '#rootCollection'; |
|
| 81 | - $cachedRoutes = $this->cache->get($key); |
|
| 82 | - if (!$cachedRoutes) { |
|
| 83 | - parent::loadRoutes(); |
|
| 84 | - $cachedRoutes = $this->serializeRouteCollection($this->root); |
|
| 85 | - $this->cache->set($key, $cachedRoutes, ($this->config->getSystemValueBool('debug') ? 3 : 3600)); |
|
| 86 | - } |
|
| 87 | - $matcher = new CompiledUrlMatcher($cachedRoutes, $this->context); |
|
| 88 | - $this->eventLogger->start('cacheroute:url:match', 'Symfony URL match call'); |
|
| 89 | - try { |
|
| 90 | - $parameters = $matcher->match($url); |
|
| 91 | - } catch (ResourceNotFoundException $e) { |
|
| 92 | - if (!str_ends_with($url, '/')) { |
|
| 93 | - // We allow links to apps/files? for backwards compatibility reasons |
|
| 94 | - // However, since Symfony does not allow empty route names, the route |
|
| 95 | - // we need to match is '/', so we need to append the '/' here. |
|
| 96 | - try { |
|
| 97 | - $parameters = $matcher->match($url . '/'); |
|
| 98 | - } catch (ResourceNotFoundException $newException) { |
|
| 99 | - // If we still didn't match a route, we throw the original exception |
|
| 100 | - throw $e; |
|
| 101 | - } |
|
| 102 | - } else { |
|
| 103 | - throw $e; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - $this->eventLogger->end('cacheroute:url:match'); |
|
| 79 | + $this->eventLogger->start('cacheroute:match', 'Match route'); |
|
| 80 | + $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . '#rootCollection'; |
|
| 81 | + $cachedRoutes = $this->cache->get($key); |
|
| 82 | + if (!$cachedRoutes) { |
|
| 83 | + parent::loadRoutes(); |
|
| 84 | + $cachedRoutes = $this->serializeRouteCollection($this->root); |
|
| 85 | + $this->cache->set($key, $cachedRoutes, ($this->config->getSystemValueBool('debug') ? 3 : 3600)); |
|
| 86 | + } |
|
| 87 | + $matcher = new CompiledUrlMatcher($cachedRoutes, $this->context); |
|
| 88 | + $this->eventLogger->start('cacheroute:url:match', 'Symfony URL match call'); |
|
| 89 | + try { |
|
| 90 | + $parameters = $matcher->match($url); |
|
| 91 | + } catch (ResourceNotFoundException $e) { |
|
| 92 | + if (!str_ends_with($url, '/')) { |
|
| 93 | + // We allow links to apps/files? for backwards compatibility reasons |
|
| 94 | + // However, since Symfony does not allow empty route names, the route |
|
| 95 | + // we need to match is '/', so we need to append the '/' here. |
|
| 96 | + try { |
|
| 97 | + $parameters = $matcher->match($url . '/'); |
|
| 98 | + } catch (ResourceNotFoundException $newException) { |
|
| 99 | + // If we still didn't match a route, we throw the original exception |
|
| 100 | + throw $e; |
|
| 101 | + } |
|
| 102 | + } else { |
|
| 103 | + throw $e; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + $this->eventLogger->end('cacheroute:url:match'); |
|
| 107 | 107 | |
| 108 | - $this->eventLogger->end('cacheroute:match'); |
|
| 109 | - return $parameters; |
|
| 110 | - } |
|
| 108 | + $this->eventLogger->end('cacheroute:match'); |
|
| 109 | + return $parameters; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * @param array{action:mixed, ...} $parameters |
|
| 114 | - */ |
|
| 115 | - protected function callLegacyActionRoute(array $parameters): void { |
|
| 116 | - /* |
|
| 112 | + /** |
|
| 113 | + * @param array{action:mixed, ...} $parameters |
|
| 114 | + */ |
|
| 115 | + protected function callLegacyActionRoute(array $parameters): void { |
|
| 116 | + /* |
|
| 117 | 117 | * Closures cannot be serialized to cache, so for legacy routes calling an action we have to include the routes.php file again |
| 118 | 118 | */ |
| 119 | - $app = $parameters['app']; |
|
| 120 | - $this->useCollection($app); |
|
| 121 | - parent::requireRouteFile($parameters['route-file'], $app); |
|
| 122 | - $collection = $this->getCollection($app); |
|
| 123 | - $parameters['action'] = $collection->get($parameters['_route'])?->getDefault('action'); |
|
| 124 | - parent::callLegacyActionRoute($parameters); |
|
| 125 | - } |
|
| 119 | + $app = $parameters['app']; |
|
| 120 | + $this->useCollection($app); |
|
| 121 | + parent::requireRouteFile($parameters['route-file'], $app); |
|
| 122 | + $collection = $this->getCollection($app); |
|
| 123 | + $parameters['action'] = $collection->get($parameters['_route'])?->getDefault('action'); |
|
| 124 | + parent::callLegacyActionRoute($parameters); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Create a \OC\Route\Route. |
|
| 129 | - * Deprecated |
|
| 130 | - * |
|
| 131 | - * @param string $name Name of the route to create. |
|
| 132 | - * @param string $pattern The pattern to match |
|
| 133 | - * @param array $defaults An array of default parameter values |
|
| 134 | - * @param array $requirements An array of requirements for parameters (regexes) |
|
| 135 | - */ |
|
| 136 | - public function create($name, $pattern, array $defaults = [], array $requirements = []): Route { |
|
| 137 | - $this->legacyCreatedRoutes[] = $name; |
|
| 138 | - return parent::create($name, $pattern, $defaults, $requirements); |
|
| 139 | - } |
|
| 127 | + /** |
|
| 128 | + * Create a \OC\Route\Route. |
|
| 129 | + * Deprecated |
|
| 130 | + * |
|
| 131 | + * @param string $name Name of the route to create. |
|
| 132 | + * @param string $pattern The pattern to match |
|
| 133 | + * @param array $defaults An array of default parameter values |
|
| 134 | + * @param array $requirements An array of requirements for parameters (regexes) |
|
| 135 | + */ |
|
| 136 | + public function create($name, $pattern, array $defaults = [], array $requirements = []): Route { |
|
| 137 | + $this->legacyCreatedRoutes[] = $name; |
|
| 138 | + return parent::create($name, $pattern, $defaults, $requirements); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Require a routes.php file |
|
| 143 | - */ |
|
| 144 | - protected function requireRouteFile(string $file, string $appName): void { |
|
| 145 | - $this->legacyCreatedRoutes = []; |
|
| 146 | - parent::requireRouteFile($file, $appName); |
|
| 147 | - foreach ($this->legacyCreatedRoutes as $routeName) { |
|
| 148 | - $route = $this->collection?->get($routeName); |
|
| 149 | - if ($route === null) { |
|
| 150 | - /* Should never happen */ |
|
| 151 | - throw new \Exception("Could not find route $routeName"); |
|
| 152 | - } |
|
| 153 | - if ($route->hasDefault('action')) { |
|
| 154 | - $route->setDefault('route-file', $file); |
|
| 155 | - $route->setDefault('app', $appName); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - } |
|
| 141 | + /** |
|
| 142 | + * Require a routes.php file |
|
| 143 | + */ |
|
| 144 | + protected function requireRouteFile(string $file, string $appName): void { |
|
| 145 | + $this->legacyCreatedRoutes = []; |
|
| 146 | + parent::requireRouteFile($file, $appName); |
|
| 147 | + foreach ($this->legacyCreatedRoutes as $routeName) { |
|
| 148 | + $route = $this->collection?->get($routeName); |
|
| 149 | + if ($route === null) { |
|
| 150 | + /* Should never happen */ |
|
| 151 | + throw new \Exception("Could not find route $routeName"); |
|
| 152 | + } |
|
| 153 | + if ($route->hasDefault('action')) { |
|
| 154 | + $route->setDefault('route-file', $file); |
|
| 155 | + $route->setDefault('app', $appName); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | 159 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public function generate($name, $parameters = [], $absolute = false) { |
| 50 | 50 | asort($parameters); |
| 51 | - $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . sha1(json_encode($parameters)) . (int)$absolute; |
|
| 51 | + $key = $this->context->getHost().'#'.$this->context->getBaseUrl().$name.sha1(json_encode($parameters)).(int) $absolute; |
|
| 52 | 52 | $cachedKey = $this->cache->get($key); |
| 53 | 53 | if ($cachedKey) { |
| 54 | 54 | return $cachedKey; |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | return parent::findMatchingRoute($url); |
| 78 | 78 | |
| 79 | 79 | $this->eventLogger->start('cacheroute:match', 'Match route'); |
| 80 | - $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . '#rootCollection'; |
|
| 80 | + $key = $this->context->getHost().'#'.$this->context->getBaseUrl().'#rootCollection'; |
|
| 81 | 81 | $cachedRoutes = $this->cache->get($key); |
| 82 | 82 | if (!$cachedRoutes) { |
| 83 | 83 | parent::loadRoutes(); |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | // However, since Symfony does not allow empty route names, the route |
| 95 | 95 | // we need to match is '/', so we need to append the '/' here. |
| 96 | 96 | try { |
| 97 | - $parameters = $matcher->match($url . '/'); |
|
| 97 | + $parameters = $matcher->match($url.'/'); |
|
| 98 | 98 | } catch (ResourceNotFoundException $newException) { |
| 99 | 99 | // If we still didn't match a route, we throw the original exception |
| 100 | 100 | throw $e; |