dev/core#1744 - Civi/API - Simplify event naming
authorTim Otten <totten@civicrm.org>
Wed, 6 May 2020 02:37:40 +0000 (19:37 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 6 May 2020 03:06:56 +0000 (20:06 -0700)
25 files changed:
Civi/API/Event/AuthorizeEvent.php
Civi/API/Event/ExceptionEvent.php
Civi/API/Event/PrepareEvent.php
Civi/API/Event/ResolveEvent.php
Civi/API/Event/RespondEvent.php
Civi/API/Events.php
Civi/API/Kernel.php
Civi/API/Provider/AdhocProvider.php
Civi/API/Provider/MagicFunctionProvider.php
Civi/API/Provider/ReflectionProvider.php
Civi/API/Provider/StaticProvider.php
Civi/API/Subscriber/APIv3SchemaAdapter.php
Civi/API/Subscriber/ChainSubscriber.php
Civi/API/Subscriber/DebugSubscriber.php
Civi/API/Subscriber/DynamicFKAuthorization.php
Civi/API/Subscriber/I18nSubscriber.php
Civi/API/Subscriber/PermissionCheck.php
Civi/API/Subscriber/TransactionSubscriber.php
Civi/API/Subscriber/WhitelistSubscriber.php
Civi/API/Subscriber/WrapperAdapter.php
Civi/Api4/Event/Subscriber/Generic/AbstractPrepareSubscriber.php
Civi/Api4/Event/Subscriber/PermissionCheckSubscriber.php
Civi/Api4/Provider/ActionObjectProvider.php
tests/phpunit/Civi/API/Event/PrepareEventTest.php
tests/phpunit/Civi/API/KernelTest.php

index 82c63438247f6901459d31eae8e35ab852c78b4b..9d146a9b40f6824976ff62101ba3b02d02c9f526 100644 (file)
@@ -14,6 +14,12 @@ namespace Civi\API\Event;
 /**
  * Class AuthorizeEvent
  * @package Civi\API\Event
+ *
+ * Determine whether the API request is allowed for the current user.
+ * For successful execution, at least one listener must invoke
+ * $event->authorize().
+ *
+ * Event name: 'civi.api.authorize'
  */
 class AuthorizeEvent extends Event {
   /**
index 8860e51724ed28259184f1e87d8cf5a07a34156e..3b95ea539b1de53a5071d86dd5ba91f293bc3ebe 100644 (file)
@@ -12,8 +12,9 @@
 namespace Civi\API\Event;
 
 /**
- * Class ExceptionEvent
- * @package Civi\API\Event
+ * Handle any exceptions that occur while processing an API request.
+ *
+ * Event name: 'civi.api.exception'
  */
 class ExceptionEvent extends Event {
 
index 8fcf7fb134c50a6c06559c10eaa132c5bfbb5f64..1c6576173c89598a17b99a14ad5df2f345836ff6 100644 (file)
@@ -16,6 +16,10 @@ use Civi\API\Provider\WrappingProvider;
 /**
  * Class PrepareEvent
  * @package Civi\API\Event
+ *
+ * Apply any pre-execution filtering to the API request.
+ *
+ * Event name: 'civi.api.prepare'
  */
 class PrepareEvent extends Event {
 
index b0f6f01a31c815c3cafaf9dadd5cb7ff094854c1..8d574a0327829161e19e0a3e8abeaf45953fd6d9 100644 (file)
@@ -14,6 +14,12 @@ namespace Civi\API\Event;
 /**
  * Class ResolveEvent
  * @package Civi\API\Event
+ *
+ * Determine which API provider executes the given request. For successful
+ * execution, at least one listener must invoke
+ * $event->setProvider($provider).
+ *
+ * Event name: 'civi.api.resolve'
  */
 class ResolveEvent extends Event {
 
index 8f560b6109765ca71ddc9245028baf1056a5f456..462cc6edab22fc358ab79d0e6e3c4d23e93a3507 100644 (file)
@@ -14,6 +14,10 @@ namespace Civi\API\Event;
 /**
  * Class RespondEvent
  * @package Civi\API\Event
+ *
+ * Apply post-execution filtering to the API request/response.
+ *
+ * Event name: 'civi.api.respond'
  */
 class RespondEvent extends Event {
   /**
index ff6f4caa1d5ad2aea4def1c623365f1d041bc61f..6bbecb8f037f46d0362c7b5bb772280181a450e8 100644 (file)
@@ -22,27 +22,20 @@ namespace Civi\API;
 class Events {
 
   /**
-   * Determine whether the API request is allowed for the current user.
-   * For successful execution, at least one listener must invoke
-   * $event->authorize().
-   *
    * @see AuthorizeEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const AUTHORIZE = 'civi.api.authorize';
 
   /**
-   * Determine which API provider executes the given request. For successful
-   * execution, at least one listener must invoke
-   * $event->setProvider($provider).
-   *
    * @see ResolveEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const RESOLVE = 'civi.api.resolve';
 
   /**
-   * Apply pre-execution logic
-   *
    * @see PrepareEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const PREPARE = 'civi.api.prepare';
 
@@ -50,6 +43,7 @@ class Events {
    * Apply post-execution logic
    *
    * @see RespondEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const RESPOND = 'civi.api.respond';
 
@@ -57,6 +51,7 @@ class Events {
    * Handle any exceptions.
    *
    * @see ExceptionEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const EXCEPTION = 'civi.api.exception';
 
@@ -80,11 +75,11 @@ class Events {
    */
   public static function allEvents() {
     return [
-      self::AUTHORIZE,
-      self::EXCEPTION,
-      self::PREPARE,
-      self::RESOLVE,
-      self::RESPOND,
+      'civi.api.authorize',
+      'civi.api.exception',
+      'civi.api.prepare',
+      'civi.api.resolve',
+      'civi.api.respond',
     ];
   }
 
@@ -93,11 +88,11 @@ class Events {
    * @see \CRM_Utils_Hook::eventDefs
    */
   public static function hookEventDefs($e) {
-    $e->inspector->addEventClass(self::AUTHORIZE, 'Civi\API\Event\AuthorizeEvent');
-    $e->inspector->addEventClass(self::EXCEPTION, 'Civi\API\Event\ExceptionEvent');
-    $e->inspector->addEventClass(self::PREPARE, 'Civi\API\Event\PrepareEvent');
-    $e->inspector->addEventClass(self::RESOLVE, 'Civi\API\Event\ResolveEvent');
-    $e->inspector->addEventClass(self::RESPOND, 'Civi\API\Event\RespondEvent');
+    $e->inspector->addEventClass('civi.api.authorize', 'Civi\API\Event\AuthorizeEvent');
+    $e->inspector->addEventClass('civi.api.exception', 'Civi\API\Event\ExceptionEvent');
+    $e->inspector->addEventClass('civi.api.prepare', 'Civi\API\Event\PrepareEvent');
+    $e->inspector->addEventClass('civi.api.resolve', 'Civi\API\Event\ResolveEvent');
+    $e->inspector->addEventClass('civi.api.respond', 'Civi\API\Event\RespondEvent');
   }
 
 }
index 13e55c34455d07a4fc502bac3643e9e29fe4b852..feb8b662e45ce2fdb3dfc103e119c365fd769598 100644 (file)
@@ -83,7 +83,7 @@ class Kernel {
     }
     catch (\Exception $e) {
       if ($apiRequest) {
-        $this->dispatcher->dispatch(Events::EXCEPTION, new ExceptionEvent($e, NULL, $apiRequest, $this));
+        $this->dispatcher->dispatch('civi.api.exception', new ExceptionEvent($e, NULL, $apiRequest, $this));
       }
 
       if ($e instanceof \PEAR_Exception) {
@@ -197,7 +197,7 @@ class Kernel {
    */
   public function resolve($apiRequest) {
     /** @var \Civi\API\Event\ResolveEvent $resolveEvent */
-    $resolveEvent = $this->dispatcher->dispatch(Events::RESOLVE, new ResolveEvent($apiRequest, $this));
+    $resolveEvent = $this->dispatcher->dispatch('civi.api.resolve', new ResolveEvent($apiRequest, $this));
     $apiRequest = $resolveEvent->getApiRequest();
     if (!$resolveEvent->getApiProvider()) {
       throw new \Civi\API\Exception\NotImplementedException("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)");
@@ -216,7 +216,7 @@ class Kernel {
    */
   public function authorize($apiProvider, $apiRequest) {
     /** @var \Civi\API\Event\AuthorizeEvent $event */
-    $event = $this->dispatcher->dispatch(Events::AUTHORIZE, new AuthorizeEvent($apiProvider, $apiRequest, $this));
+    $event = $this->dispatcher->dispatch('civi.api.authorize', new AuthorizeEvent($apiProvider, $apiRequest, $this));
     if (!$event->isAuthorized()) {
       throw new \Civi\API\Exception\UnauthorizedException("Authorization failed");
     }
@@ -235,7 +235,7 @@ class Kernel {
    */
   public function prepare($apiProvider, $apiRequest) {
     /** @var \Civi\API\Event\PrepareEvent $event */
-    $event = $this->dispatcher->dispatch(Events::PREPARE, new PrepareEvent($apiProvider, $apiRequest, $this));
+    $event = $this->dispatcher->dispatch('civi.api.prepare', new PrepareEvent($apiProvider, $apiRequest, $this));
     return [$event->getApiProvider(), $event->getApiRequest()];
   }
 
@@ -253,7 +253,7 @@ class Kernel {
    */
   public function respond($apiProvider, $apiRequest, $result) {
     /** @var \Civi\API\Event\RespondEvent $event */
-    $event = $this->dispatcher->dispatch(Events::RESPOND, new RespondEvent($apiProvider, $apiRequest, $result, $this));
+    $event = $this->dispatcher->dispatch('civi.api.respond', new RespondEvent($apiProvider, $apiRequest, $result, $this));
     return $event->getResponse();
   }
 
index fcab72e1821f73283a499ebb38848438e4f8c661..f7855635a821437813d617169c5a395ad4f86db2 100644 (file)
@@ -27,10 +27,10 @@ class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
     // to override standard implementations -- which is
     // handy for testing/mocking.
     return [
-      Events::RESOLVE => [
+      'civi.api.resolve' => [
         ['onApiResolve', Events::W_EARLY],
       ],
-      Events::AUTHORIZE => [
+      'civi.api.authorize' => [
         ['onApiAuthorize', Events::W_EARLY],
       ],
     ];
index 8fab3ff19cabb41f1716b230bddae8b007147ea5..febcaa06226a4aa9c123874618526c47b6f643bf 100644 (file)
@@ -25,7 +25,7 @@ class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterfa
    */
   public static function getSubscribedEvents() {
     return [
-      Events::RESOLVE => [
+      'civi.api.resolve' => [
         ['onApiResolve', Events::W_MIDDLE],
       ],
     ];
index 87b95bb18b725924c67b58d751ccc953b5ed6b78..ac432b8fa310ce8b260e9ef9c602f83e3490fe33 100644 (file)
@@ -24,11 +24,11 @@ class ReflectionProvider implements EventSubscriberInterface, ProviderInterface
    */
   public static function getSubscribedEvents() {
     return [
-      Events::RESOLVE => [
+      'civi.api.resolve' => [
         // TODO decide if we really want to override others
         ['onApiResolve', Events::W_EARLY],
       ],
-      Events::AUTHORIZE => [
+      'civi.api.authorize' => [
         // TODO decide if we really want to override others
         ['onApiAuthorize', Events::W_EARLY],
       ],
index 31dc36c7a6eda21973f8e32e389b9518d9f0dbad..138d561b1fd5567742afe7b5bbe022f8727172dd 100644 (file)
@@ -29,10 +29,10 @@ class StaticProvider extends AdhocProvider {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::RESOLVE => [
+      'civi.api.resolve' => [
         ['onApiResolve', Events::W_MIDDLE],
       ],
-      Events::AUTHORIZE => [
+      'civi.api.authorize' => [
         ['onApiAuthorize', Events::W_MIDDLE],
       ],
     ];
index a2ebabd450009dc2824cffdb9af8c1f8ecbcee19..fa15949348b48accce8a44a6436e7900d617fce9 100644 (file)
@@ -25,7 +25,7 @@ class APIv3SchemaAdapter implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => [
+      'civi.api.prepare' => [
         ['onApiPrepare', Events::W_MIDDLE],
         ['onApiPrepare_validate', Events::W_LATE],
       ],
index 8834495633819bb8504a4be195e92b7b7440faf9..ff4f79256e1a23456f107aa9c9febf708240991a 100644 (file)
@@ -40,7 +40,7 @@ class ChainSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::RESPOND => ['onApiRespond', Events::W_EARLY],
+      'civi.api.respond' => ['onApiRespond', Events::W_EARLY],
     ];
   }
 
index 88f529f972e916ee85726dbc48100473f9bb351f..ac1abad89494c6daaf929c36db7a7cc8446f841b 100644 (file)
@@ -11,7 +11,6 @@
 
 namespace Civi\API\Subscriber;
 
-use Civi\API\Events;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 /**
@@ -30,8 +29,8 @@ class DebugSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => ['onApiPrepare', 999],
-      Events::RESPOND => ['onApiRespond', -999],
+      'civi.api.prepare' => ['onApiPrepare', 999],
+      'civi.api.respond' => ['onApiRespond', -999],
     ];
   }
 
index ecaf5ff428ecb8719090530fead9281366a65b5d..6a19cded035058c8a9d8ac931e7cfd27e95cda45 100644 (file)
@@ -36,7 +36,7 @@ class DynamicFKAuthorization implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::AUTHORIZE => [
+      'civi.api.authorize' => [
         ['onApiAuthorize', Events::W_EARLY],
       ],
     ];
index f89964c0b2a2e893587e973f91c69d2c76d2b286..d3b732972a2a17f59698a3a32d172858823edeaf 100644 (file)
@@ -32,8 +32,8 @@ class I18nSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE],
-      Events::RESPOND => ['onApiRespond', Events::W_LATE],
+      'civi.api.prepare' => ['onApiPrepare', Events::W_MIDDLE],
+      'civi.api.respond' => ['onApiRespond', Events::W_LATE],
     ];
   }
 
index bc2f18af6adbf880c98427db836b3d3b49ca06f4..9441800733ce01cd88b1c83877ebd66ee74217ff 100644 (file)
@@ -26,7 +26,7 @@ class PermissionCheck implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::AUTHORIZE => [
+      'civi.api.authorize' => [
         ['onApiAuthorize', Events::W_LATE],
       ],
     ];
index 8e7ff6a31bdb5df9da145dbfa68e177af45b0902..88f34f69b294369dfde2cf9db5b746e51f035dfb 100644 (file)
@@ -34,9 +34,9 @@ class TransactionSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => ['onApiPrepare', Events::W_EARLY],
-      Events::RESPOND => ['onApiRespond', Events::W_MIDDLE],
-      Events::EXCEPTION => ['onApiException', Events::W_EARLY],
+      'civi.api.prepare' => ['onApiPrepare', Events::W_EARLY],
+      'civi.api.respond' => ['onApiRespond', Events::W_MIDDLE],
+      'civi.api.exception' => ['onApiException', Events::W_EARLY],
     ];
   }
 
index 2efd8eef96d2da2217677a57121f404dcb40fccc..3a43128364bea010da09bb70c2e59843d1ca1860 100644 (file)
@@ -30,8 +30,8 @@ class WhitelistSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::AUTHORIZE => ['onApiAuthorize', Events::W_EARLY],
-      Events::RESPOND => ['onApiRespond', Events::W_MIDDLE],
+      'civi.api.authorize' => ['onApiAuthorize', Events::W_EARLY],
+      'civi.api.respond' => ['onApiRespond', Events::W_MIDDLE],
     ];
   }
 
index 6b90161e914aa92fd2a5769e66c763d88c1fa2e9..c7216731427fb662c704a59a87ba4dbfba7d803c 100644 (file)
@@ -26,8 +26,8 @@ class WrapperAdapter implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE],
-      Events::RESPOND => ['onApiRespond', Events::W_EARLY * 2],
+      'civi.api.prepare' => ['onApiPrepare', Events::W_MIDDLE],
+      'civi.api.respond' => ['onApiRespond', Events::W_EARLY * 2],
     ];
   }
 
index aae758b2a8129dd30ceb4c1012c5990f0a55128e..3835c70d28850af74b78d57a3130f1365778b887 100644 (file)
@@ -22,7 +22,6 @@
 namespace Civi\Api4\Event\Subscriber\Generic;
 
 use Civi\API\Event\PrepareEvent;
-use Civi\API\Events;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 abstract class AbstractPrepareSubscriber implements EventSubscriberInterface {
@@ -32,7 +31,7 @@ abstract class AbstractPrepareSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => 'onApiPrepare',
+      'civi.api.prepare' => 'onApiPrepare',
     ];
   }
 
index 500b0c6930f8b1e094d3668bcc776e2cb8747530..70fecac90789eb619b5976f3a131bfa8bad8a111 100644 (file)
@@ -26,7 +26,7 @@ class PermissionCheckSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::AUTHORIZE => [
+      'civi.api.authorize' => [
         ['onApiAuthorize', Events::W_LATE],
       ],
     ];
index b31c3d323f9ab997d181928bcf25cfbdb75563af..9fa607c65e3d3c3ee282f7ddf0985b256e99cc4f 100644 (file)
@@ -31,7 +31,7 @@ class ActionObjectProvider implements EventSubscriberInterface, ProviderInterfac
     // to override standard implementations -- which is
     // handy for testing/mocking.
     return [
-      Events::RESOLVE => [
+      'civi.api.resolve' => [
         ['onApiResolve', Events::W_EARLY],
       ],
     ];
index 258a7bf642126dd3ff1d666626df53b1a8d616a2..ce5516c98b28978861832c88f8e3fd86f9895961 100644 (file)
@@ -2,7 +2,6 @@
 namespace Civi\API\Event;
 
 use Symfony\Component\EventDispatcher\EventDispatcher;
-use Civi\API\Events;
 use Civi\API\Kernel;
 
 /**
@@ -45,7 +44,7 @@ class PrepareEventTest extends \CiviUnitTestCase {
    * @dataProvider getPrepareExamples
    */
   public function testOnPrepare($onPrepare, $inputApiCall, $expectResult) {
-    $this->dispatcher->addListener(Events::PREPARE, [$this, $onPrepare]);
+    $this->dispatcher->addListener('civi.api.prepare', [$this, $onPrepare]);
     $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
     $result = call_user_func_array([$this->kernel, 'runSafe'], $inputApiCall);
     $this->assertEquals($expectResult, $result['values']);
index 6d8829b01d652c0100b7d89a2b6493ccaa45c010..32ef294712ce4acf0719eb97477534b58dc272d7 100644 (file)
@@ -39,10 +39,10 @@ class KernelTest extends \CiviUnitTestCase {
     ]);
 
     $expectedEventSequence = [
-      ['name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'],
-      ['name' => Events::AUTHORIZE, 'class' => 'Civi\API\Event\AuthorizeEvent'],
-      ['name' => Events::PREPARE, 'class' => 'Civi\API\Event\PrepareEvent'],
-      ['name' => Events::RESPOND, 'class' => 'Civi\API\Event\RespondEvent'],
+      ['name' => 'civi.api.resolve', 'class' => 'Civi\API\Event\ResolveEvent'],
+      ['name' => 'civi.api.authorize', 'class' => 'Civi\API\Event\AuthorizeEvent'],
+      ['name' => 'civi.api.prepare', 'class' => 'Civi\API\Event\PrepareEvent'],
+      ['name' => 'civi.api.respond', 'class' => 'Civi\API\Event\RespondEvent'],
     ];
     $this->assertEquals($expectedEventSequence, $this->actualEventSequence);
     $this->assertEquals('frob', $result['values'][98]);
@@ -50,10 +50,10 @@ class KernelTest extends \CiviUnitTestCase {
 
   public function testResolveException() {
     $test = $this;
-    $this->dispatcher->addListener(Events::RESOLVE, function () {
+    $this->dispatcher->addListener('civi.api.resolve', function () {
       throw new \API_Exception('Oh My God', 'omg', ['the' => 'badzes']);
     }, Events::W_EARLY);
-    $this->dispatcher->addListener(Events::EXCEPTION, function (\Civi\API\Event\ExceptionEvent $event) use ($test) {
+    $this->dispatcher->addListener('civi.api.exception', function (\Civi\API\Event\ExceptionEvent $event) use ($test) {
       $test->assertEquals('Oh My God', $event->getException()->getMessage());
     });
 
@@ -63,8 +63,8 @@ class KernelTest extends \CiviUnitTestCase {
     ]);
 
     $expectedEventSequence = [
-      ['name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'],
-      ['name' => Events::EXCEPTION, 'class' => 'Civi\API\Event\ExceptionEvent'],
+      ['name' => 'civi.api.resolve', 'class' => 'Civi\API\Event\ResolveEvent'],
+      ['name' => 'civi.api.exception', 'class' => 'Civi\API\Event\ExceptionEvent'],
     ];
     $this->assertEquals($expectedEventSequence, $this->actualEventSequence);
     $this->assertEquals('Oh My God', $result['error_message']);