3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
12 namespace Civi\API\Provider
;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
18 * This class manages the loading of API's using strict file+function naming
21 class MagicFunctionProvider
implements EventSubscriberInterface
, ProviderInterface
{
26 public static function getSubscribedEvents() {
29 ['onApiResolve', Events
::W_MIDDLE
],
35 * @var array (string $cachekey => array('function' => string, 'is_generic' => bool))
41 public function __construct() {
46 * @param \Civi\API\Event\ResolveEvent $event
47 * API resolution event.
49 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
50 $apiRequest = $event->getApiRequest();
51 $resolved = $this->resolve($apiRequest);
52 if ($resolved['function']) {
53 $apiRequest +
= $resolved;
54 $event->setApiRequest($apiRequest);
55 $event->setApiProvider($this);
56 $event->stopPropagation();
62 * @param array $apiRequest
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.
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]);
81 $result = $function($apiRequest);
84 elseif ($apiRequest['function'] && !$apiRequest['is_generic']) {
85 $result = $function($apiRequest['params']);
95 public function getEntityNames($version) {
97 $include_dirs = array_unique(explode(PATH_SEPARATOR
, get_include_path()));
98 #$include_dirs = array(dirname(__FILE__). '/../../');
99 foreach ($include_dirs as $include_dir) {
100 $api_dir = implode(DIRECTORY_SEPARATOR
,
101 [$include_dir, 'api', 'v' . $version]);
102 if (!is_dir($api_dir)) {
105 $iterator = new \
DirectoryIterator($api_dir);
106 foreach ($iterator as $fileinfo) {
107 $file = $fileinfo->getFilename();
109 // Check for entities with a master file ("api/v3/MyEntity.php")
110 $parts = explode(".", $file);
111 if (end($parts) == "php" && $file != "utils.php" && !preg_match('/Tests?.php$/', $file)) {
112 // without the ".php"
113 $entities[] = substr($file, 0, -4);
116 // Check for entities with standalone action files (eg "api/v3/MyEntity/MyAction.php").
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) {
125 $entities = array_diff($entities, ['Generic']);
126 $entities = array_unique($entities);
134 * @param int $version
135 * @param string $entity
138 public function getActionNames($version, $entity) {
139 $entity = _civicrm_api_get_camel_name($entity);
140 $entities = $this->getEntityNames($version);
141 if (!in_array($entity, $entities)) {
144 $this->loadEntity($entity, $version);
146 $functions = get_defined_functions();
148 $prefix = 'civicrm_api' . $version . '_' . _civicrm_api_get_entity_name_from_camel($entity) . '_';
149 $prefixGeneric = 'civicrm_api' . $version . '_generic_';
150 foreach ($functions['user'] as $fct) {
151 if (strpos($fct, $prefix) === 0) {
152 $actions[] = substr($fct, strlen($prefix));
154 elseif (strpos($fct, $prefixGeneric) === 0) {
155 $actions[] = substr($fct, strlen($prefixGeneric));
162 * Look up the implementation for a given API request.
164 * @param array $apiRequest
166 * - entity: string, required.
167 * - action: string, required.
169 * - version: scalar, required.
173 * - function: callback (mixed)
174 * - is_generic: boolean
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];
182 $camelName = _civicrm_api_get_camel_name($apiRequest['entity'], $apiRequest['version']);
183 $actionCamelName = _civicrm_api_get_camel_name($apiRequest['action']);
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
189 // FIXME: This has the affect of masking bugs in load order; this is
190 // included to provide bug-compatibility.
191 $this->cache
[$cachekey] = ['function' => $stdFunction, 'is_generic' => FALSE];
192 return $this->cache
[$cachekey];
196 // By convention, the $camelName.php is more likely to contain the
197 // function, so test it first
198 'api/v' . $apiRequest['version'] . '/' . $camelName . '.php',
199 'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php',
201 foreach ($stdFiles as $stdFile) {
202 if (\CRM_Utils_File
::isIncludable($stdFile)) {
203 require_once $stdFile;
204 if (function_exists($stdFunction)) {
205 $this->cache
[$cachekey] = ['function' => $stdFunction, 'is_generic' => FALSE];
206 return $this->cache
[$cachekey];
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']);
216 // By convention, the Generic.php is more likely to contain the
217 // function, so test it first
218 'api/v' . $apiRequest['version'] . '/Generic.php',
219 'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php',
221 foreach ($genericFiles as $genericFile) {
222 if (\CRM_Utils_File
::isIncludable($genericFile)) {
223 require_once $genericFile;
224 if (function_exists($genericFunction)) {
225 $this->cache
[$cachekey] = ['function' => $genericFunction, 'is_generic' => TRUE];
226 return $this->cache
[$cachekey];
231 $this->cache
[$cachekey] = ['function' => FALSE, 'is_generic' => FALSE];
232 return $this->cache
[$cachekey];
236 * Determine the function name for a given API request.
238 * @param string $entity
240 * @param string $action
242 * @param int $version
247 protected function getFunctionName($entity, $action, $version) {
248 $entity = _civicrm_api_get_entity_name_from_camel($entity);
249 return 'civicrm_api' . $version . '_' . $entity . '_' . $action;
253 * Load/require all files related to an entity.
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").
258 * @param string $entity
260 * @param int $version
263 protected function loadEntity($entity, $version) {
264 $camelName = _civicrm_api_get_camel_name($entity, $version);
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;
272 // Check for standalone action files; to match _civicrm_api_resolve(), only load the first one
273 // array($relativeFilePath => TRUE)
275 $include_dirs = array_unique(explode(PATH_SEPARATOR
, get_include_path()));
276 foreach ($include_dirs as $include_dir) {
277 foreach ([$camelName, 'Generic'] as $name) {
278 $action_dir = implode(DIRECTORY_SEPARATOR
,
279 [$include_dir, 'api', "v${version}", $name]);
280 if (!is_dir($action_dir)) {
284 $iterator = new \
DirectoryIterator($action_dir);
285 foreach ($iterator as $fileinfo) {
286 $file = $fileinfo->getFilename();
287 if (array_key_exists($file, $loaded_files)) {
288 // action provided by an earlier item on include_path
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;