Fix api getActions to also load generic actions
[civicrm-core.git] / Civi / API / Provider / MagicFunctionProvider.php
index f3162041b5e67d449ff55c26bce03ca60ac31bb8..74a38dbcac5d3d90474674ecf538cbcdd792169e 100644 (file)
@@ -34,6 +34,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  * conventions.
  */
 class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterface {
+  /**
+   * @return array
+   */
   public static function getSubscribedEvents() {
     return array(
       Events::RESOLVE => array(
@@ -47,10 +50,16 @@ class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterfa
    */
   private $cache;
 
+  /**
+   *
+   */
   function __construct() {
     $this->cache = array();
   }
 
+  /**
+   * @param \Civi\API\Event\ResolveEvent $event
+   */
   public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
     $apiRequest = $event->getApiRequest();
     $resolved = $this->resolve($apiRequest);
@@ -130,7 +139,7 @@ class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterfa
 
     $functions = get_defined_functions();
     $actions = array();
-    $prefix = 'civicrm_api' . $version . '_' . strtolower($entity) . '_';
+    $prefix = 'civicrm_api' . $version . '_' . _civicrm_api_get_entity_name_from_camel($entity) . '_';
     $prefixGeneric = 'civicrm_api' . $version . '_generic_';
     foreach ($functions['user'] as $fct) {
       if (strpos($fct, $prefix) === 0) {
@@ -248,22 +257,24 @@ class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterfa
     $loaded_files = array(); // array($relativeFilePath => TRUE)
     $include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path()));
     foreach ($include_dirs as $include_dir) {
-      $action_dir = implode(DIRECTORY_SEPARATOR, array($include_dir, 'api', "v${version}", $camelName));
-      if (!is_dir($action_dir)) {
-        continue;
-      }
-
-      $iterator = new \DirectoryIterator($action_dir);
-      foreach ($iterator as $fileinfo) {
-        $file = $fileinfo->getFilename();
-        if (array_key_exists($file, $loaded_files)) {
-          continue; // action provided by an earlier item on include_path
+      foreach (array($camelName, 'Generic') as $name) {
+        $action_dir = implode(DIRECTORY_SEPARATOR, array($include_dir, 'api', "v${version}", $name));
+        if (!is_dir($action_dir)) {
+          continue;
         }
 
-        $parts = explode(".", $file);
-        if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file)) {
-          require_once $action_dir . DIRECTORY_SEPARATOR . $file;
-          $loaded_files[$file] = TRUE;
+        $iterator = new \DirectoryIterator($action_dir);
+        foreach ($iterator as $fileinfo) {
+          $file = $fileinfo->getFilename();
+          if (array_key_exists($file, $loaded_files)) {
+            continue; // action provided by an earlier item on include_path
+          }
+
+          $parts = explode(".", $file);
+          if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file)) {
+            require_once $action_dir . DIRECTORY_SEPARATOR . $file;
+            $loaded_files[$file] = TRUE;
+          }
         }
       }
     }