Merge pull request #2853 from colemanw/js
[civicrm-core.git] / Civi / API / Provider / ReflectionProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 public static function getSubscribedEvents() {
37 return array(
38 Events::RESOLVE => array(
39 array('onApiResolve', Events::W_EARLY), // TODO decide if we really want to override others
40 ),
41 Events::AUTHORIZE => array(
42 array('onApiAuthorize', Events::W_EARLY), // TODO decide if we really want to override others
43 ),
44 );
45 }
46
47 /**
48 * @var \Civi\API\Kernel
49 */
50 private $apiKernel;
51
52 /**
53 * @var array (string $entityName => array(string $actionName))
54 */
55 private $actions;
56
57 /**
58 * @param \Civi\API\Kernel $apiKernel
59 */
60 public function __construct($apiKernel) {
61 $this->apiKernel = $apiKernel;
62 $this->actions = array(
63 'Entity' => array('get', 'getactions'),
64 '*' => array('getactions'), // 'getfields'
65 );
66 }
67
68 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
69 $apiRequest = $event->getApiRequest();
70 $actions = isset($this->actions[$apiRequest['entity']]) ? $this->actions[$apiRequest['entity']] : $this->actions['*'];
71 if (in_array($apiRequest['action'], $actions)) {
72 $apiRequest['is_metadata'] = TRUE;
73 $event->setApiRequest($apiRequest);
74 $event->setApiProvider($this);
75 $event->stopPropagation(); // TODO decide if we really want to override others
76 }
77 }
78
79 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
80 $apiRequest = $event->getApiRequest();
81 if (isset($apiRequest['is_metadata'])) {
82 // if (\CRM_Core_Permission::check('access AJAX API') || \CRM_Core_Permission::check('access CiviCRM')) {
83 $event->authorize();
84 $event->stopPropagation();
85 // }
86 }
87 }
88
89 /**
90 * {inheritdoc}
91 */
92 public function invoke($apiRequest) {
93 if ($apiRequest['entity'] == 'Entity' && $apiRequest['action'] == 'get') {
94 return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']));
95 }
96 switch ($apiRequest['action']) {
97 case 'getactions':
98 return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']));
99 // case 'getfields':
100 // return $this->doGetFields($apiRequest);
101 default:
102 }
103
104 // We shouldn't get here because onApiResolve() checks $this->actions
105 throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
106 }
107
108 /**
109 * {inheritdoc}
110 */
111 function getEntityNames($version) {
112 return array('Entity');
113 }
114
115 /**
116 * {inheritdoc}
117 */
118 function getActionNames($version, $entity) {
119 return isset($this->actions[$entity]) ? $this->actions[$entity] : $this->actions['*'];
120 }
121 }