Skip to content

Commit

Permalink
Merge pull request #54 from kiy0taka/next/dev/event-dispatcher
Browse files Browse the repository at this point in the history
Symfony/EventDispatcher
  • Loading branch information
chihiro-adachi authored Jul 5, 2023
2 parents c017ec3 + 03a29d9 commit 375a9ef
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 23 deletions.
4 changes: 4 additions & 0 deletions app/config/eccube/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,7 @@ services:

Eccube\Http\RequestHandler:
decorates: 'form.type_extension.form.request_handler'

Eccube\EventDispatcher\EventDispatcherDecorator:
decorates: event_dispatcher
decoration_priority: -10 # debug時にTraceableEventDispatcherをラップできるようにpriorityを下げる
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Eccube\Util\StringUtil;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpKernel\KernelEvents;
use Eccube\KernelEvents;

class TemplateController extends AbstractController
{
Expand Down
26 changes: 11 additions & 15 deletions src/application/Eccube/Controller/InstallPluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

use Eccube\Controller\Install\InstallController;
use Eccube\Entity\Plugin;
use Eccube\EventDispatcher\EventDispatcher;
use Eccube\Exception\PluginException;
use Eccube\Http\Exception\BadRequestHttpException;
use Eccube\Http\Exception\NotFoundHttpException;
use Eccube\Http\JsonResponse;
use Eccube\Http\RedirectResponse;
use Eccube\Http\Request;
use Eccube\Repository\PluginRepository;
use Eccube\Routing\Annotation\Route;
use Eccube\Service\Composer\ComposerApiService;
use Eccube\Service\PluginService;
use Eccube\Service\SystemService;
use Eccube\Util\CacheUtil;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;
use Eccube\Http\JsonResponse;
use Eccube\Http\RedirectResponse;
use Eccube\Http\Request;
use Eccube\Http\Exception\BadRequestHttpException;
use Eccube\Http\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Eccube\KernelEvents;

class InstallPluginController extends InstallController
{
Expand All @@ -39,14 +39,10 @@ class InstallPluginController extends InstallController
/** @var PluginRepository */
protected $pluginReposigoty;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

public function __construct(CacheUtil $cacheUtil, PluginRepository $pluginRespository, EventDispatcherInterface $eventDispatcher)
public function __construct(CacheUtil $cacheUtil, PluginRepository $pluginRespository)
{
$this->cacheUtil = $cacheUtil;
$this->pluginReposigoty = $pluginRespository;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -85,15 +81,15 @@ public function plugins(Request $request)
* @param SystemService $systemService
* @param PluginService $pluginService
* @param string $code
* @param EventDispatcherInterface $dispatcher
* @param EventDispatcher $dispatcher
*
* @return JsonResponse
*
* @throws BadRequestHttpException
* @throws NotFoundHttpException
* @throws PluginException
*/
public function pluginEnable(Request $request, SystemService $systemService, PluginService $pluginService, $code, EventDispatcherInterface $dispatcher)
public function pluginEnable(Request $request, SystemService $systemService, PluginService $pluginService, $code, EventDispatcher $dispatcher)
{
if (!$request->isXmlHttpRequest()) {
throw new BadRequestHttpException();
Expand Down Expand Up @@ -187,7 +183,7 @@ public function isValidTransaction($token)
*
* @Route("/install/plugin/check_api", name="install_plugin_check_api", methods={"PUT"})
*/
public function checkWebApiRequirements(Request $request, ComposerApiService $composerApiService, EventDispatcherInterface $dispatcher)
public function checkWebApiRequirements(Request $request, ComposerApiService $composerApiService, EventDispatcher $dispatcher)
{
if (!$request->isXmlHttpRequest()) {
throw new BadRequestHttpException();
Expand Down
7 changes: 3 additions & 4 deletions src/application/Eccube/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
use Eccube\Event\EventArgs;
use Eccube\Mailer\Exception\TransportException;
use Eccube\Mailer\Mailer;
use Eccube\EventDispatcher\EventDispatcher;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\MailHistoryRepository;
use Eccube\Repository\MailTemplateRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Eccube\Mime\Address;
use Eccube\Mime\Email;

Expand Down Expand Up @@ -81,7 +80,7 @@ class MailService
* @param MailTemplateRepository $mailTemplateRepository
* @param MailHistoryRepository $mailHistoryRepository
* @param BaseInfoRepository $baseInfoRepository
* @param EventDispatcherInterface $eventDispatcher
* @param EventDispatcher $eventDispatcher
* @param \Twig\Environment $twig
* @param EccubeConfig $eccubeConfig
* @param ContainerInterface $container
Expand All @@ -91,7 +90,7 @@ public function __construct(
MailTemplateRepository $mailTemplateRepository,
MailHistoryRepository $mailHistoryRepository,
BaseInfoRepository $baseInfoRepository,
EventDispatcherInterface $eventDispatcher,
EventDispatcher $eventDispatcher,
\Twig\Environment $twig,
EccubeConfig $eccubeConfig,
ContainerInterface $container
Expand Down
7 changes: 4 additions & 3 deletions src/application/Eccube/Service/SystemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

namespace Eccube\Service;

use Eccube\EventDispatcher\Event;
use Eccube\EventDispatcher\EventSubscriberInterface;
use Eccube\ORM\EntityManager;
use Eccube\Util\StringUtil;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Eccube\KernelEvents;
use function explode;
use function file_exists;
use function file_get_contents;
Expand Down Expand Up @@ -141,7 +142,7 @@ public function getMaintenanceToken(): ?string
/**
* KernelEvents::TERMINATE で設定されるEvent
*/
public function disableMaintenanceEvent(TerminateEvent $event)
public function disableMaintenanceEvent(Event $event)
{
if ($this->disableMaintenanceAfterResponse) {
$this->switchMaintenance(false, $this->maintenanceMode);
Expand Down
31 changes: 31 additions & 0 deletions src/framework/Eccube/EventDispatcher/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\EventDispatcher;

class Event
{
private object $event;

public function __construct(object $event)
{
$this->event = $event;
}

/**
* @return object
*/
public function getEvent(): object
{
return $this->event;
}
}
35 changes: 35 additions & 0 deletions src/framework/Eccube/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\EventDispatcher;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EventDispatcher
{
private EventDispatcherInterface $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

public function dispatch(object $event, string $eventName = null): object
{
return $this->eventDispatcher->dispatch($event, $eventName);
}

public function addListener(string $eventName, $listener, int $priority = 0): void
{
$this->eventDispatcher->addListener($eventName, $listener, $priority);
}
}
36 changes: 36 additions & 0 deletions src/framework/Eccube/EventDispatcher/EventDispatcherDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\EventDispatcher;

use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class EventDispatcherDecorator extends EventDispatcher
{
protected function callListeners(iterable $listeners, string $eventName, object $event)
{
$stoppable = $event instanceof StoppableEventInterface;

foreach ($listeners as $listener) {
if ($stoppable && $event->isPropagationStopped()) {
break;
}
if (is_array($listener) && $listener[0] instanceof EventSubscriberInterface) {
$listener(new Event($event), $eventName, $this);
continue;
}
$listener($event, $eventName, $this);
}
}

}
18 changes: 18 additions & 0 deletions src/framework/Eccube/EventDispatcher/EventSubscriberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\EventDispatcher;

interface EventSubscriberInterface extends \Symfony\Component\EventDispatcher\EventSubscriberInterface
{

}
27 changes: 27 additions & 0 deletions src/framework/Eccube/KernelEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube;

use Symfony\Component\HttpKernel\KernelEvents as SymfonyKernelEvents;

class KernelEvents
{
public const REQUEST = SymfonyKernelEvents::REQUEST;
public const EXCEPTION = SymfonyKernelEvents::EXCEPTION;
public const CONTROLLER = SymfonyKernelEvents::CONTROLLER;
public const CONTROLLER_ARGUMENTS = SymfonyKernelEvents::CONTROLLER_ARGUMENTS;
public const VIEW = SymfonyKernelEvents::VIEW;
public const RESPONSE = SymfonyKernelEvents::RESPONSE;
public const FINISH_REQUEST = SymfonyKernelEvents::FINISH_REQUEST;
public const TERMINATE = SymfonyKernelEvents::TERMINATE;
}

0 comments on commit 375a9ef

Please sign in to comment.