-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathphinx.php
153 lines (134 loc) · 4.2 KB
/
phinx.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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 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
{
$dotenv = Dotenv::createMutable($app->envPath(), $app->envFile());
return $dotenv->load();
}
/**
* 载入运行时环境变量.
*
* @throws \RuntimeException
*/
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',
],
],
];