3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 namespace Civi\API\Provider
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * This class defines operations for inspecting the API's metadata.
36 class ReflectionProvider
implements EventSubscriberInterface
, ProviderInterface
{
41 public static function getSubscribedEvents() {
44 // TODO decide if we really want to override others
45 ['onApiResolve', Events
::W_EARLY
],
47 Events
::AUTHORIZE
=> [
48 // TODO decide if we really want to override others
49 ['onApiAuthorize', Events
::W_EARLY
],
55 * @var \Civi\API\Kernel
60 * @var array (string $entityName => array(string $actionName))
65 * @param \Civi\API\Kernel $apiKernel
68 public function __construct($apiKernel) {
69 $this->apiKernel
= $apiKernel;
71 'Entity' => ['get', 'getactions'],
73 '*' => ['getactions'],
78 * @param \Civi\API\Event\ResolveEvent $event
79 * API resolution event.
81 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
82 $apiRequest = $event->getApiRequest();
83 $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
84 if (in_array($apiRequest['action'], $actions)) {
85 $apiRequest['is_metadata'] = TRUE;
86 $event->setApiRequest($apiRequest);
87 $event->setApiProvider($this);
88 $event->stopPropagation();
89 // TODO decide if we really want to override others
94 * @param \Civi\API\Event\AuthorizeEvent $event
95 * API authorization event.
97 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent
$event) {
98 $apiRequest = $event->getApiRequest();
99 if (isset($apiRequest['is_metadata'])) {
100 // if (\CRM_Core_Permission::check('access AJAX API')
101 // || \CRM_Core_Permission::check('access CiviCRM')) {
103 $event->stopPropagation();
110 * @param array $apiRequest
112 * @throws \API_Exception
114 public function invoke($apiRequest) {
115 if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
116 return civicrm_api3_create_success($this->apiKernel
->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
118 switch ($apiRequest['action']) {
120 return civicrm_api3_create_success($this->apiKernel
->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
123 // return $this->doGetFields($apiRequest);
128 // We shouldn't get here because onApiResolve() checks $this->actions
129 throw new \
API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
134 * @param int $version
137 public function getEntityNames($version) {
143 * @param int $version
144 * @param string $entity
147 public function getActionNames($version, $entity) {
148 $entity = _civicrm_api_get_camel_name($entity, $version);
149 return isset($this->actions
[$entity]) ?
$this->actions
[$entity] : $this->actions
['*'];