<?php declare(strict_types=1);
namespace ZweiPunktStageWare\Subscriber;
use Cocur\Slugify\SlugifyInterface;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* When we create a copy of the Shopware installation with StageWare we add a
* variable to the .env file of the test environment. With this variable we can
* detect if a shop is the "production" shop or a test envorinment.
*
* The variable contains the name of the stage and will be displayed in the
* frontend of the test environment.
*/
class Stage implements EventSubscriberInterface
{
public const PROFILE_NAME_ENV_FIELD = "STAGEWARE_STAGE_NAME";
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRenderEvent',
];
}
public function onStorefrontRenderEvent(StorefrontRenderEvent $event): void
{
$isStage = $event
->getRequest()
->server
->has(self::PROFILE_NAME_ENV_FIELD)
;
if (!$isStage) {
return;
}
$stageName = $event
->getRequest()
->server
->get(self::PROFILE_NAME_ENV_FIELD)
;
$params = $event->getParameters();
if (!isset($params['page'])) {
return;
}
$page = $params['page'];
$slug = trim($stageName);
$slug = str_replace(' ', '-', $slug);
$slug = strtolower($slug);
$slug = preg_replace("/[^a-z0-9|-]+/", "", $slug);
$page->assign([
'stageware' => [
'name' => $stageName,
'slug' => $slug
]
]);
}
}