It builds proxies from given parameters, a template and class metadata.
Since: 2.4
Author: Marco Pivetta (ocramius@gmail.com)
Example #1
0
 /**
  * Initializes a new instance of the <tt>ProxyFactory</tt> class that is
  * connected to the given <tt>EntityManager</tt>.
  *
  * @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.
  */
 public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false)
 {
     $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNs);
     $proxyGenerator->setPlaceholder('baseProxyInterface', 'Doctrine\\ORM\\Proxy\\Proxy');
     parent::__construct($proxyGenerator, $em->getMetadataFactory(), $autoGenerate);
     $this->em = $em;
     $this->uow = $em->getUnitOfWork();
     $this->proxyNs = $proxyNs;
 }
 /**
  * Initializes a new instance of the <tt>ProxyFactory</tt> class that is
  * connected to the given <tt>DocumentManager</tt>.
  *
  * @param DocumentManager $manager
  * @param string          $proxyDir                              The directory to use for the proxy classes. It
  *                                                               must exist.
  * @param string          $proxyNamespace                        The namespace to use for the proxy classes.
  * @param int             $autoGenerate                          Whether to automatically generate proxy classes.
  */
 public function __construct(DocumentManager $manager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER)
 {
     $this->metadataFactory = $manager->getMetadataFactory();
     $this->uow = $manager->getUnitOfWork();
     $this->proxyNamespace = $proxyNamespace;
     $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace);
     $proxyGenerator->setPlaceholder('baseProxyInterface', 'Doctrine\\ODM\\OrientDB\\Proxy\\Proxy');
     parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate);
 }
Example #3
0
 /**
  * 
  * @param EntityManagerInterface $em
  * @param string $proxyDirectory
  * @param string $proxyNamespace
  * @param bool $autoGenerate
  */
 public function __construct(EntityManagerInterface $em, $proxyDirectory, $proxyNamespace, $autoGenerate = true)
 {
     $proxyGenerator = new ProxyGenerator($proxyDirectory, $proxyNamespace);
     $proxyGenerator->setPlaceholder('baseProxyInterface', 'Pasinter\\OHM\\Proxy\\ProxyInterface');
     parent::__construct($proxyGenerator, $em->getMetadataFactory(), $autoGenerate);
     $this->em = $em;
     $this->proxyDirectory = $proxyDirectory;
     $this->proxyNamespace = $proxyNamespace;
 }
Example #4
0
 /**
  * Initializes a new instance of the <tt>ProxyFactory</tt> class that is
  * connected to the given <tt>DocumentManager</tt>.
  *
  * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager The DocumentManager the new factory works for.
  * @param string                                $proxyDir        The directory to use for the proxy classes. It
  *                                                               must exist.
  * @param string                                $proxyNamespace  The namespace to use for the proxy classes.
  * @param integer                               $autoGenerate    Whether to automatically generate proxy classes.
  */
 public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER)
 {
     $this->metadataFactory = $documentManager->getMetadataFactory();
     $this->uow = $documentManager->getUnitOfWork();
     $this->proxyNamespace = $proxyNamespace;
     $this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager());
     $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace);
     $proxyGenerator->setPlaceholder('baseProxyInterface', Proxy::class);
     parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate);
 }
