dev/joomla/#28 5.27 Upgrade fails
[civicrm-core.git] / Civi / API / Provider / ReflectionProvider.php
CommitLineData
82376c19
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
82376c19 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 |
82376c19 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
82376c19
TO
11
12namespace Civi\API\Provider;
8882ff5c 13
82376c19
TO
14use Civi\API\Events;
15use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17/**
18 * This class defines operations for inspecting the API's metadata.
19 */
20class ReflectionProvider implements EventSubscriberInterface, ProviderInterface {
34f3bbd9 21
6550386a
EM
22 /**
23 * @return array
24 */
82376c19 25 public static function getSubscribedEvents() {
c64f69d9 26 return [
39b870b8 27 'civi.api.resolve' => [
8882ff5c 28 // TODO decide if we really want to override others
c64f69d9
CW
29 ['onApiResolve', Events::W_EARLY],
30 ],
39b870b8 31 'civi.api.authorize' => [
8882ff5c 32 // TODO decide if we really want to override others
c64f69d9
CW
33 ['onApiAuthorize', Events::W_EARLY],
34 ],
35 ];
82376c19
TO
36 }
37
38 /**
39 * @var \Civi\API\Kernel
40 */
41 private $apiKernel;
42
43 /**
f33d2b8c
TO
44 * List of all entities and their supported actions
45 *
46 * array(string $entityName => string[] $actionNames).
47 *
48 * @var array
82376c19
TO
49 */
50 private $actions;
51
52 /**
53 * @param \Civi\API\Kernel $apiKernel
8882ff5c 54 * The API kernel.
82376c19
TO
55 */
56 public function __construct($apiKernel) {
57 $this->apiKernel = $apiKernel;
c64f69d9
CW
58 $this->actions = [
59 'Entity' => ['get', 'getactions'],
34f3bbd9
SL
60 // 'getfields'
61 '*' => ['getactions'],
c64f69d9 62 ];
82376c19
TO
63 }
64
6550386a
EM
65 /**
66 * @param \Civi\API\Event\ResolveEvent $event
8882ff5c 67 * API resolution event.
6550386a 68 */
82376c19
TO
69 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
70 $apiRequest = $event->getApiRequest();
92776611 71 $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
82376c19
TO
72 if (in_array($apiRequest['action'], $actions)) {
73 $apiRequest['is_metadata'] = TRUE;
74 $event->setApiRequest($apiRequest);
75 $event->setApiProvider($this);
8882ff5c
TO
76 $event->stopPropagation();
77 // TODO decide if we really want to override others
82376c19
TO
78 }
79 }
80
6550386a
EM
81 /**
82 * @param \Civi\API\Event\AuthorizeEvent $event
8882ff5c 83 * API authorization event.
6550386a 84 */
82376c19
TO
85 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
86 $apiRequest = $event->getApiRequest();
87 if (isset($apiRequest['is_metadata'])) {
8882ff5c
TO
88 // if (\CRM_Core_Permission::check('access AJAX API')
89 // || \CRM_Core_Permission::check('access CiviCRM')) {
82376c19
TO
90 $event->authorize();
91 $event->stopPropagation();
92 // }
93 }
94 }
95
96 /**
3dde2c6c 97 * @inheritDoc
257e7666
EM
98 * @param array $apiRequest
99 * @return array
100 * @throws \API_Exception
82376c19
TO
101 */
102 public function invoke($apiRequest) {
a14e9d08
CW
103 if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
104 return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
82376c19
TO
105 }
106 switch ($apiRequest['action']) {
107 case 'getactions':
a14e9d08 108 return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
8882ff5c
TO
109
110 //case 'getfields':
111 // return $this->doGetFields($apiRequest);
112
82376c19
TO
113 default:
114 }
115
116 // We shouldn't get here because onApiResolve() checks $this->actions
8882ff5c 117 throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
82376c19
TO
118 }
119
120 /**
3dde2c6c 121 * @inheritDoc
257e7666
EM
122 * @param int $version
123 * @return array
82376c19 124 */
8882ff5c 125 public function getEntityNames($version) {
c64f69d9 126 return ['Entity'];
82376c19
TO
127 }
128
129 /**
3dde2c6c 130 * @inheritDoc
257e7666
EM
131 * @param int $version
132 * @param string $entity
133 * @return array
82376c19 134 */
8882ff5c 135 public function getActionNames($version, $entity) {
92776611 136 $entity = _civicrm_api_get_camel_name($entity, $version);
2e1f50d6 137 return $this->actions[$entity] ?? $this->actions['*'];
82376c19 138 }
96025800 139
6550386a 140}