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); } /**