*/ private $defaultTypeMap = [ 'CategoryTree' => self::RECORD_TREE_SERVICE_NAME, ]; /** * @var array> */ private $data = []; private ServiceFactory $serviceFactory; private Metadata $metadata; public function __construct(ServiceFactory $serviceFactory, Metadata $metadata) { $this->serviceFactory = $serviceFactory; $this->metadata = $metadata; } /** * @return Service<\Espo\ORM\Entity> */ public function get(string $entityType): Service { if (!array_key_exists($entityType, $this->data)) { $this->load($entityType); } return $this->data[$entityType]; } private function load(string $entityType): void { if (!$this->metadata->get(['scopes', $entityType, 'entity'])) { throw new Error("Can't create record service '{$entityType}', there's no such entity type."); } if ($this->serviceFactory->checkExists($entityType)) { $service = $this->serviceFactory->createWith($entityType, ['entityType' => $entityType]); if (!$service instanceof Service) { $this->loadDefault($entityType); return; } $this->data[$entityType] = $service; return; } $this->loadDefault($entityType); } private function loadDefault(string $entityType): void { $default = self::RECORD_SERVICE_NAME; $type = $this->metadata->get(['scopes', $entityType, 'type']); if ($type) { $default = $this->defaultTypeMap[$type] ?? $default; } $obj = $this->serviceFactory->createWith($default, ['entityType' => $entityType]); if (!$obj instanceof Service) { throw new RuntimeException("Service class {$default} is not instance of Record."); } //$obj->setEntityType($entityType); $this->data[$entityType] = $obj; } }