Created
June 9, 2016 04:19
-
-
Save nojimage/5974027d544afb48323b7d9106e0a340 to your computer and use it in GitHub Desktop.
ビヘイビアが実行するイベントをOn/Offするやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Behavior; | |
use Cake\Event\EventDispatcherInterface; | |
use Cake\Event\EventListenerInterface; | |
/** | |
* ビヘイビアが実行するイベントをコントロール | |
*/ | |
trait BehaviorEventTrait | |
{ | |
/** | |
* ビヘイビアイベントの停止 | |
* | |
* @param string $name | |
* @param array $options | |
* @return void | |
*/ | |
public function stopBehaviorEvent($name, array $options = []) | |
{ | |
if (!$this->_behaviors->has($name)) { | |
return; | |
} | |
$behavior = $this->_behaviors->get($name); | |
if ($this->_behaviors instanceof EventDispatcherInterface && $behavior instanceof EventListenerInterface) { | |
$this->_behaviors->eventManager()->off($behavior); | |
} | |
} | |
/** | |
* ビヘイビアイベントの再開 | |
* | |
* @param string $name | |
* @param array $options | |
* @return void | |
*/ | |
public function restoreBehaviorEvent($name, array $options = []) | |
{ | |
if (!$this->_behaviors->has($name)) { | |
return; | |
} | |
$behavior = $this->_behaviors->get($name); | |
if ($this->_behaviors instanceof EventDispatcherInterface && $behavior instanceof EventListenerInterface) { | |
$this->_behaviors->eventManager()->on($behavior); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tableクラスでuse。
プラグイン化は後ほど。 #たぶん