This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace Xethron\MigrationsGenerator; |
||
2 | |||
3 | use Way\Generators\Commands\GeneratorCommand; |
||
4 | use Symfony\Component\Console\Input\InputOption; |
||
5 | use Symfony\Component\Console\Input\InputArgument; |
||
6 | |||
7 | use Way\Generators\Generator; |
||
8 | use Way\Generators\Filesystem\Filesystem; |
||
9 | use Way\Generators\Compilers\TemplateCompiler; |
||
10 | use Illuminate\Database\Migrations\MigrationRepositoryInterface; |
||
11 | |||
12 | use Xethron\MigrationsGenerator\Generators\SchemaGenerator; |
||
13 | use Xethron\MigrationsGenerator\Syntax\AddToTable; |
||
14 | use Xethron\MigrationsGenerator\Syntax\DroppedTable; |
||
15 | use Xethron\MigrationsGenerator\Syntax\AddForeignKeysToTable; |
||
16 | use Xethron\MigrationsGenerator\Syntax\RemoveForeignKeysFromTable; |
||
17 | |||
18 | use Illuminate\Contracts\Config\Repository as Config; |
||
19 | |||
20 | class MigrateGenerateCommand extends GeneratorCommand { |
||
21 | |||
22 | /** |
||
23 | * The console command name. |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $name = 'migrate:generate'; |
||
27 | |||
28 | /** |
||
29 | * The console command description. |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Generate a migration from an existing table structure.'; |
||
33 | |||
34 | /** |
||
35 | * @var \Way\Generators\Filesystem\Filesystem |
||
36 | */ |
||
37 | protected $file; |
||
38 | |||
39 | /** |
||
40 | * @var \Way\Generators\Compilers\TemplateCompiler |
||
41 | */ |
||
42 | protected $compiler; |
||
43 | |||
44 | /** |
||
45 | * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
46 | */ |
||
47 | protected $repository; |
||
48 | |||
49 | /** |
||
50 | * @var \Illuminate\Config\Repository $config |
||
51 | */ |
||
52 | protected $config; |
||
53 | |||
54 | /** |
||
55 | * @var \Xethron\MigrationsGenerator\Generators\SchemaGenerator |
||
56 | */ |
||
57 | protected $schemaGenerator; |
||
58 | |||
59 | /** |
||
60 | * Array of Fields to create in a new Migration |
||
61 | * Namely: Columns, Indexes and Foreign Keys |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $fields = array(); |
||
65 | |||
66 | /** |
||
67 | * List of Migrations that has been done |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $migrations = array(); |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $log = false; |
||
76 | |||
77 | /** |
||
78 | * @var int |
||
79 | */ |
||
80 | protected $batch; |
||
81 | |||
82 | /** |
||
83 | * Filename date prefix (Y_m_d_His) |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $datePrefix; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $migrationName; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $method; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $table; |
||
102 | |||
103 | /** |
||
104 | * @var string|null |
||
105 | */ |
||
106 | protected $connection = null; |
||
107 | |||
108 | /** |
||
109 | * @param \Way\Generators\Generator $generator |
||
110 | * @param \Way\Generators\Filesystem\Filesystem $file |
||
111 | * @param \Way\Generators\Compilers\TemplateCompiler $compiler |
||
112 | * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
113 | * @param \Illuminate\Config\Repository $config |
||
114 | */ |
||
115 | public function __construct( |
||
116 | Generator $generator, |
||
117 | Filesystem $file, |
||
118 | TemplateCompiler $compiler, |
||
119 | MigrationRepositoryInterface $repository, |
||
120 | Config $config |
||
121 | ) |
||
122 | { |
||
123 | $this->file = $file; |
||
124 | $this->compiler = $compiler; |
||
125 | $this->repository = $repository; |
||
126 | $this->config = $config; |
||
127 | |||
128 | parent::__construct( $generator ); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Execute the console command. Added for Laravel 5.5 |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function handle() |
||
137 | { |
||
138 | $this->fire(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Execute the console command. |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | public function fire() |
||
147 | { |
||
148 | $this->info( 'Using connection: '. $this->option( 'connection' ) ."\n" ); |
||
149 | if ($this->option('connection') !== $this->config->get('database.default')) { |
||
150 | $this->connection = $this->option('connection'); |
||
151 | } |
||
152 | $this->schemaGenerator = new SchemaGenerator( |
||
153 | $this->option('connection'), |
||
154 | $this->option('defaultIndexNames'), |
||
155 | $this->option('defaultFKNames') |
||
156 | ); |
||
157 | |||
158 | if ( $this->argument( 'tables' ) ) { |
||
159 | $tables = explode( ',', $this->argument( 'tables' ) ); |
||
160 | } elseif ( $this->option('tables') ) { |
||
161 | $tables = explode( ',', $this->option( 'tables' ) ); |
||
162 | } else { |
||
163 | $tables = $this->schemaGenerator->getTables(); |
||
164 | } |
||
165 | |||
166 | $tables = $this->removeExcludedTables($tables); |
||
167 | $this->info( 'Generating migrations for: '. implode( ', ', $tables ) ); |
||
168 | |||
169 | if (!$this->option( 'no-interaction' )) { |
||
170 | $this->log = $this->askYn('Do you want to log these migrations in the migrations table?'); |
||
171 | } |
||
172 | |||
173 | if ( $this->log ) { |
||
174 | $this->repository->setSource( $this->option( 'connection' ) ); |
||
175 | if ( ! $this->repository->repositoryExists() ) { |
||
176 | $options = array('--database' => $this->option( 'connection' ) ); |
||
177 | $this->call('migrate:install', $options); |
||
178 | } |
||
179 | $batch = $this->repository->getNextBatchNumber(); |
||
180 | $this->batch = $this->askNumeric( 'Next Batch Number is: '. $batch .'. We recommend using Batch Number 0 so that it becomes the "first" migration', 0 ); |
||
181 | } |
||
182 | |||
183 | $this->info( "Setting up Tables and Index Migrations" ); |
||
184 | $this->datePrefix = date( 'Y_m_d_His' ); |
||
185 | $this->generateTablesAndIndices( $tables ); |
||
186 | $this->info( "\nSetting up Foreign Key Migrations\n" ); |
||
187 | $this->datePrefix = date( 'Y_m_d_His', strtotime( '+1 second' ) ); |
||
188 | $this->generateForeignKeys( $tables ); |
||
189 | $this->info( "\nFinished!\n" ); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Ask for user input: Yes/No |
||
194 | * @param string $question Question to ask |
||
195 | * @return boolean Answer from user |
||
196 | */ |
||
197 | protected function askYn( $question ) { |
||
198 | $answer = $this->ask( $question .' [Y/n] '); |
||
199 | while ( ! in_array( strtolower( $answer ), [ 'y', 'n', 'yes', 'no' ] ) ) { |
||
200 | $answer = $this->ask('Please choose either yes or no. '); |
||
201 | } |
||
202 | return in_array( strtolower( $answer ), [ 'y', 'yes' ] ); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Ask user for a Numeric Value, or blank for default |
||
207 | * @param string $question Question to ask |
||
208 | * @param int|float $default Default Value (optional) |
||
209 | * @return int|float Answer |
||
210 | */ |
||
211 | protected function askNumeric( $question, $default = null ) { |
||
212 | $ask = 'Your answer needs to be a numeric value'; |
||
213 | |||
214 | if ( ! is_null( $default ) ) { |
||
215 | $question .= ' [Default: '. $default .'] '; |
||
216 | $ask .= ' or blank for default'; |
||
217 | } |
||
218 | |||
219 | $answer = $this->ask( $question ); |
||
220 | |||
221 | while ( ! is_numeric( $answer ) and ! ( $answer == '' and ! is_null( $default ) ) ) { |
||
222 | $answer = $this->ask( $ask .'. '); |
||
223 | } |
||
224 | if ( $answer == '' ) { |
||
225 | $answer = $default; |
||
226 | } |
||
227 | return $answer; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Generate tables and index migrations. |
||
232 | * |
||
233 | * @param array $tables List of tables to create migrations for |
||
234 | * @return void |
||
235 | */ |
||
236 | View Code Duplication | protected function generateTablesAndIndices( array $tables ) |
|
237 | { |
||
238 | $this->method = 'create'; |
||
239 | |||
240 | foreach ( $tables as $table ) { |
||
241 | $this->table = $table; |
||
242 | $this->migrationName = 'create_'. $this->table .'_table'; |
||
243 | $this->fields = $this->schemaGenerator->getFields( $this->table ); |
||
244 | |||
245 | $this->generate(); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Generate foreign key migrations. |
||
251 | * |
||
252 | * @param array $tables List of tables to create migrations for |
||
253 | * @return void |
||
254 | */ |
||
255 | View Code Duplication | protected function generateForeignKeys( array $tables ) |
|
256 | { |
||
257 | $this->method = 'table'; |
||
258 | |||
259 | foreach ( $tables as $table ) { |
||
260 | $this->table = $table; |
||
261 | $this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table'; |
||
262 | $this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table ); |
||
263 | |||
264 | $this->generate(); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Generate Migration for the current table. |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function generate() |
||
274 | { |
||
275 | if ( $this->fields ) { |
||
0 ignored issues
–
show
|
|||
276 | parent::fire(); |
||
277 | |||
278 | if ( $this->log ) { |
||
279 | $file = $this->datePrefix . '_' . $this->migrationName; |
||
280 | $this->repository->log($file, $this->batch); |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * The path where the file will be created |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | protected function getFileGenerationPath() |
||
291 | { |
||
292 | $path = $this->getPathByOptionOrConfig( 'path', 'migration_target_path' ); |
||
293 | $migrationName = str_replace('/', '_', $this->migrationName); |
||
294 | $fileName = $this->getDatePrefix() . '_' . $migrationName . '.php'; |
||
295 | |||
296 | return "{$path}/{$fileName}"; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Get the date prefix for the migration. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | protected function getDatePrefix() |
||
305 | { |
||
306 | return $this->datePrefix; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Fetch the template data |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getTemplateData() |
||
315 | { |
||
316 | if ( $this->method == 'create' ) { |
||
317 | $up = (new AddToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection, 'create'); |
||
318 | $down = (new DroppedTable)->drop($this->table, $this->connection); |
||
319 | } |
||
320 | |||
321 | if ( $this->method == 'table' ) { |
||
322 | $up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection); |
||
323 | $down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection); |
||
324 | } |
||
325 | |||
326 | return [ |
||
327 | 'CLASS' => ucwords(camel_case($this->migrationName)), |
||
328 | 'UP' => $up, |
||
329 | 'DOWN' => $down |
||
330 | ]; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Get path to template for generator |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | protected function getTemplatePath() |
||
339 | { |
||
340 | return $this->getPathByOptionOrConfig( 'templatePath', 'migration_template_path' ); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get the console command arguments. |
||
345 | * |
||
346 | * @return array |
||
347 | */ |
||
348 | protected function getArguments() |
||
349 | { |
||
350 | return [ |
||
351 | ['tables', InputArgument::OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], |
||
352 | ]; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Get the console command options. |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | protected function getOptions() |
||
361 | { |
||
362 | return [ |
||
0 ignored issues
–
show
The return type of
return array(array('conn...ames for migrations')); (array<string|null>[] ) is incompatible with the return type of the parent method Way\Generators\Commands\...atorCommand::getOptions of type array<string|null>[] .
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function
Loading history...
|
|||
363 | ['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get( 'database.default' )], |
||
364 | ['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], |
||
365 | ['ignore', 'i', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to ignore, separated by a comma: users,posts,comments' ], |
||
366 | ['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'], |
||
367 | ['templatePath', 'tp', InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'], |
||
368 | ['defaultIndexNames', null, InputOption::VALUE_NONE, 'Don\'t use db index names for migrations'], |
||
369 | ['defaultFKNames', null, InputOption::VALUE_NONE, 'Don\'t use db foreign key names for migrations'], |
||
370 | ]; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Remove all the tables to exclude from the array of tables |
||
375 | * |
||
376 | * @param array $tables |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | protected function removeExcludedTables( array $tables ) |
||
381 | { |
||
382 | $excludes = $this->getExcludedTables(); |
||
383 | $tables = array_diff($tables, $excludes); |
||
384 | |||
385 | return $tables; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get a list of tables to exclude |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | protected function getExcludedTables() |
||
394 | { |
||
395 | $excludes = ['migrations']; |
||
396 | $ignore = $this->option('ignore'); |
||
397 | if ( ! empty($ignore)) { |
||
398 | return array_merge($excludes, explode(',', $ignore)); |
||
399 | } |
||
400 | |||
401 | return $excludes; |
||
402 | } |
||
403 | |||
404 | } |
||
405 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.