-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBundleGenerator.php
More file actions
224 lines (195 loc) · 6.1 KB
/
BundleGenerator.php
File metadata and controls
224 lines (195 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Admingenerator\GeneratorBundle\Generator;
use Sensio\Bundle\GeneratorBundle\Generator\Generator as BaseBundleGenerator;
use Sensio\Bundle\GeneratorBundle\Model\Bundle;
use Symfony\Component\Filesystem\Filesystem;
/**
* Generates an admin bundle.
*
* @author Cedric LOMBARDOT
*/
class BundleGenerator extends BaseBundleGenerator
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var string
*/
private $skeletonDir;
/**
* @var string
*/
protected $generator;
/**
* @var string
*/
protected $prefix;
/**
* @var array
*/
protected $actions = array(
'New' => array('views' => array(
'index',
'form',
)),
'List' => array('views' => array(
'index',
'results',
'filters',
'row'
)),
'Excel' => array('views' => array()),
'Edit' => array('views' => array(
'index',
'form',
)),
'Show' => array('views' => array('index')),
'Actions' => array('views' => array('index'))
);
/**
* @var array
*/
protected $forms = array('New', 'Filters', 'Edit');
/**
* @param string $skeletonDir
*/
public function __construct(Filesystem $filesystem, $skeletonDir)
{
$this->filesystem = $filesystem;
$this->skeletonDir = $skeletonDir;
if (method_exists($this, 'setSkeletonDirs')) {
$this->setSkeletonDirs($this->skeletonDir);
}
}
/**
* @param string $generator
*/
public function setGenerator($generator)
{
$this->generator = $generator;
}
/**
* @param string $prefix
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* @param Bundle $bundle
* @param string $modelName
*/
public function generate(Bundle $bundle, $modelName, $formsOnly)
{
$dir = $bundle->getTargetDirectory();
// Retrieves model folder depending on chosen Model Manager
$modelFolder = '';
switch ($this->generator) {
case 'propel':
$modelFolder = 'Model';
break;
case 'doctrine':
$modelFolder = 'Entity';
break;
case 'doctrine_orm':
$modelFolder = 'Document';
break;
}
if (false === strpos($bundle->getNamespace(), '\\')) {
$bundleName = $bundle->getNamespace();
$namespacePrefix = null;
} else {
list( $namespacePrefix, $bundleName) = explode('\\', $bundle->getNamespace(), 2);
}
$parameters = array(
'namespace' => $bundle->getNamespace(),
'bundle' => $bundle->getName(),
'generator' => 'admingenerator.generator.'.$this->generator,
'namespace_prefix' => $namespacePrefix,
'bundle_name' => $bundleName,
'model_folder' => $modelFolder,
'model_name' => $modelName,
'prefix' => ucfirst($this->prefix),
'forms_only' => $formsOnly
);
if (!file_exists($dir.'/'.$bundle->getName().'.php')) {
$this->renderGeneratedFile('Bundle.php.twig', $dir.'/'.$bundle->getName().'.php', $parameters);
}
foreach ($this->forms as $form) {
$parameters['form'] = $form;
$formFile = $dir.'/Form/Type/'.($this->prefix ? ucfirst($this->prefix).'/' : '').$form.'Type.php';
$this->copyPreviousFile($formFile);
$this->renderGeneratedFile(
'DefaultType.php.twig',
$formFile,
$parameters
);
}
$optionsFile = $dir.'/Form/Type/'.($this->prefix ? ucfirst($this->prefix).'/' : '').'Options.php';
$this->copyPreviousFile($optionsFile);
$this->renderGeneratedFile(
'DefaultOptions.php.twig',
$optionsFile,
$parameters
);
$generatorFile = $dir.'/Resources/config/'.($this->prefix ? ucfirst($this->prefix).'-' : '').'generator.yml';
$this->copyPreviousFile($generatorFile);
$this->renderGeneratedFile(
'generator.yml.twig',
$generatorFile,
$parameters
);
if ($formsOnly) {
return;
}
foreach ($this->actions as $action => $actionProperties) {
$parameters['action'] = $action;
$controllerFile = $dir.'/Controller/'
.($this->prefix ? ucfirst($this->prefix).'/' : '').$action.'Controller.php';
$this->copyPreviousFile($controllerFile);
$this->renderGeneratedFile(
'DefaultController.php.twig',
$controllerFile,
$parameters
);
foreach ($actionProperties['views'] as $templateName) {
$templateFile = $dir.'/Resources/views/'.ucfirst($this->prefix).$action.'/'.$templateName.'.html.twig';
$this->copyPreviousFile($templateFile);
$this->renderGeneratedFile(
'default_view.html.twig',
$templateFile,
$parameters + array('view' => $templateName)
);
}
}
}
/**
* @param string $template
* @param string $target
*/
protected function renderGeneratedFile($template, $target, array $parameters)
{
$this->renderFile($template, $target, $parameters);
}
/**
* @param string $oldname
*/
protected function copyPreviousFile($oldname)
{
if (file_exists($oldname)) {
$newname = $oldname.'~';
// Find unused copy name
if (file_exists($newname)) {
$key = 0;
do {
$key++;
} while (file_exists($oldname.'~'.$key));
$newname = $oldname.'~'.$key;
}
// Create new copy
rename($oldname, $newname);
}
}
}