| 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 | * @var array (string $entityName => array(string $actionName)) |
| 45 | */ |
| 46 | private $actions; |
| 47 | |
| 48 | /** |
| 49 | * @param \Civi\API\Kernel $apiKernel |
| 50 | * The API kernel. |
| 51 | */ |
| 52 | public function __construct($apiKernel) { |
| 53 | $this->apiKernel = $apiKernel; |
| 54 | $this->actions = [ |
| 55 | 'Entity' => ['get', 'getactions'], |
| 56 | // 'getfields' |
| 57 | '*' => ['getactions'], |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param \Civi\API\Event\ResolveEvent $event |
| 63 | * API resolution event. |
| 64 | */ |
| 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 |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param \Civi\API\Event\AuthorizeEvent $event |
| 79 | * API authorization event. |
| 80 | */ |
| 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')) { |
| 86 | $event->authorize(); |
| 87 | $event->stopPropagation(); |
| 88 | // } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @inheritDoc |
| 94 | * @param array $apiRequest |
| 95 | * @return array |
| 96 | * @throws \API_Exception |
| 97 | */ |
| 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'); |
| 101 | } |
| 102 | switch ($apiRequest['action']) { |
| 103 | case 'getactions': |
| 104 | return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']); |
| 105 | |
| 106 | //case 'getfields': |
| 107 | // return $this->doGetFields($apiRequest); |
| 108 | |
| 109 | default: |
| 110 | } |
| 111 | |
| 112 | // We shouldn't get here because onApiResolve() checks $this->actions |
| 113 | throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']'); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @inheritDoc |
| 118 | * @param int $version |
| 119 | * @return array |
| 120 | */ |
| 121 | public function getEntityNames($version) { |
| 122 | return ['Entity']; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @inheritDoc |
| 127 | * @param int $version |
| 128 | * @param string $entity |
| 129 | * @return array |
| 130 | */ |
| 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['*']; |
| 134 | } |
| 135 | |
| 136 | } |