Commit | Line | Data |
---|---|---|
787604ff TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
787604ff | 5 | +--------------------------------------------------------------------+ |
39de6fd5 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
787604ff 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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | namespace Civi\API\Provider; | |
8882ff5c | 29 | |
787604ff TO |
30 | use Civi\API\Events; |
31 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
32 | ||
33 | /** | |
34 | * This class manages the loading of API's using strict file+function naming | |
35 | * conventions. | |
36 | */ | |
37 | class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterface { | |
6550386a EM |
38 | /** |
39 | * @return array | |
40 | */ | |
787604ff TO |
41 | public static function getSubscribedEvents() { |
42 | return array( | |
43 | Events::RESOLVE => array( | |
44 | array('onApiResolve', Events::W_MIDDLE), | |
45 | ), | |
46 | ); | |
47 | } | |
48 | ||
c65db512 TO |
49 | /** |
50 | * @var array (string $cachekey => array('function' => string, 'is_generic' => bool)) | |
51 | */ | |
52 | private $cache; | |
53 | ||
6550386a EM |
54 | /** |
55 | * | |
56 | */ | |
8882ff5c | 57 | public function __construct() { |
c65db512 TO |
58 | $this->cache = array(); |
59 | } | |
60 | ||
6550386a EM |
61 | /** |
62 | * @param \Civi\API\Event\ResolveEvent $event | |
8882ff5c | 63 | * API resolution event. |
6550386a | 64 | */ |
787604ff TO |
65 | public function onApiResolve(\Civi\API\Event\ResolveEvent $event) { |
66 | $apiRequest = $event->getApiRequest(); | |
c65db512 | 67 | $resolved = $this->resolve($apiRequest); |
787604ff TO |
68 | if ($resolved['function']) { |
69 | $apiRequest += $resolved; | |
70 | $event->setApiRequest($apiRequest); | |
71 | $event->setApiProvider($this); | |
72 | $event->stopPropagation(); | |
73 | } | |
74 | } | |
75 | ||
82376c19 TO |
76 | /** |
77 | * {inheritdoc} | |
78 | */ | |
787604ff TO |
79 | public function invoke($apiRequest) { |
80 | $function = $apiRequest['function']; | |
81 | if ($apiRequest['function'] && $apiRequest['is_generic']) { | |
82 | // Unlike normal API implementations, generic implementations require explicit | |
83 | // knowledge of the entity and action (as well as $params). Bundle up these bits | |
84 | // into a convenient data structure. | |
85 | $result = $function($apiRequest); | |
86 | } | |
87 | elseif ($apiRequest['function'] && !$apiRequest['is_generic']) { | |
88 | $result = isset($extra) ? $function($apiRequest['params'], $extra) : $function($apiRequest['params']); | |
89 | } | |
90 | return $result; | |
91 | } | |
92 | ||
82376c19 TO |
93 | /** |
94 | * {inheritdoc} | |
95 | */ | |
8882ff5c | 96 | public function getEntityNames($version) { |
82376c19 TO |
97 | $entities = array(); |
98 | $include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path())); | |
99 | #$include_dirs = array(dirname(__FILE__). '/../../'); | |
100 | foreach ($include_dirs as $include_dir) { | |
8882ff5c TO |
101 | $api_dir = implode(DIRECTORY_SEPARATOR, |
102 | array($include_dir, 'api', 'v' . $version)); | |
103 | if (!is_dir($api_dir)) { | |
82376c19 TO |
104 | continue; |
105 | } | |
106 | $iterator = new \DirectoryIterator($api_dir); | |
107 | foreach ($iterator as $fileinfo) { | |
108 | $file = $fileinfo->getFilename(); | |
109 | ||
110 | // Check for entities with a master file ("api/v3/MyEntity.php") | |
111 | $parts = explode(".", $file); | |
8882ff5c | 112 | if (end($parts) == "php" && $file != "utils.php" && !preg_match('/Tests?.php$/', $file)) { |
82376c19 TO |
113 | // without the ".php" |
114 | $entities[] = substr($file, 0, -4); | |
115 | } | |
116 | ||
8882ff5c | 117 | // Check for entities with standalone action files (eg "api/v3/MyEntity/MyAction.php"). |
82376c19 TO |
118 | $action_dir = $api_dir . DIRECTORY_SEPARATOR . $file; |
119 | if (preg_match('/^[A-Z][A-Za-z0-9]*$/', $file) && is_dir($action_dir)) { | |
120 | if (count(glob("$action_dir/[A-Z]*.php")) > 0) { | |
121 | $entities[] = $file; | |
122 | } | |
123 | } | |
124 | } | |
125 | } | |
126 | $entities = array_diff($entities, array('Generic')); | |
127 | $entities = array_unique($entities); | |
128 | sort($entities); | |
129 | ||
130 | return $entities; | |
131 | } | |
132 | ||
133 | /** | |
134 | * {inheritdoc} | |
135 | */ | |
136 | public function getActionNames($version, $entity) { | |
92776611 | 137 | $entity = _civicrm_api_get_camel_name($entity); |
82376c19 TO |
138 | $entities = $this->getEntityNames($version); |
139 | if (!in_array($entity, $entities)) { | |
140 | return array(); | |
c65db512 TO |
141 | } |
142 | $this->loadEntity($entity, $version); | |
143 | ||
144 | $functions = get_defined_functions(); | |
145 | $actions = array(); | |
6cbff284 | 146 | $prefix = 'civicrm_api' . $version . '_' . _civicrm_api_get_entity_name_from_camel($entity) . '_'; |
c65db512 TO |
147 | $prefixGeneric = 'civicrm_api' . $version . '_generic_'; |
148 | foreach ($functions['user'] as $fct) { | |
149 | if (strpos($fct, $prefix) === 0) { | |
150 | $actions[] = substr($fct, strlen($prefix)); | |
151 | } | |
152 | elseif (strpos($fct, $prefixGeneric) === 0) { | |
153 | $actions[] = substr($fct, strlen($prefixGeneric)); | |
154 | } | |
155 | } | |
156 | return $actions; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Look up the implementation for a given API request | |
161 | * | |
8882ff5c TO |
162 | * @param array $apiRequest |
163 | * Array with keys: | |
164 | * - entity: string, required. | |
165 | * - action: string, required. | |
166 | * - params: array. | |
167 | * - version: scalar, required. | |
c65db512 | 168 | * |
8882ff5c TO |
169 | * @return array |
170 | * Array with keys: | |
171 | * - function: callback (mixed) | |
172 | * - is_generic: boolean | |
c65db512 TO |
173 | */ |
174 | protected function resolve($apiRequest) { | |
175 | $cachekey = strtolower($apiRequest['entity']) . ':' . strtolower($apiRequest['action']) . ':' . $apiRequest['version']; | |
176 | if (isset($this->cache[$cachekey])) { | |
177 | return $this->cache[$cachekey]; | |
178 | } | |
179 | ||
180 | $camelName = _civicrm_api_get_camel_name($apiRequest['entity'], $apiRequest['version']); | |
181 | $actionCamelName = _civicrm_api_get_camel_name($apiRequest['action']); | |
182 | ||
183 | // Determine if there is an entity-specific implementation of the action | |
184 | $stdFunction = $this->getFunctionName($apiRequest['entity'], $apiRequest['action'], $apiRequest['version']); | |
185 | if (function_exists($stdFunction)) { | |
186 | // someone already loaded the appropriate file | |
8882ff5c TO |
187 | // FIXME: This has the affect of masking bugs in load order; this is |
188 | // included to provide bug-compatibility. | |
c65db512 TO |
189 | $this->cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE); |
190 | return $this->cache[$cachekey]; | |
191 | } | |
192 | ||
193 | $stdFiles = array( | |
8882ff5c TO |
194 | // By convention, the $camelName.php is more likely to contain the |
195 | // function, so test it first | |
c65db512 TO |
196 | 'api/v' . $apiRequest['version'] . '/' . $camelName . '.php', |
197 | 'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php', | |
198 | ); | |
199 | foreach ($stdFiles as $stdFile) { | |
200 | if (\CRM_Utils_File::isIncludable($stdFile)) { | |
201 | require_once $stdFile; | |
202 | if (function_exists($stdFunction)) { | |
203 | $this->cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE); | |
204 | return $this->cache[$cachekey]; | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | // Determine if there is a generic implementation of the action | |
210 | require_once 'api/v3/Generic.php'; | |
211 | # $genericFunction = 'civicrm_api3_generic_' . $apiRequest['action']; | |
212 | $genericFunction = $this->getFunctionName('generic', $apiRequest['action'], $apiRequest['version']); | |
213 | $genericFiles = array( | |
8882ff5c TO |
214 | // By convention, the Generic.php is more likely to contain the |
215 | // function, so test it first | |
c65db512 TO |
216 | 'api/v' . $apiRequest['version'] . '/Generic.php', |
217 | 'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php', | |
218 | ); | |
219 | foreach ($genericFiles as $genericFile) { | |
220 | if (\CRM_Utils_File::isIncludable($genericFile)) { | |
221 | require_once $genericFile; | |
222 | if (function_exists($genericFunction)) { | |
223 | $this->cache[$cachekey] = array('function' => $genericFunction, 'is_generic' => TRUE); | |
224 | return $this->cache[$cachekey]; | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | $this->cache[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE); | |
230 | return $this->cache[$cachekey]; | |
231 | } | |
232 | ||
233 | /** | |
8882ff5c TO |
234 | * Determine the function name for a given API request. |
235 | * | |
c65db512 | 236 | * @param string $entity |
8882ff5c | 237 | * API entity name. |
c65db512 | 238 | * @param string $action |
8882ff5c TO |
239 | * API action name. |
240 | * @param int $version | |
241 | * API version. | |
2a6da8d7 | 242 | * |
c65db512 TO |
243 | * @return string |
244 | */ | |
245 | protected function getFunctionName($entity, $action, $version) { | |
246 | $entity = _civicrm_api_get_entity_name_from_camel($entity); | |
247 | return 'civicrm_api' . $version . '_' . $entity . '_' . $action; | |
248 | } | |
249 | ||
250 | /** | |
251 | * Load/require all files related to an entity. | |
252 | * | |
253 | * This should not normally be called because it's does a file-system scan; it's | |
254 | * only appropriate when introspection is really required (eg for "getActions"). | |
255 | * | |
256 | * @param string $entity | |
8882ff5c | 257 | * API entity name. |
c65db512 | 258 | * @param int $version |
8882ff5c | 259 | * API version. |
c65db512 TO |
260 | */ |
261 | protected function loadEntity($entity, $version) { | |
262 | $camelName = _civicrm_api_get_camel_name($entity, $version); | |
263 | ||
264 | // Check for master entity file; to match _civicrm_api_resolve(), only load the first one | |
265 | $stdFile = 'api/v' . $version . '/' . $camelName . '.php'; | |
266 | if (\CRM_Utils_File::isIncludable($stdFile)) { | |
267 | require_once $stdFile; | |
268 | } | |
269 | ||
270 | // Check for standalone action files; to match _civicrm_api_resolve(), only load the first one | |
271 | $loaded_files = array(); // array($relativeFilePath => TRUE) | |
272 | $include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path())); | |
273 | foreach ($include_dirs as $include_dir) { | |
07007b7a | 274 | foreach (array($camelName, 'Generic') as $name) { |
8882ff5c TO |
275 | $action_dir = implode(DIRECTORY_SEPARATOR, |
276 | array($include_dir, 'api', "v${version}", $name)); | |
07007b7a CW |
277 | if (!is_dir($action_dir)) { |
278 | continue; | |
c65db512 TO |
279 | } |
280 | ||
07007b7a CW |
281 | $iterator = new \DirectoryIterator($action_dir); |
282 | foreach ($iterator as $fileinfo) { | |
283 | $file = $fileinfo->getFilename(); | |
284 | if (array_key_exists($file, $loaded_files)) { | |
285 | continue; // action provided by an earlier item on include_path | |
286 | } | |
287 | ||
288 | $parts = explode(".", $file); | |
289 | if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file)) { | |
290 | require_once $action_dir . DIRECTORY_SEPARATOR . $file; | |
291 | $loaded_files[$file] = TRUE; | |
292 | } | |
c65db512 TO |
293 | } |
294 | } | |
295 | } | |
296 | } | |
297 | ||
2a6da8d7 | 298 | } |