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
|
|
|
* SeedCommand |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
class SeedCommand extends Command |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
use DbHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string Table name where migrations info is kept |
20
|
|
|
*/ |
21
|
|
|
const SEEDS_TABLE = 'seeds'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Configuration of command |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('seed') |
30
|
|
|
->setDescription('Command for run seed') |
31
|
|
|
; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute method of command |
36
|
|
|
* |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* @param OutputInterface $output |
39
|
|
|
* |
40
|
|
|
* @return int|null|void |
41
|
|
|
*/ |
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
if (!is_dir(SEEDS_PATH) || !is_readable(SEEDS_PATH)) { |
45
|
|
|
throw new \RunTimeException(sprintf('Seeds path `%s` is not good', SEEDS_PATH)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$output->writeln([ |
49
|
|
|
'<info>Run seeds</info>', |
50
|
|
|
sprintf('Ensure table `%s` presence', self::SEEDS_TABLE) |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$this->safeCreateTable(self::SEEDS_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::SEEDS_TABLE), |
58
|
|
|
sprintf('Error: `%s`', $e->getMessage()), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$finder = new Finder(); |
63
|
|
|
$finder->files()->name('*.php')->in(SEEDS_PATH); |
64
|
|
|
|
65
|
|
|
$this->runActions($finder, $output, self::SEEDS_TABLE, 'run'); |
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.