1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Console\Traits\DbHelper; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* MigrateCommand |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
class MigrateCommand extends Command |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
use DbHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string Table name where migrations info is kept |
20
|
|
|
*/ |
21
|
|
|
const MIGRATIONS_TABLE = 'migrations'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Configuration of command |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('migrate') |
30
|
|
|
->setDescription('Command for run migration') |
31
|
|
|
; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute method of command |
36
|
|
|
* |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* @param OutputInterface $output |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
if (!is_dir(MIGRATIONS_PATH) || !is_readable(MIGRATIONS_PATH)) { |
45
|
|
|
throw new \RunTimeException(sprintf('Migrations path `%s` is not good', MIGRATIONS_PATH)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$output->writeln([ |
49
|
|
|
'<info>Run migrations</info>', |
50
|
|
|
sprintf('Ensure table `%s` presence', self::MIGRATIONS_TABLE) |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$this->safeCreateTable(self::MIGRATIONS_TABLE); |
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
$output->writeln([ |
57
|
|
|
sprintf('Can\'t ensure table `%s` presence. Please verify DB connection params and presence of database named', self::MIGRATIONS_TABLE), |
58
|
|
|
sprintf('Error: `%s`', $e->getMessage()), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$finder = new Finder(); |
63
|
|
|
$finder->files()->name('*.php')->in(MIGRATIONS_PATH); |
64
|
|
|
|
65
|
|
|
$this->runActions($finder, $output, self::MIGRATIONS_TABLE, 'up'); |
66
|
|
|
|
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.