Merge pull request #4772 from jitendrapurohit/CRM-15750
[civicrm-core.git] / Civi / API / Provider / ReflectionProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 use Civi\API\Events;
30 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
31
32 /**
33 * This class defines operations for inspecting the API's metadata.
34 */
35 class ReflectionProvider implements EventSubscriberInterface, ProviderInterface {
36 /**
37 * @return array
38 */
39 public static function getSubscribedEvents() {
40 return array(
41 Events::RESOLVE => array(
42 array('onApiResolve', Events::W_EARLY), // TODO decide if we really want to override others
43 ),
44 Events::AUTHORIZE => array(
45 array('onApiAuthorize', Events::W_EARLY), // TODO decide if we really want to override others
46 ),
47 );
48 }
49
50 /**
51 * @var \Civi\API\Kernel
52 */
53 private $apiKernel;
54
55 /**
56 * @var array (string $entityName => array(string $actionName))
57 */
58 private $actions;
59
60 /**
61 * @param \Civi\API\Kernel $apiKernel
62 */
63 public function __construct($apiKernel) {
64 $this->apiKernel = $apiKernel;
65 $this->actions = array(
66 // FIXME: We really need to deal with the api's lack of uniformity wrt case (Entity or entity)
67 'Entity' => array('get', 'getactions'),
68 'entity' => array('get', 'getactions'),
69 '*' => array('getactions'), // 'getfields'
70 );
71 }
72
73 /**
74 * @param \Civi\API\Event\ResolveEvent $event
75 */
76 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
77 $apiRequest = $event->getApiRequest();
78 $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
79 if (in_array($apiRequest['action'], $actions)) {
80 $apiRequest['is_metadata'] = TRUE;
81 $event->setApiRequest($apiRequest);
82 $event->setApiProvider($this);
83 $event->stopPropagation(); // TODO decide if we really want to override others
84 }
85 }
86
87 /**
88 * @param \Civi\API\Event\AuthorizeEvent $event
89 */
90 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
91 $apiRequest = $event->getApiRequest();
92 if (isset($apiRequest['is_metadata'])) {
93 // if (\CRM_Core_Permission::check('access AJAX API') || \CRM_Core_Permission::check('access CiviCRM')) {
94 $event->authorize();
95 $event->stopPropagation();
96 // }
97 }
98 }
99
100 /**
101 * {inheritdoc}
102 */
103 public function invoke($apiRequest) {
104 if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
105 return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
106 }
107 switch ($apiRequest['action']) {
108 case 'getactions':
109 return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
110 // case 'getfields':
111 // return $this->doGetFields($apiRequest);
112 default:
113 }
114
115 // We shouldn't get here because onApiResolve() checks $this->actions
116 throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
117 }
118
119 /**
120 * {inheritdoc}
121 */
122 function getEntityNames($version) {
123 return array('Entity');
124 }
125
126 /**
127 * {inheritdoc}
128 */
129 function getActionNames($version, $entity) {
130 $entity = _civicrm_api_get_camel_name($entity, $version);
131 return isset($this->actions[$entity]) ? $this->actions[$entity] : $this->actions['*'];
132 }
133 }