Commit | Line | Data |
---|---|---|
787604ff TO |
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 { | |
6550386a EM |
37 | /** |
38 | * @return array | |
39 | */ | |
787604ff TO |
40 | public static function getSubscribedEvents() { |
41 | return array( | |
42 | Events::RESOLVE => array( | |
43 | array('onApiResolve', Events::W_MIDDLE), | |
44 | ), | |
45 | ); | |
46 | } | |
47 | ||
c65db512 TO |
48 | /** |
49 | * @var array (string $cachekey => array('function' => string, 'is_generic' => bool)) | |
50 | */ | |
51 | private $cache; | |
52 | ||
6550386a EM |
53 | /** |
54 | * | |
55 | */ | |
c65db512 TO |
56 | function __construct() { |
57 | $this->cache = array(); | |
58 | } | |
59 | ||
6550386a EM |
60 | /** |
61 | * @param \Civi\API\Event\ResolveEvent $event | |
62 | */ | |
787604ff TO |
63 | public function onApiResolve(\Civi\API\Event\ResolveEvent $event) { |
64 | $apiRequest = $event->getApiRequest(); | |
c65db512 | 65 | $resolved = $this->resolve($apiRequest); |
787604ff TO |
66 | if ($resolved['function']) { |
67 | $apiRequest += $resolved; | |
68 | $event->setApiRequest($apiRequest); | |
69 | $event->setApiProvider($this); | |
70 | $event->stopPropagation(); | |
71 | } | |
72 | } | |
73 | ||
82376c19 TO |
74 | /** |
75 | * {inheritdoc} | |
76 | */ | |
787604ff TO |
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 | ||
82376c19 TO |
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) { | |
92776611 | 134 | $entity = _civicrm_api_get_camel_name($entity); |
82376c19 TO |
135 | $entities = $this->getEntityNames($version); |
136 | if (!in_array($entity, $entities)) { | |
137 | return array(); | |
c65db512 TO |
138 | } |
139 | $this->loadEntity($entity, $version); | |
140 | ||
141 | $functions = get_defined_functions(); | |
142 | $actions = array(); | |
6cbff284 | 143 | $prefix = 'civicrm_api' . $version . '_' . _civicrm_api_get_entity_name_from_camel($entity) . '_'; |
c65db512 TO |
144 | $prefixGeneric = 'civicrm_api' . $version . '_generic_'; |
145 | foreach ($functions['user'] as $fct) { | |
146 | if (strpos($fct, $prefix) === 0) { | |
147 | $actions[] = substr($fct, strlen($prefix)); | |
148 | } | |
149 | elseif (strpos($fct, $prefixGeneric) === 0) { | |
150 | $actions[] = substr($fct, strlen($prefixGeneric)); | |
151 | } | |
152 | } | |
153 | return $actions; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Look up the implementation for a given API request | |
158 | * | |
159 | * @param $apiRequest array with keys: | |
160 | * - entity: string, required | |
161 | * - action: string, required | |
162 | * - params: array | |
163 | * - version: scalar, required | |
164 | * | |
165 | * @return array with keys | |
166 | * - function: callback (mixed) | |
167 | * - is_generic: boolean | |
168 | */ | |
169 | protected function resolve($apiRequest) { | |
170 | $cachekey = strtolower($apiRequest['entity']) . ':' . strtolower($apiRequest['action']) . ':' . $apiRequest['version']; | |
171 | if (isset($this->cache[$cachekey])) { | |
172 | return $this->cache[$cachekey]; | |
173 | } | |
174 | ||
175 | $camelName = _civicrm_api_get_camel_name($apiRequest['entity'], $apiRequest['version']); | |
176 | $actionCamelName = _civicrm_api_get_camel_name($apiRequest['action']); | |
177 | ||
178 | // Determine if there is an entity-specific implementation of the action | |
179 | $stdFunction = $this->getFunctionName($apiRequest['entity'], $apiRequest['action'], $apiRequest['version']); | |
180 | if (function_exists($stdFunction)) { | |
181 | // someone already loaded the appropriate file | |
182 | // FIXME: This has the affect of masking bugs in load order; this is included to provide bug-compatibility | |
183 | $this->cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE); | |
184 | return $this->cache[$cachekey]; | |
185 | } | |
186 | ||
187 | $stdFiles = array( | |
188 | // By convention, the $camelName.php is more likely to contain the function, so test it first | |
189 | 'api/v' . $apiRequest['version'] . '/' . $camelName . '.php', | |
190 | 'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php', | |
191 | ); | |
192 | foreach ($stdFiles as $stdFile) { | |
193 | if (\CRM_Utils_File::isIncludable($stdFile)) { | |
194 | require_once $stdFile; | |
195 | if (function_exists($stdFunction)) { | |
196 | $this->cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE); | |
197 | return $this->cache[$cachekey]; | |
198 | } | |
199 | } | |
200 | } | |
201 | ||
202 | // Determine if there is a generic implementation of the action | |
203 | require_once 'api/v3/Generic.php'; | |
204 | # $genericFunction = 'civicrm_api3_generic_' . $apiRequest['action']; | |
205 | $genericFunction = $this->getFunctionName('generic', $apiRequest['action'], $apiRequest['version']); | |
206 | $genericFiles = array( | |
207 | // By convention, the Generic.php is more likely to contain the function, so test it first | |
208 | 'api/v' . $apiRequest['version'] . '/Generic.php', | |
209 | 'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php', | |
210 | ); | |
211 | foreach ($genericFiles as $genericFile) { | |
212 | if (\CRM_Utils_File::isIncludable($genericFile)) { | |
213 | require_once $genericFile; | |
214 | if (function_exists($genericFunction)) { | |
215 | $this->cache[$cachekey] = array('function' => $genericFunction, 'is_generic' => TRUE); | |
216 | return $this->cache[$cachekey]; | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | $this->cache[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE); | |
222 | return $this->cache[$cachekey]; | |
223 | } | |
224 | ||
225 | /** | |
226 | * @param string $entity | |
227 | * @param string $action | |
2a6da8d7 EM |
228 | * @param $version |
229 | * | |
c65db512 TO |
230 | * @return string |
231 | */ | |
232 | protected function getFunctionName($entity, $action, $version) { | |
233 | $entity = _civicrm_api_get_entity_name_from_camel($entity); | |
234 | return 'civicrm_api' . $version . '_' . $entity . '_' . $action; | |
235 | } | |
236 | ||
237 | /** | |
238 | * Load/require all files related to an entity. | |
239 | * | |
240 | * This should not normally be called because it's does a file-system scan; it's | |
241 | * only appropriate when introspection is really required (eg for "getActions"). | |
242 | * | |
243 | * @param string $entity | |
244 | * @param int $version | |
245 | * | |
246 | * @return void | |
247 | */ | |
248 | protected function loadEntity($entity, $version) { | |
249 | $camelName = _civicrm_api_get_camel_name($entity, $version); | |
250 | ||
251 | // Check for master entity file; to match _civicrm_api_resolve(), only load the first one | |
252 | $stdFile = 'api/v' . $version . '/' . $camelName . '.php'; | |
253 | if (\CRM_Utils_File::isIncludable($stdFile)) { | |
254 | require_once $stdFile; | |
255 | } | |
256 | ||
257 | // Check for standalone action files; to match _civicrm_api_resolve(), only load the first one | |
258 | $loaded_files = array(); // array($relativeFilePath => TRUE) | |
259 | $include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path())); | |
260 | foreach ($include_dirs as $include_dir) { | |
07007b7a CW |
261 | foreach (array($camelName, 'Generic') as $name) { |
262 | $action_dir = implode(DIRECTORY_SEPARATOR, array($include_dir, 'api', "v${version}", $name)); | |
263 | if (!is_dir($action_dir)) { | |
264 | continue; | |
c65db512 TO |
265 | } |
266 | ||
07007b7a CW |
267 | $iterator = new \DirectoryIterator($action_dir); |
268 | foreach ($iterator as $fileinfo) { | |
269 | $file = $fileinfo->getFilename(); | |
270 | if (array_key_exists($file, $loaded_files)) { | |
271 | continue; // action provided by an earlier item on include_path | |
272 | } | |
273 | ||
274 | $parts = explode(".", $file); | |
275 | if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file)) { | |
276 | require_once $action_dir . DIRECTORY_SEPARATOR . $file; | |
277 | $loaded_files[$file] = TRUE; | |
278 | } | |
c65db512 TO |
279 | } |
280 | } | |
281 | } | |
282 | } | |
283 | ||
2a6da8d7 | 284 | } |