| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | namespace Civi\API\Provider; |
| 13 | |
| 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 { |
| 21 | |
| 22 | /** |
| 23 | * @return array |
| 24 | */ |
| 25 | public static function getSubscribedEvents() { |
| 26 | return [ |
| 27 | Events::RESOLVE => [ |
| 28 | // TODO decide if we really want to override others |
| 29 | ['onApiResolve', Events::W_EARLY], |
| 30 | ], |
| 31 | Events::AUTHORIZE => [ |
| 32 | // TODO decide if we really want to override others |
| 33 | ['onApiAuthorize', Events::W_EARLY], |
| 34 | ], |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @var \Civi\API\Kernel |
| 40 | */ |
| 41 | private $apiKernel; |
| 42 | |
| 43 | /** |
| 44 | * List of all entities and their supported actions |
| 45 | * |
| 46 | * array(string $entityName => string[] $actionNames). |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | private $actions; |
| 51 | |
| 52 | /** |
| 53 | * @param \Civi\API\Kernel $apiKernel |
| 54 | * The API kernel. |
| 55 | */ |
| 56 | public function __construct($apiKernel) { |
| 57 | $this->apiKernel = $apiKernel; |
| 58 | $this->actions = [ |
| 59 | 'Entity' => ['get', 'getactions'], |
| 60 | // 'getfields' |
| 61 | '*' => ['getactions'], |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param \Civi\API\Event\ResolveEvent $event |
| 67 | * API resolution event. |
| 68 | */ |
| 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 |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param \Civi\API\Event\AuthorizeEvent $event |
| 83 | * API authorization event. |
| 84 | */ |
| 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')) { |
| 90 | $event->authorize(); |
| 91 | $event->stopPropagation(); |
| 92 | // } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @inheritDoc |
| 98 | * @param array $apiRequest |
| 99 | * @return array |
| 100 | * @throws \API_Exception |
| 101 | */ |
| 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'); |
| 105 | } |
| 106 | switch ($apiRequest['action']) { |
| 107 | case 'getactions': |
| 108 | return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']); |
| 109 | |
| 110 | //case 'getfields': |
| 111 | // return $this->doGetFields($apiRequest); |
| 112 | |
| 113 | default: |
| 114 | } |
| 115 | |
| 116 | // We shouldn't get here because onApiResolve() checks $this->actions |
| 117 | throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']'); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @inheritDoc |
| 122 | * @param int $version |
| 123 | * @return array |
| 124 | */ |
| 125 | public function getEntityNames($version) { |
| 126 | return ['Entity']; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @inheritDoc |
| 131 | * @param int $version |
| 132 | * @param string $entity |
| 133 | * @return array |
| 134 | */ |
| 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['*']; |
| 138 | } |
| 139 | |
| 140 | } |