dev/joomla/#28 5.27 Upgrade fails
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\API\Provider;
13
14 use Civi\API\Events;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17 /**
18 * An adhoc provider is useful for creating mock API implementations.
19 */
20 class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
21
22 /**
23 * @return array
24 */
25 public static function getSubscribedEvents() {
26 // Using a high priority allows adhoc implementations
27 // to override standard implementations -- which is
28 // handy for testing/mocking.
29 return [
30 'civi.api.resolve' => [
31 ['onApiResolve', Events::W_EARLY],
32 ],
33 'civi.api.authorize' => [
34 ['onApiAuthorize', Events::W_EARLY],
35 ],
36 ];
37 }
38
39 /**
40 * List of adhoc actions
41 *
42 * array(string $ame => array('perm' => string, 'callback' => callable))
43 *
44 * @var array
45 */
46 protected $actions = [];
47
48 /**
49 * @var string
50 */
51 protected $entity;
52
53 /**
54 * @var int
55 */
56 protected $version;
57
58 /**
59 * @param int $version
60 * API version.
61 * @param string $entity
62 * API entity.
63 */
64 public function __construct($version, $entity) {
65 $this->entity = $entity;
66 $this->version = $version;
67 }
68
69 /**
70 * Register a new API.
71 *
72 * @param string $name
73 * Action name.
74 * @param string $perm
75 * Permissions required for invoking the action.
76 * @param mixed $callback
77 * The function which executes the API.
78 * @return AdhocProvider
79 */
80 public function addAction($name, $perm, $callback) {
81 $this->actions[strtolower($name)] = [
82 'perm' => $perm,
83 'callback' => $callback,
84 ];
85 return $this;
86 }
87
88 /**
89 * @param \Civi\API\Event\ResolveEvent $event
90 * API resolution event.
91 */
92 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
93 $apiRequest = $event->getApiRequest();
94 if ($this->matchesRequest($apiRequest)) {
95 $event->setApiRequest($apiRequest);
96 $event->setApiProvider($this);
97 $event->stopPropagation();
98 }
99 }
100
101 /**
102 * @param \Civi\API\Event\AuthorizeEvent $event
103 * API authorization event.
104 */
105 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
106 $apiRequest = $event->getApiRequest();
107 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) {
108 $event->authorize();
109 $event->stopPropagation();
110 }
111 }
112
113 /**
114 * @inheritDoc
115 * @param array $apiRequest
116 * @return array|mixed
117 */
118 public function invoke($apiRequest) {
119 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
120 }
121
122 /**
123 * @inheritDoc
124 * @param int $version
125 * @return array
126 */
127 public function getEntityNames($version) {
128 return [$this->entity];
129 }
130
131 /**
132 * @inheritDoc
133 * @param int $version
134 * @param string $entity
135 * @return array
136 */
137 public function getActionNames($version, $entity) {
138 if ($version == $this->version && $entity == $this->entity) {
139 return array_keys($this->actions);
140 }
141 else {
142 return [];
143 }
144 }
145
146 /**
147 * @param array $apiRequest
148 * The full description of the API request.
149 *
150 * @return bool
151 */
152 public function matchesRequest($apiRequest) {
153 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
154 }
155
156 }