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-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-01-07 20:46:02 +03:00
|
|
|
/* Addendum annotation reflection extensions */
|
2009-05-29 01:06:24 +04:00
|
|
|
if ( ! class_exists('Addendum', false)) {
|
|
|
|
require __DIR__ . '/../../../../vendor/addendum/annotations.php';
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
require __DIR__ . '/DoctrineAnnotations.php';
|
2009-01-05 23:18:56 +03:00
|
|
|
|
|
|
|
/**
|
2009-02-06 20:16:39 +03:00
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations
|
|
|
|
* with the help of the Addendum reflection extensions.
|
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-01-22 22:38:10 +03:00
|
|
|
class AnnotationDriver
|
|
|
|
{
|
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-05-29 01:06:24 +04:00
|
|
|
$annotClass = new \ReflectionAnnotatedClass($className);
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate DoctrineEntity annotation
|
2009-05-29 14:23:13 +04:00
|
|
|
if (($entityAnnot = $annotClass->getAnnotation('Entity')) === false) {
|
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-05-29 14:23:13 +04:00
|
|
|
if ($tableAnnot = $annotClass->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
if ($inheritanceTypeAnnot = $annotClass->getAnnotation('InheritanceType')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->setInheritanceType($inheritanceTypeAnnot->value);
|
|
|
|
}
|
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate DiscriminatorColumn annotation
|
2009-05-29 14:23:13 +04:00
|
|
|
if ($discrColumnAnnot = $annotClass->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
if ($discrValueAnnot = $annotClass->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
if ($subClassesAnnot = $annotClass->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
if ($changeTrackingAnnot = $annotClass->getAnnotation('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-01-05 23:18:56 +03:00
|
|
|
foreach ($annotClass->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
|
|
|
|
|
|
|
// Check for DoctrineJoinColummn/DoctrineJoinColumns annotations
|
|
|
|
$joinColumns = array();
|
2009-05-29 14:23:13 +04:00
|
|
|
if ($joinColumnAnnot = $property->getAnnotation('JoinColumn')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinColumns[] = array(
|
|
|
|
'name' => $joinColumnAnnot->name,
|
|
|
|
'referencedColumnName' => $joinColumnAnnot->referencedColumnName,
|
|
|
|
'unique' => $joinColumnAnnot->unique,
|
|
|
|
'nullable' => $joinColumnAnnot->nullable,
|
|
|
|
'onDelete' => $joinColumnAnnot->onDelete,
|
|
|
|
'onUpdate' => $joinColumnAnnot->onUpdate
|
|
|
|
);
|
2009-05-29 14:23:13 +04:00
|
|
|
} else if ($joinColumnsAnnot = $property->getAnnotation('JoinColumns')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinColumns = $joinColumnsAnnot->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field can only be annotated with one of: DoctrineColumn,
|
|
|
|
// DoctrineOneToOne, DoctrineOneToMany, DoctrineManyToOne, DoctrineManyToMany
|
2009-05-29 14:23:13 +04:00
|
|
|
if ($columnAnnot = $property->getAnnotation('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;
|
|
|
|
}
|
|
|
|
if ($idAnnot = $property->getAnnotation('Id')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
2009-05-29 14:23:13 +04:00
|
|
|
if ($generatedValueAnnot = $property->getAnnotation('GeneratedValue')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$metadata->setIdGeneratorType($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-05-29 14:23:13 +04:00
|
|
|
if ($seqGeneratorAnnot = $property->getAnnotation('SequenceGenerator')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$metadata->setSequenceGeneratorDefinition(array(
|
|
|
|
'sequenceName' => $seqGeneratorAnnot->sequenceName,
|
|
|
|
'allocationSize' => $seqGeneratorAnnot->allocationSize,
|
|
|
|
'initialValue' => $seqGeneratorAnnot->initialValue
|
|
|
|
));
|
2009-05-29 14:23:13 +04:00
|
|
|
} else if ($tblGeneratorAnnot = $property->getAnnotation('TableGenerator')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
throw new DoctrineException("DoctrineTableGenerator not yet implemented.");
|
|
|
|
}
|
|
|
|
|
2009-05-29 14:23:13 +04:00
|
|
|
} else if ($oneToOneAnnot = $property->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
} else if ($oneToManyAnnot = $property->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
} else if ($manyToOneAnnot = $property->getAnnotation('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-05-29 14:23:13 +04:00
|
|
|
} else if ($manyToManyAnnot = $property->getAnnotation('ManyToMany')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array();
|
2009-05-29 14:23:13 +04:00
|
|
|
if ($joinTableAnnot = $property->getAnnotation('JoinTable')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array(
|
|
|
|
'name' => $joinTableAnnot->name,
|
|
|
|
'schema' => $joinTableAnnot->schema,
|
|
|
|
'joinColumns' => $joinTableAnnot->joinColumns,
|
|
|
|
'inverseJoinColumns' => $joinTableAnnot->inverseJoinColumns
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$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
|
|
|
}
|