/** * Flush caches according to the previously registered node changes. * * @return void */ public function commit() { foreach ($this->tagsToFlush as $tag) { $this->routeCachingService->flushCachesByTag($tag); } $this->tagsToFlush = array(); }
/** * Updates affected redirects in order to avoid redundant or circular redirections. * * @param RedirectInterface $newRedirect * @throws Exception if creating the redirect would cause conflicts * @return void */ protected function updateDependingRedirects(RedirectInterface $newRedirect) { /** @var $existingRedirectForSourceUriPath Redirect */ $existingRedirectForSourceUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost(), false); if ($existingRedirectForSourceUriPath !== null) { $this->removeAndLog($existingRedirectForSourceUriPath, sprintf('Existing redirect for the source URI path "%s" removed.', $newRedirect->getSourceUriPath())); $this->routerCachingService->flushCachesForUriPath($existingRedirectForSourceUriPath->getSourceUriPath()); } /** @var $existingRedirectForTargetUriPath Redirect */ $existingRedirectForTargetUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getTargetUriPath(), $newRedirect->getHost(), false); if ($existingRedirectForTargetUriPath !== null) { $this->removeAndLog($existingRedirectForTargetUriPath, sprintf('Existing redirect for the target URI path "%s" removed.', $newRedirect->getTargetUriPath())); $this->routerCachingService->flushCachesForUriPath($existingRedirectForTargetUriPath->getSourceUriPath()); } $obsoleteRedirectInstances = $this->redirectRepository->findByTargetUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost()); /** @var $obsoleteRedirect Redirect */ foreach ($obsoleteRedirectInstances as $obsoleteRedirect) { if ($obsoleteRedirect->getSourceUriPath() === $newRedirect->getTargetUriPath()) { $this->redirectRepository->remove($obsoleteRedirect); } else { $obsoleteRedirect->setTargetUriPath($newRedirect->getTargetUriPath()); $this->redirectRepository->update($obsoleteRedirect); } } }
/** * @test */ public function addRedirectEmitSignalAndFlushesRouterCacheForAffectedUri() { $this->mockRedirectRepository->expects($this->atLeastOnce())->method('findByTargetUriPathAndHost')->willReturn([]); $this->mockRouterCachingService->expects($this->once())->method('flushCachesForUriPath')->with('some/relative/path'); $this->redirectServiceMock->expects($this->atLeastOnce())->method('emitRedirectCreated'); $this->redirectStorage->addRedirect('some/relative/path', 'target'); }
/** * Removes all routing cache entries for the given $nodeData * * @param NodeInterface $node * @return void */ protected function flushRoutingCacheForNode(NodeInterface $node) { $nodeData = $node->getNodeData(); $nodeDataIdentifier = $this->persistenceManager->getIdentifierByObject($nodeData); if ($nodeDataIdentifier === null) { return; } $this->routerCachingService->flushCachesByTag($nodeDataIdentifier); }
/** * @test */ public function storeResolvedUriPathConvertsObjectsImplementingCacheAwareInterfaceToCacheEntryIdentifier() { $mockObject = $this->createMock(CacheAwareInterface::class); $mockObject->expects($this->atLeastOnce())->method('getCacheEntryIdentifier')->will($this->returnValue('objectIdentifier')); $routeValues = ['b' => 'route values', 'someObject' => $mockObject]; $cacheIdentifier = '264b593d59582adea4ccc52b33cc093f'; $matchingUriPath = 'uncached/matching/uri'; $this->mockResolveCache->expects($this->once())->method('set')->with($cacheIdentifier, $matchingUriPath); $this->routerCachingService->storeResolvedUriPath($matchingUriPath, $routeValues); }
/** * @test */ public function routeStoresMatchResultsInCacheIfNotFoundInCache() { $router = $this->getAccessibleMock(Router::class, ['createRoutesFromConfiguration']); $this->inject($router, 'routerCachingService', $this->mockRouterCachingService); $this->inject($router, 'systemLogger', $this->mockSystemLogger); $matchResults = ['some' => 'match results']; $mockHttpRequest = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); $mockRoute1 = $this->getMockBuilder(Route::class)->getMock(); $mockRoute1->expects($this->once())->method('matches')->with($mockHttpRequest)->will($this->returnValue(false)); $mockRoute2 = $this->getMockBuilder(Route::class)->getMock(); $mockRoute2->expects($this->once())->method('matches')->with($mockHttpRequest)->will($this->returnValue(true)); $mockRoute2->expects($this->once())->method('getMatchResults')->will($this->returnValue($matchResults)); $router->_set('routes', [$mockRoute1, $mockRoute2]); $this->mockRouterCachingService->expects($this->once())->method('storeMatchResults')->with($mockHttpRequest, $matchResults); $this->assertSame($matchResults, $router->route($mockHttpRequest)); }
/** * Builds the corresponding uri (excluding protocol and host) by iterating * through all configured routes and calling their respective resolves() * method. If no matching route is found, an empty string is returned. * Note: calls of this message are cached by RouterCachingAspect * * @param array $routeValues Key/value pairs to be resolved. E.g. array('@package' => 'MyPackage', '@controller' => 'MyController'); * @return string * @throws NoMatchingRouteException */ public function resolve(array $routeValues) { $cachedResolvedUriPath = $this->routerCachingService->getCachedResolvedUriPath($routeValues); if ($cachedResolvedUriPath !== false) { return $cachedResolvedUriPath; } $this->lastResolvedRoute = null; $this->createRoutesFromConfiguration(); /** @var $route Route */ foreach ($this->routes as $route) { if ($route->resolves($routeValues)) { $this->lastResolvedRoute = $route; $resolvedUriPath = $route->getResolvedUriPath(); if ($resolvedUriPath !== null) { $this->routerCachingService->storeResolvedUriPath($resolvedUriPath, $routeValues); } return $resolvedUriPath; } } $this->systemLogger->log('Router resolve(): Could not resolve a route for building an URI for the given route values.', LOG_WARNING, $routeValues); throw new NoMatchingRouteException('Could not resolve a route and its corresponding URI for the given parameters. This may be due to referring to a not existing package / controller / action while building a link or URI. Refer to log and check the backtrace for more details.', 1301610453); }