1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package Phinx |
27
|
|
|
* @subpackage Phinx\Seed |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Seed; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Adapter\AdapterInterface; |
32
|
|
|
use Phinx\Db\Table; |
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Abstract Seed Class. |
38
|
|
|
* |
39
|
|
|
* It is expected that the seeds you write extend from this class. |
40
|
|
|
* |
41
|
|
|
* This abstract class proxies the various database methods to your specified |
42
|
|
|
* adapter. |
43
|
|
|
* |
44
|
|
|
* @author Rob Morgan <[email protected]> |
45
|
|
|
*/ |
46
|
|
|
abstract class AbstractSeed implements SeedInterface |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var \Phinx\Db\Adapter\AdapterInterface |
50
|
|
|
*/ |
51
|
|
|
protected $adapter; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
55
|
|
|
*/ |
56
|
|
|
protected $input; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
60
|
|
|
*/ |
61
|
|
|
protected $output; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Class Constructor. |
65
|
|
|
* |
66
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
67
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
68
|
|
|
*/ |
69
|
11 |
View Code Duplication |
final public function __construct(InputInterface $input = null, OutputInterface $output = null) |
|
|
|
|
70
|
|
|
{ |
71
|
11 |
|
if (!is_null($input)) { |
72
|
11 |
|
$this->setInput($input); |
73
|
11 |
|
} |
74
|
11 |
|
if (!is_null($output)) { |
75
|
11 |
|
$this->setOutput($output); |
76
|
11 |
|
} |
77
|
|
|
|
78
|
11 |
|
$this->init(); |
79
|
11 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Initialize method. |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
11 |
|
protected function init() |
87
|
|
|
{ |
88
|
11 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function run() |
94
|
|
|
{ |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return seeds dependencies. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getDependencies() |
103
|
|
|
{ |
104
|
|
|
return []; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function setAdapter(AdapterInterface $adapter) |
111
|
|
|
{ |
112
|
|
|
$this->adapter = $adapter; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
11 |
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
11 |
|
*/ |
120
|
11 |
|
public function getAdapter() |
121
|
|
|
{ |
122
|
|
|
return $this->adapter; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
1 |
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function setInput(InputInterface $input) |
129
|
|
|
{ |
130
|
|
|
$this->input = $input; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
11 |
|
|
135
|
|
|
/** |
136
|
11 |
|
* {@inheritdoc} |
137
|
11 |
|
*/ |
138
|
|
|
public function getInput() |
139
|
|
|
{ |
140
|
|
|
return $this->input; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
1 |
|
*/ |
146
|
|
|
public function setOutput(OutputInterface $output) |
147
|
|
|
{ |
148
|
|
|
$this->output = $output; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
6 |
|
} |
152
|
|
|
|
153
|
6 |
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function getOutput() |
157
|
|
|
{ |
158
|
|
|
return $this->output; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function getName() |
165
|
|
|
{ |
166
|
|
|
return get_class($this); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function execute($sql) |
173
|
|
|
{ |
174
|
|
|
return $this->getAdapter()->execute($sql); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function query($sql) |
181
|
|
|
{ |
182
|
|
|
return $this->getAdapter()->query($sql); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function fetchRow($sql) |
189
|
|
|
{ |
190
|
|
|
return $this->getAdapter()->fetchRow($sql); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function fetchAll($sql) |
197
|
|
|
{ |
198
|
|
|
return $this->getAdapter()->fetchAll($sql); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
public function insert($table, $data) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
// convert to table object |
207
|
|
|
if (is_string($table)) { |
208
|
|
|
$table = new Table($table, [], $this->getAdapter()); |
209
|
|
|
} |
210
|
|
|
$table->insert($data)->save(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public function hasTable($tableName) |
217
|
|
|
{ |
218
|
|
|
return $this->getAdapter()->hasTable($tableName); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
|
|
public function table($tableName, $options = []) |
225
|
|
|
{ |
226
|
|
|
return new Table($tableName, $options, $this->getAdapter()); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.