Skip to content

Instantly share code, notes, and snippets.

@izayoi256
Created November 9, 2020 08:34
Show Gist options
  • Save izayoi256/68f71a662f8e56ca0b183e4421a85071 to your computer and use it in GitHub Desktop.
Save izayoi256/68f71a662f8e56ca0b183e4421a85071 to your computer and use it in GitHub Desktop.
[EC-CUBE4] イベントリスナーを責務で切り分ける

なにも1プラグインにつき1イベントリスナーしか作っちゃいけないなんて道理はないんです。

会員管理画面に項目を追加する処理と、ブラック会員登録した会員をログアウトとリダイレクトさせる処理を同じクラス内に記述する必要なくないですか?

なんでもかんでも1クラスにまとめて1000行とかになったコードなんて読むの大変じゃないですか?

機能ごとに切り分けましょう。

責務とは

これ読んで下さい。

https://gist.github.com/izayoi256/f70f860a8dbe29ceb0b2a1af2643b12f

<?php
namespace Customize\EventListener;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class AddCustomerEditColumnEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'@admin/Customer/edit.twig' => 'onAdminCustomerEditTwig',
EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE => 'onAdminCustomerEditIndexComplete',
];
}
public function onAdminCustomerEditTwig(TemplateEvent $event): void
{
$event->addSnippet('会員編集画面をカスタマイズするスニペット');
}
public function onAdminCustomerEditIndexComplete(EventArgs $event): void
{
// 関連するイベントであれば1クラスに複数書いてまとめる
}
}
<?php
namespace Customize\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class BlockCustomerEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onRequest',
];
}
public function onRequest(GetResponseEvent $event): void
{
// ブラックリスト入り会員の場合はログアウトさせてリダイレクトする。
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment