4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Action
;
15 use Civi\API\Exception\NotImplementedException
;
16 use Civi\Api4\Generic\BasicGetAction
;
17 use Civi\Api4\Utils\ReflectionUtils
;
20 * Get all API actions for the $ENTITY entity.
22 * Includes a list of accepted parameters for each action, descriptions and other documentation.
24 class GetActions
extends BasicGetAction
{
26 private $_actions = [];
28 private $_actionsToGet;
30 protected function getRecords() {
31 $this->_actionsToGet
= $this->_itemsToGet('name');
33 $entityReflection = new \
ReflectionClass('\Civi\Api4\\' . $this->_entityName
);
34 foreach ($entityReflection->getMethods(\ReflectionMethod
::IS_STATIC | \ReflectionMethod
::IS_PUBLIC
) as $method) {
35 $actionName = $method->getName();
36 if ($actionName != 'permissions' && $actionName != 'getInfo' && $actionName[0] != '_') {
37 $this->loadAction($actionName, $method);
40 if (!$this->_actionsToGet ||
count($this->_actionsToGet
) > count($this->_actions
)) {
41 // Search for entity-specific actions in extensions
42 foreach (\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
43 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($ext['filePath']));
44 $this->scanDir($dir . 'Civi/Api4/Action/' . $this->_entityName
);
46 // Search for entity-specific actions in core
47 $this->scanDir(\CRM_Utils_File
::addTrailingSlash(__DIR__
) . $this->_entityName
);
49 ksort($this->_actions
);
50 return $this->_actions
;
56 private function scanDir($dir) {
58 foreach (glob("$dir/*.php") as $file) {
60 preg_match('/(\w*)\.php$/', $file, $matches);
61 $actionName = array_pop($matches);
62 $actionClass = new \
ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName
. '\\' . $actionName);
63 if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) {
64 $this->loadAction(lcfirst($actionName));
72 * @param \ReflectionMethod $method
74 private function loadAction($actionName, $method = NULL) {
76 if (!isset($this->_actions
[$actionName]) && (!$this->_actionsToGet ||
in_array($actionName, $this->_actionsToGet
))) {
77 $action = \Civi\API\Request
::create($this->getEntityName(), $actionName, ['version' => 4]);
78 if (is_object($action)) {
79 $this->_actions
[$actionName] = ['name' => $actionName];
80 if ($this->_isFieldSelected('description', 'comment', 'see')) {
81 $vars = ['entity' => $this->getEntityName(), 'action' => $actionName];
82 // Docblock from action class
83 $actionDocs = ReflectionUtils
::getCodeDocs($action->reflect(), NULL, $vars);
84 unset($actionDocs['method']);
85 // Docblock from action factory function in entity class. This takes precedence since most action classes are generic.
87 $methodDocs = ReflectionUtils
::getCodeDocs($method, 'Method', $vars);
88 // Allow method doc to inherit class doc
89 if (strpos($method->getDocComment(), '@inheritDoc') !== FALSE && !empty($methodDocs['comment']) && !empty($actionDocs['comment'])) {
90 $methodDocs['comment'] .= "\n\n" . $actionDocs['comment'];
92 $actionDocs = array_filter($methodDocs) +
$actionDocs;
94 $this->_actions
[$actionName] +
= $actionDocs;
96 if ($this->_isFieldSelected('params')) {
97 $this->_actions
[$actionName]['params'] = $action->getParamInfo();
98 // Language param is only relevant on multilingual sites
99 $languageLimit = (array) \Civi
::settings()->get('languageLimit');
100 if (count($languageLimit) < 2) {
101 unset($this->_actions
[$actionName]['params']['language']);
103 elseif (isset($this->_actions
[$actionName]['params']['language'])) {
104 $this->_actions
[$actionName]['params']['language']['options'] = array_keys($languageLimit);
110 catch (NotImplementedException
$e) {
114 public function fields() {
118 'description' => 'Action name',
121 'name' => 'description',
122 'description' => 'Description from docblock',
126 'description' => 'Comments from docblock',
130 'data_type' => 'Array',
131 'description' => 'Any @see annotations from docblock',
135 'description' => 'List of all accepted parameters',
136 'data_type' => 'Array',