custom/plugins/ZweiPunktStageWare/src/Subscriber/Stage.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ZweiPunktStageWare\Subscriber;
  3. use Cocur\Slugify\SlugifyInterface;
  4. use Shopware\Storefront\Event\StorefrontRenderEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. /**
  7.  * When we create a copy of the Shopware installation with StageWare we add a
  8.  * variable to the .env file of the test environment. With this variable we can
  9.  * detect if a shop is the "production" shop or a test envorinment.
  10.  *
  11.  * The variable contains the name of the stage and will be displayed in the
  12.  * frontend of the test environment.
  13.  */
  14. class Stage implements EventSubscriberInterface
  15. {
  16.     public const PROFILE_NAME_ENV_FIELD "STAGEWARE_STAGE_NAME";
  17.     /**
  18.      * @return array<string, string>
  19.      */
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             StorefrontRenderEvent::class => 'onStorefrontRenderEvent',
  24.         ];
  25.     }
  26.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event): void
  27.     {
  28.         $isStage $event
  29.             ->getRequest()
  30.             ->server
  31.             ->has(self::PROFILE_NAME_ENV_FIELD)
  32.         ;
  33.         if (!$isStage) {
  34.             return;
  35.         }
  36.         $stageName $event
  37.             ->getRequest()
  38.             ->server
  39.             ->get(self::PROFILE_NAME_ENV_FIELD)
  40.         ;
  41.         $params $event->getParameters();
  42.         if (!isset($params['page'])) {
  43.             return;
  44.         }
  45.         $page $params['page'];
  46.         $slug trim($stageName);
  47.         $slug str_replace(' ''-'$slug);
  48.         $slug strtolower($slug);
  49.         $slug preg_replace("/[^a-z0-9|-]+/"""$slug);
  50.         $page->assign([
  51.             'stageware' => [
  52.                 'name' => $stageName,
  53.                 'slug' => $slug
  54.             ]
  55.         ]);
  56.     }
  57. }