2009-01-05 23:18:56 +03:00
|
|
|
<?php
|
2009-02-04 19:35:36 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Mapping\Driver;
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
use Doctrine\Common\DoctrineException;
|
2009-07-08 19:25:41 +04:00
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
|
|
|
use Doctrine\Common\Annotations\AnnotationReader;
|
2009-02-04 19:35:36 +03:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2009-02-07 20:02:13 +03:00
|
|
|
use Doctrine\ORM\Mapping\MappingException;
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
require __DIR__ . '/DoctrineAnnotations.php';
|
2009-01-05 23:18:56 +03:00
|
|
|
|
|
|
|
/**
|
2009-07-08 19:25:41 +04:00
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations.
|
2009-01-05 23:18:56 +03:00
|
|
|
*
|
2009-05-29 14:23:13 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-02-06 20:16:39 +03:00
|
|
|
* @since 2.0
|
2009-01-05 23:18:56 +03:00
|
|
|
*/
|
2009-06-06 01:40:47 +04:00
|
|
|
class AnnotationDriver implements Driver
|
2009-01-22 22:38:10 +03:00
|
|
|
{
|
2009-07-15 20:31:59 +04:00
|
|
|
/** The AnnotationReader. */
|
|
|
|
private $_reader;
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
/**
|
2009-07-15 20:31:59 +04:00
|
|
|
* Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
|
|
|
|
* docblock annotations.
|
|
|
|
*
|
|
|
|
* @param AnnotationReader $reader The AnnotationReader to use.
|
|
|
|
*/
|
|
|
|
public function __construct(AnnotationReader $reader)
|
|
|
|
{
|
|
|
|
$this->_reader = $reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2009-01-05 23:18:56 +03:00
|
|
|
*/
|
2009-02-04 19:35:36 +03:00
|
|
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
2009-01-05 23:18:56 +03:00
|
|
|
{
|
2009-07-08 19:25:41 +04:00
|
|
|
$class = $metadata->getReflectionClass();
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate DoctrineEntity annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if (($entityAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\Entity')) === null) {
|
2009-03-30 23:43:05 +04:00
|
|
|
throw DoctrineException::updateMe("$className is no entity.");
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate DoctrineTable annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($tableAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\Table')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$metadata->setPrimaryTable(array(
|
|
|
|
'name' => $tableAnnot->name,
|
2009-05-29 14:23:13 +04:00
|
|
|
'schema' => $tableAnnot->schema
|
2009-02-04 19:35:36 +03:00
|
|
|
));
|
2009-01-06 21:30:51 +03:00
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate InheritanceType annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($inheritanceTypeAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\InheritanceType')) {
|
2009-06-07 21:20:37 +04:00
|
|
|
$metadata->setInheritanceType(constant('\Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate DiscriminatorColumn annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($discrColumnAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\DiscriminatorColumn')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->setDiscriminatorColumn(array(
|
|
|
|
'name' => $discrColumnAnnot->name,
|
|
|
|
'type' => $discrColumnAnnot->type,
|
|
|
|
'length' => $discrColumnAnnot->length
|
|
|
|
));
|
|
|
|
}
|
2009-05-05 21:20:55 +04:00
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate DiscriminatorValue annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($discrValueAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\DiscriminatorValue')) {
|
2009-04-12 23:02:12 +04:00
|
|
|
$metadata->setDiscriminatorValue($discrValueAnnot->value);
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
|
|
|
// Evaluate DoctrineSubClasses annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($subClassesAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\SubClasses')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->setSubclasses($subClassesAnnot->value);
|
|
|
|
}
|
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
// Evaluate DoctrineChangeTrackingPolicy annotation
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($changeTrackingAnnot = $this->_reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\ChangeTrackingPolicy')) {
|
2009-05-05 21:20:55 +04:00
|
|
|
$metadata->setChangeTrackingPolicy($changeTrackingAnnot->value);
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate annotations on properties/fields
|
2009-07-08 19:25:41 +04:00
|
|
|
foreach ($class->getProperties() as $property) {
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($metadata->hasField($property->getName())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping = array();
|
|
|
|
$mapping['fieldName'] = $property->getName();
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2009-06-30 20:00:28 +04:00
|
|
|
// Check for JoinColummn/JoinColumns annotations
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinColumns = array();
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinColumns[] = array(
|
2009-06-30 20:00:28 +04:00
|
|
|
'name' => $joinColumnAnnot->name,
|
|
|
|
'referencedColumnName' => $joinColumnAnnot->referencedColumnName,
|
|
|
|
'unique' => $joinColumnAnnot->unique,
|
|
|
|
'nullable' => $joinColumnAnnot->nullable,
|
|
|
|
'onDelete' => $joinColumnAnnot->onDelete,
|
|
|
|
'onUpdate' => $joinColumnAnnot->onUpdate
|
2009-02-04 19:35:36 +03:00
|
|
|
);
|
2009-07-15 20:31:59 +04:00
|
|
|
} else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) {
|
2009-07-08 19:25:41 +04:00
|
|
|
foreach ($joinColumnsAnnot->value as $joinColumn) {
|
|
|
|
//$joinColumns = $joinColumnsAnnot->value;
|
|
|
|
$joinColumns[] = array(
|
|
|
|
'name' => $joinColumn->name,
|
|
|
|
'referencedColumnName' => $joinColumn->referencedColumnName,
|
|
|
|
'unique' => $joinColumn->unique,
|
|
|
|
'nullable' => $joinColumn->nullable,
|
|
|
|
'onDelete' => $joinColumn->onDelete,
|
|
|
|
'onUpdate' => $joinColumn->onUpdate
|
|
|
|
);
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2009-06-30 20:00:28 +04:00
|
|
|
// Field can only be annotated with one of:
|
|
|
|
// @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($columnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
if ($columnAnnot->type == null) {
|
2009-03-30 23:43:05 +04:00
|
|
|
throw DoctrineException::updateMe("Missing type on property " . $property->getName());
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
$mapping['type'] = $columnAnnot->type;
|
|
|
|
$mapping['length'] = $columnAnnot->length;
|
2009-02-02 14:55:50 +03:00
|
|
|
$mapping['nullable'] = $columnAnnot->nullable;
|
2009-07-17 22:13:03 +04:00
|
|
|
if (isset($columnAnnot->default)) {
|
|
|
|
$mapping['default'] = $columnAnnot->default;
|
|
|
|
}
|
2009-05-29 14:23:13 +04:00
|
|
|
if (isset($columnAnnot->name)) {
|
|
|
|
$mapping['columnName'] = $columnAnnot->name;
|
|
|
|
}
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($generatedValueAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) {
|
2009-06-07 21:20:37 +04:00
|
|
|
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2009-07-17 22:13:03 +04:00
|
|
|
if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
|
2009-07-18 15:41:37 +04:00
|
|
|
$metadata->setVersioned(true);
|
2009-07-17 22:13:03 +04:00
|
|
|
$metadata->setVersionField($mapping['fieldName']);
|
|
|
|
|
|
|
|
if ( ! isset($mapping['default'])) {
|
|
|
|
// TODO: When we have timestamp optimistic locking
|
|
|
|
// we'll have to figure out a better way to do this?
|
|
|
|
// Can we set the default value to be NOW() ?
|
|
|
|
$mapping['default'] = 1;
|
|
|
|
}
|
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapField($mapping);
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
// Check for SequenceGenerator/TableGenerator definition
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($seqGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\SequenceGenerator')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$metadata->setSequenceGeneratorDefinition(array(
|
|
|
|
'sequenceName' => $seqGeneratorAnnot->sequenceName,
|
|
|
|
'allocationSize' => $seqGeneratorAnnot->allocationSize,
|
|
|
|
'initialValue' => $seqGeneratorAnnot->initialValue
|
|
|
|
));
|
2009-07-15 20:31:59 +04:00
|
|
|
} else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) {
|
2009-07-18 15:41:37 +04:00
|
|
|
throw DoctrineException::tableIdGeneratorNotImplemented();
|
2009-03-30 23:43:05 +04:00
|
|
|
}
|
2009-07-15 20:31:59 +04:00
|
|
|
} else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
|
|
|
|
$mapping['cascade'] = $oneToOneAnnot->cascade;
|
|
|
|
$metadata->mapOneToOne($mapping);
|
2009-07-15 20:31:59 +04:00
|
|
|
} else if ($oneToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToMany')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
|
|
|
|
$mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
|
2009-01-09 19:25:06 +03:00
|
|
|
$mapping['cascade'] = $oneToManyAnnot->cascade;
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapOneToMany($mapping);
|
2009-07-15 20:31:59 +04:00
|
|
|
} else if ($manyToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToOne')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
2009-02-07 20:02:13 +03:00
|
|
|
$mapping['cascade'] = $manyToOneAnnot->cascade;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
|
|
|
|
$metadata->mapManyToOne($mapping);
|
2009-07-15 20:31:59 +04:00
|
|
|
} else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array();
|
2009-07-15 20:31:59 +04:00
|
|
|
if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array(
|
|
|
|
'name' => $joinTableAnnot->name,
|
2009-07-15 20:31:59 +04:00
|
|
|
'schema' => $joinTableAnnot->schema
|
2009-02-04 19:35:36 +03:00
|
|
|
);
|
2009-07-08 19:25:41 +04:00
|
|
|
|
|
|
|
foreach ($joinTableAnnot->joinColumns as $joinColumn) {
|
|
|
|
$joinTable['joinColumns'][] = array(
|
|
|
|
'name' => $joinColumn->name,
|
|
|
|
'referencedColumnName' => $joinColumn->referencedColumnName,
|
|
|
|
'unique' => $joinColumn->unique,
|
|
|
|
'nullable' => $joinColumn->nullable,
|
|
|
|
'onDelete' => $joinColumn->onDelete,
|
|
|
|
'onUpdate' => $joinColumn->onUpdate
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) {
|
|
|
|
$joinTable['inverseJoinColumns'][] = array(
|
|
|
|
'name' => $joinColumn->name,
|
|
|
|
'referencedColumnName' => $joinColumn->referencedColumnName,
|
|
|
|
'unique' => $joinColumn->unique,
|
|
|
|
'nullable' => $joinColumn->nullable,
|
|
|
|
'onDelete' => $joinColumn->onDelete,
|
|
|
|
'onUpdate' => $joinColumn->onUpdate
|
|
|
|
);
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2009-07-08 19:25:41 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinTable'] = $joinTable;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $manyToManyAnnot->targetEntity;
|
|
|
|
$mapping['mappedBy'] = $manyToManyAnnot->mappedBy;
|
2009-02-07 20:02:13 +03:00
|
|
|
$mapping['cascade'] = $manyToManyAnnot->cascade;
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapManyToMany($mapping);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate LifecycleListener annotation
|
|
|
|
if (($lifecycleListenerAnnot = $this->_reader->getClassAnnotation($class, '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\PostSave'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postSave);
|
|
|
|
}
|
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
|
|
|
|
}
|
|
|
|
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\PostDelete'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postDelete);
|
|
|
|
}
|
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
|
|
|
|
}
|
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2009-02-05 20:34:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the class with the specified name should have its metadata loaded.
|
2009-05-29 14:23:13 +04:00
|
|
|
* This is only the case if it is annotated with either @Entity or
|
|
|
|
* @MappedSuperclass in the class doc block.
|
2009-02-05 20:34:44 +03:00
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTransient($className)
|
|
|
|
{
|
|
|
|
$refClass = new \ReflectionClass($className);
|
|
|
|
$docComment = $refClass->getDocComment();
|
2009-07-15 14:59:35 +04:00
|
|
|
return strpos($docComment, 'Entity') === false &&
|
|
|
|
strpos($docComment, 'MappedSuperclass') === false;
|
2009-02-05 20:34:44 +03:00
|
|
|
}
|
2009-05-30 13:37:56 +04:00
|
|
|
|
|
|
|
public function preload()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|