Commit da07bf4a authored by romanb's avatar romanb

[2.0] Small refactorings.

parent d674f192
......@@ -4,18 +4,11 @@ namespace Doctrine\Common;
class DoctrineException extends \Exception
{
private $_innerException;
private static $_messages = array();
public function __construct($message = "", \Exception $innerException = null)
public function __construct($message = "", \Exception $cause = null)
{
parent::__construct($message);
$this->_innerException = $innerException;
}
public function getInnerException()
{
return $this->_innerException;
parent::__construct($message, 0, $cause);
}
public static function notImplemented($method, $class)
......
......@@ -39,6 +39,11 @@ class EventArgs
{
private static $_emptyEventArgsInstance;
/**
* Gets the single, empty EventArgs instance.
*
* @return EventArgs
*/
public static function getEmptyInstance()
{
if ( ! self::$_emptyEventArgsInstance) {
......
......@@ -25,7 +25,7 @@ namespace Doctrine\Common;
* Contract for classes that provide the service of notifying listeners of
* changes to their properties.
*
* @author robo
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*/
interface NotifyPropertyChanged
......@@ -35,6 +35,6 @@ interface NotifyPropertyChanged
*
* @param PropertyChangedListener $listener
*/
public function addPropertyChangedListener(PropertyChangedListener $listener);
function addPropertyChangedListener(PropertyChangedListener $listener);
}
......@@ -25,11 +25,11 @@ namespace Doctrine\Common;
* Contract for classes that are potential listeners of a <tt>NotifyPropertyChanged</tt>
* implementor.
*
* @author robo
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*/
interface PropertyChangedListener
{
public function propertyChanged($sender, $propertyName, $oldValue, $newValue);
function propertyChanged($sender, $propertyName, $oldValue, $newValue);
}
......@@ -309,8 +309,13 @@ class EntityManager
*/
public function getReference($entityName, $identifier)
{
if ($this->_config->getAllowPartialObjects()) {
$entity = new $entityName;
$this->getClassMetadata($entityName)->setEntityIdentifier($entity, $identifier);
} else {
$entity = $this->_proxyFactory->getReferenceProxy($entityName, $identifier);
}
return $entity;
}
......@@ -321,23 +326,12 @@ class EntityManager
*/
public function setFlushMode($flushMode)
{
if ( ! $this->_isFlushMode($flushMode)) {
if ( ! ($flushMode >= 1 && $flushMode <= 4)) {
throw EntityManagerException::invalidFlushMode();
}
$this->_flushMode = $flushMode;
}
/**
* Checks whether the given value is a valid flush mode.
*
* @param string $value
* @return boolean
*/
private function _isFlushMode($value)
{
return $value >= 1 && $value <= 4;
}
/**
* Gets the currently used flush mode.
*
......
......@@ -33,42 +33,42 @@ final class Events
{
private function __construct() {}
/**
* The preDelete event occurs for a given entity before the respective
* EntityManager delete operation for that entity is executed.
* The preRemove event occurs for a given entity before the respective
* EntityManager remove operation for that entity is executed.
*
* This is an entity lifecycle event.
*
* @var string
*/
const preDelete = 'preDelete';
const preRemove = 'preRemove';
/**
* The postDelete event occurs for an entity after the entity has
* The postRemove event occurs for an entity after the entity has
* been deleted. It will be invoked after the database delete operations.
*
* This is an entity lifecycle event.
*
* @var string
*/
const postDelete = 'postDelete';
const postRemove = 'postRemove';
/**
* The preSave event occurs for a given entity before the respective
* EntityManager save operation for that entity is executed.
* The prePersist event occurs for a given entity before the respective
* EntityManager persist operation for that entity is executed.
*
* This is an entity lifecycle event.
*
* @var string
*/
const preSave = 'preSave';
const prePersist = 'prePersist';
/**
* The postSave event occurs for an entity after the entity has
* The postPersist event occurs for an entity after the entity has
* been made persistent. It will be invoked after the database insert operations.
* Generated primary key values are available in the postSave event.
* Generated primary key values are available in the postPersist event.
*
* This is an entity lifecycle event.
*
* @var string
*/
const postSave = 'postSave';
const postPersist = 'postPersist';
/**
* The preUpdate event occurs before the database update operations to
* entity data.
......
......@@ -21,7 +21,6 @@
namespace Doctrine\ORM\Mapping;
use \ReflectionClass;
use Doctrine\Common\DoctrineException;
/**
......@@ -261,7 +260,7 @@ final class ClassMetadata
*
* @var boolean
*/
public $joinSubclasses = true;
//public $joinSubclasses = true;
/**
* The discriminator value of this class.
......@@ -288,7 +287,6 @@ final class ClassMetadata
*
* name => <tableName>
* schema => <schemaName>
* catalog => <catalogName> //TODO: remove catalog? needed?
*
* @var array
*/
......@@ -408,7 +406,7 @@ final class ClassMetadata
$this->namespace = substr($entityName, 0, strrpos($entityName, '\\'));
$this->primaryTable['name'] = str_replace($this->namespace . '\\', '', $this->name);
$this->rootEntityName = $entityName;
$this->reflClass = new ReflectionClass($entityName);
$this->reflClass = new \ReflectionClass($entityName);
}
/**
......
......@@ -58,14 +58,19 @@ class AnnotationDriver implements Driver
{
$class = $metadata->getReflectionClass();
$classAnnotations = $this->_reader->getClassAnnotations($class);
// Evaluate DoctrineEntity annotation
if (($entityAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\Entity')) === null) {
if ( ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
throw DoctrineException::updateMe("$className is no entity.");
}
$entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
// Evaluate DoctrineTable annotation
if ($tableAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\Table')) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\Table'])) {
$tableAnnot = $classAnnotations['Doctrine\ORM\Mapping\Table'];
$metadata->setPrimaryTable(array(
'name' => $tableAnnot->name,
'schema' => $tableAnnot->schema
......@@ -73,12 +78,14 @@ class AnnotationDriver implements Driver
}
// Evaluate InheritanceType annotation
if ($inheritanceTypeAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\InheritanceType')) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\InheritanceType'])) {
$inheritanceTypeAnnot = $classAnnotations['Doctrine\ORM\Mapping\InheritanceType'];
$metadata->setInheritanceType(constant('\Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
}
// Evaluate DiscriminatorColumn annotation
if ($discrColumnAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\DiscriminatorColumn')) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) {
$discrColumnAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'];
$metadata->setDiscriminatorColumn(array(
'name' => $discrColumnAnnot->name,
'type' => $discrColumnAnnot->type,
......@@ -87,17 +94,20 @@ class AnnotationDriver implements Driver
}
// Evaluate DiscriminatorValue annotation
if ($discrValueAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\DiscriminatorValue')) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorValue'])) {
$discrValueAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorValue'];
$metadata->setDiscriminatorValue($discrValueAnnot->value);
}
// Evaluate DoctrineSubClasses annotation
if ($subClassesAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\SubClasses')) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\SubClasses'])) {
$subClassesAnnot = $classAnnotations['Doctrine\ORM\Mapping\SubClasses'];
$metadata->setSubclasses($subClassesAnnot->value);
}
// Evaluate DoctrineChangeTrackingPolicy annotation
if ($changeTrackingAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\ChangeTrackingPolicy')) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
$changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
$metadata->setChangeTrackingPolicy($changeTrackingAnnot->value);
}
......@@ -237,15 +247,16 @@ class AnnotationDriver implements Driver
}
// Evaluate LifecycleListener annotation
if (($lifecycleListenerAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\LifecycleListener'))) {
if (isset($classAnnotations['Doctrine\ORM\Mapping\LifecycleListener'])) {
$lifecycleListenerAnnot = $classAnnotations['Doctrine\ORM\Mapping\LifecycleListener'];
foreach ($class->getMethods() as $method) {
if ($method->isPublic()) {
$annotations = $this->_reader->getMethodAnnotations($method);
if (isset($annotations['Doctrine\ORM\Mapping\PreSave'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preSave);
if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostSave'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postSave);
if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist);
}
if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
......@@ -253,11 +264,11 @@ class AnnotationDriver implements Driver
if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate);
}
if (isset($annotations['Doctrine\ORM\Mapping\PreDelete'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preDelete);
if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostDelete'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postDelete);
if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
......@@ -265,7 +276,6 @@ class AnnotationDriver implements Driver
}
}
}
}
/**
......@@ -278,10 +288,9 @@ class AnnotationDriver implements Driver
*/
public function isTransient($className)
{
$refClass = new \ReflectionClass($className);
$docComment = $refClass->getDocComment();
return strpos($docComment, 'Entity') === false &&
strpos($docComment, 'MappedSuperclass') === false;
$classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className));
return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) &&
! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
}
public function preload()
......
......@@ -108,12 +108,12 @@ final class ChangeTrackingPolicy extends \Doctrine\Common\Annotations\Annotation
/* Annotations for lifecycle callbacks */
final class LifecycleListener extends \Doctrine\Common\Annotations\Annotation {}
final class PreSave extends \Doctrine\Common\Annotations\Annotation {}
final class PostSave extends \Doctrine\Common\Annotations\Annotation {}
final class PrePersist extends \Doctrine\Common\Annotations\Annotation {}
final class PostPersist extends \Doctrine\Common\Annotations\Annotation {}
final class PreUpdate extends \Doctrine\Common\Annotations\Annotation {}
final class PostUpdate extends \Doctrine\Common\Annotations\Annotation {}
final class PreDelete extends \Doctrine\Common\Annotations\Annotation {}
final class PostDelete extends \Doctrine\Common\Annotations\Annotation {}
final class PreRemove extends \Doctrine\Common\Annotations\Annotation {}
final class PostRemove extends \Doctrine\Common\Annotations\Annotation {}
final class PostLoad extends \Doctrine\Common\Annotations\Annotation {}
/* Generic annotation for Doctrine extensions */
......
......@@ -448,6 +448,7 @@ class StandardEntityPersister
}
} else {
// Inject collection
//TODO: Eager load
$this->_class->reflFields[$field]->setValue(
$entity, new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName)
));
......
......@@ -322,6 +322,7 @@ class UnitOfWork implements PropertyChangedListener
$conn->commit();
} catch (\Exception $e) {
$conn->rollback();
$this->clear();
throw $e;
}
......@@ -342,6 +343,9 @@ class UnitOfWork implements PropertyChangedListener
$this->_orphanRemovals = array();
}
/**
* Executes any extra updates that have been scheduled.
*/
private function _executeExtraUpdates()
{
foreach ($this->_extraUpdates as $oid => $update) {
......@@ -659,8 +663,8 @@ class UnitOfWork implements PropertyChangedListener
$className = $class->name;
$persister = $this->getEntityPersister($className);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postSave]);
$hasListeners = $this->_evm->hasListeners(Events::postSave);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postPersist]);
$hasListeners = $this->_evm->hasListeners(Events::postPersist);
if ($hasLifecycleCallbacks || $hasListeners) {
$entities = array();
}
......@@ -693,10 +697,10 @@ class UnitOfWork implements PropertyChangedListener
if ($hasLifecycleCallbacks || $hasListeners) {
foreach ($entities as $entity) {
if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postSave, $entity);
$class->invokeLifecycleCallbacks(Events::postPersist, $entity);
}
if ($hasListeners) {
$this->_evm->dispatchEvent(Events::postSave, new LifecycleEventArgs($entity));
$this->_evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($entity));
}
}
}
......@@ -755,8 +759,8 @@ class UnitOfWork implements PropertyChangedListener
$className = $class->name;
$persister = $this->getEntityPersister($className);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postDelete]);
$hasListeners = $this->_evm->hasListeners(Events::postDelete);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postRemove]);
$hasListeners = $this->_evm->hasListeners(Events::postRemove);
foreach ($this->_entityDeletions as $oid => $entity) {
if (get_class($entity) == $className) {
......@@ -764,10 +768,10 @@ class UnitOfWork implements PropertyChangedListener
unset($this->_entityDeletions[$oid]);
if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postDelete, $entity);
$class->invokeLifecycleCallbacks(Events::postRemove, $entity);
}
if ($hasListeners) {
$this->_evm->dispatchEvent(Events::postDelete, new LifecycleEventArgs($entity));
$this->_evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($entity));
}
}
}
......@@ -1162,11 +1166,11 @@ class UnitOfWork implements PropertyChangedListener
}
break;
case self::STATE_NEW:
if (isset($class->lifecycleCallbacks[Events::preSave])) {
$class->invokeLifecycleCallbacks(Events::preSave, $entity);
if (isset($class->lifecycleCallbacks[Events::prePersist])) {
$class->invokeLifecycleCallbacks(Events::prePersist, $entity);
}
if ($this->_evm->hasListeners(Events::preSave)) {
$this->_evm->dispatchEvent(Events::preSave, new LifecycleEventArgs($entity));
if ($this->_evm->hasListeners(Events::prePersist)) {
$this->_evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($entity));
}
$idGen = $class->idGenerator;
......@@ -1237,11 +1241,11 @@ class UnitOfWork implements PropertyChangedListener
// nothing to do
break;
case self::STATE_MANAGED:
if (isset($class->lifecycleCallbacks[Events::preDelete])) {
$class->invokeLifecycleCallbacks(Events::preDelete, $entity);
if (isset($class->lifecycleCallbacks[Events::preRemove])) {
$class->invokeLifecycleCallbacks(Events::preRemove, $entity);
}
if ($this->_evm->hasListeners(Events::preDelete)) {
$this->_evm->dispatchEvent(Events::preDelete, new LifecycleEventArgs($entity));
if ($this->_evm->hasListeners(Events::preRemove)) {
$this->_evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($entity));
}
$this->scheduleForDelete($entity);
break;
......@@ -1483,19 +1487,19 @@ class UnitOfWork implements PropertyChangedListener
*/
public function clear()
{
$this->_identityMap = array();
$this->_entityIdentifiers = array();
$this->_originalEntityData = array();
$this->_entityChangeSets = array();
$this->_entityStates = array();
$this->_scheduledForDirtyCheck = array();
$this->_entityInsertions = array();
$this->_entityUpdates = array();
$this->_entityDeletions = array();
$this->_collectionDeletions = array();
//$this->_collectionCreations = array();
$this->_collectionUpdates = array();
//$this->_orphanRemovals = array();
$this->_identityMap =
$this->_entityIdentifiers =
$this->_originalEntityData =
$this->_entityChangeSets =
$this->_entityStates =
$this->_scheduledForDirtyCheck =
$this->_entityInsertions =
$this->_entityUpdates =
$this->_entityDeletions =
$this->_collectionDeletions =
//$this->_collectionCreations =
$this->_collectionUpdates =
$this->_orphanRemovals = array();
$this->_commitOrderCalculator->clear();
}
......
......@@ -17,7 +17,7 @@ class DoctrineExceptionTest extends \Doctrine\Tests\DoctrineTestCase
{
$e1 = \Doctrine\Common\DoctrineException::testException();
$e2 = \Doctrine\Common\DoctrineException::testException2('param1', $e1);
$this->assertEquals($e1, $e2->getInnerException());
$this->assertEquals($e1, $e2->getPrevious());
}
public function testNotImplemented()
......
......@@ -24,8 +24,8 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->persist($entity);
$this->_em->flush();
$this->assertTrue($entity->preSaveCallbackInvoked);
$this->assertTrue($entity->postSaveCallbackInvoked);
$this->assertTrue($entity->prePersistCallbackInvoked);
$this->assertTrue($entity->postPersistCallbackInvoked);
$this->_em->clear();
......@@ -49,8 +49,8 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
class LifecycleCallbackTestEntity
{
/* test stuff */
public $preSaveCallbackInvoked = false;
public $postSaveCallbackInvoked = false;
public $prePersistCallbackInvoked = false;
public $postPersistCallbackInvoked = false;
public $postLoadCallbackInvoked = false;
/**
......@@ -63,14 +63,14 @@ class LifecycleCallbackTestEntity
*/
public $value;
/** @PreSave */
public function doStuffOnPreSave() {
$this->preSaveCallbackInvoked = true;
/** @PrePersist */
public function doStuffOnPrePersist() {
$this->prePersistCallbackInvoked = true;
}
/** @PostSave */
public function doStuffOnPostSave() {
$this->postSaveCallbackInvoked = true;
/** @PostPersist */
public function doStuffOnPostPersist() {
$this->postPersistCallbackInvoked = true;
}
/** @PostLoad */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment