From: Tim Otten Date: Fri, 3 Mar 2017 22:38:20 +0000 (-0800) Subject: CRM-19813 - CRM_Utils_Hook - Cleanup existing dual-emit events X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c73e3098ad04a4c1a96870614022bcac68d3b663;p=civicrm-core.git 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. --- diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index dec10d814a..aa2f52707c 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -330,10 +330,8 @@ abstract class CRM_Utils_Hook { */ 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 @@ abstract class CRM_Utils_Hook { */ 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 @@ abstract class CRM_Utils_Hook { * 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 @@ abstract class CRM_Utils_Hook { */ 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 4e016f47bd..00feba2c01 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 @@ class CaseChangeEvent extends \Symfony\Component\EventDispatcher\Event { $this->analyzer = $analyzer; } + /** + * @inheritDoc + */ + public function getHookValues() { + return array($this->analyzer); + } + } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index cf59e2c58a..c385d98746 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -244,6 +244,8 @@ class 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 @@ class 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 d4c99024de..dfbce6582c 100644 --- a/Civi/Core/Event/PostEvent.php +++ b/Civi/Core/Event/PostEvent.php @@ -31,7 +31,18 @@ namespace Civi\Core\Event; * 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 a2f7dc138e..af01c623c4 100644 --- a/Civi/Core/Event/PreEvent.php +++ b/Civi/Core/Event/PreEvent.php @@ -31,7 +31,18 @@ namespace Civi\Core\Event; * 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 d58eed3df1..ab3722d0fa 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 @@ class UnhandledExceptionEvent extends \Symfony\Component\EventDispatcher\Event { $this->exception = $e; } + /** + * @inheritDoc + */ + public function getHookValues() { + return array($this->exception, $this->request); + } + }