forked from ezsystems/LegacyBridge
/
LegacyKernelListener.php
79 lines (68 loc) · 2.55 KB
/
LegacyKernelListener.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
<?php
/**
* File containing the LegacyKernelListener 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\EzPublishLegacyBundle\EventListener;
use eZ\Publish\Core\MVC\Legacy\LegacyEvents;
use eZ\Publish\Core\MVC\Legacy\Event\PreResetLegacyKernelEvent;
use eZINI;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\Core\MVC\Symfony\Event\ScopeChangeEvent;
/**
* Resets eZINI when the Legacy Kernel is reset.
* Resets legacy kernel handler when used in a command.
*/
class LegacyKernelListener extends ContainerAware implements EventSubscriberInterface
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct( EventDispatcherInterface $eventDispatcher )
{
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents()
{
return [
LegacyEvents::PRE_RESET_LEGACY_KERNEL => 'onKernelReset',
ConsoleEvents::COMMAND => 'onConsoleCommand'
];
}
public function onKernelReset( PreResetLegacyKernelEvent $event )
{
$event->getLegacyKernel()->runCallback(
function() {
eZINI::resetAllInstances();
},
true,
false
);
}
public function onConfigScopeChange( ScopeChangeEvent $event )
{
$this->resetKernelHandler();
}
public function onConsoleCommand( ConsoleCommandEvent $event )
{
$this->resetKernelHandler();
$this->eventDispatcher->addListener( MVCEvents::CONFIG_SCOPE_CHANGE, [ $this, 'onConfigScopeChange' ], -1 );
$this->eventDispatcher->addListener( MVCEvents::CONFIG_SCOPE_RESTORE, [ $this, 'onConfigScopeChange' ], -1 );
}
private function resetKernelHandler()
{
$legacyHandlerCLI = $this->container->get( 'ezpublish_legacy.kernel_handler.cli' );
$this->container->set( 'ezpublish_legacy.kernel.lazy', null );
$this->container->set( 'ezpublish_legacy.kernel_handler', $legacyHandlerCLI );
$this->container->set( 'ezpublish_legacy.kernel_handler.web', $legacyHandlerCLI );
}
}