/**
* 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 {
/**
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 {
/**
* Class PrepareEvent
* @package Civi\API\Event
+ *
+ * Apply any pre-execution filtering to the API request.
+ *
+ * Event name: 'civi.api.prepare'
*/
class PrepareEvent extends 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 {
/**
* Class RespondEvent
* @package Civi\API\Event
+ *
+ * Apply post-execution filtering to the API request/response.
+ *
+ * Event name: 'civi.api.respond'
*/
class RespondEvent extends Event {
/**
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';
* Apply post-execution logic
*
* @see RespondEvent
+ * @deprecated - You may simply use the event name directly. dev/core#1744
*/
const RESPOND = 'civi.api.respond';
* Handle any exceptions.
*
* @see ExceptionEvent
+ * @deprecated - You may simply use the event name directly. dev/core#1744
*/
const EXCEPTION = 'civi.api.exception';
*/
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',
];
}
* @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');
}
}
}
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) {
*/
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!)");
*/
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");
}
*/
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()];
}
*/
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();
}
// 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],
],
];
*/
public static function getSubscribedEvents() {
return [
- Events::RESOLVE => [
+ 'civi.api.resolve' => [
['onApiResolve', Events::W_MIDDLE],
],
];
*/
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],
],
*/
public static function getSubscribedEvents() {
return [
- Events::RESOLVE => [
+ 'civi.api.resolve' => [
['onApiResolve', Events::W_MIDDLE],
],
- Events::AUTHORIZE => [
+ 'civi.api.authorize' => [
['onApiAuthorize', Events::W_MIDDLE],
],
];
*/
public static function getSubscribedEvents() {
return [
- Events::PREPARE => [
+ 'civi.api.prepare' => [
['onApiPrepare', Events::W_MIDDLE],
['onApiPrepare_validate', Events::W_LATE],
],
*/
public static function getSubscribedEvents() {
return [
- Events::RESPOND => ['onApiRespond', Events::W_EARLY],
+ 'civi.api.respond' => ['onApiRespond', Events::W_EARLY],
];
}
namespace Civi\API\Subscriber;
-use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
*/
public static function getSubscribedEvents() {
return [
- Events::PREPARE => ['onApiPrepare', 999],
- Events::RESPOND => ['onApiRespond', -999],
+ 'civi.api.prepare' => ['onApiPrepare', 999],
+ 'civi.api.respond' => ['onApiRespond', -999],
];
}
*/
public static function getSubscribedEvents() {
return [
- Events::AUTHORIZE => [
+ 'civi.api.authorize' => [
['onApiAuthorize', Events::W_EARLY],
],
];
*/
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],
];
}
*/
public static function getSubscribedEvents() {
return [
- Events::AUTHORIZE => [
+ 'civi.api.authorize' => [
['onApiAuthorize', Events::W_LATE],
],
];
*/
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],
];
}
*/
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],
];
}
*/
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],
];
}
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 {
*/
public static function getSubscribedEvents() {
return [
- Events::PREPARE => 'onApiPrepare',
+ 'civi.api.prepare' => 'onApiPrepare',
];
}
*/
public static function getSubscribedEvents() {
return [
- Events::AUTHORIZE => [
+ 'civi.api.authorize' => [
['onApiAuthorize', Events::W_LATE],
],
];
// to override standard implementations -- which is
// handy for testing/mocking.
return [
- Events::RESOLVE => [
+ 'civi.api.resolve' => [
['onApiResolve', Events::W_EARLY],
],
];
namespace Civi\API\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
-use Civi\API\Events;
use Civi\API\Kernel;
/**
* @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']);
]);
$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]);
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());
});
]);
$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']);