1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Petkopara\CrudGeneratorBundle\Generator; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\Inflector; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
7
|
|
|
use Petkopara\CrudGeneratorBundle\Configuration\Configuration; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator; |
10
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
11
|
|
|
|
12
|
|
|
class PetkoparaCrudGenerator extends DoctrineCrudGenerator |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $formFilterGenerator; |
16
|
|
|
protected $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @param BundleInterface $bundle |
21
|
|
|
* @param string $entity |
22
|
|
|
* @param ClassMetadataInfo $metadata |
23
|
|
|
* @param Configuration $config |
24
|
|
|
* @throws RuntimeException |
25
|
|
|
*/ |
26
|
6 |
|
public function generateCrud(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, Configuration $config) |
27
|
|
|
{ |
28
|
6 |
|
$this->config = $config; |
29
|
6 |
|
$this->routeNamePrefix = self::getRouteNamePrefix($config->getRoutePrefix()); |
30
|
6 |
|
$this->actions = $config->getCrudActions(); |
31
|
|
|
|
32
|
6 |
|
if (count($metadata->identifier) != 1) { |
33
|
|
|
throw new RuntimeException('The CRUD generator does not support entity classes with multiple or no primary keys.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
6 |
|
$this->entity = $entity; |
37
|
6 |
|
$this->entitySingularized = lcfirst(Inflector::singularize($entity)); |
38
|
6 |
|
$this->entityPluralized = lcfirst(Inflector::pluralize($entity)); |
39
|
6 |
|
$this->bundle = $bundle; |
40
|
6 |
|
$this->metadata = $metadata; |
41
|
|
|
|
42
|
6 |
|
$this->setFormat($config->getFormat()); |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
//define where to save the view files |
46
|
6 |
|
if (!$config->getBundleViews()) { //save in root Resources |
47
|
6 |
|
$dir = sprintf('%s/Resources/views/%s', $this->rootDir, str_replace('\\', '/', strtolower($this->entity))); |
48
|
6 |
|
} else { //save in bundle Resources |
49
|
|
|
$dir = sprintf('%s/Resources/views/%s', $bundle->getPath(), str_replace('\\', '/', $this->entity)); |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
$this->generateCrudControllerClass(); |
53
|
|
|
|
54
|
6 |
|
if (!file_exists($dir)) { |
55
|
6 |
|
$this->filesystem->mkdir($dir, 0777); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
6 |
|
$this->generateIndexView($dir); |
59
|
|
|
|
60
|
6 |
|
if (in_array('show', $this->actions)) { |
61
|
6 |
|
$this->generateShowView($dir); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
6 |
|
if (in_array('new', $this->actions)) { |
65
|
6 |
|
$this->generateNewView($dir); |
66
|
6 |
|
} |
67
|
|
|
|
68
|
6 |
|
if (in_array('edit', $this->actions)) { |
69
|
6 |
|
$this->generateEditView($dir); |
70
|
6 |
|
} |
71
|
|
|
|
72
|
6 |
|
$this->generateTestClass(); |
73
|
6 |
|
$this->generateConfiguration(); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generates the index.html.twig template in the final bundle. |
78
|
|
|
* |
79
|
|
|
* @param string $dir The path to the folder that hosts templates in the bundle |
80
|
|
|
*/ |
81
|
6 |
|
protected function generateIndexView($dir) |
82
|
|
|
{ |
83
|
6 |
|
$this->renderFile('crud/views/index.html.twig.twig', $dir . '/index.html.twig', array( |
84
|
6 |
|
'bundle' => $this->bundle->getName(), |
85
|
6 |
|
'entity' => $this->entity, |
86
|
6 |
|
'entity_pluralized' => $this->entityPluralized, |
87
|
6 |
|
'entity_singularized' => $this->entitySingularized, |
88
|
6 |
|
'identifier' => $this->metadata->identifier[0], |
89
|
6 |
|
'fields' => $this->metadata->fieldMappings, |
90
|
6 |
|
'actions' => $this->actions, |
91
|
6 |
|
'record_actions' => $this->getRecordActions(), |
92
|
6 |
|
'route_prefix' => $this->config->getRoutePrefix(), |
93
|
6 |
|
'route_name_prefix' => $this->routeNamePrefix, |
94
|
6 |
|
'base_template' => $this->config->getBaseTemplate(), |
95
|
6 |
|
'without_bulk_action' => $this->config->getWithoutBulk(), |
96
|
6 |
|
'without_sorting' => $this->config->getWithoutSorting(), |
97
|
6 |
|
'without_page_size' => $this->config->getWithoutPageSize(), |
98
|
6 |
|
'filter_type' => $this->config->getFilterType(), |
99
|
6 |
|
)); |
100
|
6 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generates the show.html.twig template in the final bundle. |
104
|
|
|
* |
105
|
|
|
* @param string $dir The path to the folder that hosts templates in the bundle |
106
|
|
|
*/ |
107
|
6 |
View Code Duplication |
protected function generateShowView($dir) |
|
|
|
|
108
|
|
|
{ |
109
|
6 |
|
$this->renderFile('crud/views/show.html.twig.twig', $dir . '/show.html.twig', array( |
110
|
6 |
|
'bundle' => $this->bundle->getName(), |
111
|
6 |
|
'entity' => $this->entity, |
112
|
6 |
|
'entity_singularized' => $this->entitySingularized, |
113
|
6 |
|
'identifier' => $this->metadata->identifier[0], |
114
|
6 |
|
'fields' => $this->metadata->fieldMappings, |
115
|
6 |
|
'actions' => $this->actions, |
116
|
6 |
|
'route_prefix' => $this->config->getRoutePrefix(), |
117
|
6 |
|
'route_name_prefix' => $this->routeNamePrefix, |
118
|
6 |
|
'base_template' => $this->config->getBaseTemplate(), |
119
|
6 |
|
)); |
120
|
6 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Generates the new.html.twig template in the final bundle. |
124
|
|
|
* |
125
|
|
|
* @param string $dir The path to the folder that hosts templates in the bundle |
126
|
|
|
*/ |
127
|
6 |
|
protected function generateNewView($dir) |
128
|
|
|
{ |
129
|
6 |
|
$this->renderFile('crud/views/new.html.twig.twig', $dir . '/new.html.twig', array( |
130
|
6 |
|
'bundle' => $this->bundle->getName(), |
131
|
6 |
|
'entity' => $this->entity, |
132
|
6 |
|
'entity_singularized' => $this->entitySingularized, |
133
|
6 |
|
'route_prefix' => $this->config->getRoutePrefix(), |
134
|
6 |
|
'route_name_prefix' => $this->routeNamePrefix, |
135
|
6 |
|
'actions' => $this->actions, |
136
|
6 |
|
'fields' => $this->metadata->fieldMappings, |
137
|
6 |
|
'base_template' => $this->config->getBaseTemplate(), |
138
|
6 |
|
)); |
139
|
6 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Generates the edit.html.twig template in the final bundle. |
143
|
|
|
* |
144
|
|
|
* @param string $dir The path to the folder that hosts templates in the bundle |
145
|
|
|
*/ |
146
|
6 |
View Code Duplication |
protected function generateEditView($dir) |
|
|
|
|
147
|
|
|
{ |
148
|
6 |
|
$this->renderFile('crud/views/edit.html.twig.twig', $dir . '/edit.html.twig', array( |
149
|
6 |
|
'route_prefix' => $this->config->getRoutePrefix(), |
150
|
6 |
|
'route_name_prefix' => $this->routeNamePrefix, |
151
|
6 |
|
'identifier' => $this->metadata->identifier[0], |
152
|
6 |
|
'entity' => $this->entity, |
153
|
6 |
|
'entity_singularized' => $this->entitySingularized, |
154
|
6 |
|
'fields' => $this->metadata->fieldMappings, |
155
|
6 |
|
'bundle' => $this->bundle->getName(), |
156
|
6 |
|
'actions' => $this->actions, |
157
|
6 |
|
'base_template' => $this->config->getBaseTemplate(), |
158
|
6 |
|
)); |
159
|
6 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Generates the controller class only. |
163
|
|
|
*/ |
164
|
6 |
|
protected function generateCrudControllerClass() { |
165
|
6 |
|
$dir = $this->bundle->getPath(); |
166
|
|
|
|
167
|
6 |
|
$parts = explode('\\', $this->entity); |
168
|
6 |
|
$entityClass = array_pop($parts); |
169
|
6 |
|
$entityNamespace = implode('\\', $parts); |
170
|
|
|
|
171
|
6 |
|
$target = sprintf( |
172
|
6 |
|
'%s/Controller/%s/%sController.php', $dir, str_replace('\\', '/', $entityNamespace), $entityClass |
173
|
6 |
|
); |
174
|
|
|
|
175
|
6 |
|
if (!$this->config->getOverwrite() && file_exists($target)) { |
176
|
|
|
throw new \RuntimeException('Unable to generate the controller as it already exists.'); |
177
|
|
|
} |
178
|
|
|
|
179
|
6 |
|
$this->renderFile('crud/controller.php.twig', $target, array( |
180
|
6 |
|
'actions' => $this->actions, |
181
|
6 |
|
'route_prefix' => $this->config->getRoutePrefix(), |
182
|
6 |
|
'route_name_prefix' => $this->routeNamePrefix, |
183
|
6 |
|
'bundle' => $this->bundle->getName(), |
184
|
6 |
|
'entity' => $this->entity, |
185
|
6 |
|
'entity_singularized' => $this->entitySingularized, |
186
|
6 |
|
'entity_pluralized' => $this->entityPluralized, |
187
|
6 |
|
'entity_class' => $entityClass, |
188
|
6 |
|
'namespace' => $this->bundle->getNamespace(), |
189
|
6 |
|
'entity_namespace' => $entityNamespace, |
190
|
6 |
|
'format' => $this->config->getFormat(), |
191
|
6 |
|
'bundle_views' => $this->config->getBundleViews(), |
192
|
6 |
|
'filter_type' => $this->config->getFilterTYpe(), |
193
|
6 |
|
'without_sorting' => $this->config->getWithoutSorting(), |
194
|
6 |
|
'without_page_size' => $this->config->getWithoutPageSize(), |
195
|
6 |
|
'identifier' => $this->metadata->identifier[0], |
196
|
|
|
|
197
|
6 |
|
)); |
198
|
6 |
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
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.