1
0
mirror of synced 2025-01-30 20:11:49 +03:00

Prototype for a proxy extension that avoids loads when calling for a getter that is named after an identifier.

This commit is contained in:
Benjamin Eberlei 2011-05-20 20:50:03 +02:00
parent 85fb1a3ebb
commit bc4e14a99f
3 changed files with 32 additions and 3 deletions

View File

@ -209,6 +209,11 @@ class ProxyFactory
$methods .= $parameterString . ')';
$methods .= PHP_EOL . ' {' . PHP_EOL;
if ($this->isShortIdentifierGetter($method, $class)) {
$methods .= ' if ($this->__isInitialized__ === false) {' . PHP_EOL;
$methods .= ' return $this->_identifier["' . lcfirst(substr($method->getName(), 3)) . '"];' . PHP_EOL;
$methods .= ' }' . PHP_EOL;
}
$methods .= ' $this->__load();' . PHP_EOL;
$methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');';
$methods .= PHP_EOL . ' }' . PHP_EOL;
@ -218,6 +223,21 @@ class ProxyFactory
return $methods;
}
/**
* @param ReflectionMethod $method
* @param ClassMetadata $class
* @return bool
*/
private function isShortIdentifierGetter($method, $class)
{
return (
$method->getNumberOfParameters() == 0 &&
substr($method->getName(), 0, 3) == "get" &&
in_array(lcfirst(substr($method->getName(), 3)), $class->identifier, true) &&
(($method->getEndLine() - $method->getStartLine()) <= 4)
);
}
/**
* Generates the code for the __sleep method for a proxy class.
*

View File

@ -78,7 +78,7 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
$reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
$this->assertFalse($reference->postLoadCallbackInvoked);
$reference->getId(); // trigger proxy load
$reference->getValue(); // trigger proxy load
$this->assertTrue($reference->postLoadCallbackInvoked);
}
@ -210,6 +210,10 @@ class LifecycleCallbackTestEntity
return $this->id;
}
public function getValue() {
return $this->value;
}
/** @PrePersist */
public function doStuffOnPrePersist() {
$this->prePersistCallbackInvoked = true;

View File

@ -31,8 +31,8 @@ class DDC381Test extends \Doctrine\Tests\OrmFunctionalTestCase
$entity = $this->_em->getReference('Doctrine\Tests\ORM\Functional\Ticket\DDC381Entity', $persistedId);
// explicitly load proxy
$id = $entity->getId();
// explicitly load proxy (getId() does not trigger reload of proxy)
$id = $entity->getOtherMethod();
$data = serialize($entity);
$entity = unserialize($data);
@ -55,4 +55,9 @@ class DDC381Entity
{
return $this->id;
}
public function getOtherMethod()
{
}
}