Skip to content

Commit

Permalink
CRM-19813 - CRM_Utils_Hook - Cleanup existing dual-emit events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
totten committed Mar 30, 2017
1 parent 762dc04 commit c73e309
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
29 changes: 6 additions & 23 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion Civi/CCase/Event/CaseChangeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -44,4 +45,11 @@ public function __construct($analyzer) {
$this->analyzer = $analyzer;
}

/**
* @inheritDoc
*/
public function getHookValues() {
return array($this->analyzer);
}

}
4 changes: 3 additions & 1 deletion Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
Expand Down
24 changes: 21 additions & 3 deletions Civi/Core/Event/PostEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

}
24 changes: 21 additions & 3 deletions Civi/Core/Event/PreEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

}
11 changes: 9 additions & 2 deletions Civi/Core/Event/UnhandledExceptionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,4 +52,11 @@ public function __construct($e, $request) {
$this->exception = $e;
}

/**
* @inheritDoc
*/
public function getHookValues() {
return array($this->exception, $this->request);
}

}

0 comments on commit c73e309

Please sign in to comment.