1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kujaff\VersionsBundle\Service; |
4
|
|
|
|
5
|
|
|
use kujaff\VersionsBundle\Entity\Version; |
|
|
|
|
6
|
|
|
use kujaff\VersionsBundle\Entity\BundleVersion; |
|
|
|
|
7
|
|
|
use kujaff\VersionsBundle\Exception\StructureException; |
8
|
|
|
use kujaff\VersionsBundle\Exception\InstallStateException; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use \Symfony\Component\DependencyInjection\ContainerAware; |
11
|
|
|
|
12
|
|
|
class Installer extends ContainerAware |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Return tagged services |
16
|
|
|
* |
17
|
|
|
* @param string $bundle |
18
|
|
|
* @param string $tag |
19
|
|
|
* @param $implements Fully qualified interface name must be implemented |
20
|
|
|
* @return array |
21
|
|
|
* @throws StructureException |
22
|
|
|
*/ |
23
|
|
|
private function getService($bundle, $tag, $implements) |
24
|
|
|
{ |
25
|
|
|
$fileName = $this->container->getParameter('kernel.cache_dir') . DIRECTORY_SEPARATOR . 'services.bundle.' . $tag . '.php'; |
26
|
|
|
if (file_exists($fileName) === false) { |
27
|
|
|
throw new StructureException('Unable to find service tagged "bundle."' . $tag . '.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$serviceIds = require($fileName); |
31
|
|
|
foreach ($serviceIds as $id) { |
32
|
|
|
$service = $this->container->get($id); |
33
|
|
|
if (!$service instanceof $implements) { |
34
|
|
|
throw new StructureException('Service "' . $id . '" must implements ' . $implements . '.'); |
35
|
|
|
} |
36
|
|
|
if ($service->getBundleName() == $bundle) { |
37
|
|
|
return $service; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get bundle version, assert a version si defined |
45
|
|
|
* |
46
|
|
|
* @param string $bundle |
47
|
|
|
* @return BundleVersion |
48
|
|
|
* @throws StructureException |
49
|
|
|
*/ |
50
|
|
|
private function getBundleVersion($bundle) |
51
|
|
|
{ |
52
|
|
|
$return = $this->container->get('versions.bundle')->getVersion($bundle); |
53
|
|
|
if ($return->getVersion() === null) { |
54
|
|
|
throw new StructureException('Bundle "' . $bundle . '" main class must extends kujaff\VersionsBundle\Model\VersionnedBundle.'); |
55
|
|
|
} |
56
|
|
|
return $return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Install |
61
|
|
|
* |
62
|
|
|
* @param string $bundle |
63
|
|
|
* @param boolean $force Force installation |
64
|
|
|
* @param OutputInterface $output |
65
|
|
|
* @return Version |
66
|
|
|
* @throws StructureException |
67
|
|
|
* @throws InstallStateException |
68
|
|
|
*/ |
69
|
|
|
public function install($bundle, $force = false, $output = null) |
70
|
|
|
{ |
71
|
|
|
$manager = $this->container->get('doctrine')->getManager(); |
72
|
|
|
if ($force === false) { |
73
|
|
|
$bundleVersion = $this->getBundleVersion($bundle); |
74
|
|
|
|
75
|
|
|
// already installed |
76
|
|
|
if ($bundleVersion->isInstalled()) { |
77
|
|
|
throw new InstallStateException('Bundle "' . $bundle . '" is already installed.'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($output instanceof OutputInterface) { |
82
|
|
|
$output->write('[<comment>' . $bundle . '</comment>] Installing ... '); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$service = $this->getService($bundle, 'install', 'kujaff\\VersionsBundle\\Model\\Install'); |
86
|
|
|
// bundle has a service to do stuff |
87
|
|
|
if ($service !== false) { |
88
|
|
|
$installedVersion = $service->install(); |
89
|
|
|
if (!$installedVersion instanceof Version) { |
90
|
|
|
throw new StructureException('Service "' . get_class($service) . '" install method must return an instance of kujaff\VersionsBundle\Entity\Version.'); |
91
|
|
|
} |
92
|
|
|
if ($output instanceof OutputInterface) { |
93
|
|
|
$output->writeln('<info>' . $installedVersion->asString() . '</info> installed.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($force) { |
97
|
|
|
$bundleVersion = $this->getBundleVersion($bundle); |
98
|
|
|
} |
99
|
|
|
$bundleVersion->setInstalledVersion($installedVersion); |
|
|
|
|
100
|
|
|
$bundleVersion->setInstallationDate(new \DateTime()); |
101
|
|
|
$manager->persist($bundleVersion); |
102
|
|
|
$manager->flush(); |
103
|
|
|
return $installedVersion; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($output instanceof OutputInterface) { |
107
|
|
|
$output->writeln('<info>' . $installedVersion->asString() . '</info> installed.'); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// no install service for this bundle, assume we installed the latest version |
111
|
|
|
if ($force) { |
112
|
|
|
$bundleVersion = $this->getBundleVersion($bundle); |
113
|
|
|
} |
114
|
|
|
$bundleVersion->setInstalledVersion($bundleVersion->getVersion()); |
115
|
|
|
$bundleVersion->setInstallationDate(new \DateTime()); |
116
|
|
|
$manager->persist($bundleVersion); |
117
|
|
|
$manager->flush(); |
118
|
|
|
return $bundleVersion->getInstalledVersion(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Install all bundle in required order |
123
|
|
|
* |
124
|
|
|
* @param OutputInterface $output |
125
|
|
|
*/ |
126
|
|
|
public function installAll($output = null) |
127
|
|
|
{ |
128
|
|
|
foreach ($this->container->getParameter('versions.installOrder') as $bundle => $options) { |
129
|
|
|
try { |
130
|
|
|
$bundleVersion = $this->getBundleVersion($bundle); |
131
|
|
|
} catch (\Exception $e) { |
132
|
|
|
if ($bundle != 'VersionsBundle') { |
133
|
|
|
throw $e; |
134
|
|
|
} |
135
|
|
|
$bundleVersion = new BundleVersion('VersionsBundle'); |
136
|
|
|
} |
137
|
|
|
if ($bundleVersion->isInstalled() === false) { |
138
|
|
|
$this->install($bundle, $options['force'], $output); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
foreach ($this->container->get('versions.bundle')->getBundles() as $bundle) { |
143
|
|
|
$bundleVersion = $this->getBundleVersion($bundle->getName()); |
144
|
|
|
if ($bundleVersion->isInstalled() === false) { |
145
|
|
|
$this->install($bundle->getName(), false, $output); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Update a bundle |
152
|
|
|
* |
153
|
|
|
* @param string $bundle Bundle name |
154
|
|
|
* @param Version $version Version to install, null to latest |
155
|
|
|
* @param OutputInterface $output |
156
|
|
|
* @throws InstallStateException |
157
|
|
|
*/ |
158
|
|
|
public function update($bundle, $version = null, $output = null) |
159
|
|
|
{ |
160
|
|
|
$bundleVersion = $this->getBundleVersion($bundle); |
161
|
|
|
if ($bundleVersion->isInstalled() === false) { |
162
|
|
|
throw new InstallStateException('Bundle "' . $bundle . '" is not installed.'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$version = ($version instanceof Version) ? $version : $bundleVersion->getVersion(); |
166
|
|
|
|
167
|
|
|
// already up to date |
168
|
|
|
if ($this->container->get('versions.version')->compare($bundleVersion->getInstalledVersion(), $version) >= 0) { |
169
|
|
|
return $bundleVersion->getInstalledVersion(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($output instanceof OutputInterface) { |
173
|
|
|
$output->write('[<comment>' . $bundle . '</comment>] Updating from ' . $bundleVersion->getInstalledVersion()->asString() . ' to ' . $version->asString() . ' ... '); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$service = $this->getService($bundle, 'update', 'kujaff\\VersionsBundle\\Model\\Update'); |
177
|
|
|
// an update service has be found |
178
|
|
|
if ($service !== false) { |
179
|
|
|
$installedVersion = $service->update($bundleVersion, $version); |
180
|
|
|
|
181
|
|
|
// no update service, assume we have updated bundle to the latest version |
182
|
|
|
} else { |
183
|
|
|
$installedVersion = $version; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ($output instanceof OutputInterface) { |
187
|
|
|
$output->writeln('<info>' . $installedVersion->asString() . '</info> installed.'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$bundleVersion->setInstalledVersion($installedVersion); |
191
|
|
|
$bundleVersion->setUpdateDate(new \DateTime()); |
192
|
|
|
$this->container->get('doctrine')->getManager()->flush(); |
193
|
|
|
|
194
|
|
|
return $installedVersion; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Update all bundles |
199
|
|
|
* |
200
|
|
|
* @param OutputInterface $output |
201
|
|
|
*/ |
202
|
|
|
public function updateAll($output = null) |
203
|
|
|
{ |
204
|
|
|
foreach ($this->container->getParameter('versions.updateOrder') as $order) { |
205
|
|
|
$this->update($order['bundle'], new Version($order['version']), $output); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
foreach ($this->container->get('versions.bundle')->getBundles() as $bundle) { |
209
|
|
|
$this->update($bundle->getName(), null, $output); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Uninstall |
215
|
|
|
* |
216
|
|
|
* @param string $bundle |
217
|
|
|
* @param boolean $force Force uninstall, although it's not installed |
218
|
|
|
* @throws InstallStateException |
219
|
|
|
*/ |
220
|
|
|
public function uninstall($bundle, $force = false) |
221
|
|
|
{ |
222
|
|
|
$manager = $this->container->get('doctrine')->getManager(); |
223
|
|
|
$bundleVersion = $this->getBundleVersion($bundle); |
224
|
|
|
if ($force === false && $bundleVersion->isInstalled() === false) { |
225
|
|
|
throw new InstallStateException('Bundle "' . $bundle . '" is not installed.'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$service = $this->getService($bundle, 'uninstall', 'kujaff\\VersionsBundle\\Model\\Uninstall'); |
229
|
|
|
if ($service !== false) { |
230
|
|
|
$service->uninstall(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$manager->remove($bundleVersion); |
234
|
|
|
$manager->flush(); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: