Last active
July 9, 2020 02:31
-
-
Save PJZ9n/3628cb61e01e508a8054a926ca290c9d to your computer and use it in GitHub Desktop.
ワールドをコピーする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ワールドをコピーする | |
* | |
* @param string $from コピー元ワールド名(ディレクトリ名) | |
* @param string $to コピー先ワールド名(ディレクトリ名) | |
*/ | |
public function copyWorld(string $from, string $to): void | |
{ | |
/** @var Plugin $this */ | |
$server = $this->getServer(); | |
$baseWorldPath = $server->getDataPath() . "worlds/"; | |
$fromPath = $baseWorldPath . $from . "/"; | |
$toPath = $baseWorldPath . $to . "/"; | |
if (!file_exists($fromPath)) throw new \RuntimeException("コピー元フォルダが存在しません。"); | |
if (file_exists($toPath)) throw new \RuntimeException("コピー先フォルダが既に存在します。"); | |
$iterator = new \RecursiveIteratorIterator( | |
new \RecursiveDirectoryIterator( | |
$fromPath, | |
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | |
), | |
\RecursiveIteratorIterator::LEAVES_ONLY | |
); | |
foreach ($iterator as $pathName) { | |
//コピー先パスに置換 | |
$replacedPathName = str_replace($fromPath, $toPath, $pathName); | |
//ディレクトリ作成(エラー抑制しなくない場合、別途例外処理) | |
@mkdir(dirname($replacedPathName), 0755, true); | |
//コピー実行 | |
copy($pathName, $replacedPathName); | |
//表示 | |
$this->getLogger()->debug("copy: " . $pathName . " =>" . $replacedPathName); | |
} | |
} | |
$this->copyWorld("world", "world_copy"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment