fileManager = $fileManager; $this->injectableFactory = $injectableFactory; $this->fileReader = $fileReader; } public function get(string $scope, string $name): ?string { if ( $this->sanitizeInput($scope) !== $scope || $this->sanitizeInput($name) !== $name ) { throw new Error("Bad parameters."); } $path = 'layouts/' . $scope . '/' . $name . '.json'; $params = FileReaderParams::create() ->withScope($scope); if ($this->fileReader->exists($path, $params)) { return $this->fileReader->read($path, $params); } return $this->getDefault($scope, $name); } private function getDefault(string $scope, string $name): ?string { $defaultImplClassName = 'Espo\\Custom\\Classes\\DefaultLayouts\\' . ucfirst($name) . 'Type'; if (!class_exists($defaultImplClassName)) { $defaultImplClassName = 'Espo\\Classes\\DefaultLayouts\\' . ucfirst($name) . 'Type'; } if (class_exists($defaultImplClassName)) { // @todo Use factory and interface. $defaultImpl = $this->injectableFactory->create($defaultImplClassName); if (!method_exists($defaultImpl, 'get')) { throw new Error("No 'get' method in '{$defaultImplClassName}'."); } $data = $defaultImpl->get($scope); return Json::encode($data); } $filePath = $this->defaultPath . '/' . $name . '.json'; if (!$this->fileManager->isFile($filePath)) { return null; } return $this->fileManager->getContents($filePath); } protected function sanitizeInput(string $name): string { /** @var string */ return preg_replace("([\.]{2,})", '', $name); } }