예제 #1
0
 /**
  * @test
  */
 public function buildResponseIfApplicableRetunsHttpRequestIfApplicableRedirectIsFound()
 {
     $this->mockHttpRequest->expects($this->atLeastOnce())->method('getRelativePath')->willReturn('some/relative/path');
     $mockRedirect = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock();
     $mockRedirect->expects($this->atLeastOnce())->method('getStatusCode')->willReturn(301);
     $this->mockRedirectStorage->expects($this->once())->method('getOneBySourceUriPathAndHost')->with('some/relative/path')->willReturn($mockRedirect);
     $this->inject($this->redirectService, 'redirectStorage', $this->mockRedirectStorage);
     $request = $this->redirectService->buildResponseIfApplicable($this->mockHttpRequest);
     $this->assertInstanceOf(Response::class, $request);
 }
 /**
  * Add a redirect
  *
  * Creates a new custom redirect. The optional argument ``host`` is used to define a specific redirect only valid
  * for a certain domain If no ``host`` argument is supplied, the redirect will act as a fallback redirect for all
  * domains in use. If any redirect exists with the same ``source`` property, it will be replaced if the ``force``
  * property has been set.
  *
  * @param string $source The relative URI path that should trigger the redirect
  * @param string $target The relative URI path that the redirect should point to
  * @param integer $statusCode The status code of the redirect header
  * @param string $host (optional) The host the redirect is valid for. If none is set, the redirect is valid for all
  * @param boolean $force Replace existing redirect (based on the source URI)
  * @return void
  */
 public function addCommand($source, $target, $statusCode, $host = null, $force = false)
 {
     $this->outputLine();
     $this->outputLine('<b>Create a redirect ...</b>');
     $this->outputLine();
     $redirect = $this->redirectStorage->getOneBySourceUriPathAndHost($source, $host, false);
     $isSame = $this->isSame($source, $target, $host, $statusCode, $redirect);
     if ($redirect !== null && $isSame === false && $force === false) {
         $this->outputLine('A redirect with the same source URI exist, see below:');
         $this->outputLine();
         $this->outputRedirectLine('<error>!!</error>', $redirect);
         $this->outputLine();
         $this->outputLine('Use --force to replace it');
         $this->outputLine();
         $this->sendAndExit(1);
     } elseif ($redirect !== null && $isSame === false && $force === true) {
         $this->redirectStorage->removeOneBySourceUriPathAndHost($source, $host);
         $this->outputRedirectLine('<info>--</info>', $redirect);
         $this->persistenceManager->persistAll();
     } elseif ($redirect !== null && $isSame === true) {
         $this->outputRedirectLine('<comment>~~</comment>', $redirect);
         $this->outputLine();
         $this->outputLegend();
         $this->sendAndExit();
     }
     $redirects = $this->redirectStorage->addRedirect($source, $target, $statusCode, [$host]);
     $redirect = reset($redirects);
     $this->outputRedirectLine('<info>++</info>', $redirect);
     $this->outputLine();
     $this->outputLegend();
 }
예제 #3
0
 /**
  * Searches for a matching redirect for the given HTTP response
  *
  * @param Request $httpRequest
  * @return Response|null
  * @api
  */
 public function buildResponseIfApplicable(Request $httpRequest)
 {
     try {
         $redirect = $this->redirectStorage->getOneBySourceUriPathAndHost($httpRequest->getRelativePath(), $httpRequest->getBaseUri()->getHost());
         if ($redirect === null) {
             return null;
         }
         if (isset($this->featureSwitch['hitCounter']) && $this->featureSwitch['hitCounter'] === true) {
             $this->redirectStorage->incrementHitCount($redirect);
         }
         return $this->buildResponse($httpRequest, $redirect);
     } catch (\Exception $exception) {
         // Throw exception if it's a \Neos\RedirectHandler\Exception (used for custom exception handling)
         if ($exception instanceof Exception) {
             throw $exception;
         }
         // skip triggering the redirect if there was an error accessing the database (wrong credentials, ...)
         return null;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function createRedirectsForPublishedNode(NodeInterface $node, Workspace $targetWorkspace)
 {
     $nodeType = $node->getNodeType();
     if ($targetWorkspace->getName() !== 'live' || !$nodeType->isOfType('Neos.Neos:Document')) {
         return;
     }
     $context = $this->contextFactory->create(['workspaceName' => 'live', 'invisibleContentShown' => true, 'dimensions' => $node->getContext()->getDimensions()]);
     $targetNode = $context->getNodeByIdentifier($node->getIdentifier());
     if ($targetNode === null) {
         // The page has been added
         return;
     }
     $targetNodeUriPath = $this->buildUriPathForNodeContextPath($targetNode->getContextPath());
     if ($targetNodeUriPath === null) {
         throw new Exception('The target URI path of the node could not be resolved', 1451945358);
     }
     $hosts = $this->getHostnames($node->getContext());
     // The page has been removed
     if ($node->isRemoved()) {
         $this->flushRoutingCacheForNode($targetNode);
         $statusCode = (int) $this->defaultStatusCode['gone'];
         $this->redirectStorage->addRedirect($targetNodeUriPath, '', $statusCode, $hosts);
         return;
     }
     // compare the "old" node URI to the new one
     $nodeUriPath = $this->buildUriPathForNodeContextPath($node->getContextPath());
     // use the same regexp than the ContentContextBar Ember View
     $nodeUriPath = preg_replace('/@[A-Za-z0-9;&,\\-_=]+/', '', $nodeUriPath);
     if ($nodeUriPath === null || $nodeUriPath === $targetNodeUriPath) {
         // The page node path has not been changed
         return;
     }
     $this->flushRoutingCacheForNode($targetNode);
     $statusCode = (int) $this->defaultStatusCode['redirect'];
     $this->redirectStorage->addRedirect($targetNodeUriPath, $nodeUriPath, $statusCode, $hosts);
     $q = new FlowQuery([$node]);
     foreach ($q->children('[instanceof Neos.Neos:Document]') as $childrenNode) {
         $this->createRedirectsForPublishedNode($childrenNode, $targetWorkspace);
     }
 }