1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace eZ\Bundle\PlatformBehatBundle\Initializer; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\EventDispatcher\Event\ExampleTested; |
6
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
7
|
|
|
use eZ\Publish\Core\MVC\Symfony\Event\ScopeChangeEvent; |
8
|
|
|
use eZ\Publish\Core\MVC\Symfony\MVCEvents; |
9
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
12
|
|
|
|
13
|
|
|
final class BehatSiteAccessInitializer implements EventSubscriberInterface |
14
|
|
|
{ |
15
|
|
|
public const EZPLATFORM_SITEACCESS_ENV_VAR = 'EZPLATFORM_SITEACCESS'; |
16
|
|
|
|
17
|
|
|
private const DEFAULT_SITEACCESS_PARAMETER = 'ezpublish.siteaccess.default'; |
18
|
|
|
|
19
|
|
|
private const EVENT_DISPATCHER_SERVICE_ID = 'event_dispatcher'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var KernelInterface |
23
|
|
|
*/ |
24
|
|
|
private $kernel; |
25
|
|
|
|
26
|
|
|
public function __construct(KernelInterface $kernel) |
27
|
|
|
{ |
28
|
|
|
$this->kernel = $kernel; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getSubscribedEvents() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
ScenarioTested::BEFORE => ['setSiteAccess'], |
35
|
|
|
ExampleTested::BEFORE => ['setSiteAccess'], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setSiteAccess(): void |
40
|
|
|
{ |
41
|
|
|
$siteAccess = $this->getSiteAccess(); |
42
|
|
|
$this->kernel->getContainer()->get(self::EVENT_DISPATCHER_SERVICE_ID)->dispatch(MVCEvents::CONFIG_SCOPE_CHANGE, new ScopeChangeEvent($siteAccess)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function getSiteAccess(): SiteAccess |
46
|
|
|
{ |
47
|
|
|
$siteAccessFromEnvVar = getenv(self::EZPLATFORM_SITEACCESS_ENV_VAR); |
48
|
|
|
$defaultSiteAccess = $this->kernel->getContainer()->getParameter(self::DEFAULT_SITEACCESS_PARAMETER); |
49
|
|
|
|
50
|
|
|
$siteAccessName = $siteAccessFromEnvVar !== false ? $siteAccessFromEnvVar : $defaultSiteAccess; |
51
|
|
|
|
52
|
|
|
return new SiteAccess($siteAccessName, 'cli'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|