1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Command; |
12
|
|
|
|
13
|
|
|
use IrishDan\ResponsiveImageBundle\ImageEntityClassLocator; |
14
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand; |
15
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\Validators; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
20
|
|
|
use Symfony\Component\Console\Question\Question; |
21
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class CreateImageEntityCommand |
26
|
|
|
* |
27
|
|
|
* @package IrishDan\ResponsiveImageBundle\Command |
28
|
|
|
*/ |
29
|
|
|
class GenerateImageEntityCrudCommand extends GenerateDoctrineCrudCommand |
30
|
|
|
{ |
31
|
|
|
protected $responsiveImageEntity; |
32
|
|
|
protected $imageEntityShorthand; |
33
|
|
|
protected $entityName; |
34
|
|
|
protected $bundle; |
35
|
|
|
protected $doctrine; |
36
|
|
|
protected $entityShortNotation; |
37
|
|
|
protected $metadata; |
38
|
|
|
|
39
|
|
|
public function __construct(ImageEntityClassLocator $entityClassFinder, $doctrine) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
|
43
|
|
|
$this->responsiveImageEntity = $entityClassFinder->getClassName(); |
44
|
|
|
$this->doctrine = $doctrine; |
45
|
|
|
$em = $this->doctrine->getManager(); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$this->metadata = $em->getClassMetadata($this->responsiveImageEntity); |
49
|
|
|
} catch (\Exception $e) { |
50
|
|
|
throw new \RuntimeException( |
51
|
|
|
sprintf( |
52
|
|
|
'Entity "%s" does not exist. Create it with the "doctrine:generate:entity" command and then execute this command again.', |
53
|
|
|
$this->responsiveImageEntity |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$namespace = $this->metadata->namespace; |
59
|
|
|
|
60
|
|
|
// This is bit hacky but it'll do for now. |
61
|
|
|
// Lets get rid of the '\Entity'. |
62
|
|
|
if (strpos($namespace, '\\Entity') > 0) { |
63
|
|
|
$namespace = substr($namespace, 0, -7); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$namespaceParts = explode('\\', $namespace); |
67
|
|
|
$this->bundle = array_pop($namespaceParts); |
68
|
|
|
$entityNameParts = explode('\\', $this->responsiveImageEntity); |
69
|
|
|
$this->entityName = array_pop($entityNameParts); |
70
|
|
|
$this->entityShortNotation = $this->bundle . ':' . $this->entityName; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function configure() |
74
|
|
|
{ |
75
|
|
|
// This limits CRUD generation to the single entity defined in configuration |
76
|
|
|
|
77
|
|
|
$this |
78
|
|
|
->setName('responsive_image:generate:crud') |
79
|
|
|
->setDescription('Generates the CRUD for responsive image entity') |
80
|
|
|
->setDefinition( |
81
|
|
|
[ |
82
|
|
|
new InputOption('route-prefix', '', InputOption::VALUE_REQUIRED, 'The route prefix'), |
83
|
|
|
new InputOption( |
84
|
|
|
'format', |
85
|
|
|
'', |
86
|
|
|
InputOption::VALUE_REQUIRED, |
87
|
|
|
'The format used for configuration files (php, xml, yml, or annotation)', |
88
|
|
|
'annotation' |
89
|
|
|
), |
90
|
|
|
new InputOption( |
91
|
|
|
'overwrite', |
92
|
|
|
'', |
93
|
|
|
InputOption::VALUE_NONE, |
94
|
|
|
'Overwrite any existing controller or form class when generating the CRUD contents' |
95
|
|
|
), |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @see Command |
102
|
|
|
* |
103
|
|
|
* @param InputInterface $input |
104
|
|
|
* @param OutputInterface $output |
105
|
|
|
* |
106
|
|
|
* @return int|null |
107
|
|
|
*/ |
108
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
109
|
|
|
{ |
110
|
|
|
$questionHelper = $this->getQuestionHelper(); |
111
|
|
|
|
112
|
|
View Code Duplication |
if ($input->isInteractive()) { |
|
|
|
|
113
|
|
|
$question = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true); |
114
|
|
|
if (!$questionHelper->ask($input, $output, $question)) { |
115
|
|
|
$output->writeln('<error>Command aborted</error>'); |
116
|
|
|
|
117
|
|
|
return 1; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$entity = Validators::validateEntityName($this->entityShortNotation); |
122
|
|
|
$bundle = $this->bundle; |
123
|
|
|
|
124
|
|
|
$format = Validators::validateFormat($input->getOption('format')); |
125
|
|
|
$prefix = $this->getRoutePrefix($input, $entity); |
126
|
|
|
|
127
|
|
|
$questionHelper->writeSection($output, 'CRUD generation'); |
128
|
|
|
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle); |
129
|
|
|
|
130
|
|
|
$generator = $this->getGenerator($bundle); |
131
|
|
|
|
132
|
|
|
// $withWrite = true; |
|
|
|
|
133
|
|
|
// $forceOverwrite = true; |
|
|
|
|
134
|
|
|
// @TODO: Perhaps Don't force overwrite |
135
|
|
|
$generator->generate($bundle, $this->entityName, $this->metadata[0], $format, $prefix, true, true); |
136
|
|
|
|
137
|
|
|
$output->writeln('Generating the CRUD code: <info>OK</info>'); |
138
|
|
|
|
139
|
|
|
$errors = []; |
140
|
|
|
$runner = $questionHelper->getRunner($output, $errors); |
141
|
|
|
|
142
|
|
|
// routing |
143
|
|
|
$output->write('Updating the routing: '); |
144
|
|
|
if ('annotation' != $format) { |
145
|
|
|
$runner($this->updateRouting($questionHelper, $input, $output, $bundle, $format, $entity, $prefix)); |
146
|
|
|
} |
147
|
|
|
else { |
148
|
|
|
$runner($this->updateAnnotationRouting($bundle, $entity, $prefix)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$questionHelper->writeGeneratorSummary($output, $errors); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
155
|
|
|
{ |
156
|
|
|
$questionHelper = $this->getQuestionHelper(); |
157
|
|
|
$questionHelper->writeSection($output, 'Welcome to the Doctrine2 CRUD generator'); |
158
|
|
|
|
159
|
|
|
// namespace |
160
|
|
|
$output->writeln( |
161
|
|
|
[ |
162
|
|
|
'', |
163
|
|
|
'This command helps you generate CRUD controllers and templates.', |
164
|
|
|
'', |
165
|
|
|
'First, give the name of the existing entity for which you want to generate a CRUD', |
166
|
|
|
'(use the shortcut notation like <comment>AcmeBlogBundle:Post</comment>)', |
167
|
|
|
'', |
168
|
|
|
] |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$entity = $this->entityName; |
172
|
|
|
$bundle = $this->bundle; |
173
|
|
|
try { |
174
|
|
|
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle) . '\\' . $entity; |
175
|
|
|
$this->getEntityMetadata($entityClass); |
176
|
|
|
} catch (\Exception $e) { |
177
|
|
|
throw new \RuntimeException( |
178
|
|
|
sprintf( |
179
|
|
|
'Entity "%s" does not exist in the "%s" bundle. You may have mistyped the bundle name or maybe the entity doesn\'t exist yet (create it first with the "doctrine:generate:entity" command).', |
180
|
|
|
$entity, |
181
|
|
|
$bundle |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// format |
187
|
|
|
$format = $input->getOption('format'); |
188
|
|
|
$output->writeln( |
189
|
|
|
[ |
190
|
|
|
'', |
191
|
|
|
'Determine the format to use for the generated CRUD.', |
192
|
|
|
'', |
193
|
|
|
] |
194
|
|
|
); |
195
|
|
|
$question = new Question( |
196
|
|
|
$questionHelper->getQuestion('Configuration format (yml, xml, php, or annotation)', $format), $format |
197
|
|
|
); |
198
|
|
|
$question->setValidator(['Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateFormat']); |
199
|
|
|
$format = $questionHelper->ask($input, $output, $question); |
200
|
|
|
$input->setOption('format', $format); |
201
|
|
|
|
202
|
|
|
// route prefix |
203
|
|
|
$prefix = $this->getRoutePrefix($input, $entity); |
204
|
|
|
$output->writeln( |
205
|
|
|
[ |
206
|
|
|
'', |
207
|
|
|
'Determine the routes prefix (all the routes will be "mounted" under this', |
208
|
|
|
'prefix: /prefix/, /prefix/new, ...).', |
209
|
|
|
'', |
210
|
|
|
] |
211
|
|
|
); |
212
|
|
|
$prefix = $questionHelper->ask( |
213
|
|
|
$input, |
214
|
|
|
$output, |
215
|
|
|
new Question($questionHelper->getQuestion('Routes prefix', '/' . $prefix), '/' . $prefix) |
216
|
|
|
); |
217
|
|
|
$input->setOption('route-prefix', $prefix); |
218
|
|
|
|
219
|
|
|
// summary |
220
|
|
|
$output->writeln( |
221
|
|
|
[ |
222
|
|
|
'', |
223
|
|
|
$this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), |
|
|
|
|
224
|
|
|
'', |
225
|
|
|
sprintf('You are going to generate a CRUD controller for "<info>%s:%s</info>"', $bundle, $entity), |
226
|
|
|
sprintf('using the "<info>%s</info>" format.', $format), |
227
|
|
|
'', |
228
|
|
|
] |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected function getSkeletonDirs(BundleInterface $bundle = null) |
233
|
|
|
{ |
234
|
|
|
$skeletonDirs = []; |
235
|
|
|
|
236
|
|
|
if (is_dir( |
237
|
|
|
$dir = $this->getContainer()->get('kernel')->getRootdir() . '/Resources/ResponsiveImageBundle/skeleton' |
238
|
|
|
)) { |
239
|
|
|
$skeletonDirs[] = $dir; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$skeletonDirs[] = __DIR__ . '/../Resources/skeleton'; |
243
|
|
|
|
244
|
|
|
return $skeletonDirs; |
245
|
|
|
} |
246
|
|
|
} |
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.