3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 namespace Civi\API\Provider
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * This class manages the loading of API's using strict file+function naming
37 class MagicFunctionProvider
implements EventSubscriberInterface
, ProviderInterface
{
41 public static function getSubscribedEvents() {
43 Events
::RESOLVE
=> array(
44 array('onApiResolve', Events
::W_MIDDLE
),
50 * @var array (string $cachekey => array('function' => string, 'is_generic' => bool))
57 public function __construct() {
58 $this->cache
= array();
62 * @param \Civi\API\Event\ResolveEvent $event
63 * API resolution event.
65 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
66 $apiRequest = $event->getApiRequest();
67 $resolved = $this->resolve($apiRequest);
68 if ($resolved['function']) {
69 $apiRequest +
= $resolved;
70 $event->setApiRequest($apiRequest);
71 $event->setApiProvider($this);
72 $event->stopPropagation();
79 public function invoke($apiRequest) {
80 $function = $apiRequest['function'];
81 if ($apiRequest['function'] && $apiRequest['is_generic']) {
82 // Unlike normal API implementations, generic implementations require explicit
83 // knowledge of the entity and action (as well as $params). Bundle up these bits
84 // into a convenient data structure.
85 $result = $function($apiRequest);
87 elseif ($apiRequest['function'] && !$apiRequest['is_generic']) {
88 $result = isset($extra) ?
$function($apiRequest['params'], $extra) : $function($apiRequest['params']);
96 public function getEntityNames($version) {
98 $include_dirs = array_unique(explode(PATH_SEPARATOR
, get_include_path()));
99 #$include_dirs = array(dirname(__FILE__). '/../../');
100 foreach ($include_dirs as $include_dir) {
101 $api_dir = implode(DIRECTORY_SEPARATOR
,
102 array($include_dir, 'api', 'v' . $version));
103 if (!is_dir($api_dir)) {
106 $iterator = new \
DirectoryIterator($api_dir);
107 foreach ($iterator as $fileinfo) {
108 $file = $fileinfo->getFilename();
110 // Check for entities with a master file ("api/v3/MyEntity.php")
111 $parts = explode(".", $file);
112 if (end($parts) == "php" && $file != "utils.php" && !preg_match('/Tests?.php$/', $file)) {
113 // without the ".php"
114 $entities[] = substr($file, 0, -4);
117 // Check for entities with standalone action files (eg "api/v3/MyEntity/MyAction.php").
118 $action_dir = $api_dir . DIRECTORY_SEPARATOR
. $file;
119 if (preg_match('/^[A-Z][A-Za-z0-9]*$/', $file) && is_dir($action_dir)) {
120 if (count(glob("$action_dir/[A-Z]*.php")) > 0) {
126 $entities = array_diff($entities, array('Generic'));
127 $entities = array_unique($entities);
136 public function getActionNames($version, $entity) {
137 $entity = _civicrm_api_get_camel_name($entity);
138 $entities = $this->getEntityNames($version);
139 if (!in_array($entity, $entities)) {
142 $this->loadEntity($entity, $version);
144 $functions = get_defined_functions();
146 $prefix = 'civicrm_api' . $version . '_' . _civicrm_api_get_entity_name_from_camel($entity) . '_';
147 $prefixGeneric = 'civicrm_api' . $version . '_generic_';
148 foreach ($functions['user'] as $fct) {
149 if (strpos($fct, $prefix) === 0) {
150 $actions[] = substr($fct, strlen($prefix));
152 elseif (strpos($fct, $prefixGeneric) === 0) {
153 $actions[] = substr($fct, strlen($prefixGeneric));
160 * Look up the implementation for a given API request
162 * @param array $apiRequest
164 * - entity: string, required.
165 * - action: string, required.
167 * - version: scalar, required.
171 * - function: callback (mixed)
172 * - is_generic: boolean
174 protected function resolve($apiRequest) {
175 $cachekey = strtolower($apiRequest['entity']) . ':' . strtolower($apiRequest['action']) . ':' . $apiRequest['version'];
176 if (isset($this->cache
[$cachekey])) {
177 return $this->cache
[$cachekey];
180 $camelName = _civicrm_api_get_camel_name($apiRequest['entity'], $apiRequest['version']);
181 $actionCamelName = _civicrm_api_get_camel_name($apiRequest['action']);
183 // Determine if there is an entity-specific implementation of the action
184 $stdFunction = $this->getFunctionName($apiRequest['entity'], $apiRequest['action'], $apiRequest['version']);
185 if (function_exists($stdFunction)) {
186 // someone already loaded the appropriate file
187 // FIXME: This has the affect of masking bugs in load order; this is
188 // included to provide bug-compatibility.
189 $this->cache
[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE);
190 return $this->cache
[$cachekey];
194 // By convention, the $camelName.php is more likely to contain the
195 // function, so test it first
196 'api/v' . $apiRequest['version'] . '/' . $camelName . '.php',
197 'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php',
199 foreach ($stdFiles as $stdFile) {
200 if (\CRM_Utils_File
::isIncludable($stdFile)) {
201 require_once $stdFile;
202 if (function_exists($stdFunction)) {
203 $this->cache
[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE);
204 return $this->cache
[$cachekey];
209 // Determine if there is a generic implementation of the action
210 require_once 'api/v3/Generic.php';
211 # $genericFunction = 'civicrm_api3_generic_' . $apiRequest['action'];
212 $genericFunction = $this->getFunctionName('generic', $apiRequest['action'], $apiRequest['version']);
213 $genericFiles = array(
214 // By convention, the Generic.php is more likely to contain the
215 // function, so test it first
216 'api/v' . $apiRequest['version'] . '/Generic.php',
217 'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php',
219 foreach ($genericFiles as $genericFile) {
220 if (\CRM_Utils_File
::isIncludable($genericFile)) {
221 require_once $genericFile;
222 if (function_exists($genericFunction)) {
223 $this->cache
[$cachekey] = array('function' => $genericFunction, 'is_generic' => TRUE);
224 return $this->cache
[$cachekey];
229 $this->cache
[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE);
230 return $this->cache
[$cachekey];
234 * Determine the function name for a given API request.
236 * @param string $entity
238 * @param string $action
240 * @param int $version
245 protected function getFunctionName($entity, $action, $version) {
246 $entity = _civicrm_api_get_entity_name_from_camel($entity);
247 return 'civicrm_api' . $version . '_' . $entity . '_' . $action;
251 * Load/require all files related to an entity.
253 * This should not normally be called because it's does a file-system scan; it's
254 * only appropriate when introspection is really required (eg for "getActions").
256 * @param string $entity
258 * @param int $version
261 protected function loadEntity($entity, $version) {
262 $camelName = _civicrm_api_get_camel_name($entity, $version);
264 // Check for master entity file; to match _civicrm_api_resolve(), only load the first one
265 $stdFile = 'api/v' . $version . '/' . $camelName . '.php';
266 if (\CRM_Utils_File
::isIncludable($stdFile)) {
267 require_once $stdFile;
270 // Check for standalone action files; to match _civicrm_api_resolve(), only load the first one
271 $loaded_files = array(); // array($relativeFilePath => TRUE)
272 $include_dirs = array_unique(explode(PATH_SEPARATOR
, get_include_path()));
273 foreach ($include_dirs as $include_dir) {
274 foreach (array($camelName, 'Generic') as $name) {
275 $action_dir = implode(DIRECTORY_SEPARATOR
,
276 array($include_dir, 'api', "v${version}", $name));
277 if (!is_dir($action_dir)) {
281 $iterator = new \
DirectoryIterator($action_dir);
282 foreach ($iterator as $fileinfo) {
283 $file = $fileinfo->getFilename();
284 if (array_key_exists($file, $loaded_files)) {
285 continue; // action provided by an earlier item on include_path
288 $parts = explode(".", $file);
289 if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file)) {
290 require_once $action_dir . DIRECTORY_SEPARATOR
. $file;
291 $loaded_files[$file] = TRUE;