|
58
|
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 LGPL. For more information, see |
|
|
17 |
* <http://www.doctrine-project.org>. |
|
|
18 |
*/ |
|
|
19 |
|
|
|
20 |
namespace Doctrine\Common\DataFixtures; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* ReferenceRepository class manages references for |
|
|
24 |
* fixtures in order to easily support the relations |
|
|
25 |
* between fixtures |
|
|
26 |
* |
|
|
27 |
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com> |
|
|
28 |
*/ |
|
|
29 |
class ReferenceRepository |
|
|
30 |
{ |
|
|
31 |
/** |
|
|
32 |
* List of named references to the fixture objects |
|
|
33 |
* gathered during loads of fixtures |
|
|
34 |
* |
|
|
35 |
* @var array |
|
|
36 |
*/ |
|
|
37 |
private $references = array(); |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* List of identifiers stored for references |
|
|
41 |
* in case if reference gets unmanaged, it will |
|
|
42 |
* use a proxy referenced by this identity |
|
|
43 |
* |
|
|
44 |
* @var array |
|
|
45 |
*/ |
|
|
46 |
private $identities = array(); |
|
|
47 |
|
|
|
48 |
/** |
|
|
49 |
* Currently used object manager |
|
|
50 |
* |
|
|
51 |
* @var object - object manager |
|
|
52 |
*/ |
|
|
53 |
private $manager; |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Initialize the ReferenceRepository |
|
|
57 |
* |
|
|
58 |
* @param object $manager |
|
|
59 |
*/ |
|
|
60 |
public function __construct($manager) |
|
|
61 |
{ |
|
|
62 |
$this->manager = $manager; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
/** |
|
|
66 |
* Set the reference entry identified by $name |
|
|
67 |
* and referenced to $reference. If $name |
|
|
68 |
* already is set, it overrides it |
|
|
69 |
* |
|
|
70 |
* @param string $name |
|
|
71 |
* @param object $reference |
|
|
72 |
*/ |
|
|
73 |
public function setReference($name, $reference) |
|
|
74 |
{ |
|
|
75 |
$this->references[$name] = $reference; |
|
|
76 |
// in case if reference is set after flush, store its identity |
|
|
77 |
$uow = $this->manager->getUnitOfWork(); |
|
|
78 |
if ($uow->isInIdentityMap($reference)) { |
|
|
79 |
if ($uow instanceof \Doctrine\ORM\UnitOfWork) { |
|
|
80 |
$this->identities[$name] = $uow->getEntityIdentifier($reference); |
|
|
81 |
} else { |
|
|
82 |
$this->identities[$name] = $uow->getDocumentIdentifier($reference); |
|
|
83 |
} |
|
|
84 |
} |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
/** |
|
|
88 |
* Store the identifier of a reference |
|
|
89 |
* |
|
|
90 |
* @param string $name |
|
|
91 |
* @param mixed $identity |
|
|
92 |
*/ |
|
|
93 |
public function setReferenceIdentity($name, $identity) |
|
|
94 |
{ |
|
|
95 |
$this->identities[$name] = $identity; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
/** |
|
|
99 |
* Set the reference entry identified by $name |
|
|
100 |
* and referenced to managed $object. $name must |
|
|
101 |
* not be set yet |
|
|
102 |
* |
|
|
103 |
* Notice: in case if identifier is generated after |
|
|
104 |
* the record is inserted, be sure tu use this method |
|
|
105 |
* after $object is flushed |
|
|
106 |
* |
|
|
107 |
* @param string $name |
|
|
108 |
* @param object $object - managed object |
|
|
109 |
* @throws BadMethodCallException - if repository already has |
|
|
110 |
* a reference by $name |
|
|
111 |
* @return void |
|
|
112 |
*/ |
|
|
113 |
public function addReference($name, $object) |
|
|
114 |
{ |
|
|
115 |
if (isset($this->references[$name])) { |
|
|
116 |
throw new \BadMethodCallException("Reference to: ({$name}) already exists, use method setReference in order to override it"); |
|
|
117 |
} |
|
|
118 |
$this->setReference($name, $object); |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
/** |
|
|
122 |
* Loads an object using stored reference |
|
|
123 |
* named by $name |
|
|
124 |
* |
|
|
125 |
* @param string $name |
|
|
126 |
* @return object |
|
|
127 |
*/ |
|
|
128 |
public function getReference($name) |
|
|
129 |
{ |
|
|
130 |
$reference = $this->references[$name]; |
|
|
131 |
$meta = $this->manager->getClassMetadata(get_class($reference)); |
|
|
132 |
$uow = $this->manager->getUnitOfWork(); |
|
|
133 |
if (!$uow->isInIdentityMap($reference) && isset($this->identities[$name])) { |
|
|
134 |
$reference = $this->manager->getReference( |
|
|
135 |
$meta->name, |
|
|
136 |
$this->identities[$name] |
|
|
137 |
); |
|
|
138 |
$this->references[$name] = $reference; // allready in identity map |
|
|
139 |
} |
|
|
140 |
return $reference; |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
/** |
|
|
144 |
* Check if an object is stored using reference |
|
|
145 |
* named by $name |
|
|
146 |
* |
|
|
147 |
* @param string $name |
|
|
148 |
* @return boolean |
|
|
149 |
*/ |
|
|
150 |
public function hasReference($name) |
|
|
151 |
{ |
|
|
152 |
return isset($this->references[$name]); |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
/** |
|
|
156 |
* Searches for a reference name in the |
|
|
157 |
* list of stored references |
|
|
158 |
* |
|
|
159 |
* @param object $reference |
|
|
160 |
* @return string |
|
|
161 |
*/ |
|
|
162 |
public function getReferenceName($reference) |
|
|
163 |
{ |
|
|
164 |
return array_search($reference, $this->references, true); |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
/** |
|
|
168 |
* Checks if reference has identity stored |
|
|
169 |
* |
|
|
170 |
* @param string $name |
|
|
171 |
*/ |
|
|
172 |
public function hasIdentity($name) |
|
|
173 |
{ |
|
|
174 |
return array_key_exists($name, $this->identities); |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
/** |
|
|
178 |
* Get all stored references |
|
|
179 |
* |
|
|
180 |
* @return array |
|
|
181 |
*/ |
|
|
182 |
public function getReferences() |
|
|
183 |
{ |
|
|
184 |
return $this->references; |
|
|
185 |
} |
|
|
186 |
} |