加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
phinx.php 4.57 KB
一键复制 编辑 原始数据 按行查看 历史
小牛New 提交于 2019-12-14 12:31 . style(any): code poem
<?php
declare(strict_types=1);
/*
* This file is part of the your app package.
*
* The PHP Application For Code Poem For You.
* (c) 2018-2099 http://yourdomian.com All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidFileException;
use Dotenv\Exception\InvalidPathException;
use Leevel\Di\Container;
use Leevel\Kernel\App;
use Leevel\Kernel\IApp;
use Symfony\Component\Console\Input\ArgvInput;
/**
* ---------------------------------------------------------------
* Composer
* ---------------------------------------------------------------.
*
* 用于管理 PHP 依赖包
*/
require __DIR__.'/vendor/autoload.php';
/**
* ---------------------------------------------------------------
* 创建应用
* ---------------------------------------------------------------.
*
* 注册应用基础服务
*/
$container = Container::singletons();
$app = new App($container, realpath(__DIR__));
/*
* ---------------------------------------------------------------
* 载入环境
* ---------------------------------------------------------------.
*
* 读取 phinx 运行环境
*/
$input = new ArgvInput();
if ($input->hasParameterOption('-e')) {
$env = $input->getParameterOption('-e');
} elseif ($input->hasParameterOption('--environment')) {
$env = $input->getParameterOption('--environment');
} else {
$env = 'env';
}
putenv('RUNTIME_ENVIRONMENT='.$env);
/**
* 载入配置.
*/
class PhinxLoad
{
/**
* 执行入口.
*/
public function handle(IApp $app): array
{
$this->checkRuntimeEnv($app);
return $this->loadEnvData($app);
}
/**
* 载入环境变量数据.
*/
private function loadEnvData(IApp $app): array
{
$oldEnv = $_ENV;
$_ENV = [];
try {
(new Dotenv($app->envPath(), $app->envFile()))->overload();
} catch (InvalidPathException $e) {
throw new RuntimeException($e->getMessage());
} catch (InvalidFileException $e) {
throw new RuntimeException($e->getMessage());
}
$result = $_ENV;
$_ENV = array_merge($oldEnv, $_ENV);
return $result;
}
/**
* 载入运行时环境变量.
*/
private function checkRuntimeEnv(IApp $app)
{
if (!getenv('RUNTIME_ENVIRONMENT')) {
return;
}
$file = '.'.getenv('RUNTIME_ENVIRONMENT');
// 校验运行时环境,防止测试用例清空非测试库的业务数据
if (!is_file($fullFile = $app->envPath().'/'.$file)) {
$e = sprintf('Env file `%s` was not found.', $fullFile);
throw new RuntimeException($e);
}
$app->setEnvFile($file);
}
}
/*
* ---------------------------------------------------------------
* 读取配置
* ---------------------------------------------------------------.
*
* 读取配置并且返回配置值
*/
(new PhinxLoad())->handle($app);
return [
'paths' => [
'migrations' => 'database/migrations',
'seeds' => 'database/seeds',
],
'environments' => [
'defaut_migration_table' => 'phinxlog',
'default_database' => 'development',
'production' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_NAME', ''),
'user' => Leevel::env('DATABASE_USER', 'root'),
'pass' => Leevel::env('DATABASE_PASSWORD', ''),
'port' => Leevel::env('DATABASE_PORT', 3306),
'charset' => 'utf8',
],
'development' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_NAME', ''),
'user' => Leevel::env('DATABASE_USER', 'root'),
'pass' => Leevel::env('DATABASE_PASSWORD', ''),
'port' => Leevel::env('DATABASE_PORT', 3306),
'charset' => 'utf8',
],
'env.phpunit' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_NAME', ''),
'user' => Leevel::env('DATABASE_USER', 'root'),
'pass' => Leevel::env('DATABASE_PASSWORD', ''),
'port' => Leevel::env('DATABASE_PORT', 3306),
'charset' => 'utf8',
],
],
];
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化