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