From 1baeaf6ea7df5a8a7108ab5497c2b440d9161b27 Mon Sep 17 00:00:00 2001 From: ryuring Date: Fri, 15 Nov 2024 17:54:49 +0900 Subject: [PATCH] =?UTF-8?q?PagesTable=20=E7=AD=89=E3=81=AE=20beforeSave=20?= =?UTF-8?q?=E3=82=84=E3=80=81afterSave=20=E3=81=8C=E5=8B=95=E4=BD=9C?= =?UTF-8?q?=E3=81=97=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92=E6=94=B9?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/baser-core/src/Utility/BcUtil.php | 39 ++++++++++--- .../tests/TestCase/Utility/BcUtilTest.php | 56 +++++++++---------- 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/plugins/baser-core/src/Utility/BcUtil.php b/plugins/baser-core/src/Utility/BcUtil.php index 2aaaf6dae8..2d82b7e56d 100644 --- a/plugins/baser-core/src/Utility/BcUtil.php +++ b/plugins/baser-core/src/Utility/BcUtil.php @@ -1641,12 +1641,23 @@ public static function fgetcsvReg(&$handle, $length = null, $d = ',', $e = '"') */ public static function offEvent(EventManagerInterface $eventManager, string $eventKey) { - $eventListeners = $eventManager->listeners($eventKey); $globalEventManager = $eventManager->instance(); - if ($eventListeners) { - foreach($eventListeners as $eventListener) { - $eventManager->off($eventKey, $eventListener['callable']); - $globalEventManager->off($eventKey, $eventListener['callable']); + $eventListeners = [ + 'local' => $eventManager->prioritisedListeners($eventKey), + 'global' => $globalEventManager->prioritisedListeners($eventKey) + ]; + if ($eventListeners['local']) { + foreach($eventListeners['local'] as $listeners) { + foreach($listeners as $listener) { + $eventManager->off($eventKey, $listener['callable']); + } + } + } + if ($eventListeners['global']) { + foreach($eventListeners['global'] as $listeners) { + foreach($listeners as $listener) { + $globalEventManager->off($eventKey, $listener['callable']); + } } } return $eventListeners; @@ -1656,16 +1667,26 @@ public static function offEvent(EventManagerInterface $eventManager, string $eve * イベントをオンにする * @param EventManagerInterface $eventManager * @param string $eventKey - * @param EventListenerInterface[] $eventListeners + * @param array $eventListeners * @checked * @noTodo * @unitTest */ public static function onEvent(EventManagerInterface $eventManager, string $eventKey, array $eventListeners) { - if ($eventListeners) { - foreach($eventListeners as $eventListener) { - $eventManager->on($eventKey, $eventListener['callable']); + $globalEventManager = $eventManager->instance(); + if (!empty($eventListeners['local'])) { + foreach($eventListeners['local'] as $priority => $listeners) { + foreach($listeners as $listener) { + $eventManager->on($eventKey, ['priority' => $priority], $listener['callable']); + } + } + } + if (!empty($eventListeners['global'])) { + foreach($eventListeners['global'] as $priority => $listeners) { + foreach($listeners as $listener) { + $globalEventManager->on($eventKey, ['priority' => $priority], $listener['callable']); + } } } } diff --git a/plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php b/plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php index 02095c45a9..4cf51abfe6 100644 --- a/plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php +++ b/plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php @@ -1204,36 +1204,32 @@ public static function fgetcsvRegDataProvider() */ public function testOnEventOffEvent(): void { - //offEventをテスト - $eventManager = $this->getTableLocator()->get('BcBlog.BlogPosts')->getEventManager(); - $beforeSaveListeners = BcUtil::offEvent($eventManager, 'Model.beforeMarshal'); - //戻り値を確認 - $this->assertCount(1, $beforeSaveListeners); - //eventがOFFしたかどうか確認 - $listeners = $eventManager->listeners('Model.beforeMarshal'); - $this->assertCount(0, $listeners); - - //onEventをテスト - BcUtil::onEvent($eventManager, 'Model.beforeMarshal', $beforeSaveListeners); - //eventがONしたかどうか確認 - $listeners = $eventManager->listeners('Model.beforeMarshal'); - $this->assertCount(1, $listeners); - $this->assertEquals($beforeSaveListeners, $listeners); - } - - /** - * test retry - */ - public function testRetry() - { - //正常実行 - $rs = BcUtil::retry(1, function () {return 2;}, 3); - $this->assertEquals(2, $rs); - - //異常実行 - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('リトライ回数は正の整数値で指定してください。'); - BcUtil::retry(-1, function () {return 0;}, 1); + $eventManager = EventManager::instance(); + $eventKey = 'testOnEvent'; + $bcEvenListener = new class extends BcEventListener { + public $events = ['event1']; + public $layer = 'Controller'; + public function event1() { + return 'event1'; + } + }; + // onEvent() でイベントを設定 + BcUtil::onEvent($eventManager, $eventKey, [ + 'local' => [ + 10 => [ + ['callable' => [$bcEvenListener, 'event1']] + ] + ] + ]); + // listeners() イベントの登録を確認 + $listeners = $eventManager->listeners($eventKey); + $this->assertIsCallable($listeners[0]['callable']); + + // offEvent() でイベントを解除 + BcUtil::offEvent($eventManager, $eventKey); + // listeners() イベントの解除を確認 + $listeners = $eventManager->listeners($eventKey); + $this->assertEmpty($listeners); } /**