fileManager = $fileManager; $this->module = $module; $this->pathProvider = $pathProvider; } /** * Merge data of resource files. * * @return array|\stdClass */ public function unify(string $path, bool $noCustom = false) { if ($this->useObjects) { return $this->unifyObject($path, $noCustom); } return $this->unifyArray($path, $noCustom); } /** * @return array */ private function unifyArray(string $path, bool $noCustom = false) { /** @var array */ $data = $this->unifySingle($this->pathProvider->getCore() . $path, true); foreach ($this->getModuleList() as $moduleName) { $filePath = $this->pathProvider->getModule($moduleName) . $path; /** @var array */ $newData = $this->unifySingle($filePath, true); /** @var array */ $data = Util::merge($data, $newData); } if ($noCustom) { return $data; } $customFilePath = $this->pathProvider->getCustom() . $path; /** @var array */ $newData = $this->unifySingle($customFilePath, true); /** @var array */ return Util::merge($data, $newData); } /** * @return \stdClass */ private function unifyObject(string $path, bool $noCustom = false) { /** @var \stdClass */ $data = $this->unifySingle($this->pathProvider->getCore() . $path, true); foreach ($this->getModuleList() as $moduleName) { $filePath = $this->pathProvider->getModule($moduleName) . $path; /** @var \stdClass */ $data = DataUtil::merge( $data, $this->unifySingle($filePath, true) ); } if ($noCustom) { return $data; } $customFilePath = $this->pathProvider->getCustom() . $path; /** @var \stdClass */ return DataUtil::merge( $data, $this->unifySingle($customFilePath, true) ); } /** * @return array|\stdClass */ private function unifySingle(string $dirPath, bool $recursively) { $data = []; $unsets = []; if ($this->useObjects) { $data = (object) []; } if (empty($dirPath) || !$this->fileManager->exists($dirPath)) { return $data; } $fileList = $this->fileManager->getFileList($dirPath, $recursively, '\.json$'); $dirName = $this->fileManager->getDirName($dirPath, false); foreach ($fileList as $dirName => $item) { if (is_array($item)) { /** @var string $dirName */ // Only a first level of a sub-directory. $itemValue = $this->unifySingle( Util::concatPath($dirPath, $dirName), false ); if ($this->useObjects) { /** @var \stdClass $data */ $data->$dirName = $itemValue; continue; } /** @var array $data */ $data[$dirName] = $itemValue; continue; } /** @var string $item */ $fileName = $item; if ($fileName === $this->unsetFileName) { $fileContent = $this->fileManager->getContents($dirPath . '/' . $fileName); $unsets = Json::decode($fileContent, true); continue; } $itemValue = $this->getContents($dirPath . '/' . $fileName); if (empty($itemValue)) { continue; } $name = $this->fileManager->getFileName($fileName, '.json'); if ($this->useObjects) { /** @var \stdClass $data */ $data->$name = $itemValue; continue; } /** @var array $data */ $data[$name] = $itemValue; } if ($this->useObjects) { /** @var \stdClass $data */ /** @var \stdClass */ return DataUtil::unsetByKey($data, $unsets); } /** @var array $data */ /** @var array */ return Util::unsetInArray($data, $unsets); } /** * @return \stdClass|array * @throws JsonException */ private function getContents(string $path) { $fileContent = $this->fileManager->getContents($path); try { return Json::decode($fileContent, !$this->useObjects); } catch (JsonException $e) { throw new JsonException( "JSON syntax error in '{$path}'." ); } } /** * @return string[] */ private function getModuleList(): array { return $this->module->getOrderedList(); } }