vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Search/EventListener/HitListener.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\PageBundle\Search\EventListener;
  11. use Massive\Bundle\SearchBundle\Search\Event\HitEvent;
  12. use Sulu\Bundle\PageBundle\Document\BasePageDocument;
  13. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  14. /**
  15.  * Listen to for new hits. If document instance of structure
  16.  * prefix the current resource locator prefix to the URL.
  17.  */
  18. class HitListener
  19. {
  20.     /**
  21.      * @var RequestAnalyzerInterface
  22.      */
  23.     private $requestAnalyzer;
  24.     public function __construct(RequestAnalyzerInterface $requestAnalyzer)
  25.     {
  26.         $this->requestAnalyzer $requestAnalyzer;
  27.     }
  28.     /**
  29.      * Prefix url of document with current resourcelocator prefix.
  30.      */
  31.     public function onHit(HitEvent $event)
  32.     {
  33.         $document $event->getHit()->getDocument();
  34.         if ($document instanceof BasePageDocument) {
  35.             return;
  36.         }
  37.         $url $document->getUrl();
  38.         if (!$url) {
  39.             return;
  40.         }
  41.         if ('/' != \substr($url01)) {
  42.             // is absolute URL
  43.             return;
  44.         }
  45.         $url \sprintf(
  46.             '%s/%s',
  47.             \rtrim($this->requestAnalyzer->getResourceLocatorPrefix(), '/'),
  48.             \ltrim($document->getUrl(), '/')
  49.         );
  50.         $document->setUrl($url);
  51.     }
  52. }