Ai
1 Star 0 Fork 0

.何以致契阔/thinkphp 数据库迁移工具

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
UsePhinx.php 4.62 KB
一键复制 编辑 原始数据 按行查看 历史
.何以致契阔 提交于 2024-10-08 09:51 +08:00 . up
<?php
namespace tlan_turing\migration;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Composer\Script\Event;
class UsePhinx
{
public static function run(Event $event)
{
if (!$event->isDevMode()) {
return;
}
$files = [
'vendor/robmorgan/phinx/LICENSE' => 'phinx/LICENSE',
'vendor/robmorgan/phinx/README.md' => 'phinx/README.md',
'vendor/robmorgan/phinx/src/Phinx/Config/' => 'phinx/Config/',
'vendor/robmorgan/phinx/src/Phinx/Db/' => 'phinx/Db/',
'vendor/robmorgan/phinx/src/Phinx/Migration/' => 'phinx/Migration/',
'vendor/robmorgan/phinx/src/Phinx/Seed/' => 'phinx/Seed/',
'vendor/robmorgan/phinx/src/Phinx/Util/' => 'phinx/Util/',
];
$io = $event->getIO();
$fs = new Filesystem;
//clear
$fs->remove('phinx');
foreach ($files as $from => $to) {
// check pattern
$pattern = null;
if (strpos($from, '#') > 0) {
[$from, $pattern] = explode('#', $from, 2);
}
// check the overwrite newer files disable flag (? in end of path)
$overwriteNewerFiles = substr($to, -1) != '?';
if (!$overwriteNewerFiles) {
$to = substr($to, 0, -1);
}
// Check the renaming of file for direct moving (file-to-file)
$isRenameFile = substr($to, -1) != '/' && !is_dir($from);
if (file_exists($to) && !is_dir($to) && !$isRenameFile) {
throw new \InvalidArgumentException('Destination directory is not a directory.');
}
try {
if ($isRenameFile) {
$fs->mkdir(dirname($to));
} else {
$fs->mkdir($to);
}
} catch (IOException $e) {
throw new \InvalidArgumentException(sprintf('<error>Could not create directory %s.</error>', $to), $e->getCode(), $e);
}
if (false === file_exists($from)) {
throw new \InvalidArgumentException(sprintf('<error>Source directory or file "%s" does not exist.</error>', $from));
}
if (is_dir($from)) {
$finder = new Finder;
$finder->files()->ignoreDotFiles(false)->in($from);
if ($pattern) {
$finder->path("#{$pattern}#");
}
foreach ($finder as $file) {
$dest = sprintf('%s/%s', $to, $file->getRelativePathname());
try {
$fs->copy($file, $dest, $overwriteNewerFiles);
// replace namespace
$content = file_get_contents($dest);
$replaces = [
'use Symfony\Component\Console\Input\InputInterface;' => 'use think\console\Input as InputInterface;',
'use Symfony\Component\Console\Output\OutputInterface;' => 'use think\console\Output as OutputInterface;',
'\Symfony\Component\Console\Output\OutputInterface' => '\think\console\Output',
'\Symfony\Component\Console\Input\InputInterface' => '\think\console\Input',
'use Symfony\Component\Console\Output\NullOutput;' => 'use think\migration\NullOutput;',
];
$content = str_replace(array_keys($replaces), array_values($replaces), $content);
file_put_contents($dest, $content);
} catch (IOException $e) {
throw new \InvalidArgumentException(sprintf('<error>Could not copy %s</error>', $file->getBaseName()), $e->getCode(), $e);
}
}
} else {
try {
if ($isRenameFile) {
$fs->copy($from, $to, $overwriteNewerFiles);
} else {
$fs->copy($from, $to . '/' . basename($from), $overwriteNewerFiles);
}
} catch (IOException $e) {
throw new \InvalidArgumentException(sprintf('<error>Could not copy %s</error>', $from), $e->getCode(), $e);
}
}
$io->write(sprintf('Copied file(s) from <comment>%s</comment> to <comment>%s</comment>.', $from, $to));
}
//clear
$fs->remove('vendor/robmorgan/phinx');
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tlan_turing/turing-migration.git
git@gitee.com:tlan_turing/turing-migration.git
tlan_turing
turing-migration
thinkphp 数据库迁移工具
master

搜索帮助