Ai
1 Star 0 Fork 0

wfdaj/custom-php-mvc-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
run_migrations.php 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
Chad Chapman 提交于 2024-08-05 09:44 +08:00 . Mostly caught up with base project
<?php
use Core\DB;
use Dotenv\Dotenv;
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
require_once('./vendor/autoload.php');
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
// load configuration and helper functions
require_once(ROOT . DS . 'config' . DS . 'config.php');
$isCli = php_sapi_name() == 'cli';
if(!RUN_MIGRATIONS_FROM_BROWSER && !$isCli) die('restricted');
function autoload($className){
$classAry = explode('\\',$className);
$class = array_pop($classAry);
$subPath = strtolower(implode(DS,$classAry));
$path = ROOT . DS . $subPath . DS . $class . '.php';
if(file_exists($path)){
require_once($path);
}
}
spl_autoload_register('autoload');
$db = DB::getInstance();
$migrationTable = $db->query("SHOW TABLES LIKE 'migrations'")->results();
$previousMigs = [];
$migrationsRun = [];
if(!empty($migrationTable)){
$query = $db->query("SELECT migration FROM migrations")->results();
foreach($query as $q){
$previousMigs[] = $q->migration;
}
}
// get all files
$migrations = glob('migrations'.DS.'*.php');
foreach($migrations as $fileName){
$klass = str_replace('migrations'.DS,'',$fileName);
$klass = str_replace('.php','',$klass);
if(!in_array($klass,$previousMigs)){
$klassNamespace = 'Migrations\\'.$klass;
$mig = new $klassNamespace($isCli);
$mig->up();
$db->insert('migrations',['migration'=>$klass]);
$migrationsRun[] = $klassNamespace;
}
}
if(sizeof($migrationsRun) == 0){
if($isCli){
echo "\e[0;37;42m\n\n"." No new migrations to run.\n\e[0m\n";
} else {
echo '<p style="color:#006600;">No new migrations to run.</p>';
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wfdaj/custom-php-mvc-framework.git
git@gitee.com:wfdaj/custom-php-mvc-framework.git
wfdaj
custom-php-mvc-framework
custom-php-mvc-framework
main

搜索帮助