From c73e3098ad04a4c1a96870614022bcac68d3b663 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 3 Mar 2017 14:38:20 -0800 Subject: [PATCH] CRM-19813 - CRM_Utils_Hook - Cleanup existing dual-emit events There are a handful of events which have been dual-emitted by explicitly calling both the dispatcher and Hook::invoke(). The dispatcher now calls Hook::invoke automatically (if applicable), so we can omit that. --- CRM/Utils/Hook.php | 29 +++++---------------- Civi/CCase/Event/CaseChangeEvent.php | 10 ++++++- Civi/Core/Container.php | 4 ++- Civi/Core/Event/PostEvent.php | 24 ++++++++++++++--- Civi/Core/Event/PreEvent.php | 24 ++++++++++++++--- Civi/Core/Event/UnhandledExceptionEvent.php | 11 ++++++-- 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index dec10d814a75..aa2f52707c09 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -330,10 +330,8 @@ public function requireCiviModules(&$moduleList) { */ public static function pre($op, $objectName, $id, &$params) { $event = new \Civi\Core\Event\PreEvent($op, $objectName, $id, $params); - \Civi::service('dispatcher')->dispatch("hook_civicrm_pre", $event); - \Civi::service('dispatcher')->dispatch("hook_civicrm_pre::$objectName", $event); - return self::singleton() - ->invoke(4, $op, $objectName, $id, $params, self::$_nullObject, self::$_nullObject, 'civicrm_pre'); + \Civi::dispatcher()->dispatch('hook_civicrm_pre', $event); + return $event->getReturnValues(); } /** @@ -354,10 +352,8 @@ public static function pre($op, $objectName, $id, &$params) { */ public static function post($op, $objectName, $objectId, &$objectRef = NULL) { $event = new \Civi\Core\Event\PostEvent($op, $objectName, $objectId, $objectRef); - \Civi::service('dispatcher')->dispatch("hook_civicrm_post", $event); - \Civi::service('dispatcher')->dispatch("hook_civicrm_post::$objectName", $event); - return self::singleton() - ->invoke(4, $op, $objectName, $objectId, $objectRef, self::$_nullObject, self::$_nullObject, 'civicrm_post'); + \Civi::dispatcher()->dispatch('hook_civicrm_post', $event); + return $event->getReturnValues(); } /** @@ -1806,16 +1802,8 @@ public static function permission_check($permission, &$granted) { * Reserved for future use. */ public static function unhandledException($exception, $request = NULL) { - self::singleton() - ->invoke(2, $exception, $request, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_unhandled_exception'); - // == 4.4 == - // $event = new stdClass(); - // $event->exception = $exception; - // CRM_Core_LegacyErrorHandler::handleException($event); - - // == 4.5+ == $event = new \Civi\Core\Event\UnhandledExceptionEvent($exception, self::$_nullObject); - \Civi::service('dispatcher')->dispatch("hook_civicrm_unhandled_exception", $event); + \Civi::dispatcher()->dispatch('hook_civicrm_unhandled_exception', $event); } /** @@ -2087,12 +2075,7 @@ public static function angularModules(&$angularModules) { */ public static function caseChange(\Civi\CCase\Analyzer $analyzer) { $event = new \Civi\CCase\Event\CaseChangeEvent($analyzer); - \Civi::service('dispatcher')->dispatch("hook_civicrm_caseChange", $event); - - self::singleton()->invoke(1, $analyzer, - self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, - 'civicrm_caseChange' - ); + \Civi::dispatcher()->dispatch('hook_civicrm_caseChange', $event); } /** diff --git a/Civi/CCase/Event/CaseChangeEvent.php b/Civi/CCase/Event/CaseChangeEvent.php index 4e016f47bdd0..00feba2c01d6 100644 --- a/Civi/CCase/Event/CaseChangeEvent.php +++ b/Civi/CCase/Event/CaseChangeEvent.php @@ -26,12 +26,13 @@ */ namespace Civi\CCase\Event; +use Civi\Core\Event\GenericHookEvent; /** * Class CaseChangeEvent * @package Civi\API\Event */ -class CaseChangeEvent extends \Symfony\Component\EventDispatcher\Event { +class CaseChangeEvent extends GenericHookEvent { /** * @var \Civi\CCase\Analyzer */ @@ -44,4 +45,11 @@ public function __construct($analyzer) { $this->analyzer = $analyzer; } + /** + * @inheritDoc + */ + public function getHookValues() { + return array($this->analyzer); + } + } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index cf59e2c58afc..c385d9874653 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -244,6 +244,8 @@ public function createEventDispatcher($container) { $dispatcher = new CiviEventDispatcher($container); $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); + $dispatcher->addListener('hook_civicrm_pre', array('\Civi\Core\Event\PreEvent', 'dispatchSubevent'), 100); + $dispatcher->addListener('hook_civicrm_post', array('\Civi\Core\Event\PostEvent', 'dispatchSubevent'), 100); $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\Events', 'delegateToXmlListeners')); @@ -254,7 +256,7 @@ public function createEventDispatcher($container) { $dispatcher->addListener('hook_civicrm_unhandled_exception', array( 'CRM_Core_LegacyErrorHandler', 'handleException', - )); + ), -200); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Activity_ActionMapping', 'onRegisterActionMappings')); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contact_ActionMapping', 'onRegisterActionMappings')); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings')); diff --git a/Civi/Core/Event/PostEvent.php b/Civi/Core/Event/PostEvent.php index d4c99024de47..dfbce6582c7c 100644 --- a/Civi/Core/Event/PostEvent.php +++ b/Civi/Core/Event/PostEvent.php @@ -31,7 +31,18 @@ * Class AuthorizeEvent * @package Civi\API\Event */ -class PostEvent extends \Symfony\Component\EventDispatcher\Event { +class PostEvent extends GenericHookEvent { + + /** + * This adapter automatically emits a narrower event. + * + * For example, `hook_civicrm_pre(Contact, ...)` will also dispatch `hook_civicrm_pre::Contact`. + * + * @param \Civi\Core\Event\PostEvent $event + */ + public static function dispatchSubevent(PostEvent $event) { + \Civi::service('dispatcher')->dispatch("hook_civicrm_post::" . $event->entity, $event); + } /** * @var string 'create'|'edit'|'delete' etc @@ -59,11 +70,18 @@ class PostEvent extends \Symfony\Component\EventDispatcher\Event { * @param $id * @param $object */ - public function __construct($action, $entity, $id, $object) { + public function __construct($action, $entity, $id, &$object) { $this->action = $action; $this->entity = $entity; $this->id = $id; - $this->object = $object; + $this->object = &$object; + } + + /** + * @inheritDoc + */ + public function getHookValues() { + return array($this->action, $this->entity, $this->id, &$this->object); } } diff --git a/Civi/Core/Event/PreEvent.php b/Civi/Core/Event/PreEvent.php index a2f7dc138e0b..af01c623c493 100644 --- a/Civi/Core/Event/PreEvent.php +++ b/Civi/Core/Event/PreEvent.php @@ -31,7 +31,18 @@ * Class AuthorizeEvent * @package Civi\API\Event */ -class PreEvent extends \Symfony\Component\EventDispatcher\Event { +class PreEvent extends GenericHookEvent { + + /** + * This adapter automatically emits a narrower event. + * + * For example, `hook_civicrm_pre(Contact, ...)` will also dispatch `hook_civicrm_pre::Contact`. + * + * @param \Civi\Core\Event\PreEvent $event + */ + public static function dispatchSubevent(PreEvent $event) { + \Civi::service('dispatcher')->dispatch("hook_civicrm_pre::" . $event->entity, $event); + } /** * @var string 'create'|'edit'|'delete' etc @@ -59,11 +70,18 @@ class PreEvent extends \Symfony\Component\EventDispatcher\Event { * @param $id * @param $params */ - public function __construct($action, $entity, $id, $params) { + public function __construct($action, $entity, $id, &$params) { $this->action = $action; $this->entity = $entity; $this->id = $id; - $this->params = $params; + $this->params = &$params; + } + + /** + * @inheritDoc + */ + public function getHookValues() { + return array($this->action, $this->entity, $this->id, &$this->params); } } diff --git a/Civi/Core/Event/UnhandledExceptionEvent.php b/Civi/Core/Event/UnhandledExceptionEvent.php index d58eed3df1de..ab3722d0fafd 100644 --- a/Civi/Core/Event/UnhandledExceptionEvent.php +++ b/Civi/Core/Event/UnhandledExceptionEvent.php @@ -28,10 +28,10 @@ namespace Civi\Core\Event; /** - * Class AuthorizeEvent + * Class UnhandledExceptionEvent * @package Civi\API\Event */ -class UnhandledExceptionEvent extends \Symfony\Component\EventDispatcher\Event { +class UnhandledExceptionEvent extends GenericHookEvent { /** * @var \Exception @@ -52,4 +52,11 @@ public function __construct($e, $request) { $this->exception = $e; } + /** + * @inheritDoc + */ + public function getHookValues() { + return array($this->exception, $this->request); + } + }