3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 namespace Civi\API\Provider
;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
18 * An adhoc provider is useful for creating mock API implementations.
20 class AdhocProvider
implements EventSubscriberInterface
, ProviderInterface
{
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.
30 'civi.api.resolve' => [
31 ['onApiResolve', Events
::W_EARLY
],
33 'civi.api.authorize' => [
34 ['onApiAuthorize', Events
::W_EARLY
],
40 * List of adhoc actions
42 * array(string $ame => array('perm' => string, 'callback' => callable))
46 protected $actions = [];
61 * @param string $entity
64 public function __construct($version, $entity) {
65 $this->entity
= $entity;
66 $this->version
= $version;
75 * Permissions required for invoking the action.
76 * @param mixed $callback
77 * The function which executes the API.
78 * @return AdhocProvider
80 public function addAction($name, $perm, $callback) {
81 $this->actions
[strtolower($name)] = [
83 'callback' => $callback,
89 * @param \Civi\API\Event\ResolveEvent $event
90 * API resolution event.
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();
102 * @param \Civi\API\Event\AuthorizeEvent $event
103 * API authorization event.
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'])) {
109 $event->stopPropagation();
115 * @param array $apiRequest
116 * @return array|mixed
118 public function invoke($apiRequest) {
119 return call_user_func($this->actions
[strtolower($apiRequest['action'])]['callback'], $apiRequest);
124 * @param int $version
127 public function getEntityNames($version) {
128 return [$this->entity
];
133 * @param int $version
134 * @param string $entity
137 public function getActionNames($version, $entity) {
138 if ($version == $this->version
&& $entity == $this->entity
) {
139 return array_keys($this->actions
);
147 * @param array $apiRequest
148 * The full description of the API request.
152 public function matchesRequest($apiRequest) {
153 return $apiRequest['entity'] == $this->entity
&& $apiRequest['version'] == $this->version
&& isset($this->actions
[strtolower($apiRequest['action'])]);