Example #5
0
 /**
  * Initializes a new instance of the <tt>ProxyFactory</tt> class that is
  * connected to the given <tt>EntityManager</tt>.
  *
  * @param EntityManagerInterface $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|int            $autoGenerate The strategy for automatically generating proxy classes. Possible
  *                                             values are constants of Doctrine\Common\Proxy\AbstractProxyFactory.
  */
 public function __construct(EntityManagerInterface $em, $proxyDir, $proxyNs, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER)
 {
     $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNs);
     $proxyGenerator->setPlaceholder('baseProxyInterface', 'Doctrine\\ORM\\Proxy\\Proxy');
     parent::__construct($proxyGenerator, $em->getMetadataFactory(), $autoGenerate);
     $this->em = $em;
     $this->uow = $em->getUnitOfWork();
     $this->proxyNs = $proxyNs;
     $this->identifierFlattener = new IdentifierFlattener($this->uow, $em->getMetadataFactory());
 }
 /**
  * @param $className
  *
  * @return string
  */
 private function generateProxyClass($className)
 {
     $proxyClassName = 'Doctrine\\Tests\\Common\\Proxy\\MagicMethodProxy\\__CG__\\' . $className;
     if (class_exists($proxyClassName, false)) {
         return $proxyClassName;
     }
     $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
     $metadata->expects($this->any())->method('getName')->will($this->returnValue($className));
     $metadata->expects($this->any())->method('getIdentifier')->will($this->returnValue(array('id')));
     $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue(new ReflectionClass($className)));
     $metadata->expects($this->any())->method('isIdentifier')->will($this->returnCallback(function ($fieldName) {
         return 'id' === $fieldName;
     }));
     $metadata->expects($this->any())->method('hasField')->will($this->returnCallback(function ($fieldName) {
         return in_array($fieldName, array('id', 'publicField'));
     }));
     $metadata->expects($this->any())->method('hasAssociation')->will($this->returnValue(false));
     $metadata->expects($this->any())->method('getFieldNames')->will($this->returnValue(array('id', 'publicField')));
     $metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue(array('id')));
     $metadata->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
     $metadata->expects($this->any())->method('getTypeOfField')->will($this->returnValue('string'));
     $this->proxyGenerator->generateProxyClass($metadata, $this->proxyGenerator->getProxyFileName($className));
     require_once $this->proxyGenerator->getProxyFileName($className);
     return $proxyClassName;
 }
