public function groupWrite($group, $groupConfig) { $sourceFile = "{$this->configPath}/{$group}.php"; $targetFile = "{$this->configPath}/{$this->environment}/{$group}.php"; if (!$this->files->exists($sourceFile)) { throw new \RuntimeException("Config:{$group} not exisits"); } // 如果是强制写入或者目标文件不存在的情况下, copy if ($this->overwrite || !$this->files->exists($targetFile)) { if (!$this->files->copy($sourceFile, $targetFile)) { throw new \RuntimeException("Copy config:{$group} failed"); } $content = file_get_contents($targetFile); foreach ($groupConfig as $key => $value) { $pattern[] = '%' . strtoupper($key) . '%'; $replacements[] = $value; } $content = str_replace($pattern, $replacements, $content); if (!file_put_contents($targetFile, $content)) { throw new \RuntimeException('Writing config file ' . $group . '... fail.'); } } else { logger::info("Copy config:{$group}, current is not overwrite mode and target file already exists, ignore copy."); } }
} /** *改变文件权限 */ public function change_file_mode($path, $mode) { if (is_numeric($mode) && is_file($path)) { chmod($path, $mode); } } /** * 创建新的文件夹 */ public function make_new_filedir($path, $dir_name) { $path = $path . self::system_dir . $dir_name; mkdir($path); } /** *创建新的文件 */ public function make_new_file($path, $file_name) { $path = $path . self::system_dir . $file_name; $file = fopen($path, 'w'); fclose($file); } } //加入test代码 $test = new base_filesystem(); $test->make_new_file("/home/zhangchao/Downloads", "apple.txt");