Skip to content

Commit

Permalink
PagesTable 等の beforeSave や、afterSave が動作しない問題を改善
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 15, 2024
1 parent 990a3d5 commit 1baeaf6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 deletions.
39 changes: 30 additions & 9 deletions plugins/baser-core/src/Utility/BcUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']);
}
}
}
}
Expand Down
56 changes: 26 additions & 30 deletions plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 1baeaf6

Please sign in to comment.