1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\GeneratorBundle\Maker; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\GeneratorBundle\Helper\DoctrineHelper; |
6
|
|
|
use Kunstmaan\GeneratorBundle\Helper\SymfonyVersionChecker; |
7
|
|
|
use Symfony\Bundle\MakerBundle\ConsoleStyle; |
8
|
|
|
use Symfony\Bundle\MakerBundle\DependencyBuilder; |
9
|
|
|
use Symfony\Bundle\MakerBundle\Generator; |
10
|
|
|
use Symfony\Bundle\MakerBundle\InputConfiguration; |
11
|
|
|
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
12
|
|
|
use Symfony\Bundle\MakerBundle\Str; |
13
|
|
|
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
18
|
|
|
use Symfony\Component\Finder\Finder; |
19
|
|
|
use Symfony\Component\Yaml\Yaml; |
20
|
|
|
use Twig\Environment; |
21
|
|
|
use Twig\Loader\FilesystemLoader; |
22
|
|
|
|
23
|
|
|
class MakeDefaultPageparts extends AbstractMaker |
24
|
|
|
{ |
25
|
|
|
/** @var Filesystem */ |
26
|
|
|
private $fs; |
27
|
|
|
|
28
|
|
|
/** @var Environment */ |
29
|
|
|
private $twig; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $projectDir; |
33
|
|
|
|
34
|
|
View Code Duplication |
public function __construct($projectDir) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->projectDir = $projectDir; |
37
|
|
|
$this->fs = new Filesystem(); |
38
|
|
|
|
39
|
|
|
$this->twig = new Environment(new FilesystemLoader([__DIR__ . '/../Resources/skeleton']), [ |
40
|
|
|
'debug' => true, |
41
|
|
|
'cache' => false, |
42
|
|
|
'strict_variables' => true, |
43
|
|
|
'autoescape' => false, |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the command name for your maker (e.g. make:report). |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public static function getCommandName(): string |
53
|
|
|
{ |
54
|
|
|
return 'make:default-pageparts'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Configure the command: set description, input arguments, options, etc. |
59
|
|
|
* |
60
|
|
|
* By default, all arguments will be asked interactively. If you want |
61
|
|
|
* to avoid that, use the $inputConfig->setArgumentAsNonInteractive() method. |
62
|
|
|
* |
63
|
|
|
* @param Command $command |
64
|
|
|
* @param InputConfiguration $inputConfig |
65
|
|
|
*/ |
66
|
|
|
public function configureCommand(Command $command, InputConfiguration $inputConfig) |
67
|
|
|
{ |
68
|
|
|
$command |
69
|
|
|
->setDescription('Copy the default pageparts') |
70
|
|
|
->addOption('prefix', null, InputOption::VALUE_REQUIRED, 'The prefix to be used in the table name of the generated entities', '') |
71
|
|
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Configure any library dependencies that your maker requires. |
76
|
|
|
* |
77
|
|
|
* @param DependencyBuilder $dependencies |
78
|
|
|
*/ |
79
|
|
|
public function configureDependencies(DependencyBuilder $dependencies) |
80
|
|
|
{ |
81
|
|
|
$dependencies->addClassDependency( |
82
|
|
|
Environment::class, |
83
|
|
|
'twig' |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$dependencies->addClassDependency( |
87
|
|
|
Filesystem::class, |
88
|
|
|
'filesystem' |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$dependencies->addClassDependency( |
92
|
|
|
Finder::class, |
93
|
|
|
'finder' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Called after normal code generation: allows you to do anything. |
99
|
|
|
* |
100
|
|
|
* @param InputInterface $input |
101
|
|
|
* @param ConsoleStyle $io |
102
|
|
|
* @param Generator $generator |
103
|
|
|
*/ |
104
|
|
|
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) |
105
|
|
|
{ |
106
|
|
|
if (false === SymfonyVersionChecker::isSymfony4()) { |
107
|
|
|
throw new \InvalidArgumentException('This command can only be executed on symfony 4.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$dbPrefix = ''; |
111
|
|
|
if ($input->getOption('prefix')) { |
112
|
|
|
$dbPrefix = rtrim($input->getOption('prefix'), '_') . '_'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$types = []; |
116
|
|
|
foreach ($this->getAllDefaultPageParts() as $pagePart) { |
117
|
|
|
$pagePartName = $pagePart->getBasename('.php'); |
118
|
|
|
|
119
|
|
|
$pagePartClassDetails = $generator->createClassNameDetails($pagePartName, 'Entity\\PageParts\\'); |
120
|
|
|
$adminTypeClassDetails = $generator->createClassNameDetails($pagePartName . 'AdminType', 'Form\\PageParts\\'); |
121
|
|
|
|
122
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Entity/PageParts/' . $pagePartName . '.php', $this->renderPagePartClass($pagePartClassDetails, $adminTypeClassDetails, $generator->getRootNamespace(), $dbPrefix)); |
123
|
|
|
|
124
|
|
|
if ($pagePartName === 'AbstractPagePart') { |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Form/PageParts/' . $adminTypeClassDetails->getShortName() . '.php', $this->renderAdminTypeClass($pagePartClassDetails, $adminTypeClassDetails, $generator->getRootNamespace())); |
129
|
|
|
|
130
|
|
|
$this->fs->mirror(__DIR__ . '/../Resources/skeleton/default-pageparts/templates/PageParts/' . $pagePartName, $this->projectDir . '/templates/pageparts/' . Str::asSnakeCase(str_replace('PagePart', 'Pagepart', $pagePartName))); |
131
|
|
|
|
132
|
|
|
$types[] = [ |
133
|
|
|
'name' => str_replace('PagePart', '', $pagePartClassDetails->getShortName()), |
134
|
|
|
'class' => $pagePartClassDetails->getFullName(), |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->renderExtraClasses($generator->getRootNamespace(), $dbPrefix); |
139
|
|
|
|
140
|
|
|
$originalTypes = []; |
141
|
|
|
$data = Yaml::parse(file_get_contents($this->projectDir . '/config/kunstmaancms/pageparts/main.yml')); |
142
|
|
View Code Duplication |
if (array_key_exists('kunstmaan_page_part', $data)) { |
|
|
|
|
143
|
|
|
$originalTypes = $data['kunstmaan_page_part']['pageparts']['main']['types']; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$types = array_merge($originalTypes, $types); |
147
|
|
|
|
148
|
|
|
$data['kunstmaan_page_part']['pageparts']['main']['types'] = $types; |
149
|
|
|
$ymlData = Yaml::dump($data, 5); |
150
|
|
|
file_put_contents($this->projectDir . '/config/kunstmaancms/pageparts/main.yml', $ymlData); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function renderPagePartClass(ClassNameDetails $pagePartClassDetails, ClassNameDetails $adminTypeClassDetails, $rootNamespace, $dbPrefix): string |
154
|
|
|
{ |
155
|
|
|
return $this->twig->render('/default-pageparts/entities/pageparts/' . $pagePartClassDetails->getShortName() . '.php', [ |
156
|
|
|
'namespace' => $rootNamespace, |
157
|
|
|
'table_name' => $dbPrefix . DoctrineHelper::convertToTableName($pagePartClassDetails->getShortName()), |
158
|
|
|
'admin_type_class' => $adminTypeClassDetails->getShortName(), |
159
|
|
|
'admin_type_full' => $adminTypeClassDetails->getFullName(), |
160
|
|
|
]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function renderAdminTypeClass(ClassNameDetails $pagePartClassDetails, ClassNameDetails $adminTypeClassDetails, $rootNamespace): string |
164
|
|
|
{ |
165
|
|
|
return $this->twig->render('/default-pageparts/forms/pageparts/' . $adminTypeClassDetails->getShortName() . '.php', [ |
166
|
|
|
'namespace' => $rootNamespace, |
167
|
|
|
'pagepart_class' => $pagePartClassDetails->getShortName(), |
168
|
|
|
'pagepart_class_full' => $pagePartClassDetails->getFullName(), |
169
|
|
|
]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private function renderExtraClasses($rootNamespace, $dbPrefix) |
173
|
|
|
{ |
174
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Entity/UspItem.php', $this->twig->render('/default-pageparts/entities/UspItem.php', [ |
175
|
|
|
'namespace' => $rootNamespace, |
176
|
|
|
'db_prefix' => $dbPrefix, |
177
|
|
|
])); |
178
|
|
|
|
179
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Form/UspItemAdminType.php', $this->twig->render('/default-pageparts/forms/UspItemAdminType.php', [ |
180
|
|
|
'namespace' => $rootNamespace, |
181
|
|
|
])); |
182
|
|
|
|
183
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Entity/MapItem.php', $this->twig->render('/default-pageparts/entities/MapItem.php', [ |
184
|
|
|
'namespace' => $rootNamespace, |
185
|
|
|
'db_prefix' => $dbPrefix, |
186
|
|
|
])); |
187
|
|
|
|
188
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Form/MapItemAdminType.php', $this->twig->render('/default-pageparts/forms/MapItemAdminType.php', [ |
189
|
|
|
'namespace' => $rootNamespace, |
190
|
|
|
])); |
191
|
|
|
|
192
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Entity/GalleryRow.php', $this->twig->render('/default-pageparts/entities/GalleryRow.php', [ |
193
|
|
|
'namespace' => $rootNamespace, |
194
|
|
|
'db_prefix' => $dbPrefix, |
195
|
|
|
])); |
196
|
|
|
|
197
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Form/GalleryRowAdminType.php', $this->twig->render('/default-pageparts/forms/GalleryRowAdminType.php', [ |
198
|
|
|
'namespace' => $rootNamespace, |
199
|
|
|
])); |
200
|
|
|
|
201
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Entity/GalleryRowMediaItem.php', $this->twig->render('/default-pageparts/entities/GalleryRowMediaItem.php', [ |
202
|
|
|
'namespace' => $rootNamespace, |
203
|
|
|
'db_prefix' => $dbPrefix, |
204
|
|
|
])); |
205
|
|
|
|
206
|
|
|
$this->fs->dumpFile($this->projectDir . '/src/Form/GalleryRowMediaItemAdminType.php', $this->twig->render('/default-pageparts/forms/GalleryRowMediaItemAdminType.php', [ |
207
|
|
|
'namespace' => $rootNamespace, |
208
|
|
|
])); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function getAllDefaultPageParts(): Finder |
212
|
|
|
{ |
213
|
|
|
$finder = (new Finder())->files()->name('*PagePart.php')->in(__DIR__ . '/../Resources/skeleton/default-pageparts/entities/pageparts')->sortByName(); |
214
|
|
|
|
215
|
|
|
return $finder; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
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.