1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Common\Helper; |
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\Console\Question\Question; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* GenerateSeedCommand |
13
|
|
|
*/ |
14
|
|
|
class GenerateSeedCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Configuration of command |
18
|
|
|
*/ |
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this |
22
|
|
|
->setName('generate:seed') |
23
|
|
|
->setDescription('Command generate:seed') |
24
|
|
|
; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Execute method of command |
29
|
|
|
* @param InputInterface $input |
30
|
|
|
* @param OutputInterface $output |
31
|
|
|
* |
32
|
|
|
* @return int|null|void |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$output->writeln(['<comment>Welcome to the seed generator</comment>']); |
38
|
|
|
|
39
|
|
|
$helper = $this->getHelper('question'); |
40
|
|
|
$question = new Question('<info>Please enter the name of the seed: </info>'); |
41
|
|
|
$question->setValidator(function ($answer) { |
42
|
|
|
if (strlen(trim($answer)) === 0) { |
43
|
|
|
throw new \RunTimeException('The name of the seed should be not empty'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $answer; |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
$seedName = $helper->ask($input, $output, $question); |
50
|
|
|
$path = $this->getPathForSeed($seedName); |
51
|
|
|
$placeHolders = [ |
52
|
|
|
'<class>', |
53
|
|
|
]; |
54
|
|
|
$replacements = [ |
55
|
|
|
Helper::underscoreToCamelCase($seedName, true), |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$this->generateCode($placeHolders, $replacements, 'SeedTemplate.tpl', $path); |
59
|
|
|
|
60
|
|
|
$output->writeln(sprintf('Generated new seed class to "<info>%s</info>"', realpath($path))); |
61
|
|
|
|
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generate code |
67
|
|
|
* |
68
|
|
|
* @param array $placeHolders |
69
|
|
|
* @param array $replacements |
70
|
|
|
* @param string $templateName |
71
|
|
|
* @param string $resultPath |
72
|
|
|
* @return bool|int |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function generateCode($placeHolders, $replacements, $templateName, $resultPath) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$templatePath = CODE_TEMPLATE_PATH . '/' . $templateName; |
77
|
|
|
if (false === file_exists($templatePath)) { |
78
|
|
|
throw new \RunTimeException(sprintf('Not found template %s', $templatePath)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$template = file_get_contents($templatePath); |
82
|
|
|
|
83
|
|
|
$code = str_replace($placeHolders, $replacements, $template); |
84
|
|
|
|
85
|
|
|
return file_put_contents($resultPath, $code); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $seedName |
90
|
|
|
* @return string |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
private function getPathForSeed($seedName) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$dir = rtrim(SEEDS_PATH, '/'); |
96
|
|
|
if (!file_exists($dir)) { |
97
|
|
|
throw new \Exception(sprintf('Seed directory "%s" does not exist.', $dir)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$baseName = date('YmdHis') . '_' . $seedName . '.php'; |
101
|
|
|
|
102
|
|
|
return $dir . '/' . $baseName; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.