1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Common\Helper; |
6
|
|
|
use App\Console\Traits\CodeGenerate; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\Question; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* GenerateSeedCommand |
14
|
|
|
*/ |
15
|
|
|
class GenerateSeedCommand extends Command |
16
|
|
|
{ |
17
|
|
|
use CodeGenerate; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Configuration of command |
21
|
|
|
*/ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
$this |
25
|
|
|
->setName('generate:seed') |
26
|
|
|
->setDescription('Command generate:seed') |
27
|
|
|
; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Execute method of command |
32
|
|
|
* @param InputInterface $input |
33
|
|
|
* @param OutputInterface $output |
34
|
|
|
* |
35
|
|
|
* @return int|null|void |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$output->writeln(['<comment>Welcome to the seed generator</comment>']); |
41
|
|
|
|
42
|
|
|
$helper = $this->getHelper('question'); |
43
|
|
|
$question = new Question('<info>Please enter the name of the seed: </info>'); |
44
|
|
|
$question->setValidator(function($answer) { |
45
|
|
|
if (strlen(trim($answer)) === 0) { |
46
|
|
|
throw new \RunTimeException('The name of the seed should be not empty'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $answer; |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$seedName = $helper->ask($input, $output, $question); |
53
|
|
|
$baseName = date('YmdHis').'_'.$seedName.'.php'; |
54
|
|
|
$path = $this->getPath($baseName, SEEDS_PATH); |
55
|
|
|
$placeHolders = [ |
56
|
|
|
'<class>', |
57
|
|
|
]; |
58
|
|
|
$replacements = [ |
59
|
|
|
Helper::underscoreToCamelCase($seedName, true), |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
$this->generateCode($placeHolders, $replacements, 'SeedTemplate.tpl', $path); |
63
|
|
|
|
64
|
|
|
$output->writeln(sprintf('Generated new seed class to "<info>%s</info>"', realpath($path))); |
65
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.