Merge pull request #14840 from MegaphoneJon/core-1130
[civicrm-core.git] / Civi / API / Events.php
CommitLineData
132ec342
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
132ec342 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
132ec342
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
132ec342
TO
27namespace Civi\API;
28
29/**
30 * The API kernel dispatches a series of events while processing each API request.
787604ff 31 * For a successful API request, the sequence is RESOLVE => AUTHORIZE => PREPARE => RESPOND.
132ec342
TO
32 * If an exception arises in any stage, then the sequence is aborted and the EXCEPTION
33 * event is dispatched.
34 *
35 * Event subscribers which are concerned about the order of execution should assign
36 * a weight to their subscription (such as W_EARLY, W_MIDDLE, or W_LATE).
8882ff5c 37 * W_LATE).
132ec342
TO
38 */
39class Events {
40
41 /**
42 * Determine whether the API request is allowed for the current user.
8882ff5c
TO
43 * For successful execution, at least one listener must invoke
44 * $event->authorize().
132ec342
TO
45 *
46 * @see AuthorizeEvent
47 */
f247addc 48 const AUTHORIZE = 'civi.api.authorize';
132ec342 49
787604ff
TO
50 /**
51 * Determine which API provider executes the given request. For successful
8882ff5c
TO
52 * execution, at least one listener must invoke
53 * $event->setProvider($provider).
787604ff
TO
54 *
55 * @see ResolveEvent
56 */
f247addc 57 const RESOLVE = 'civi.api.resolve';
787604ff 58
132ec342
TO
59 /**
60 * Apply pre-execution logic
61 *
62 * @see PrepareEvent
63 */
f247addc 64 const PREPARE = 'civi.api.prepare';
132ec342
TO
65
66 /**
67 * Apply post-execution logic
68 *
69 * @see RespondEvent
70 */
f247addc 71 const RESPOND = 'civi.api.respond';
132ec342
TO
72
73 /**
fe482240 74 * Handle any exceptions.
132ec342
TO
75 *
76 * @see ExceptionEvent
77 */
f247addc 78 const EXCEPTION = 'civi.api.exception';
132ec342
TO
79
80 /**
81 * Weight - Early
82 */
83 const W_EARLY = 100;
84
85 /**
86 * Weight - Middle
87 */
88 const W_MIDDLE = 0;
89
90 /**
91 * Weight - Late
92 */
93 const W_LATE = -100;
70265090
TO
94
95 /**
96 * @return array<string>
97 */
98 public static function allEvents() {
c64f69d9 99 return [
70265090
TO
100 self::AUTHORIZE,
101 self::EXCEPTION,
102 self::PREPARE,
103 self::RESOLVE,
104 self::RESPOND,
c64f69d9 105 ];
70265090 106 }
96025800 107
0d0031a0
TO
108 /**
109 * @param \Civi\Core\Event\GenericHookEvent $e
47e7c2f8 110 * @see \CRM_Utils_Hook::eventDefs
0d0031a0
TO
111 */
112 public static function hookEventDefs($e) {
113 $e->inspector->addEventClass(self::AUTHORIZE, 'Civi\API\Event\AuthorizeEvent');
114 $e->inspector->addEventClass(self::EXCEPTION, 'Civi\API\Event\ExceptionEvent');
115 $e->inspector->addEventClass(self::PREPARE, 'Civi\API\Event\PrepareEvent');
116 $e->inspector->addEventClass(self::RESOLVE, 'Civi\API\Event\ResolveEvent');
117 $e->inspector->addEventClass(self::RESPOND, 'Civi\API\Event\RespondEvent');
118 }
119
132ec342 120}