vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/URLAliasService.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Event;
  8. use eZ\Publish\API\Repository\URLAliasService as URLAliasServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Content\Location;
  10. use eZ\Publish\API\Repository\Events\URLAlias\BeforeCreateGlobalUrlAliasEvent;
  11. use eZ\Publish\API\Repository\Events\URLAlias\BeforeCreateUrlAliasEvent;
  12. use eZ\Publish\API\Repository\Events\URLAlias\BeforeRefreshSystemUrlAliasesForLocationEvent;
  13. use eZ\Publish\API\Repository\Events\URLAlias\BeforeRemoveAliasesEvent;
  14. use eZ\Publish\API\Repository\Events\URLAlias\CreateGlobalUrlAliasEvent;
  15. use eZ\Publish\API\Repository\Events\URLAlias\CreateUrlAliasEvent;
  16. use eZ\Publish\API\Repository\Events\URLAlias\RefreshSystemUrlAliasesForLocationEvent;
  17. use eZ\Publish\API\Repository\Events\URLAlias\RemoveAliasesEvent;
  18. use eZ\Publish\API\Repository\Values\Content\URLAlias;
  19. use eZ\Publish\SPI\Repository\Decorator\URLAliasServiceDecorator;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. class URLAliasService extends URLAliasServiceDecorator
  22. {
  23.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  24.     protected $eventDispatcher;
  25.     public function __construct(
  26.         URLAliasServiceInterface $innerService,
  27.         EventDispatcherInterface $eventDispatcher
  28.     ) {
  29.         parent::__construct($innerService);
  30.         $this->eventDispatcher $eventDispatcher;
  31.     }
  32.     public function createUrlAlias(
  33.         Location $location,
  34.         string $path,
  35.         string $languageCode,
  36.         bool $forwarding false,
  37.         bool $alwaysAvailable false
  38.     ): URLAlias {
  39.         $eventData = [
  40.             $location,
  41.             $path,
  42.             $languageCode,
  43.             $forwarding,
  44.             $alwaysAvailable,
  45.         ];
  46.         $beforeEvent = new BeforeCreateUrlAliasEvent(...$eventData);
  47.         $this->eventDispatcher->dispatch($beforeEvent);
  48.         if ($beforeEvent->isPropagationStopped()) {
  49.             return $beforeEvent->getUrlAlias();
  50.         }
  51.         $urlAlias $beforeEvent->hasUrlAlias()
  52.             ? $beforeEvent->getUrlAlias()
  53.             : $this->innerService->createUrlAlias($location$path$languageCode$forwarding$alwaysAvailable);
  54.         $this->eventDispatcher->dispatch(
  55.             new CreateUrlAliasEvent($urlAlias, ...$eventData)
  56.         );
  57.         return $urlAlias;
  58.     }
  59.     public function createGlobalUrlAlias(
  60.         string $resource,
  61.         string $path,
  62.         string $languageCode,
  63.         bool $forwarding false,
  64.         bool $alwaysAvailable false
  65.     ): URLAlias {
  66.         $eventData = [
  67.             $resource,
  68.             $path,
  69.             $languageCode,
  70.             $forwarding,
  71.             $alwaysAvailable,
  72.         ];
  73.         $beforeEvent = new BeforeCreateGlobalUrlAliasEvent(...$eventData);
  74.         $this->eventDispatcher->dispatch($beforeEvent);
  75.         if ($beforeEvent->isPropagationStopped()) {
  76.             return $beforeEvent->getUrlAlias();
  77.         }
  78.         $urlAlias $beforeEvent->hasUrlAlias()
  79.             ? $beforeEvent->getUrlAlias()
  80.             : $this->innerService->createGlobalUrlAlias($resource$path$languageCode$forwarding$alwaysAvailable);
  81.         $this->eventDispatcher->dispatch(
  82.             new CreateGlobalUrlAliasEvent($urlAlias, ...$eventData)
  83.         );
  84.         return $urlAlias;
  85.     }
  86.     public function removeAliases(array $aliasList): void
  87.     {
  88.         $eventData = [$aliasList];
  89.         $beforeEvent = new BeforeRemoveAliasesEvent(...$eventData);
  90.         $this->eventDispatcher->dispatch($beforeEvent);
  91.         if ($beforeEvent->isPropagationStopped()) {
  92.             return;
  93.         }
  94.         $this->innerService->removeAliases($aliasList);
  95.         $this->eventDispatcher->dispatch(
  96.             new RemoveAliasesEvent(...$eventData)
  97.         );
  98.     }
  99.     public function refreshSystemUrlAliasesForLocation(Location $location): void
  100.     {
  101.         $eventData = [$location];
  102.         $beforeEvent = new BeforeRefreshSystemUrlAliasesForLocationEvent(...$eventData);
  103.         $this->eventDispatcher->dispatch($beforeEvent);
  104.         if ($beforeEvent->isPropagationStopped()) {
  105.             return;
  106.         }
  107.         $this->innerService->refreshSystemUrlAliasesForLocation($location);
  108.         $this->eventDispatcher->dispatch(
  109.             new RefreshSystemUrlAliasesForLocationEvent(...$eventData)
  110.         );
  111.     }
  112. }