Commit | Line | Data |
---|---|---|
82376c19 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
82376c19 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
82376c19 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
82376c19 TO |
27 | |
28 | namespace Civi\API\Provider; | |
8882ff5c | 29 | |
82376c19 TO |
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 { | |
6550386a EM |
37 | /** |
38 | * @return array | |
39 | */ | |
82376c19 TO |
40 | public static function getSubscribedEvents() { |
41 | return array( | |
42 | Events::RESOLVE => array( | |
8882ff5c TO |
43 | // TODO decide if we really want to override others |
44 | array('onApiResolve', Events::W_EARLY), | |
82376c19 TO |
45 | ), |
46 | Events::AUTHORIZE => array( | |
8882ff5c TO |
47 | // TODO decide if we really want to override others |
48 | array('onApiAuthorize', Events::W_EARLY), | |
82376c19 TO |
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 | |
8882ff5c | 65 | * The API kernel. |
82376c19 TO |
66 | */ |
67 | public function __construct($apiKernel) { | |
68 | $this->apiKernel = $apiKernel; | |
69 | $this->actions = array( | |
70 | 'Entity' => array('get', 'getactions'), | |
71 | '*' => array('getactions'), // 'getfields' | |
72 | ); | |
73 | } | |
74 | ||
6550386a EM |
75 | /** |
76 | * @param \Civi\API\Event\ResolveEvent $event | |
8882ff5c | 77 | * API resolution event. |
6550386a | 78 | */ |
82376c19 TO |
79 | public function onApiResolve(\Civi\API\Event\ResolveEvent $event) { |
80 | $apiRequest = $event->getApiRequest(); | |
92776611 | 81 | $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']); |
82376c19 TO |
82 | if (in_array($apiRequest['action'], $actions)) { |
83 | $apiRequest['is_metadata'] = TRUE; | |
84 | $event->setApiRequest($apiRequest); | |
85 | $event->setApiProvider($this); | |
8882ff5c TO |
86 | $event->stopPropagation(); |
87 | // TODO decide if we really want to override others | |
82376c19 TO |
88 | } |
89 | } | |
90 | ||
6550386a EM |
91 | /** |
92 | * @param \Civi\API\Event\AuthorizeEvent $event | |
8882ff5c | 93 | * API authorization event. |
6550386a | 94 | */ |
82376c19 TO |
95 | public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) { |
96 | $apiRequest = $event->getApiRequest(); | |
97 | if (isset($apiRequest['is_metadata'])) { | |
8882ff5c TO |
98 | // if (\CRM_Core_Permission::check('access AJAX API') |
99 | // || \CRM_Core_Permission::check('access CiviCRM')) { | |
82376c19 TO |
100 | $event->authorize(); |
101 | $event->stopPropagation(); | |
102 | // } | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * {inheritdoc} | |
257e7666 EM |
108 | * @param array $apiRequest |
109 | * @return array | |
110 | * @throws \API_Exception | |
82376c19 TO |
111 | */ |
112 | public function invoke($apiRequest) { | |
a14e9d08 CW |
113 | if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') { |
114 | return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get'); | |
82376c19 TO |
115 | } |
116 | switch ($apiRequest['action']) { | |
117 | case 'getactions': | |
a14e9d08 | 118 | return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']); |
8882ff5c TO |
119 | |
120 | //case 'getfields': | |
121 | // return $this->doGetFields($apiRequest); | |
122 | ||
82376c19 TO |
123 | default: |
124 | } | |
125 | ||
126 | // We shouldn't get here because onApiResolve() checks $this->actions | |
8882ff5c | 127 | throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']'); |
82376c19 TO |
128 | } |
129 | ||
130 | /** | |
131 | * {inheritdoc} | |
257e7666 EM |
132 | * @param int $version |
133 | * @return array | |
82376c19 | 134 | */ |
8882ff5c | 135 | public function getEntityNames($version) { |
82376c19 TO |
136 | return array('Entity'); |
137 | } | |
138 | ||
139 | /** | |
140 | * {inheritdoc} | |
257e7666 EM |
141 | * @param int $version |
142 | * @param string $entity | |
143 | * @return array | |
82376c19 | 144 | */ |
8882ff5c | 145 | public function getActionNames($version, $entity) { |
92776611 | 146 | $entity = _civicrm_api_get_camel_name($entity, $version); |
82376c19 TO |
147 | return isset($this->actions[$entity]) ? $this->actions[$entity] : $this->actions['*']; |
148 | } | |
96025800 | 149 | |
6550386a | 150 | } |