1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Bluz PHP Team |
4
|
|
|
* @link https://github.com/bluzphp/bluzman |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Bluzman\Command\Generate; |
8
|
|
|
|
9
|
|
|
use Bluzman\Generator\GeneratorException; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Generate Module Structure |
15
|
|
|
* |
16
|
|
|
* @package Bluzman\Command\Generate |
17
|
|
|
*/ |
18
|
|
|
class ModuleCommand extends AbstractGenerateCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Command configuration |
22
|
|
|
*/ |
23
|
14 |
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
// the name of the command (the part after "bin/bluzman") |
27
|
14 |
|
->setName('generate:module') |
28
|
|
|
// the short description shown while running "php bin/bluzman list" |
29
|
14 |
|
->setDescription('Generate a new module') |
30
|
|
|
// the full command description shown when running the command with |
31
|
|
|
// the "--help" option |
32
|
14 |
|
->setHelp('This command allows you to generate a module structure') |
33
|
|
|
; |
34
|
|
|
|
35
|
14 |
|
$this->addModuleArgument(); |
36
|
14 |
|
$this->addForceOption(); |
37
|
14 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param InputInterface $input |
41
|
|
|
* @param OutputInterface $output |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
1 |
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
45
|
|
|
{ |
46
|
1 |
|
$this->write('Running <info>generate:module</info> command'); |
47
|
|
|
try { |
48
|
|
|
// validate |
49
|
1 |
|
$this->validateModuleArgument(); |
50
|
|
|
|
51
|
|
|
// create main folder and subfolders |
52
|
1 |
|
$this->generate($input, $output); |
53
|
|
|
|
54
|
|
|
// verify it |
55
|
1 |
|
$this->verify($input, $output); |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
$this->error("ERROR: {$e->getMessage()}"); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param InputInterface $input |
63
|
|
|
* @param OutputInterface $output |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
1 |
|
protected function generate(InputInterface $input, OutputInterface $output) |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
$path = $this->getApplication()->getModulePath($input->getArgument('module')); |
69
|
1 |
|
$this->createSubFolders( |
70
|
1 |
|
$path, |
71
|
|
|
[ |
72
|
1 |
|
'controllers', |
73
|
|
|
'views' |
74
|
|
|
] |
75
|
|
|
); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $path |
80
|
|
|
* @param string[] $subFolders |
81
|
|
|
*/ |
82
|
1 |
|
protected function createSubFolders($path, array $subFolders = []) |
83
|
|
|
{ |
84
|
1 |
|
if (!$this->getFs()->exists($path)) { |
85
|
1 |
|
$this->getFs()->mkdir($path); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
foreach ($subFolders as $subFolderName) { |
89
|
1 |
|
$subFolderPath = $path . DIRECTORY_SEPARATOR . $subFolderName; |
90
|
1 |
|
if ($this->getFs()->exists($subFolderPath)) { |
91
|
|
|
$this->comment(" |> Directory <info>$subFolderPath</info> already exists"); |
92
|
|
|
} else { |
93
|
1 |
|
$this->getFs()->mkdir($subFolderPath, 0755); |
94
|
1 |
|
$this->getFs()->touch([$subFolderPath . DIRECTORY_SEPARATOR . '.keep']); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param InputInterface $input |
101
|
|
|
* @param OutputInterface $output |
102
|
|
|
* @return void |
103
|
|
|
* @throws GeneratorException |
104
|
|
|
*/ |
105
|
1 |
View Code Duplication |
public function verify(InputInterface $input, OutputInterface $output) : void |
|
|
|
|
106
|
|
|
{ |
107
|
1 |
|
$module = $input->getArgument('module'); |
108
|
1 |
|
$modulePath = $this->getApplication()->getModulePath($module); |
109
|
|
|
|
110
|
|
|
$paths = [ |
111
|
1 |
|
$modulePath, |
112
|
1 |
|
$modulePath . DS . 'controllers', |
113
|
1 |
|
$modulePath . DS . 'views' |
114
|
|
|
]; |
115
|
|
|
|
116
|
1 |
|
foreach ($paths as $path) { |
117
|
1 |
|
if (!$this->getFs()->exists($path)) { |
118
|
|
|
throw new GeneratorException("Directory `$path` is not exists"); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$this->write(" |> Module <info>$module</info> has been successfully created."); |
123
|
1 |
|
} |
124
|
|
|
} |
125
|
|
|
|
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.