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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Action
;
24 use Civi\API\Exception\NotImplementedException
;
25 use Civi\Api4\Generic\BasicGetAction
;
26 use Civi\Api4\Utils\ReflectionUtils
;
29 * Get all API actions for the $ENTITY entity.
31 * Includes a list of accepted parameters for each action, descriptions and other documentation.
33 class GetActions
extends BasicGetAction
{
35 private $_actions = [];
37 private $_actionsToGet;
39 protected function getRecords() {
40 $this->_actionsToGet
= $this->_itemsToGet('name');
42 $entityReflection = new \
ReflectionClass('\Civi\Api4\\' . $this->_entityName
);
43 foreach ($entityReflection->getMethods(\ReflectionMethod
::IS_STATIC | \ReflectionMethod
::IS_PUBLIC
) as $method) {
44 $actionName = $method->getName();
45 if ($actionName != 'permissions' && $actionName[0] != '_') {
46 $this->loadAction($actionName, $method);
49 if (!$this->_actionsToGet ||
count($this->_actionsToGet
) > count($this->_actions
)) {
50 // Search for entity-specific actions in extensions
51 foreach (\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
52 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($ext['filePath']));
53 $this->scanDir($dir . 'Civi/Api4/Action/' . $this->_entityName
);
55 // Search for entity-specific actions in core
56 $this->scanDir(\CRM_Utils_File
::addTrailingSlash(__DIR__
) . $this->_entityName
);
58 ksort($this->_actions
);
59 return $this->_actions
;
65 private function scanDir($dir) {
67 foreach (glob("$dir/*.php") as $file) {
69 preg_match('/(\w*)\.php$/', $file, $matches);
70 $actionName = array_pop($matches);
71 $actionClass = new \
ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName
. '\\' . $actionName);
72 if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) {
73 $this->loadAction(lcfirst($actionName));
81 * @param \ReflectionMethod $method
83 private function loadAction($actionName, $method = NULL) {
85 if (!isset($this->_actions
[$actionName]) && (!$this->_actionsToGet ||
in_array($actionName, $this->_actionsToGet
))) {
86 $action = \Civi\API\Request
::create($this->getEntityName(), $actionName, ['version' => 4]);
87 if (is_object($action)) {
88 $this->_actions
[$actionName] = ['name' => $actionName];
89 if ($this->_isFieldSelected('description', 'comment', 'see')) {
90 $vars = ['entity' => $this->getEntityName(), 'action' => $actionName];
91 // Docblock from action class
92 $actionDocs = ReflectionUtils
::getCodeDocs($action->reflect(), NULL, $vars);
93 unset($actionDocs['method']);
94 // Docblock from action factory function in entity class. This takes precedence since most action classes are generic.
96 $methodDocs = ReflectionUtils
::getCodeDocs($method, 'Method', $vars);
97 // Allow method doc to inherit class doc
98 if (strpos($method->getDocComment(), '@inheritDoc') !== FALSE && !empty($methodDocs['comment']) && !empty($actionDocs['comment'])) {
99 $methodDocs['comment'] .= "\n\n" . $actionDocs['comment'];
101 $actionDocs = array_filter($methodDocs) +
$actionDocs;
103 $this->_actions
[$actionName] +
= $actionDocs;
105 if ($this->_isFieldSelected('params')) {
106 $this->_actions
[$actionName]['params'] = $action->getParamInfo();
107 // Language param is only relevant on multilingual sites
108 $languageLimit = (array) \Civi
::settings()->get('languageLimit');
109 if (count($languageLimit) < 2) {
110 unset($this->_actions
[$actionName]['params']['language']);
112 elseif (isset($this->_actions
[$actionName]['params']['language'])) {
113 $this->_actions
[$actionName]['params']['language']['options'] = array_keys($languageLimit);
119 catch (NotImplementedException
$e) {
123 public function fields() {
127 'description' => 'Action name',
130 'name' => 'description',
131 'description' => 'Description from docblock',
135 'description' => 'Comments from docblock',
139 'data_type' => 'Array',
140 'description' => 'Any @see annotations from docblock',
144 'description' => 'List of all accepted parameters',
145 'data_type' => 'Array',