|
0
|
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\ORM\Proxy; |
|
|
21 |
|
|
|
22 |
use Doctrine\ORM\EntityManager, |
|
|
23 |
Doctrine\ORM\Mapping\ClassMetadata, |
|
|
24 |
Doctrine\ORM\Mapping\AssociationMapping; |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* This factory is used to create proxy objects for entities at runtime. |
|
|
28 |
* |
|
|
29 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
30 |
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com> |
|
|
31 |
* @since 2.0 |
|
|
32 |
*/ |
|
|
33 |
class ProxyFactory |
|
|
34 |
{ |
|
|
35 |
/** The EntityManager this factory is bound to. */ |
|
|
36 |
private $_em; |
|
|
37 |
/** Whether to automatically (re)generate proxy classes. */ |
|
|
38 |
private $_autoGenerate; |
|
|
39 |
/** The namespace that contains all proxy classes. */ |
|
|
40 |
private $_proxyNamespace; |
|
|
41 |
/** The directory that contains all proxy classes. */ |
|
|
42 |
private $_proxyDir; |
|
|
43 |
|
|
|
44 |
/** |
|
|
45 |
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is |
|
|
46 |
* connected to the given <tt>EntityManager</tt>. |
|
|
47 |
* |
|
|
48 |
* @param EntityManager $em The EntityManager the new factory works for. |
|
|
49 |
* @param string $proxyDir The directory to use for the proxy classes. It must exist. |
|
|
50 |
* @param string $proxyNs The namespace to use for the proxy classes. |
|
|
51 |
* @param boolean $autoGenerate Whether to automatically generate proxy classes. |
|
|
52 |
*/ |
|
|
53 |
public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false) |
|
|
54 |
{ |
|
|
55 |
if ( ! $proxyDir) { |
|
|
56 |
throw ProxyException::proxyDirectoryRequired(); |
|
|
57 |
} |
|
|
58 |
if ( ! $proxyNs) { |
|
|
59 |
throw ProxyException::proxyNamespaceRequired(); |
|
|
60 |
} |
|
|
61 |
$this->_em = $em; |
|
|
62 |
$this->_proxyDir = $proxyDir; |
|
|
63 |
$this->_autoGenerate = $autoGenerate; |
|
|
64 |
$this->_proxyNamespace = $proxyNs; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* Gets a reference proxy instance for the entity of the given type and identified by |
|
|
69 |
* the given identifier. |
|
|
70 |
* |
|
|
71 |
* @param string $className |
|
|
72 |
* @param mixed $identifier |
|
|
73 |
* @return object |
|
|
74 |
*/ |
|
|
75 |
public function getProxy($className, $identifier) |
|
|
76 |
{ |
|
|
77 |
$proxyClassName = str_replace('\\', '', $className) . 'Proxy'; |
|
|
78 |
$fqn = $this->_proxyNamespace . '\\' . $proxyClassName; |
|
|
79 |
|
|
|
80 |
if (! class_exists($fqn, false)) { |
|
|
81 |
$fileName = $this->_proxyDir . DIRECTORY_SEPARATOR . $proxyClassName . '.php'; |
|
|
82 |
if ($this->_autoGenerate) { |
|
|
83 |
$this->_generateProxyClass($this->_em->getClassMetadata($className), $proxyClassName, $fileName, self::$_proxyClassTemplate); |
|
|
84 |
} |
|
|
85 |
require $fileName; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
if ( ! $this->_em->getMetadataFactory()->hasMetadataFor($fqn)) { |
|
|
89 |
$this->_em->getMetadataFactory()->setMetadataFor($fqn, $this->_em->getClassMetadata($className)); |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
$entityPersister = $this->_em->getUnitOfWork()->getEntityPersister($className); |
|
|
93 |
|
|
|
94 |
return new $fqn($entityPersister, $identifier); |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* Generates proxy classes for all given classes. |
|
|
99 |
* |
|
|
100 |
* @param array $classes The classes (ClassMetadata instances) for which to generate proxies. |
|
|
101 |
* @param string $toDir The target directory of the proxy classes. If not specified, the |
|
|
102 |
* directory configured on the Configuration of the EntityManager used |
|
|
103 |
* by this factory is used. |
|
|
104 |
*/ |
|
|
105 |
public function generateProxyClasses(array $classes, $toDir = null) |
|
|
106 |
{ |
|
|
107 |
$proxyDir = $toDir ?: $this->_proxyDir; |
|
|
108 |
$proxyDir = rtrim($proxyDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
|
109 |
foreach ($classes as $class) { |
|
|
110 |
/* @var $class ClassMetadata */ |
|
|
111 |
if ($class->isMappedSuperclass) { |
|
|
112 |
continue; |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
$proxyClassName = str_replace('\\', '', $class->name) . 'Proxy'; |
|
|
116 |
$proxyFileName = $proxyDir . $proxyClassName . '.php'; |
|
|
117 |
$this->_generateProxyClass($class, $proxyClassName, $proxyFileName, self::$_proxyClassTemplate); |
|
|
118 |
} |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
/** |
|
|
122 |
* Generates a proxy class file. |
|
|
123 |
* |
|
|
124 |
* @param $class |
|
|
125 |
* @param $originalClassName |
|
|
126 |
* @param $proxyClassName |
|
|
127 |
* @param $file The path of the file to write to. |
|
|
128 |
*/ |
|
|
129 |
private function _generateProxyClass($class, $proxyClassName, $fileName, $file) |
|
|
130 |
{ |
|
|
131 |
$methods = $this->_generateMethods($class); |
|
|
132 |
$sleepImpl = $this->_generateSleep($class); |
|
|
133 |
$cloneImpl = $class->reflClass->hasMethod('__clone') ? 'parent::__clone();' : ''; // hasMethod() checks case-insensitive |
|
|
134 |
|
|
|
135 |
$placeholders = array( |
|
|
136 |
'<namespace>', |
|
|
137 |
'<proxyClassName>', '<className>', |
|
|
138 |
'<methods>', '<sleepImpl>', '<cloneImpl>' |
|
|
139 |
); |
|
|
140 |
|
|
|
141 |
if(substr($class->name, 0, 1) == "\\") { |
|
|
142 |
$className = substr($class->name, 1); |
|
|
143 |
} else { |
|
|
144 |
$className = $class->name; |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
$replacements = array( |
|
|
148 |
$this->_proxyNamespace, |
|
|
149 |
$proxyClassName, $className, |
|
|
150 |
$methods, $sleepImpl, $cloneImpl |
|
|
151 |
); |
|
|
152 |
|
|
|
153 |
$file = str_replace($placeholders, $replacements, $file); |
|
|
154 |
|
|
|
155 |
file_put_contents($fileName, $file, LOCK_EX); |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
/** |
|
|
159 |
* Generates the methods of a proxy class. |
|
|
160 |
* |
|
|
161 |
* @param ClassMetadata $class |
|
|
162 |
* @return string The code of the generated methods. |
|
|
163 |
*/ |
|
|
164 |
private function _generateMethods(ClassMetadata $class) |
|
|
165 |
{ |
|
|
166 |
$methods = ''; |
|
|
167 |
|
|
|
168 |
foreach ($class->reflClass->getMethods() as $method) { |
|
|
169 |
/* @var $method ReflectionMethod */ |
|
|
170 |
if ($method->isConstructor() || in_array(strtolower($method->getName()), array("__sleep", "__clone"))) { |
|
|
171 |
continue; |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
if ($method->isPublic() && ! $method->isFinal() && ! $method->isStatic()) { |
|
|
175 |
$methods .= "\n" . ' public function '; |
|
|
176 |
if ($method->returnsReference()) { |
|
|
177 |
$methods .= '&'; |
|
|
178 |
} |
|
|
179 |
$methods .= $method->getName() . '('; |
|
|
180 |
$firstParam = true; |
|
|
181 |
$parameterString = $argumentString = ''; |
|
|
182 |
|
|
|
183 |
foreach ($method->getParameters() as $param) { |
|
|
184 |
if ($firstParam) { |
|
|
185 |
$firstParam = false; |
|
|
186 |
} else { |
|
|
187 |
$parameterString .= ', '; |
|
|
188 |
$argumentString .= ', '; |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
// We need to pick the type hint class too |
|
|
192 |
if (($paramClass = $param->getClass()) !== null) { |
|
|
193 |
$parameterString .= '\\' . $paramClass->getName() . ' '; |
|
|
194 |
} else if ($param->isArray()) { |
|
|
195 |
$parameterString .= 'array '; |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
if ($param->isPassedByReference()) { |
|
|
199 |
$parameterString .= '&'; |
|
|
200 |
} |
|
|
201 |
|
|
|
202 |
$parameterString .= '$' . $param->getName(); |
|
|
203 |
$argumentString .= '$' . $param->getName(); |
|
|
204 |
|
|
|
205 |
if ($param->isDefaultValueAvailable()) { |
|
|
206 |
$parameterString .= ' = ' . var_export($param->getDefaultValue(), true); |
|
|
207 |
} |
|
|
208 |
} |
|
|
209 |
|
|
|
210 |
$methods .= $parameterString . ')'; |
|
|
211 |
$methods .= "\n" . ' {' . "\n"; |
|
|
212 |
$methods .= ' $this->__load();' . "\n"; |
|
|
213 |
$methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');'; |
|
|
214 |
$methods .= "\n" . ' }' . "\n"; |
|
|
215 |
} |
|
|
216 |
} |
|
|
217 |
|
|
|
218 |
return $methods; |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
/** |
|
|
222 |
* Generates the code for the __sleep method for a proxy class. |
|
|
223 |
* |
|
|
224 |
* @param $class |
|
|
225 |
* @return string |
|
|
226 |
*/ |
|
|
227 |
private function _generateSleep(ClassMetadata $class) |
|
|
228 |
{ |
|
|
229 |
$sleepImpl = ''; |
|
|
230 |
|
|
|
231 |
if ($class->reflClass->hasMethod('__sleep')) { |
|
|
232 |
$sleepImpl .= "return array_merge(array('__isInitialized__'), parent::__sleep());"; |
|
|
233 |
} else { |
|
|
234 |
$sleepImpl .= "return array('__isInitialized__', "; |
|
|
235 |
$first = true; |
|
|
236 |
|
|
|
237 |
foreach ($class->getReflectionProperties() as $name => $prop) { |
|
|
238 |
if ($first) { |
|
|
239 |
$first = false; |
|
|
240 |
} else { |
|
|
241 |
$sleepImpl .= ', '; |
|
|
242 |
} |
|
|
243 |
|
|
|
244 |
$sleepImpl .= "'" . $name . "'"; |
|
|
245 |
} |
|
|
246 |
|
|
|
247 |
$sleepImpl .= ');'; |
|
|
248 |
} |
|
|
249 |
|
|
|
250 |
return $sleepImpl; |
|
|
251 |
} |
|
|
252 |
|
|
|
253 |
/** Proxy class code template */ |
|
|
254 |
private static $_proxyClassTemplate = |
|
|
255 |
'<?php |
|
|
256 |
|
|
|
257 |
namespace <namespace>; |
|
|
258 |
|
|
|
259 |
/** |
|
|
260 |
* THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE. |
|
|
261 |
*/ |
|
|
262 |
class <proxyClassName> extends \<className> implements \Doctrine\ORM\Proxy\Proxy |
|
|
263 |
{ |
|
|
264 |
private $_entityPersister; |
|
|
265 |
private $_identifier; |
|
|
266 |
public $__isInitialized__ = false; |
|
|
267 |
public function __construct($entityPersister, $identifier) |
|
|
268 |
{ |
|
|
269 |
$this->_entityPersister = $entityPersister; |
|
|
270 |
$this->_identifier = $identifier; |
|
|
271 |
} |
|
|
272 |
/** @private */ |
|
|
273 |
public function __load() |
|
|
274 |
{ |
|
|
275 |
if (!$this->__isInitialized__ && $this->_entityPersister) { |
|
|
276 |
$this->__isInitialized__ = true; |
|
|
277 |
|
|
|
278 |
if (method_exists($this, "__wakeup")) { |
|
|
279 |
// call this after __isInitialized__to avoid infinite recursion |
|
|
280 |
// but before loading to emulate what ClassMetadata::newInstance() |
|
|
281 |
// provides. |
|
|
282 |
$this->__wakeup(); |
|
|
283 |
} |
|
|
284 |
|
|
|
285 |
if ($this->_entityPersister->load($this->_identifier, $this) === null) { |
|
|
286 |
throw new \Doctrine\ORM\EntityNotFoundException(); |
|
|
287 |
} |
|
|
288 |
unset($this->_entityPersister, $this->_identifier); |
|
|
289 |
} |
|
|
290 |
} |
|
|
291 |
|
|
|
292 |
<methods> |
|
|
293 |
|
|
|
294 |
public function __sleep() |
|
|
295 |
{ |
|
|
296 |
<sleepImpl> |
|
|
297 |
} |
|
|
298 |
|
|
|
299 |
public function __clone() |
|
|
300 |
{ |
|
|
301 |
if (!$this->__isInitialized__ && $this->_entityPersister) { |
|
|
302 |
$this->__isInitialized__ = true; |
|
|
303 |
$class = $this->_entityPersister->getClassMetadata(); |
|
|
304 |
$original = $this->_entityPersister->load($this->_identifier); |
|
|
305 |
if ($original === null) { |
|
|
306 |
throw new \Doctrine\ORM\EntityNotFoundException(); |
|
|
307 |
} |
|
|
308 |
foreach ($class->reflFields AS $field => $reflProperty) { |
|
|
309 |
$reflProperty->setValue($this, $reflProperty->getValue($original)); |
|
|
310 |
} |
|
|
311 |
unset($this->_entityPersister, $this->_identifier); |
|
|
312 |
} |
|
|
313 |
<cloneImpl> |
|
|
314 |
} |
|
|
315 |
}'; |
|
|
316 |
} |