dev/joomla/#28 5.27 Upgrade fails
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
CommitLineData
70265090
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
70265090 5 | |
41498ac5
TO
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 |
70265090 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
70265090
TO
11
12namespace Civi\API\Provider;
46bcf597 13
70265090
TO
14use Civi\API\Events;
15use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17/**
18 * An adhoc provider is useful for creating mock API implementations.
19 */
20class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
21
6550386a
EM
22 /**
23 * @return array
24 */
70265090 25 public static function getSubscribedEvents() {
db8b4473
TO
26 // Using a high priority allows adhoc implementations
27 // to override standard implementations -- which is
28 // handy for testing/mocking.
c64f69d9 29 return [
39b870b8 30 'civi.api.resolve' => [
c64f69d9
CW
31 ['onApiResolve', Events::W_EARLY],
32 ],
39b870b8 33 'civi.api.authorize' => [
c64f69d9
CW
34 ['onApiAuthorize', Events::W_EARLY],
35 ],
36 ];
70265090
TO
37 }
38
39 /**
f33d2b8c
TO
40 * List of adhoc actions
41 *
42 * array(string $ame => array('perm' => string, 'callback' => callable))
43 *
44 * @var array
70265090 45 */
c64f69d9 46 protected $actions = [];
70265090
TO
47
48 /**
49 * @var string
50 */
17b9f7be 51 protected $entity;
70265090
TO
52
53 /**
54 * @var int
55 */
17b9f7be 56 protected $version;
70265090
TO
57
58 /**
59 * @param int $version
8882ff5c 60 * API version.
70265090 61 * @param string $entity
8882ff5c 62 * API entity.
70265090
TO
63 */
64 public function __construct($version, $entity) {
65 $this->entity = $entity;
66 $this->version = $version;
67 }
68
69 /**
8882ff5c
TO
70 * Register a new API.
71 *
70265090 72 * @param string $name
8882ff5c 73 * Action name.
70265090 74 * @param string $perm
8882ff5c
TO
75 * Permissions required for invoking the action.
76 * @param mixed $callback
77 * The function which executes the API.
066c4638 78 * @return AdhocProvider
70265090
TO
79 */
80 public function addAction($name, $perm, $callback) {
c64f69d9 81 $this->actions[strtolower($name)] = [
70265090
TO
82 'perm' => $perm,
83 'callback' => $callback,
c64f69d9 84 ];
70265090
TO
85 return $this;
86 }
87
6550386a
EM
88 /**
89 * @param \Civi\API\Event\ResolveEvent $event
8882ff5c 90 * API resolution event.
6550386a 91 */
70265090
TO
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
6550386a
EM
101 /**
102 * @param \Civi\API\Event\AuthorizeEvent $event
8882ff5c 103 * API authorization event.
6550386a 104 */
70265090
TO
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 /**
3dde2c6c 114 * @inheritDoc
257e7666
EM
115 * @param array $apiRequest
116 * @return array|mixed
70265090
TO
117 */
118 public function invoke($apiRequest) {
119 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
120 }
121
122 /**
3dde2c6c 123 * @inheritDoc
257e7666
EM
124 * @param int $version
125 * @return array
70265090 126 */
8882ff5c 127 public function getEntityNames($version) {
c64f69d9 128 return [$this->entity];
70265090
TO
129 }
130
131 /**
3dde2c6c 132 * @inheritDoc
257e7666
EM
133 * @param int $version
134 * @param string $entity
135 * @return array
70265090 136 */
8882ff5c 137 public function getActionNames($version, $entity) {
70265090
TO
138 if ($version == $this->version && $entity == $this->entity) {
139 return array_keys($this->actions);
140 }
141 else {
c64f69d9 142 return [];
70265090
TO
143 }
144 }
145
6550386a 146 /**
8882ff5c
TO
147 * @param array $apiRequest
148 * The full description of the API request.
6550386a
EM
149 *
150 * @return bool
151 */
70265090
TO
152 public function matchesRequest($apiRequest) {
153 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
154 }
96025800 155
6550386a 156}