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 * @var array (string $entityName => array(string $actionName))
49 * @param \Civi\API\Kernel $apiKernel
52 public function __construct($apiKernel) {
53 $this->apiKernel
= $apiKernel;
55 'Entity' => ['get', 'getactions'],
57 '*' => ['getactions'],
62 * @param \Civi\API\Event\ResolveEvent $event
63 * API resolution event.
65 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
66 $apiRequest = $event->getApiRequest();
67 $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
68 if (in_array($apiRequest['action'], $actions)) {
69 $apiRequest['is_metadata'] = TRUE;
70 $event->setApiRequest($apiRequest);
71 $event->setApiProvider($this);
72 $event->stopPropagation();
73 // TODO decide if we really want to override others
78 * @param \Civi\API\Event\AuthorizeEvent $event
79 * API authorization event.
81 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent
$event) {
82 $apiRequest = $event->getApiRequest();
83 if (isset($apiRequest['is_metadata'])) {
84 // if (\CRM_Core_Permission::check('access AJAX API')
85 // || \CRM_Core_Permission::check('access CiviCRM')) {
87 $event->stopPropagation();
94 * @param array $apiRequest
96 * @throws \API_Exception
98 public function invoke($apiRequest) {
99 if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
100 return civicrm_api3_create_success($this->apiKernel
->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
102 switch ($apiRequest['action']) {
104 return civicrm_api3_create_success($this->apiKernel
->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
107 // return $this->doGetFields($apiRequest);
112 // We shouldn't get here because onApiResolve() checks $this->actions
113 throw new \
API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
118 * @param int $version
121 public function getEntityNames($version) {
127 * @param int $version
128 * @param string $entity
131 public function getActionNames($version, $entity) {
132 $entity = _civicrm_api_get_camel_name($entity, $version);
133 return isset($this->actions
[$entity]) ?
$this->actions
[$entity] : $this->actions
['*'];