forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleCommandListener.php
83 lines (71 loc) · 2.42 KB
/
ConsoleCommandListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* File containing the ConsoleCommandListener class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace eZ\Bundle\EzPublishCoreBundle\EventListener;
use eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException;
use eZ\Publish\Core\MVC\Symfony\Event\ScopeChangeEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* ConsoleCommandListener event listener.
*/
class ConsoleCommandListener implements EventSubscriberInterface, SiteAccessAware
{
/**
* @var string
*/
private $defaultSiteAccessName;
/**
* @var array
*/
private $siteAccessList;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var
*/
private $siteAccess;
/**
* ConsoleCommandListener constructor.
*/
public function __construct($defaultSiteAccessName, array $siteAccessList, EventDispatcherInterface $eventDispatcher)
{
$this->defaultSiteAccessName = $defaultSiteAccessName;
$this->siteAccessList = $siteAccessList;
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents()
{
return [
ConsoleEvents::COMMAND => [
['onConsoleCommand', -1],
],
];
}
public function onConsoleCommand(ConsoleCommandEvent $event)
{
$this->siteAccess->name = $event->getInput()->getParameterOption('--siteaccess', $this->defaultSiteAccessName);
$this->siteAccess->matchingType = 'cli';
if (!in_array($this->siteAccess->name, $this->siteAccessList)) {
throw new InvalidSiteAccessException($this->siteAccess->name, $this->siteAccessList, $this->siteAccess->matchingType);
}
$this->eventDispatcher->dispatch(MVCEvents::CONFIG_SCOPE_CHANGE, new ScopeChangeEvent($this->siteAccess));
}
public function setSiteAccess(SiteAccess $siteAccess = null)
{
$this->siteAccess = $siteAccess;
}
}