Merge pull request #4923 from colemanw/invoice_id
[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
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 * @return array
39 */
40 public static function getSubscribedEvents() {
41 return array(
42 Events::RESOLVE => array(
43 // TODO decide if we really want to override others
44 array('onApiResolve', Events::W_EARLY),
45 ),
46 Events::AUTHORIZE => array(
47 // TODO decide if we really want to override others
48 array('onApiAuthorize', Events::W_EARLY),
49 ),
50 );
51 }
52
53 /**
54 * @var \Civi\API\Kernel
55 */
56 private $apiKernel;
57
58 /**
59 * @var array (string $entityName => array(string $actionName))
60 */
61 private $actions;
62
63 /**
64 * @param \Civi\API\Kernel $apiKernel
65 * The API kernel.
66 */
67 public function __construct($apiKernel) {
68 $this->apiKernel = $apiKernel;
69 $this->actions = array(
70 // FIXME: We really need to deal with the api's lack of uniformity wrt
71 // case (Entity or entity).
72 'Entity' => array('get', 'getactions'),
73 'entity' => array('get', 'getactions'),
74 '*' => array('getactions'), // 'getfields'
75 );
76 }
77
78 /**
79 * @param \Civi\API\Event\ResolveEvent $event
80 * API resolution event.
81 */
82 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
83 $apiRequest = $event->getApiRequest();
84 $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
85 if (in_array($apiRequest['action'], $actions)) {
86 $apiRequest['is_metadata'] = TRUE;
87 $event->setApiRequest($apiRequest);
88 $event->setApiProvider($this);
89 $event->stopPropagation();
90 // TODO decide if we really want to override others
91 }
92 }
93
94 /**
95 * @param \Civi\API\Event\AuthorizeEvent $event
96 * API authorization event.
97 */
98 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
99 $apiRequest = $event->getApiRequest();
100 if (isset($apiRequest['is_metadata'])) {
101 // if (\CRM_Core_Permission::check('access AJAX API')
102 // || \CRM_Core_Permission::check('access CiviCRM')) {
103 $event->authorize();
104 $event->stopPropagation();
105 // }
106 }
107 }
108
109 /**
110 * {inheritdoc}
111 * @param array $apiRequest
112 * @return array
113 * @throws \API_Exception
114 */
115 public function invoke($apiRequest) {
116 if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
117 return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
118 }
119 switch ($apiRequest['action']) {
120 case 'getactions':
121 return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
122
123 //case 'getfields':
124 // return $this->doGetFields($apiRequest);
125
126 default:
127 }
128
129 // We shouldn't get here because onApiResolve() checks $this->actions
130 throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
131 }
132
133 /**
134 * {inheritdoc}
135 * @param int $version
136 * @return array
137 */
138 public function getEntityNames($version) {
139 return array('Entity');
140 }
141
142 /**
143 * {inheritdoc}
144 * @param int $version
145 * @param string $entity
146 * @return array
147 */
148 public function getActionNames($version, $entity) {
149 $entity = _civicrm_api_get_camel_name($entity, $version);
150 return isset($this->actions[$entity]) ? $this->actions[$entity] : $this->actions['*'];
151 }
152 }