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 * This class defines operations for inspecting the API's metadata.
20 class ReflectionProvider
implements EventSubscriberInterface
, ProviderInterface
{
25 public static function getSubscribedEvents() {
28 // TODO decide if we really want to override others
29 ['onApiResolve', Events
::W_EARLY
],
31 Events
::AUTHORIZE
=> [
32 // TODO decide if we really want to override others
33 ['onApiAuthorize', Events
::W_EARLY
],
39 * @var \Civi\API\Kernel
44 * List of all entities and their supported actions
46 * array(string $entityName => string[] $actionNames).
53 * @param \Civi\API\Kernel $apiKernel
56 public function __construct($apiKernel) {
57 $this->apiKernel
= $apiKernel;
59 'Entity' => ['get', 'getactions'],
61 '*' => ['getactions'],
66 * @param \Civi\API\Event\ResolveEvent $event
67 * API resolution event.
69 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
70 $apiRequest = $event->getApiRequest();
71 $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
72 if (in_array($apiRequest['action'], $actions)) {
73 $apiRequest['is_metadata'] = TRUE;
74 $event->setApiRequest($apiRequest);
75 $event->setApiProvider($this);
76 $event->stopPropagation();
77 // TODO decide if we really want to override others
82 * @param \Civi\API\Event\AuthorizeEvent $event
83 * API authorization event.
85 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent
$event) {
86 $apiRequest = $event->getApiRequest();
87 if (isset($apiRequest['is_metadata'])) {
88 // if (\CRM_Core_Permission::check('access AJAX API')
89 // || \CRM_Core_Permission::check('access CiviCRM')) {
91 $event->stopPropagation();
98 * @param array $apiRequest
100 * @throws \API_Exception
102 public function invoke($apiRequest) {
103 if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
104 return civicrm_api3_create_success($this->apiKernel
->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
106 switch ($apiRequest['action']) {
108 return civicrm_api3_create_success($this->apiKernel
->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
111 // return $this->doGetFields($apiRequest);
116 // We shouldn't get here because onApiResolve() checks $this->actions
117 throw new \
API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
122 * @param int $version
125 public function getEntityNames($version) {
131 * @param int $version
132 * @param string $entity
135 public function getActionNames($version, $entity) {
136 $entity = _civicrm_api_get_camel_name($entity, $version);
137 return isset($this->actions
[$entity]) ?
$this->actions
[$entity] : $this->actions
['*'];