Merge pull request #14247 from pradpnayak/ActvityTags
[civicrm-core.git] / Civi / API / Provider / ReflectionProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 namespace Civi\API\Provider;
29
30 use Civi\API\Events;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
33 /**
34 * This class defines operations for inspecting the API's metadata.
35 */
36 class ReflectionProvider implements EventSubscriberInterface, ProviderInterface {
37
38 /**
39 * @return array
40 */
41 public static function getSubscribedEvents() {
42 return [
43 Events::RESOLVE => [
44 // TODO decide if we really want to override others
45 ['onApiResolve', Events::W_EARLY],
46 ],
47 Events::AUTHORIZE => [
48 // TODO decide if we really want to override others
49 ['onApiAuthorize', Events::W_EARLY],
50 ],
51 ];
52 }
53
54 /**
55 * @var \Civi\API\Kernel
56 */
57 private $apiKernel;
58
59 /**
60 * @var array (string $entityName => array(string $actionName))
61 */
62 private $actions;
63
64 /**
65 * @param \Civi\API\Kernel $apiKernel
66 * The API kernel.
67 */
68 public function __construct($apiKernel) {
69 $this->apiKernel = $apiKernel;
70 $this->actions = [
71 'Entity' => ['get', 'getactions'],
72 // 'getfields'
73 '*' => ['getactions'],
74 ];
75 }
76
77 /**
78 * @param \Civi\API\Event\ResolveEvent $event
79 * API resolution event.
80 */
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
90 }
91 }
92
93 /**
94 * @param \Civi\API\Event\AuthorizeEvent $event
95 * API authorization event.
96 */
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')) {
102 $event->authorize();
103 $event->stopPropagation();
104 // }
105 }
106 }
107
108 /**
109 * @inheritDoc
110 * @param array $apiRequest
111 * @return array
112 * @throws \API_Exception
113 */
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');
117 }
118 switch ($apiRequest['action']) {
119 case 'getactions':
120 return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
121
122 //case 'getfields':
123 // return $this->doGetFields($apiRequest);
124
125 default:
126 }
127
128 // We shouldn't get here because onApiResolve() checks $this->actions
129 throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
130 }
131
132 /**
133 * @inheritDoc
134 * @param int $version
135 * @return array
136 */
137 public function getEntityNames($version) {
138 return ['Entity'];
139 }
140
141 /**
142 * @inheritDoc
143 * @param int $version
144 * @param string $entity
145 * @return array
146 */
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['*'];
150 }
151
152 }