1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kujaff\VersionsBundle\Service; |
4
|
|
|
|
5
|
|
|
use kujaff\VersionsBundle\Model\BundleInformations; |
6
|
|
|
use steevanb\CodeGenerator\PHP\ClassGenerator; |
7
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Generate files to make bundle versionned |
12
|
|
|
*/ |
13
|
|
|
class Generator |
14
|
|
|
{ |
15
|
|
|
use BundleInformations; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Return services.yml file path |
19
|
|
|
* |
20
|
|
|
* @param BaseBundle $bundle |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
protected function getYamlFilePath(BaseBundle $bundle) |
24
|
|
|
{ |
25
|
|
|
return $bundle->getPath() . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'services.yml'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Parse Yaml service declaration |
30
|
|
|
* |
31
|
|
|
* @param BaseBundle $bundle |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
protected function parseServicesYaml(BaseBundle $bundle) |
35
|
|
|
{ |
36
|
|
|
$servicesFilePath = $this->getYamlFilePath($bundle); |
37
|
|
|
$return = array(); |
38
|
|
|
if (file_exists($servicesFilePath)) { |
39
|
|
|
$return = Yaml::parse(file_get_contents($servicesFilePath)); |
40
|
|
|
if ($return === null) { |
41
|
|
|
$return = array(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
if (array_key_exists('services', $return) === false || is_array($return['services']) === false) { |
45
|
|
|
$return['services'] = array(); |
46
|
|
|
} |
47
|
|
|
return $return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return a ClassGenerator |
52
|
|
|
* |
53
|
|
|
* @param BaseBundle $bundleInfos |
54
|
|
|
* @param string $class Class name |
55
|
|
|
* @param string $interface Interface to use, just class name |
56
|
|
|
* @return ClassGenerator |
57
|
|
|
*/ |
58
|
|
|
protected function initGenerator(BaseBundle $bundleInfos, $class, $interface) |
59
|
|
|
{ |
60
|
|
|
$return = new ClassGenerator(); |
61
|
|
|
$return->setClassName($class); |
62
|
|
|
$return->setNamespace($bundleInfos->getNamespace() . '\Service\Install'); |
63
|
|
|
$return->setTraits(array( |
64
|
|
|
'kujaff\VersionsBundle\Model\BundleNameFromClassName', |
65
|
|
|
'kujaff\VersionsBundle\Model\DoctrineHelper', |
66
|
|
|
)); |
67
|
|
|
$return->setExtends('Symfony\Component\DependencyInjection\ContainerAware'); |
68
|
|
|
$return->addUse('kujaff\VersionsBundle\Model\\' . $interface, 'Base' . $interface); |
69
|
|
|
$return->addInterface('Base' . $interface); |
70
|
|
|
$return->setConcatTraits(true); |
71
|
|
|
|
72
|
|
|
return $return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Register an installer service |
77
|
|
|
* |
78
|
|
|
* @param BaseBundle $bundleInfos |
79
|
|
|
* @param string $type Type (install, update or uninstall) |
80
|
|
|
* @param string $class Class (Install, Update or Uninstall) |
81
|
|
|
*/ |
82
|
|
|
protected function registerInstallerService(BaseBundle $bundleInfos, $type, $class) |
83
|
|
|
{ |
84
|
|
|
$serviceId = strtolower($bundleInfos->getName()) . '.installer.' . $type; |
85
|
|
|
$fullyQualifiedClass = $bundleInfos->getNamespace() . '\Service\Install\\' . $class; |
86
|
|
|
$serviceOptions = array( |
87
|
|
|
'calls' => array(array('setContainer' => array('@service_container'))), |
88
|
|
|
'tags' => array(array('name' => 'bundle.' . $type)) |
89
|
|
|
); |
90
|
|
|
$this->registerService($bundleInfos->getName(), $serviceId, $fullyQualifiedClass, $serviceOptions); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Register a new service in Resources/config/services.yml |
95
|
|
|
* |
96
|
|
|
* @param string $bundle Bundle name, ex 'FooBundle' |
97
|
|
|
* @param string $service Service name, ex 'foobundle.service' |
98
|
|
|
* @param string $class Fully qualified class name, ex 'Foo\Bar\ClassName' |
99
|
|
|
* @param array $options Options, ex array('arguments' => array('@service_container'), 'tags' => array(array('name' => 'bundle.install')) |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
|
public function registerService($bundle, $service, $class, $options = array()) |
103
|
|
|
{ |
104
|
|
|
$bundleInfos = $this->getBundleInformations($bundle); |
105
|
|
|
$services = $this->parseServicesYaml($bundleInfos); |
106
|
|
|
|
107
|
|
|
$services['services'][$service] = array_merge(array('class' => $class), $options); |
108
|
|
|
$yamlFilePath = $this->getYamlFilePath($bundleInfos); |
109
|
|
|
$yamlContent = Yaml::dump($services, 4); |
110
|
|
|
|
111
|
|
|
$result = file_put_contents($yamlFilePath, $yamlContent); |
112
|
|
|
if ($result === false) { |
113
|
|
|
throw new \Exception('Error while writing "' . $yamlFilePath . '", maybe directory or file can\'t be written.'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Indicate if a tagged service exists in bundle |
119
|
|
|
* |
120
|
|
|
* @param string $bundle Bundle name, ex 'FooBundle' |
121
|
|
|
* @param string $tag Tag name, ex 'bundle.install' |
122
|
|
|
* @return boolean |
123
|
|
|
*/ |
124
|
|
|
public function existsTaggedService($bundle, $tag) |
125
|
|
|
{ |
126
|
|
|
$services = $this->parseServicesYaml($this->getBundleInformations($bundle)); |
127
|
|
|
foreach ($services['services'] as $params) { |
128
|
|
|
if (array_key_exists('tags', $params) && is_array($params['tags'])) { |
129
|
|
|
foreach ($params['tags'] as $tagInfos) { |
130
|
|
|
if (array_key_exists('name', $tagInfos) && $tagInfos['name'] == $tag) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Generate everything to make your bundle versionned |
141
|
|
|
* |
142
|
|
|
* @param string $bundle Name of your bundle, ex 'FooBundle' |
143
|
|
|
* @param string $versionAfterInstallation Version after installation, ex '1.0.0' |
144
|
|
|
* @param string $updateTrait Trait to use in Update service, like kujaff\VersionsBundle\Model\UpdateOneVersionOneMethod |
145
|
|
|
*/ |
146
|
|
|
public function generate($bundle, $versionAfterInstallation, $updateTrait = null, $force = false) |
147
|
|
|
{ |
148
|
|
|
$this->generateInstallService($bundle, $versionAfterInstallation, $force); |
149
|
|
|
$this->generateUpdateService($bundle, $updateTrait, $force); |
150
|
|
|
$this->generateUninstallService($bundle, $force); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Generate service and register it for installation |
155
|
|
|
* |
156
|
|
|
* @param string $bundle Name of your bundle, ex 'FooBundle' |
157
|
|
|
* @param string $version Version after installation, ex '1.0.0' |
158
|
|
|
* @param boolean $force Indicate if you want to regenerate it although it exists |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
|
|
public function generateInstallService($bundle, $version, $force = false) |
162
|
|
|
{ |
163
|
|
|
$bundleInfos = $this->getBundleInformations($bundle); |
164
|
|
|
// do not create service if another one is already registered |
165
|
|
|
if ($this->existsTaggedService($bundle, 'bundle.install') && $force === false) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$generator = $this->initGenerator($bundleInfos, 'Install', 'Install'); |
170
|
|
|
|
171
|
|
|
$generator->startMethod('install', ClassGenerator::VISIBILITY_PUBLIC, false, array('Installation'), 'kujaff\VersionsBundle\Entity\Version'); |
|
|
|
|
172
|
|
|
$generator->addMethodLine($generator->getCode4Comment('Do your stuff here')); |
|
|
|
|
173
|
|
|
$generator->addMethodLine($generator->getCode4Line('return new Version(\'' . $version . '\');', 0, 0)); |
|
|
|
|
174
|
|
|
$generator->finishMethod(); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$generator->write($bundleInfos->getPath() . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'Install' . DIRECTORY_SEPARATOR . 'Install.php'); |
177
|
|
|
|
178
|
|
|
$this->registerInstallerService($bundleInfos, 'install', 'Install'); |
179
|
|
|
|
180
|
|
|
return true; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $bundle |
185
|
|
|
* @param string $trait |
186
|
|
|
* @param bool $force |
187
|
|
|
* @return bool |
188
|
|
|
* @throws \kujaff\VersionsBundle\Exception\BundleNotFoundException |
189
|
|
|
*/ |
190
|
|
|
public function generateUpdateService($bundle, $trait = null, $force = false) |
191
|
|
|
{ |
192
|
|
|
$bundleInfos = $this->getBundleInformations($bundle); |
193
|
|
|
// do not create service if another one is already registered |
194
|
|
|
if ($this->existsTaggedService($bundle, 'bundle.update') && $force === false) { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$generator = $this->initGenerator($bundleInfos, 'Update', 'Update'); |
199
|
|
|
|
200
|
|
|
if ($trait !== null) { |
201
|
|
|
$generator->addTrait($trait); |
202
|
|
|
} else { |
203
|
|
|
$generator->startMethod('update', ClassGenerator::VISIBILITY_PUBLIC, false, array('Updates'), 'kujaff\VersionsBundle\Entity\Version'); |
|
|
|
|
204
|
|
|
$generator->addMethodParameter('bundleVersion', 'kujaff\VersionsBundle\Entity\BundleVersion', null, true, 'Current installed version'); |
|
|
|
|
205
|
|
|
$generator->addMethodParameter('version', 'kujaff\VersionsBundle\Entity\Version', null, true, 'Update to this version'); |
|
|
|
|
206
|
|
|
$generator->addMethodLine($generator->getCode4Comment('Do your stuff here')); |
|
|
|
|
207
|
|
|
$generator->addMethodLine($generator->getCode4Comment('Return updated version after your patchs', 0, 0)); |
|
|
|
|
208
|
|
|
$generator->addMethodLine($generator->getCode4Line('return $version;', 0, 0)); |
|
|
|
|
209
|
|
|
$generator->finishMethod(); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$generator->write($bundleInfos->getPath() . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'Install' . DIRECTORY_SEPARATOR . 'Update.php'); |
213
|
|
|
|
214
|
|
|
$this->registerInstallerService($bundleInfos, 'update', 'Update'); |
215
|
|
|
|
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Generate service and register it for uninstall |
221
|
|
|
* |
222
|
|
|
* @param string $bundle Name of your bundle, ex 'FooBundle' |
223
|
|
|
* @param boolean $force Indicate if you want to regenerate it although it exists |
224
|
|
|
* @return boolean |
225
|
|
|
*/ |
226
|
|
|
public function generateUninstallService($bundle, $force = false) |
227
|
|
|
{ |
228
|
|
|
$bundleInfos = $this->getBundleInformations($bundle); |
229
|
|
|
// do not create service if another one is already registered |
230
|
|
|
if ($this->existsTaggedService($bundle, 'bundle.uninstall') && $force === false) { |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$generator = $this->initGenerator($bundleInfos, 'Uninstall', 'Uninstall'); |
235
|
|
|
|
236
|
|
|
$generator->startMethod('uninstall', ClassGenerator::VISIBILITY_PUBLIC, false, array('Uninstall')); |
|
|
|
|
237
|
|
|
$generator->addMethodLine($generator->getCode4Comment('Do your stuff here', 0, 0)); |
|
|
|
|
238
|
|
|
$generator->finishMethod(); |
|
|
|
|
239
|
|
|
|
240
|
|
|
$generator->write($bundleInfos->getPath() . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'Install' . DIRECTORY_SEPARATOR . 'Uninstall.php'); |
241
|
|
|
|
242
|
|
|
$this->registerInstallerService($bundleInfos, 'uninstall', 'Uninstall'); |
243
|
|
|
|
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.