|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kujaff\VersionsBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use kujaff\VersionsBundle\Entity\BundleVersion; |
|
6
|
|
|
use kujaff\VersionsBundle\Entity\Version; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Search for methods update_X_Y_Z() and call it |
|
10
|
|
|
*/ |
|
11
|
|
|
trait UpdateOneVersionOneMethod |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Find update methods (syntax : update_X_Y_Z()) |
|
16
|
|
|
* |
|
17
|
|
|
* @param UpdateInterface $object |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function findUpdateMethods(UpdateInterface $object) |
|
21
|
|
|
{ |
|
22
|
|
|
$return = array(); |
|
23
|
|
|
foreach (get_class_methods(get_class($object)) as $method) { |
|
24
|
|
|
if (substr($method, 0, 7) == 'update_') { |
|
25
|
|
|
$version = substr($method, 7); |
|
26
|
|
|
if (preg_match('/[0-9]{1,}[_]{1}[0-9]{1,}[_]{1}[0-9]{1,}/', $version)) { |
|
27
|
|
|
$return[] = str_replace('_', '.', $version); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
sort($return); |
|
32
|
|
|
return $return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Update bundle |
|
37
|
|
|
* |
|
38
|
|
|
* @param UpdateInterface $updater Use $this in your class |
|
39
|
|
|
* @param BundleVersion $bundleVersion |
|
40
|
|
|
* @param Version $version Update to this version |
|
41
|
|
|
* @return Version |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function updateOneVersionOneMethod(UpdateInterface $updater, BundleVersion $bundleVersion, Version $version) |
|
44
|
|
|
{ |
|
45
|
|
|
$methods = $this->findUpdateMethods($updater); |
|
46
|
|
|
$service = $this->container->get('versions.version'); |
|
|
|
|
|
|
47
|
|
|
foreach ($methods as $method) { |
|
48
|
|
|
if ($service->compare($method, $version) == 1) { |
|
49
|
|
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
if ($service->compare($method, $bundleVersion->getInstalledVersion()) == 1 && $service->compare($method, $bundleVersion->getVersion()) <= 0) { |
|
52
|
|
|
$this->{'update_' . str_replace('.', '_', $method)}(); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
return $version; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Update bundle |
|
60
|
|
|
* |
|
61
|
|
|
* @param BundleVersion $bundleVersion |
|
62
|
|
|
* @param Version $version Update to this version |
|
63
|
|
|
* @return Version |
|
64
|
|
|
*/ |
|
65
|
|
|
public function update(BundleVersion $bundleVersion, Version $version) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->updateOneVersionOneMethod($this, $bundleVersion, $version); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: