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