From 60f955ab8262d56ad921aac0e210caf72ac15c67 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 30 May 2015 17:09:04 -0400 Subject: [PATCH] Add an interface for EventDispatcher. Having an interface will let us not duck type on the method and instead use `instanceof`. It also lets us make typehints elsewhere in CakePHP if we need to. Refs #6689 --- src/Controller/Controller.php | 3 +- src/Datasource/RulesAwareTrait.php | 3 +- src/Event/EventDispatcherInterface.php | 55 ++++++++++++++++++++++++++ src/ORM/Table.php | 3 +- src/Validation/ValidatorAwareTrait.php | 3 +- src/View/View.php | 3 +- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/Event/EventDispatcherInterface.php diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index 01f0618ff7d..39cfed10798 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -17,6 +17,7 @@ use Cake\Controller\Exception\MissingActionException; use Cake\Datasource\ModelAwareTrait; use Cake\Event\Event; +use Cake\Event\EventDispatcherInterface; use Cake\Event\EventListenerInterface; use Cake\Event\EventManagerTrait; use Cake\Log\LogTrait; @@ -80,7 +81,7 @@ * @property \Cake\Controller\Component\SecurityComponent $Security * @link http://book.cakephp.org/3.0/en/controllers.html */ -class Controller implements EventListenerInterface +class Controller implements EventListenerInterface, EventDispatcherInterface { use EventManagerTrait; diff --git a/src/Datasource/RulesAwareTrait.php b/src/Datasource/RulesAwareTrait.php index 9524ce89dfd..5613c32fd13 100644 --- a/src/Datasource/RulesAwareTrait.php +++ b/src/Datasource/RulesAwareTrait.php @@ -17,6 +17,7 @@ use ArrayObject; use Cake\Datasource\EntityInterface; use Cake\Datasource\RulesChecker; +use Cake\Event\EventDispatcherInterface; /** * A trait that allows a class to build and apply application. @@ -51,7 +52,7 @@ public function checkRules(EntityInterface $entity, $operation = RulesChecker::C $rules = $this->rulesChecker(); $options = $options ?: new ArrayObject; $options = is_array($options) ? new ArrayObject($options) : $options; - $hasEvents = method_exists($this, 'dispatchEvent'); + $hasEvents = ($this instanceof EventDispatcherInterface); if ($hasEvents) { $event = $this->dispatchEvent( diff --git a/src/Event/EventDispatcherInterface.php b/src/Event/EventDispatcherInterface.php new file mode 100644 index 00000000000..ba577738618 --- /dev/null +++ b/src/Event/EventDispatcherInterface.php @@ -0,0 +1,55 @@ +{'validation' . ucfirst($name)}($validator); - if (method_exists($this, 'dispatchEvent')) { + if ($this instanceof EventDispatcherInterface) { $this->dispatchEvent('Model.buildValidator', compact('validator', 'name')); } } diff --git a/src/View/View.php b/src/View/View.php index a1cf32ad29f..c7636be66e2 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -17,6 +17,7 @@ use Cake\Cache\Cache; use Cake\Core\App; use Cake\Core\Plugin; +use Cake\Event\EventDispatcherInterface; use Cake\Event\EventManager; use Cake\Event\EventManagerTrait; use Cake\Log\LogTrait; @@ -56,7 +57,7 @@ * @property \Cake\View\Helper\TimeHelper $Time * @property \Cake\View\ViewBlock $Blocks */ -class View +class View implements EventDispatcherInterface { use CellTrait;