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-01-05 23:18:56 +03:00
|
|
|
/**
|
|
|
|
* Loads the metadata for the specified class into the provided container.
|
|
|
|
*/
|
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
|
|
|
$reader = new AnnotationReader(new ArrayCache);
|
|
|
|
$class = $metadata->getReflectionClass();
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate DoctrineEntity annotation
|
2009-07-08 19:25:41 +04:00
|
|
|
if (($entityAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($tableAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($inheritanceTypeAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($discrColumnAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($discrValueAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($subClassesAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($changeTrackingAnnot = $reader->getClassAnnotation($class, '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-08 19:25:41 +04:00
|
|
|
if ($joinColumnAnnot = $reader->getPropertyAnnotation($property, '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-08 19:25:41 +04:00
|
|
|
} else if ($joinColumnsAnnot = $reader->getPropertyAnnotation($property, 'JoinColumns')) {
|
|
|
|
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-08 19:25:41 +04:00
|
|
|
if ($columnAnnot = $reader->getPropertyAnnotation($property, '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-05-29 14:23:13 +04:00
|
|
|
if (isset($columnAnnot->name)) {
|
|
|
|
$mapping['columnName'] = $columnAnnot->name;
|
|
|
|
}
|
2009-07-08 19:25:41 +04:00
|
|
|
if ($idAnnot = $reader->getPropertyAnnotation($property, 'Id')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
2009-07-08 19:25:41 +04:00
|
|
|
if ($generatedValueAnnot = $reader->getPropertyAnnotation($property, '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
|
|
|
}
|
|
|
|
$metadata->mapField($mapping);
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
// Check for SequenceGenerator/TableGenerator definition
|
2009-07-08 19:25:41 +04:00
|
|
|
if ($seqGeneratorAnnot = $reader->getPropertyAnnotation($property, 'SequenceGenerator')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$metadata->setSequenceGeneratorDefinition(array(
|
|
|
|
'sequenceName' => $seqGeneratorAnnot->sequenceName,
|
|
|
|
'allocationSize' => $seqGeneratorAnnot->allocationSize,
|
|
|
|
'initialValue' => $seqGeneratorAnnot->initialValue
|
|
|
|
));
|
2009-07-08 19:25:41 +04:00
|
|
|
} else if ($tblGeneratorAnnot = $reader->getPropertyAnnotation($property, 'TableGenerator')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
throw new DoctrineException("DoctrineTableGenerator not yet implemented.");
|
|
|
|
}
|
|
|
|
|
2009-07-08 19:25:41 +04:00
|
|
|
} else if ($oneToOneAnnot = $reader->getPropertyAnnotation($property, '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-08 19:25:41 +04:00
|
|
|
} else if ($oneToManyAnnot = $reader->getPropertyAnnotation($property, '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-08 19:25:41 +04:00
|
|
|
} else if ($manyToOneAnnot = $reader->getPropertyAnnotation($property, '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-08 19:25:41 +04:00
|
|
|
} else if ($manyToManyAnnot = $reader->getPropertyAnnotation($property, 'ManyToMany')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array();
|
2009-07-08 19:25:41 +04:00
|
|
|
if ($joinTableAnnot = $reader->getPropertyAnnotation($property, 'JoinTable')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array(
|
|
|
|
'name' => $joinTableAnnot->name,
|
|
|
|
'schema' => $joinTableAnnot->schema,
|
2009-07-08 19:25:41 +04:00
|
|
|
//'joinColumns' => $joinTableAnnot->joinColumns,
|
|
|
|
//'inverseJoinColumns' => $joinTableAnnot->inverseJoinColumns
|
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-02-04 19:35:36 +03: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-05-29 14:23:13 +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
|
|
|
}
|