Skip to content

Commit

Permalink
[REF] Make instantiation of dispatcher consistent.
Browse files Browse the repository at this point in the history
We have 3 ways of iniatiging a dispatcher

Civi\Core\Container::singleton()->get('dispatcher')
Civi::service('dispatcher')
Civi::dispatcher()

Tim confirms there is no functional difference, so this consolidates on the shortest one :-)
  • Loading branch information
eileenmcnaughton committed Apr 23, 2020
1 parent 5fa8d81 commit 4162ab6
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 20 deletions.
8 changes: 5 additions & 3 deletions CRM/Core/BAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
/**
* @param array $filters
* Filter by property (e.g. 'id').
*
* @return array
* Array(scalar $id => Mapping $mapping).
* @throws \CRM_Core_Exception
*/
public static function getMappings($filters = NULL) {
static $_action_mapping;

if ($_action_mapping === NULL) {
$event = \Civi\Core\Container::singleton()->get('dispatcher')
$event = \Civi::dispatcher()
->dispatch(\Civi\ActionSchedule\Events::MAPPINGS,
new \Civi\ActionSchedule\Event\MappingRegisterEvent());
$_action_mapping = $event->getMappings();
Expand Down Expand Up @@ -507,7 +509,7 @@ protected static function prepareMailingQuery($mapping, $actionSchedule) {
$select->where("e.id = reminder.entity_id OR reminder.entity_table = 'civicrm_contact'");
}

\Civi\Core\Container::singleton()->get('dispatcher')
\Civi::dispatcher()
->dispatch(
\Civi\ActionSchedule\Events::MAILING_QUERY,
new \Civi\ActionSchedule\Event\MailingQueryEvent($actionSchedule, $mapping, $select)
Expand Down Expand Up @@ -644,7 +646,7 @@ protected static function sendReminderEmail($tokenRow, $schedule, $toContactID)
* @return \Civi\Token\TokenProcessor
*/
protected static function createTokenProcessor($schedule, $mapping) {
$tp = new \Civi\Token\TokenProcessor(\Civi\Core\Container::singleton()->get('dispatcher'), [
$tp = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [
'controller' => __CLASS__,
'actionSchedule' => $schedule,
'actionMapping' => $mapping,
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public function handleFirstRun() {
}

// OK, this looks new.
Civi::service('dispatcher')->dispatch(\Civi\Core\Event\SystemInstallEvent::EVENT_NAME, new \Civi\Core\Event\SystemInstallEvent());
Civi::dispatcher()->dispatch(\Civi\Core\Event\SystemInstallEvent::EVENT_NAME, new \Civi\Core\Event\SystemInstallEvent());
Civi::settings()->set('installed', 1);
}

Expand Down
12 changes: 6 additions & 6 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,28 +545,28 @@ public function save($hook = TRUE) {
if (!empty($this->id)) {
if ($hook) {
$preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
\Civi::service('dispatcher')->dispatch("civi.dao.preUpdate", $preEvent);
\Civi::dispatcher()->dispatch("civi.dao.preUpdate", $preEvent);
}

$result = $this->update();

if ($hook) {
$event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
\Civi::service('dispatcher')->dispatch("civi.dao.postUpdate", $event);
\Civi::dispatcher()->dispatch("civi.dao.postUpdate", $event);
}
$this->clearDbColumnValueCache();
}
else {
if ($hook) {
$preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
\Civi::service('dispatcher')->dispatch("civi.dao.preInsert", $preEvent);
\Civi::dispatcher()->dispatch("civi.dao.preInsert", $preEvent);
}

$result = $this->insert();

if ($hook) {
$event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
\Civi::service('dispatcher')->dispatch("civi.dao.postInsert", $event);
\Civi::dispatcher()->dispatch("civi.dao.postInsert", $event);
}
}
$this->free();
Expand Down Expand Up @@ -605,12 +605,12 @@ public function save($hook = TRUE) {
*/
public function delete($useWhere = FALSE) {
$preEvent = new \Civi\Core\DAO\Event\PreDelete($this);
\Civi::service('dispatcher')->dispatch("civi.dao.preDelete", $preEvent);
\Civi::dispatcher()->dispatch("civi.dao.preDelete", $preEvent);

$result = parent::delete($useWhere);

$event = new \Civi\Core\DAO\Event\PostDelete($this, $result);
\Civi::service('dispatcher')->dispatch("civi.dao.postDelete", $event);
\Civi::dispatcher()->dispatch("civi.dao.postDelete", $event);
$this->free();

$this->clearDbColumnValueCache();
Expand Down
2 changes: 1 addition & 1 deletion CRM/Cxn/ApiRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function route($cxn, $entity, $action, $params) {
}

$whitelist = \Civi\API\WhitelistRule::createAll($cxn['perm']['api']);
\Civi::service('dispatcher')
\Civi::dispatcher()
->addSubscriber(new \Civi\API\Subscriber\WhitelistSubscriber($whitelist));
CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
if ($cxn['perm']['grant'] === '*') {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static function externUrl($path = NULL, $query = NULL, $fragment = NULL,
'absolute' => $absolute,
'isSSL' => $isSSL,
]);
Civi::service('dispatcher')->dispatch('hook_civicrm_alterExternUrl', $event);
Civi::dispatcher()->dispatch('hook_civicrm_alterExternUrl', $event);
return urldecode(CRM_Utils_Url::unparseUrl($event->url));
}

Expand Down
2 changes: 1 addition & 1 deletion Civi/Core/Event/GenericHookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* $event->bang->objProperty = 'abcd';
*
* // Dispatching an event.
* Civi::service('dispatcher')->dispatch('hook_civicrm_foo', $event);
* Civi::dispatcher()->dispatch('hook_civicrm_foo', $event);
* @endCode
*
* Design Discussion:
Expand Down
2 changes: 1 addition & 1 deletion Civi/Core/Event/PostEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PostEvent extends GenericHookEvent {
* @param \Civi\Core\Event\PostEvent $event
*/
public static function dispatchSubevent(PostEvent $event) {
\Civi::service('dispatcher')->dispatch("hook_civicrm_post::" . $event->entity, $event);
\Civi::dispatcher()->dispatch("hook_civicrm_post::" . $event->entity, $event);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Civi/Core/Event/PreEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PreEvent extends GenericHookEvent {
* @param \Civi\Core\Event\PreEvent $event
*/
public static function dispatchSubevent(PreEvent $event) {
\Civi::service('dispatcher')->dispatch("hook_civicrm_pre::" . $event->entity, $event);
\Civi::dispatcher()->dispatch("hook_civicrm_pre::" . $event->entity, $event);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/CRM/Mailing/TokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testTokensWithMailingId($inputTemplateFormat, $inputTemplate, $e
]);
$contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');

$p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), [
$p = new \Civi\Token\TokenProcessor(Civi::dispatcher(), [
'mailingId' => $mailing->id,
]);
$p->addMessage('example', $inputTemplate, $inputTemplateFormat);
Expand Down Expand Up @@ -80,7 +80,7 @@ public function testTokensWithMailingObject() {
]);
$contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');

$p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), [
$p = new \Civi\Token\TokenProcessor(Civi::dispatcher(), [
'mailing' => $mailing,
]);
$p->addMessage('example', $inputTemplate, $inputTemplateFormat);
Expand Down Expand Up @@ -126,7 +126,7 @@ public function testTokensWithoutMailingJob($inputTemplateFormat, $inputTemplate
]);
$contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');

$p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), [
$p = new \Civi\Token\TokenProcessor(Civi::dispatcher(), [
'mailing' => $mailing,
]);
$p->addMessage('example', $inputTemplateText, $inputTemplateFormat);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Utils/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testExternUrl() {
* @dataProvider getExternURLs
*/
public function testAlterExternUrlHook($path, $expected) {
Civi::service('dispatcher')->addListener('hook_civicrm_alterExternUrl', [$this, 'hook_civicrm_alterExternUrl']);
Civi::dispatcher()->addListener('hook_civicrm_alterExternUrl', [$this, 'hook_civicrm_alterExternUrl']);
$externUrl = CRM_Utils_System::externUrl($path, $expected['query']);
$this->assertContains('path/altered/by/hook', $externUrl, 'Hook failed to alter URL path');
$this->assertContains($expected['query'] . '&thisWas=alteredByHook', $externUrl, 'Hook failed to alter URL query');
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/Civi/Core/Event/GenericHookEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testConstructOrdered() {
public function testDispatch() {
\CRM_Utils_Hook::singleton()->setHook('civicrm_ghet',
[$this, 'hook_civicrm_ghet']);
\Civi::service('dispatcher')->addListener('hook_civicrm_ghet',
\Civi::dispatcher()->addListener('hook_civicrm_ghet',
[$this, 'onGhet']);

$roString = 'readonly';
Expand Down

0 comments on commit 4162ab6

Please sign in to comment.