2009-07-16 17:29:15 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-07-16 17:29:15 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
2009-07-16 18:03:22 +04:00
|
|
|
namespace Doctrine\ORM\Proxy;
|
2009-07-16 17:29:15 +04:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory;
|
|
|
|
use Doctrine\Common\Proxy\ProxyDefinition;
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\Common\Util\ClassUtils;
|
2013-03-01 21:58:51 +04:00
|
|
|
use Doctrine\Common\Proxy\Proxy as BaseProxy;
|
2012-10-20 03:27:53 +04:00
|
|
|
use Doctrine\Common\Proxy\ProxyGenerator;
|
2013-01-06 17:56:11 +04:00
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException;
|
2013-01-06 18:11:30 +04:00
|
|
|
use Doctrine\ORM\Persisters\BasicEntityPersister;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityNotFoundException;
|
2009-07-16 17:29:15 +04:00
|
|
|
|
|
|
|
/**
|
2009-10-15 00:18:36 +04:00
|
|
|
* This factory is used to create proxy objects for entities at runtime.
|
2009-07-16 17:29:15 +04:00
|
|
|
*
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-07-16 18:03:22 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2013-01-06 18:11:30 +04:00
|
|
|
* @author Marco Pivetta <ocramius@gmail.com>
|
2009-07-16 17:29:15 +04:00
|
|
|
* @since 2.0
|
|
|
|
*/
|
2013-01-06 18:11:30 +04:00
|
|
|
class ProxyFactory extends AbstractProxyFactory
|
2009-07-16 17:29:15 +04:00
|
|
|
{
|
2012-12-13 14:28:55 +04:00
|
|
|
/**
|
2013-01-06 18:11:30 +04:00
|
|
|
* @var \Doctrine\ORM\EntityManager The EntityManager this factory is bound to.
|
2012-12-13 14:28:55 +04:00
|
|
|
*/
|
2012-10-20 03:27:53 +04:00
|
|
|
private $em;
|
2012-12-13 14:28:55 +04:00
|
|
|
|
|
|
|
/**
|
2012-10-20 03:27:53 +04:00
|
|
|
* @var \Doctrine\ORM\UnitOfWork The UnitOfWork this factory uses to retrieve persisters
|
2012-12-13 14:28:55 +04:00
|
|
|
*/
|
2012-10-20 03:27:53 +04:00
|
|
|
private $uow;
|
2012-12-13 14:28:55 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-10-20 03:27:53 +04:00
|
|
|
private $proxyNs;
|
2009-07-16 17:29:15 +04:00
|
|
|
|
|
|
|
/**
|
2010-04-26 15:02:30 +04:00
|
|
|
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
|
|
|
|
* connected to the given <tt>EntityManager</tt>.
|
|
|
|
*
|
2013-01-06 18:11:30 +04:00
|
|
|
* @param \Doctrine\ORM\EntityManager $em The EntityManager the new factory works for.
|
|
|
|
* @param string $proxyDir The directory to use for the proxy classes. It must exist.
|
|
|
|
* @param string $proxyNs The namespace to use for the proxy classes.
|
|
|
|
* @param boolean $autoGenerate Whether to automatically generate proxy classes.
|
2009-07-16 17:29:15 +04:00
|
|
|
*/
|
2009-10-15 00:18:36 +04:00
|
|
|
public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false)
|
2009-07-16 17:29:15 +04:00
|
|
|
{
|
2013-01-06 18:11:30 +04:00
|
|
|
$proxyGenerator = new ProxyGenerator($proxyDir, $proxyNs);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
$proxyGenerator->setPlaceholder('baseProxyInterface', 'Doctrine\ORM\Proxy\Proxy');
|
|
|
|
parent::__construct($proxyGenerator, $em->getMetadataFactory(), $autoGenerate);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
$this->em = $em;
|
|
|
|
$this->uow = $em->getUnitOfWork();
|
|
|
|
$this->proxyNs = $proxyNs;
|
2012-04-07 12:30:09 +04:00
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
/**
|
2013-01-06 18:11:30 +04:00
|
|
|
* {@inheritDoc}
|
2009-10-15 00:18:36 +04:00
|
|
|
*/
|
2013-01-06 18:11:30 +04:00
|
|
|
protected function skipClass(ClassMetadata $metadata)
|
2009-10-15 00:18:36 +04:00
|
|
|
{
|
2013-01-06 18:11:30 +04:00
|
|
|
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */
|
|
|
|
return $metadata->isMappedSuperclass || $metadata->getReflectionClass()->isAbstract();
|
2012-10-20 03:27:53 +04:00
|
|
|
}
|
2011-10-25 06:45:23 +04:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
/**
|
2013-01-06 18:11:30 +04:00
|
|
|
* {@inheritDoc}
|
2012-10-20 03:27:53 +04:00
|
|
|
*/
|
2013-01-06 18:11:30 +04:00
|
|
|
protected function createProxyDefinition($className)
|
2012-10-20 03:27:53 +04:00
|
|
|
{
|
2013-01-06 18:11:30 +04:00
|
|
|
$classMetadata = $this->em->getClassMetadata($className);
|
|
|
|
$entityPersister = $this->uow->getEntityPersister($className);
|
2011-10-20 20:35:41 +04:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
return new ProxyDefinition(
|
|
|
|
ClassUtils::generateProxyClassName($className, $this->proxyNs),
|
|
|
|
$classMetadata->getIdentifierFieldNames(),
|
|
|
|
$classMetadata->getReflectionProperties(),
|
|
|
|
$this->createInitializer($classMetadata, $entityPersister),
|
|
|
|
$this->createCloner($classMetadata, $entityPersister)
|
|
|
|
);
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
/**
|
2013-01-06 18:11:30 +04:00
|
|
|
* Creates a closure capable of initializing a proxy
|
|
|
|
*
|
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata
|
|
|
|
* @param \Doctrine\ORM\Persisters\BasicEntityPersister $entityPersister
|
|
|
|
*
|
|
|
|
* @return \Closure
|
|
|
|
*
|
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException
|
2009-10-15 00:18:36 +04:00
|
|
|
*/
|
2013-01-06 18:11:30 +04:00
|
|
|
private function createInitializer(ClassMetadata $classMetadata, BasicEntityPersister $entityPersister)
|
2009-10-15 00:18:36 +04:00
|
|
|
{
|
2012-10-20 03:27:53 +04:00
|
|
|
if ($classMetadata->getReflectionClass()->hasMethod('__wakeup')) {
|
2013-03-01 21:58:51 +04:00
|
|
|
return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) {
|
2012-10-20 03:27:53 +04:00
|
|
|
$proxy->__setInitializer(null);
|
|
|
|
$proxy->__setCloner(null);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
if ($proxy->__isInitialized()) {
|
|
|
|
return;
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
$properties = $proxy->__getLazyProperties();
|
2011-11-08 12:43:06 +04:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
foreach ($properties as $propertyName => $property) {
|
|
|
|
if (!isset($proxy->$propertyName)) {
|
|
|
|
$proxy->$propertyName = $properties[$propertyName];
|
|
|
|
}
|
2011-05-20 22:50:03 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
$proxy->__setInitialized(true);
|
|
|
|
$proxy->__wakeup();
|
2012-01-16 13:57:29 +04:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
if (null === $entityPersister->load($classMetadata->getIdentifierValues($proxy), $proxy)) {
|
|
|
|
throw new EntityNotFoundException();
|
|
|
|
}
|
|
|
|
};
|
2013-01-06 18:11:30 +04:00
|
|
|
}
|
2012-01-16 13:57:29 +04:00
|
|
|
|
2013-03-01 21:58:51 +04:00
|
|
|
return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) {
|
2013-01-06 18:11:30 +04:00
|
|
|
$proxy->__setInitializer(null);
|
|
|
|
$proxy->__setCloner(null);
|
2012-01-16 13:57:29 +04:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
if ($proxy->__isInitialized()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-05-20 22:50:03 +04:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
$properties = $proxy->__getLazyProperties();
|
|
|
|
|
|
|
|
foreach ($properties as $propertyName => $property) {
|
|
|
|
if (!isset($proxy->$propertyName)) {
|
|
|
|
$proxy->$propertyName = $properties[$propertyName];
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
2013-01-06 18:11:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
$proxy->__setInitialized(true);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
if (null === $entityPersister->load($classMetadata->getIdentifierValues($proxy), $proxy)) {
|
|
|
|
throw new EntityNotFoundException();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-01-06 18:11:30 +04:00
|
|
|
/**
|
|
|
|
* Creates a closure capable of finalizing state a cloned proxy
|
|
|
|
*
|
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata
|
|
|
|
* @param \Doctrine\ORM\Persisters\BasicEntityPersister $entityPersister
|
|
|
|
*
|
|
|
|
* @return \Closure
|
|
|
|
*
|
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException
|
|
|
|
*/
|
|
|
|
private function createCloner(ClassMetadata $classMetadata, BasicEntityPersister $entityPersister)
|
|
|
|
{
|
2013-03-01 21:58:51 +04:00
|
|
|
return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) {
|
2012-10-20 03:27:53 +04:00
|
|
|
if ($proxy->__isInitialized()) {
|
|
|
|
return;
|
2011-07-09 14:12:44 +04:00
|
|
|
}
|
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
$proxy->__setInitialized(true);
|
|
|
|
$proxy->__setInitializer(null);
|
|
|
|
$class = $entityPersister->getClassMetadata();
|
|
|
|
$original = $entityPersister->load($classMetadata->getIdentifierValues($proxy));
|
2011-08-19 07:11:58 +04:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
if (null === $original) {
|
|
|
|
throw new EntityNotFoundException();
|
|
|
|
}
|
2012-01-16 13:57:29 +04:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
foreach ($class->getReflectionClass()->getProperties() as $reflectionProperty) {
|
|
|
|
$propertyName = $reflectionProperty->getName();
|
2009-10-15 00:18:36 +04:00
|
|
|
|
2012-10-20 03:27:53 +04:00
|
|
|
if ($class->hasField($propertyName) || $class->hasAssociation($propertyName)) {
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original));
|
|
|
|
}
|
2011-02-26 14:47:59 +03:00
|
|
|
}
|
2012-10-20 03:27:53 +04:00
|
|
|
};
|
2011-02-26 14:47:59 +03:00
|
|
|
}
|
2009-07-16 17:29:15 +04:00
|
|
|
}
|