*
* Scans for api entities in core, enabled components & enabled extensions.
*
- * Also includes pseudo-entities from multi-record custom groups by default.
- *
- * @method $this setIncludeCustom(bool $value)
- * @method bool getIncludeCustom()
+ * Also includes pseudo-entities from multi-record custom groups.
*/
class Get extends \Civi\Api4\Generic\BasicGetAction {
/**
- * Include custom-field-based pseudo-entities?
- *
* @var bool
+ * @deprecated
*/
- protected $includeCustom = TRUE;
+ protected $includeCustom;
/**
* Scan all api directories to discover entities
*/
protected function getRecords() {
$entities = [];
- $toGet = $this->_itemsToGet('name');
- $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
- array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
- );
- $enabledComponents = array_keys(\CRM_Core_Component::getEnabledComponents());
- foreach ($locations as $location) {
- $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4';
- if (is_dir($dir)) {
- foreach (glob("$dir/*.php") as $file) {
- $matches = [];
- preg_match('/(\w*)\.php$/', $file, $matches);
- $className = '\Civi\Api4\\' . $matches[1];
- if (is_a($className, '\Civi\Api4\Generic\AbstractEntity', TRUE)) {
- $info = $className::getInfo();
- $entityName = $info['name'];
- $daoName = $info['dao'] ?? NULL;
- // Only include DAO entities from enabled components
- if ((!$toGet || in_array($entityName, $toGet)) &&
- (!$daoName || !defined("{$daoName}::COMPONENT") || in_array($daoName::COMPONENT, $enabledComponents))
- ) {
- $entities[$info['name']] = $info;
- }
+ $namesRequested = $this->_itemsToGet('name');
+
+ if ($namesRequested) {
+ foreach ($namesRequested as $entityName) {
+ if (strpos($entityName, 'Custom_') !== 0) {
+ $className = CoreUtil::getApiClass($entityName);
+ if ($className) {
+ $this->loadEntity($className, $entities);
}
}
}
}
+ else {
+ foreach ($this->getAllApiClasses() as $className) {
+ $this->loadEntity($className, $entities);
+ }
+ }
// Fetch custom entities unless we've already fetched everything requested
- if ($this->includeCustom && (!$toGet || array_diff($toGet, array_keys($entities)))) {
+ if (!$namesRequested || array_diff($namesRequested, array_keys($entities))) {
$this->addCustomEntities($entities);
}
return $entities;
}
+ /**
+ * @param \Civi\Api4\Generic\AbstractEntity $className
+ * @param array $entities
+ */
+ private function loadEntity($className, array &$entities) {
+ $info = $className::getInfo();
+ $daoName = $info['dao'] ?? NULL;
+ // Only include DAO entities from enabled components
+ if (!$daoName || !defined("{$daoName}::COMPONENT") || array_key_exists($daoName::COMPONENT, \CRM_Core_Component::getEnabledComponents())) {
+ $entities[$info['name']] = $info;
+ }
+ }
+
+ /**
+ * @return \Civi\Api4\Generic\AbstractEntity[]
+ */
+ private function getAllApiClasses() {
+ $classNames = [];
+ $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
+ array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
+ );
+ foreach ($locations as $location) {
+ $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4';
+ if (is_dir($dir)) {
+ foreach (glob("$dir/*.php") as $file) {
+ $className = 'Civi\Api4\\' . basename($file, '.php');
+ if (is_a($className, 'Civi\Api4\Generic\AbstractEntity', TRUE)) {
+ $classNames[] = $className;
+ }
+ }
+ }
+ }
+ return $classNames;
+ }
+
/**
* Add custom-field pseudo-entities
*