fileManager = $fileManager; $this->metadata = $metadata; $this->useObjects = $useObjects; } /** * Unite file content to the file. * * @param string $name * @param array $paths * @param boolean $recursively Note: only for first level of sub directory, * other levels of sub directories will be ignored. * * @return array|object */ public function unify(string $name, array $paths, bool $recursively = false) { $content = $this->unifySingle($paths['corePath'], $name, $recursively); if (!empty($paths['modulePath'])) { $customDir = strstr($paths['modulePath'], '{*}', true); $moduleList = isset($this->metadata) ? $this->metadata->getModuleList() : $this->fileManager->getFileList($customDir, false, '', false); foreach ($moduleList as $moduleName) { $curPath = str_replace('{*}', $moduleName, $paths['modulePath']); if ($this->useObjects) { $content = DataUtil::merge( $content, $this->unifySingle($curPath, $name, $recursively, $moduleName) ); } else { $content = Util::merge( $content, $this->unifySingle($curPath, $name, $recursively, $moduleName) ); } } } if (!empty($paths['customPath'])) { if ($this->useObjects) { $content = DataUtil::merge( $content, $this->unifySingle($paths['customPath'], $name, $recursively) ); } else { $content = Util::merge( $content, $this->unifySingle($paths['customPath'], $name, $recursively) ); } } return $content; } /** * Unite file content to the file for one directory. * * @param string $dirPath * @param string $type Name of type array("metadata", "layouts"), ex. $this->name. * @param bool $recursively Note: only for first level of sub directory, * other levels of sub directories will be ignored. * @param string $moduleName Name of module if exists. * * @return string Content of the files. */ protected function unifySingle($dirPath, $type, $recursively = false, $moduleName = '') { $content = []; $unsets = []; if ($this->useObjects) { $content = (object) []; } if (empty($dirPath) || !file_exists($dirPath)) { return $content; } $fileList = $this->fileManager->getFileList($dirPath, $recursively, '\.json$'); $dirName = $this->fileManager->getDirName($dirPath, false); foreach ($fileList as $dirName => $fileName) { if (is_array($fileName)) { /*only first level of a sub-directory*/ $itemValue = $this->unifySingle( Util::concatPath($dirPath, $dirName), $type, false, $moduleName ); if ($this->useObjects) { $content->$dirName = $itemValue; } else { $content[$dirName] = $itemValue; } continue; } 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) { $content->$name = $itemValue; } else { $content[$name] = $itemValue; } } if ($this->useObjects) { $content = DataUtil::unsetByKey($content, $unsets); } else { $content = Util::unsetInArray($content, $unsets); } return $content; } /** * Get content from files for unite files. */ protected 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}'." ); } } }