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