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