AbstractCommand.php
5.9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
// +----------------------------------------------------------------------
// | TopThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\migration\command;
use InvalidArgumentException;
use think\console\Command;
use think\console\Input;
use think\console\input\Option as InputOption;
use think\console\Output;
use Phinx\Config;
use Phinx\Migration\Manager;
use Phinx\Db\Adapter\AdapterInterface;
abstract class AbstractCommand extends Command
{
/**
* The location of the default migration template.
*/
const DEFAULT_MIGRATION_TEMPLATE = '/../../phinx/src/Phinx/Migration/Migration.template.php.dist';
/**
* The location of the default seed template.
*/
const DEFAULT_SEED_TEMPLATE = '/../../phinx/src/Phinx/Seed/Seed.template.php.dist';
/**
* @var Config
*/
protected $config;
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* @var Manager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load');
}
/**
* Bootstrap Phinx.
*
* @param Input $input
* @param Output $output
* @return void
*/
public function bootstrap(Input $input, Output $output)
{
if (!$this->getConfig()) {
$this->loadConfig($input, $output);
}
$this->loadManager($output);
// report the paths
$output->writeln('<info>using migration path</info> ' . $this->getConfig()->getMigrationPath());
try {
$output->writeln('<info>using seed path</info> ' . $this->getConfig()->getSeedPath());
} catch (\UnexpectedValueException $e) {
// do nothing as seeds are optional
}
}
/**
* Sets the config.
*
* @param Config $config
* @return AbstractCommand
*/
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
/**
* Gets the config.
*
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* Sets the database adapter.
*
* @param AdapterInterface $adapter
* @return AbstractCommand
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* Gets the database adapter.
*
* @return AdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Sets the migration manager.
*
* @param Manager $manager
* @return AbstractCommand
*/
public function setManager(Manager $manager)
{
$this->manager = $manager;
return $this;
}
/**
* Gets the migration manager.
*
* @return Manager
*/
public function getManager()
{
return $this->manager;
}
/**
* Parse the config file and load it into the config object
*
* @param Input $input
* @param Output $output
* @throws \InvalidArgumentException
* @return void
*/
protected function loadConfig(Input $input, Output $output)
{
$configFile = $input->getOption('configuration');
if (null === $configFile || false === $configFile) {
$configFile = APP_PATH . 'database' . EXT;
}
if (!is_file($configFile)) {
throw new InvalidArgumentException();
}
$output->writeln('<info>using config file</info> .' . str_replace(getcwd(), '', realpath($configFile)));
$config = new Config($configFile);
$this->setConfig($config);
}
/**
* Load the migrations manager and inject the config
*
* @param Output $output
* @return void
*/
protected function loadManager(Output $output)
{
if (null === $this->getManager()) {
$manager = new Manager($this->getConfig(), $output);
$this->setManager($manager);
}
}
/**
* Verify that the migration directory exists and is writable.
*
* @param $path
*/
protected function verifyMigrationDirectory($path)
{
if (!is_dir($path)) {
throw new InvalidArgumentException(sprintf(
'Migration directory "%s" does not exist',
$path
));
}
if (!is_writable($path)) {
throw new InvalidArgumentException(sprintf(
'Migration directory "%s" is not writable',
$path
));
}
}
/**
* Verify that the seed directory exists and is writable.
*
* @param $path
*/
protected function verifySeedDirectory($path)
{
if (!is_dir($path)) {
throw new InvalidArgumentException(sprintf(
'Seed directory "%s" does not exist',
$path
));
}
if (!is_writable($path)) {
throw new InvalidArgumentException(sprintf(
'Seed directory "%s" is not writable',
$path
));
}
}
/**
* Returns the migration template filename.
*
* @return string
*/
protected function getMigrationTemplateFilename()
{
return __DIR__ . self::DEFAULT_MIGRATION_TEMPLATE;
}
/**
* Returns the seed template filename.
*
* @return string
*/
protected function getSeedTemplateFilename()
{
return __DIR__ . self::DEFAULT_SEED_TEMPLATE;
}
}