|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Baleen\Migrations\Delta\Collection\Resolver; |
|
21
|
|
|
|
|
22
|
|
|
use Baleen\Migrations\Exception\Version\Collection\ResolverException; |
|
23
|
|
|
use Baleen\Migrations\Delta\Collection\Collection; |
|
24
|
|
|
use Baleen\Migrations\Delta\DeltaInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class AbstractResolver |
|
28
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class AbstractResolver implements ResolverInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
private $cache = []; |
|
34
|
|
|
|
|
35
|
|
|
/** @var bool */ |
|
36
|
|
|
private $cacheEnabled = true; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param bool $cacheEnabled |
|
40
|
|
|
*/ |
|
41
|
154 |
|
public function __construct($cacheEnabled = true) |
|
42
|
|
|
{ |
|
43
|
154 |
|
$this->cacheEnabled = (bool) $cacheEnabled; |
|
44
|
154 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Resolves an alias into a Delta. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $alias |
|
50
|
|
|
* @param Collection $collection |
|
51
|
|
|
* |
|
52
|
|
|
* @return DeltaInterface|null |
|
|
|
|
|
|
53
|
|
|
* |
|
54
|
|
|
* @throws ResolverException |
|
55
|
|
|
*/ |
|
56
|
105 |
|
final public function resolve($alias, Collection $collection) |
|
57
|
|
|
{ |
|
58
|
105 |
|
$alias = (string) $alias; |
|
59
|
|
|
|
|
60
|
105 |
|
$result = $this->cacheGet($alias, $collection); |
|
61
|
|
|
|
|
62
|
105 |
|
if (false === $result) { |
|
63
|
105 |
|
$result = $this->doResolve($alias, $collection); |
|
64
|
105 |
|
if (null !== $result && !$result instanceof DeltaInterface) { |
|
65
|
4 |
|
throw new ResolverException('Expected result to be either a DeltaInterface object or null.'); |
|
66
|
|
|
} |
|
67
|
101 |
|
$this->cacheSet($alias, $collection, $result); |
|
68
|
101 |
|
} |
|
69
|
|
|
|
|
70
|
101 |
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Gets an alias from the cache. Returns false if nothing could be found, a Delta if the alias was previously |
|
75
|
|
|
* resolved to a version, and null if the alias couldn't be resolved in a previous call. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $alias |
|
78
|
|
|
* @param Collection $collection |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool|null|DeltaInterface |
|
81
|
|
|
*/ |
|
82
|
105 |
|
private function cacheGet($alias, Collection $collection) |
|
83
|
|
|
{ |
|
84
|
105 |
|
$result = false; |
|
85
|
|
|
|
|
86
|
105 |
|
if ($this->cacheEnabled) { |
|
87
|
105 |
|
$hash = spl_object_hash($collection); |
|
88
|
105 |
|
if (isset($this->cache[$hash]) && array_key_exists($alias, $this->cache[$hash])) { |
|
89
|
5 |
|
$result = $this->cache[$hash][$alias]; |
|
90
|
5 |
|
} |
|
91
|
105 |
|
} |
|
92
|
|
|
|
|
93
|
105 |
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Saves the result of resolving an alias against a given collection into the cache. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $alias |
|
100
|
|
|
* @param \Baleen\Migrations\Delta\Collection\Collection $collection |
|
101
|
|
|
* @param null|DeltaInterface $result |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
101 |
|
private function cacheSet($alias, $collection, $result) |
|
106
|
|
|
{ |
|
107
|
101 |
|
if (!$this->cacheEnabled) { |
|
108
|
32 |
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
101 |
|
$hash = spl_object_hash($collection); |
|
112
|
101 |
|
if (!isset($this->cache[$hash])) { |
|
113
|
101 |
|
$this->cache[$hash] = []; // initialize the collection's cache |
|
114
|
101 |
|
} |
|
115
|
101 |
|
$this->cache[$hash][$alias] = $result; |
|
116
|
101 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritdoc |
|
120
|
|
|
*/ |
|
121
|
146 |
|
final public function clearCache(Collection $collection = null) |
|
122
|
|
|
{ |
|
123
|
146 |
|
if (null !== $collection) { |
|
124
|
145 |
|
$hash = spl_object_hash($collection); |
|
125
|
145 |
|
unset($this->cache[$hash]); |
|
126
|
145 |
|
} else { |
|
127
|
1 |
|
$this->cache = []; |
|
128
|
|
|
} |
|
129
|
146 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* doResolve |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $alias |
|
135
|
|
|
* @param Collection $collection |
|
136
|
|
|
* |
|
137
|
|
|
* @return DeltaInterface|null |
|
138
|
|
|
*/ |
|
139
|
|
|
abstract protected function doResolve($alias, Collection $collection); |
|
140
|
|
|
} |
|
141
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.