Example #7
0
 /**
  * {@inheritDoc}
  */
 public function setUp()
 {
     $this->proxyLoader = $loader = $this->getMock('stdClass', array('load'), array(), '', false);
     $this->initializerCallbackMock = $this->getMock('stdClass', array('__invoke'));
     $identifier = $this->identifier;
     $this->lazyLoadableObjectMetadata = $metadata = new LazyLoadableObjectClassMetadata();
     // emulating what should happen in a proxy factory
     $cloner = function (LazyLoadableObject $proxy) use($loader, $identifier, $metadata) {
         /* @var $proxy LazyLoadableObject|Proxy */
         if ($proxy->__isInitialized()) {
             return;
         }
         $proxy->__setInitialized(true);
         $proxy->__setInitializer(null);
         $original = $loader->load($identifier);
         if (null === $original) {
             throw new UnexpectedValueException();
         }
         foreach ($metadata->getReflectionClass()->getProperties() as $reflProperty) {
             $propertyName = $reflProperty->getName();
             if ($metadata->hasField($propertyName) || $metadata->hasAssociation($propertyName)) {
                 $reflProperty->setAccessible(true);
                 $reflProperty->setValue($proxy, $reflProperty->getValue($original));
             }
         }
     };
     $proxyClassName = 'Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\LazyLoadableObject';
     // creating the proxy class
     if (!class_exists($proxyClassName, false)) {
         $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
         $proxyGenerator->generateProxyClass($metadata);
         require_once $proxyGenerator->getProxyFileName($metadata->getName());
     }
     $this->lazyObject = new $proxyClassName($this->getClosure($this->initializerCallbackMock), $cloner);
     // setting identifiers in the proxy via reflection
     foreach ($metadata->getIdentifierFieldNames() as $idField) {
         $prop = $metadata->getReflectionClass()->getProperty($idField);
         $prop->setAccessible(true);
         $prop->setValue($this->lazyObject, $identifier[$idField]);
     }
     $this->assertFalse($this->lazyObject->__isInitialized());
 }
 public function testDoctrineProxy()
 {
     $className = 'Saxulum\\Tests\\Accessor\\Fixtures\\Entity\\Entity';
     $proxyDirectory = __DIR__ . '/../proxy/';
     $proxyNamespace = 'Proxy';
     $proxyClassName = $proxyNamespace . '\\__CG__\\' . $className;
     $proxyClassFilename = $proxyDirectory . str_replace('\\', '_', $proxyClassName) . '.php';
     if (!is_dir($proxyDirectory)) {
         mkdir($proxyDirectory, 0777, true);
     }
     $reflectionService = new RuntimeReflectionService();
     $classMetadata = new ClassMetadata(get_class(new Entity()));
     $classMetadata->initializeReflection($reflectionService);
     $proxyGenerator = new ProxyGenerator($proxyDirectory, $proxyNamespace);
     $proxyGenerator->generateProxyClass($classMetadata, $proxyClassFilename);
     require $proxyClassFilename;
     /** @var Entity $proxy */
     $proxy = new $proxyClassName();
     $proxy->setName('test');
     $this->assertEquals('test', $proxy->getName());
     unlink($proxyClassFilename);
     //rmdir($proxyDirectory);
 }
 public function testInvalidPlaceholderThrowsException()
 {
     $this->setExpectedException('Doctrine\\Common\\Proxy\\Exception\\InvalidArgumentException');
     $generator = new ProxyGenerator(__DIR__ . '/generated', 'SomeNamespace');
     $generator->setPlaceholder('<somePlaceholder>', array());
 }
 public function testCallingVariadicMethodCausesLazyLoading()
 {
     if (PHP_VERSION_ID < 50600) {
         $this->markTestSkipped('Test applies only to PHP 5.6+');
     }
     $proxyClassName = 'Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\VariadicTypeHintClass';
     /* @var $metadata \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject */
     $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
     $metadata->expects($this->any())->method('getName')->will($this->returnValue('Doctrine\\Tests\\Common\\Proxy\\VariadicTypeHintClass'));
     $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue(new \ReflectionClass('Doctrine\\Tests\\Common\\Proxy\\VariadicTypeHintClass')));
     // creating the proxy class
     if (!class_exists($proxyClassName, false)) {
         $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
         $proxyGenerator->generateProxyClass($metadata);
         require_once $proxyGenerator->getProxyFileName($metadata->getName());
     }
     /* @var $invocationMock callable|\PHPUnit_Framework_MockObject_MockObject */
     $invocationMock = $this->getMock('stdClass', array('__invoke'));
     /* @var $lazyObject \Doctrine\Tests\Common\Proxy\VariadicTypeHintClass */
     $lazyObject = new $proxyClassName(function ($proxy, $method, $parameters) use($invocationMock) {
         $invocationMock($proxy, $method, $parameters);
     }, function () {
     });
     $invocationMock->expects($this->at(0))->method('__invoke')->with($lazyObject, 'addType', array(array('type1', 'type2')));
     $invocationMock->expects($this->at(1))->method('__invoke')->with($lazyObject, 'addTypeWithMultipleParameters', array('foo', 'bar', array('baz1', 'baz2')));
     $lazyObject->addType('type1', 'type2');
     $this->assertSame(array('type1', 'type2'), $lazyObject->types);
     $lazyObject->addTypeWithMultipleParameters('foo', 'bar', 'baz1', 'baz2');
     $this->assertSame('foo', $lazyObject->foo);
     $this->assertSame('bar', $lazyObject->bar);
     $this->assertSame(array('baz1', 'baz2'), $lazyObject->baz);
 }
 public function testUseEvalIfNoFilenameIsGiven()
 {
     $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
     $className = __NAMESPACE__ . '\\EvalBase';
     $metadata = $this->createClassMetadata($className, array('id'));
     $proxyGenerator->generateProxyClass($metadata);
     $reflClass = new ReflectionClass('Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\EvalBase');
     $this->assertContains("eval()'d code", $reflClass->getFileName());
 }