|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\ContextProviderInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Handle 'any' references by letting the developer store them and retrieve them afterwards |
|
11
|
|
|
*/ |
|
12
|
|
|
class CustomReferenceResolver extends AbstractResolver implements ReferenceResolverBagInterface, |
|
13
|
|
|
EnumerableReferenceResolverInterface, ContextProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Defines the prefix for all reference identifier strings in definitions |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $referencePrefixes = array('reference:'); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Array of all references set by the currently running migrations. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $references = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get a stored reference |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $identifier format: reference:<some_custom_identifier> |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
* @throws \Exception When trying to retrieve an unset reference |
|
33
|
|
|
*/ |
|
34
|
23 |
|
public function getReferenceValue($identifier) |
|
35
|
|
|
{ |
|
36
|
23 |
|
$identifier = $this->getReferenceIdentifier($identifier); |
|
37
|
23 |
|
if (!array_key_exists($identifier, $this->references)) { |
|
38
|
1 |
|
throw new \Exception("No reference set with identifier '$identifier'"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
22 |
|
return $this->references[$identifier]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add a reference to be retrieved later. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $identifier The identifier of the reference |
|
48
|
|
|
* @param mixed $value The value of the reference |
|
49
|
|
|
* @param bool $overwrite do overwrite the existing ref if it exist without raising an exception |
|
50
|
|
|
* @return bool true if the reference is accepted by this resolver, otherwise false |
|
51
|
|
|
* @throws \Exception When there is a reference with the specified $identifier already. |
|
52
|
|
|
*/ |
|
53
|
27 |
|
public function addReference($identifier, $value, $overwrite = false) |
|
54
|
|
|
{ |
|
55
|
27 |
|
if (array_key_exists($identifier, $this->references) && !$overwrite) { |
|
56
|
1 |
|
throw new \Exception("A reference with identifier '$identifier' already exists"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
27 |
|
$this->references[$identifier] = $value; |
|
60
|
|
|
|
|
61
|
27 |
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* List all existing references |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function listReferences() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->references; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The custom reference resolver has only 'global' references, regardless of the current migration |
|
75
|
|
|
* @param string $migrationName |
|
76
|
|
|
* @return array|null |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getCurrentContext($migrationName) |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->references; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The custom reference resolver has only 'global' references, regardless of the current migration |
|
85
|
|
|
* @param string $migrationName |
|
86
|
|
|
* @param array $context |
|
87
|
|
|
*/ |
|
88
|
|
|
public function restoreContext($migrationName, array $context) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->references = $context; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|