Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Action; | |
4 | ||
5 | use Civi\API\Exception\NotImplementedException; | |
6 | use Civi\Api4\Generic\BasicGetAction; | |
7 | use Civi\Api4\Utils\ActionUtil; | |
8 | use Civi\Api4\Utils\ReflectionUtils; | |
9 | ||
10 | /** | |
11 | * Get actions for an entity with a list of accepted params | |
12 | */ | |
13 | class GetActions extends BasicGetAction { | |
14 | ||
15 | private $_actions = []; | |
16 | ||
17 | private $_actionsToGet; | |
18 | ||
19 | protected function getRecords() { | |
20 | $this->_actionsToGet = $this->_itemsToGet('name'); | |
21 | ||
22 | $entityReflection = new \ReflectionClass('\Civi\Api4\\' . $this->_entityName); | |
23 | foreach ($entityReflection->getMethods(\ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC) as $method) { | |
24 | $actionName = $method->getName(); | |
25 | if ($actionName != 'permissions' && $actionName[0] != '_') { | |
26 | $this->loadAction($actionName); | |
27 | } | |
28 | } | |
29 | if (!$this->_actionsToGet || count($this->_actionsToGet) > count($this->_actions)) { | |
30 | // Search entity-specific actions (including those provided by extensions) | |
31 | foreach (\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles() as $ext) { | |
32 | $dir = \CRM_Utils_File::addTrailingSlash(dirname($ext['filePath'])); | |
33 | $this->scanDir($dir . 'Civi/Api4/Action/' . $this->_entityName); | |
34 | } | |
35 | } | |
36 | ksort($this->_actions); | |
37 | return $this->_actions; | |
38 | } | |
39 | ||
40 | /** | |
41 | * @param $dir | |
42 | */ | |
43 | private function scanDir($dir) { | |
44 | if (is_dir($dir)) { | |
45 | foreach (glob("$dir/*.php") as $file) { | |
46 | $matches = []; | |
47 | preg_match('/(\w*).php/', $file, $matches); | |
48 | $actionName = array_pop($matches); | |
49 | $actionClass = new \ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName . '\\' . $actionName); | |
50 | if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) { | |
51 | $this->loadAction(lcfirst($actionName)); | |
52 | } | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | /** | |
58 | * @param $actionName | |
59 | */ | |
60 | private function loadAction($actionName) { | |
61 | try { | |
62 | if (!isset($this->_actions[$actionName]) && (!$this->_actionsToGet || in_array($actionName, $this->_actionsToGet))) { | |
63 | $action = ActionUtil::getAction($this->getEntityName(), $actionName); | |
64 | if (is_object($action)) { | |
65 | $this->_actions[$actionName] = ['name' => $actionName]; | |
66 | if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) { | |
67 | $actionReflection = new \ReflectionClass($action); | |
68 | $actionInfo = ReflectionUtils::getCodeDocs($actionReflection); | |
69 | unset($actionInfo['method']); | |
70 | $this->_actions[$actionName] += $actionInfo; | |
71 | } | |
72 | if ($this->_isFieldSelected('params')) { | |
73 | $this->_actions[$actionName]['params'] = $action->getParamInfo(); | |
74 | // Language param is only relevant on multilingual sites | |
75 | $languageLimit = (array) \Civi::settings()->get('languageLimit'); | |
76 | if (count($languageLimit) < 2) { | |
77 | unset($this->_actions[$actionName]['params']['language']); | |
78 | } | |
79 | elseif (isset($this->_actions[$actionName]['params']['language'])) { | |
80 | $this->_actions[$actionName]['params']['language']['options'] = array_keys($languageLimit); | |
81 | } | |
82 | } | |
83 | } | |
84 | } | |
85 | } | |
86 | catch (NotImplementedException $e) { | |
87 | } | |
88 | } | |
89 | ||
90 | public function fields() { | |
91 | return [ | |
92 | [ | |
93 | 'name' => 'name', | |
94 | 'data_type' => 'String', | |
95 | ], | |
96 | [ | |
97 | 'name' => 'description', | |
98 | 'data_type' => 'String', | |
99 | ], | |
100 | [ | |
101 | 'name' => 'comment', | |
102 | 'data_type' => 'String', | |
103 | ], | |
104 | [ | |
105 | 'name' => 'params', | |
106 | 'data_type' => 'Array', | |
107 | ], | |
108 | ]; | |
109 | } | |
110 | ||
111 | } |