3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
;
30 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
33 * This class defines operations for inspecting the API's metadata.
35 class ReflectionProvider
implements EventSubscriberInterface
, ProviderInterface
{
39 public static function getSubscribedEvents() {
41 Events
::RESOLVE
=> array(
42 array('onApiResolve', Events
::W_EARLY
), // TODO decide if we really want to override others
44 Events
::AUTHORIZE
=> array(
45 array('onApiAuthorize', Events
::W_EARLY
), // TODO decide if we really want to override others
51 * @var \Civi\API\Kernel
56 * @var array (string $entityName => array(string $actionName))
61 * @param \Civi\API\Kernel $apiKernel
63 public function __construct($apiKernel) {
64 $this->apiKernel
= $apiKernel;
65 $this->actions
= array(
66 'Entity' => array('get', 'getactions'),
67 '*' => array('getactions'), // 'getfields'
72 * @param \Civi\API\Event\ResolveEvent $event
74 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
75 $apiRequest = $event->getApiRequest();
76 $actions = isset($this->actions
[$apiRequest['entity']]) ?
$this->actions
[$apiRequest['entity']] : $this->actions
['*'];
77 if (in_array($apiRequest['action'], $actions)) {
78 $apiRequest['is_metadata'] = TRUE;
79 $event->setApiRequest($apiRequest);
80 $event->setApiProvider($this);
81 $event->stopPropagation(); // TODO decide if we really want to override others
86 * @param \Civi\API\Event\AuthorizeEvent $event
88 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent
$event) {
89 $apiRequest = $event->getApiRequest();
90 if (isset($apiRequest['is_metadata'])) {
91 // if (\CRM_Core_Permission::check('access AJAX API') || \CRM_Core_Permission::check('access CiviCRM')) {
93 $event->stopPropagation();
101 public function invoke($apiRequest) {
102 if ($apiRequest['entity'] == 'Entity' && $apiRequest['action'] == 'get') {
103 return civicrm_api3_create_success($this->apiKernel
->getEntityNames($apiRequest['version']));
105 switch ($apiRequest['action']) {
107 return civicrm_api3_create_success($this->apiKernel
->getActionNames($apiRequest['version'], $apiRequest['entity']));
109 // return $this->doGetFields($apiRequest);
113 // We shouldn't get here because onApiResolve() checks $this->actions
114 throw new \
API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
120 function getEntityNames($version) {
121 return array('Entity');
127 function getActionNames($version, $entity) {
128 return isset($this->actions
[$entity]) ?
$this->actions
[$entity] : $this->actions
['*'];