2009-01-05 23:18:56 +03:00
|
|
|
<?php
|
2009-02-04 19:35:36 +03:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2010-02-03 00:17:00 +03:00
|
|
|
use Doctrine\Common\Cache\ArrayCache,
|
2009-07-30 19:16:02 +04:00
|
|
|
Doctrine\Common\Annotations\AnnotationReader,
|
2011-07-02 21:48:43 +04:00
|
|
|
Doctrine\Common\Annotations\AnnotationRegistry,
|
2009-10-05 08:11:29 +04:00
|
|
|
Doctrine\ORM\Mapping\ClassMetadataInfo,
|
2011-02-16 19:24:42 +03:00
|
|
|
Doctrine\ORM\Mapping\MappingException;
|
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
|
|
|
*
|
2010-05-08 19:01:20 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-01-05 23:18:56 +03:00
|
|
|
*/
|
2010-01-28 22:38:16 +03:00
|
|
|
class AnnotationDriver implements Driver
|
2009-01-22 22:38:10 +03:00
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
/**
|
2010-01-28 15:46:12 +03:00
|
|
|
* The AnnotationReader.
|
|
|
|
*
|
|
|
|
* @var AnnotationReader
|
|
|
|
*/
|
2011-07-04 18:33:44 +04:00
|
|
|
protected $_reader;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-28 22:38:16 +03:00
|
|
|
/**
|
|
|
|
* The paths where to look for mapping files.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_paths = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The file extension of mapping documents.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_fileExtension = '.php';
|
2010-02-12 01:47:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
protected $_classNames;
|
2011-05-23 00:10:13 +04:00
|
|
|
|
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.
|
2011-05-23 00:10:13 +04:00
|
|
|
*
|
2011-04-30 16:23:46 +04:00
|
|
|
* @param AnnotationReader $reader The AnnotationReader to use, duck-typed.
|
2011-05-23 00:10:13 +04:00
|
|
|
* @param string|array $paths One or multiple paths where mapping classes can be found.
|
2009-07-15 20:31:59 +04:00
|
|
|
*/
|
2011-04-30 16:23:46 +04:00
|
|
|
public function __construct($reader, $paths = null)
|
2009-07-15 20:31:59 +04:00
|
|
|
{
|
|
|
|
$this->_reader = $reader;
|
2010-01-28 22:38:16 +03:00
|
|
|
if ($paths) {
|
|
|
|
$this->addPaths((array) $paths);
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 00:10:13 +04:00
|
|
|
|
2010-01-28 22:38:16 +03:00
|
|
|
/**
|
|
|
|
* Append lookup paths to metadata driver.
|
|
|
|
*
|
|
|
|
* @param array $paths
|
|
|
|
*/
|
|
|
|
public function addPaths(array $paths)
|
|
|
|
{
|
|
|
|
$this->_paths = array_unique(array_merge($this->_paths, $paths));
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-28 22:38:16 +03:00
|
|
|
/**
|
|
|
|
* Retrieve the defined metadata lookup paths.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPaths()
|
|
|
|
{
|
|
|
|
return $this->_paths;
|
|
|
|
}
|
|
|
|
|
2011-08-10 22:10:09 +04:00
|
|
|
/**
|
|
|
|
* Retrieve the current annotation reader
|
2011-12-20 01:56:19 +04:00
|
|
|
*
|
2011-08-10 22:10:09 +04:00
|
|
|
* @return AnnotationReader
|
|
|
|
*/
|
|
|
|
public function getReader()
|
|
|
|
{
|
|
|
|
return $this->_reader;
|
|
|
|
}
|
|
|
|
|
2010-01-28 22:38:16 +03:00
|
|
|
/**
|
|
|
|
* Get the file extension used to look for mapping files under
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getFileExtension()
|
|
|
|
{
|
|
|
|
return $this->_fileExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the file extension used to look for mapping files under
|
|
|
|
*
|
|
|
|
* @param string $fileExtension The file extension to set
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setFileExtension($fileExtension)
|
|
|
|
{
|
|
|
|
$this->_fileExtension = $fileExtension;
|
2009-10-08 02:14:13 +04:00
|
|
|
}
|
|
|
|
|
2009-07-15 20:31:59 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2009-01-05 23:18:56 +03:00
|
|
|
*/
|
2011-02-16 19:24:42 +03:00
|
|
|
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
|
2009-01-05 23:18:56 +03:00
|
|
|
{
|
2009-07-08 19:25:41 +04:00
|
|
|
$class = $metadata->getReflectionClass();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
$classAnnotations = $this->_reader->getClassAnnotations($class);
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2011-05-23 00:10:13 +04:00
|
|
|
// Compatibility with Doctrine Common 3.x
|
|
|
|
if ($classAnnotations && is_int(key($classAnnotations))) {
|
|
|
|
foreach ($classAnnotations as $annot) {
|
|
|
|
$classAnnotations[get_class($annot)] = $annot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 19:16:02 +04:00
|
|
|
// Evaluate Entity annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
|
|
|
|
$entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
|
2011-09-08 20:17:53 +04:00
|
|
|
if ($entityAnnot->repositoryClass !== null) {
|
2011-09-08 00:27:05 +04:00
|
|
|
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
|
|
|
|
}
|
2011-03-29 22:17:44 +04:00
|
|
|
if ($entityAnnot->readOnly) {
|
|
|
|
$metadata->markReadOnly();
|
|
|
|
}
|
2009-07-30 19:16:02 +04:00
|
|
|
} else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
|
2011-09-08 00:27:05 +04:00
|
|
|
$mappedSuperclassAnnot = $classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'];
|
|
|
|
$metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass);
|
2009-07-30 19:16:02 +04:00
|
|
|
$metadata->isMappedSuperclass = true;
|
|
|
|
} else {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
|
2010-04-30 19:30:27 +04:00
|
|
|
// Evaluate Table annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\Table'])) {
|
|
|
|
$tableAnnot = $classAnnotations['Doctrine\ORM\Mapping\Table'];
|
2009-08-21 22:13:22 +04:00
|
|
|
$primaryTable = array(
|
2009-02-04 19:35:36 +03:00
|
|
|
'name' => $tableAnnot->name,
|
2009-05-29 14:23:13 +04:00
|
|
|
'schema' => $tableAnnot->schema
|
2009-08-21 22:13:22 +04:00
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ($tableAnnot->indexes !== null) {
|
|
|
|
foreach ($tableAnnot->indexes as $indexAnnot) {
|
2010-01-06 16:23:56 +03:00
|
|
|
$primaryTable['indexes'][$indexAnnot->name] = array(
|
|
|
|
'columns' => $indexAnnot->columns
|
|
|
|
);
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ($tableAnnot->uniqueConstraints !== null) {
|
|
|
|
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) {
|
2010-01-06 16:23:56 +03:00
|
|
|
$primaryTable['uniqueConstraints'][$uniqueConstraint->name] = array(
|
|
|
|
'columns' => $uniqueConstraint->columns
|
|
|
|
);
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
$metadata->setPrimaryTable($primaryTable);
|
2009-01-06 21:30:51 +03:00
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2011-03-07 00:45:09 +03:00
|
|
|
// Evaluate NamedQueries annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedQueries'])) {
|
|
|
|
$namedQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedQueries'];
|
|
|
|
|
2011-12-17 15:35:22 +04:00
|
|
|
if (!is_array($namedQueriesAnnot->value)) {
|
|
|
|
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
|
|
|
|
}
|
|
|
|
|
2011-03-07 00:45:09 +03:00
|
|
|
foreach ($namedQueriesAnnot->value as $namedQuery) {
|
2011-12-17 15:35:22 +04:00
|
|
|
if (!($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) {
|
|
|
|
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
|
|
|
|
}
|
2011-03-07 00:45:09 +03:00
|
|
|
$metadata->addNamedQuery(array(
|
|
|
|
'name' => $namedQuery->name,
|
|
|
|
'query' => $namedQuery->query
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate InheritanceType annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\InheritanceType'])) {
|
|
|
|
$inheritanceTypeAnnot = $classAnnotations['Doctrine\ORM\Mapping\InheritanceType'];
|
2010-04-10 02:00:36 +04:00
|
|
|
$metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2010-08-08 14:29:14 +04:00
|
|
|
if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
|
|
|
|
// Evaluate DiscriminatorColumn annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) {
|
|
|
|
$discrColumnAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'];
|
|
|
|
$metadata->setDiscriminatorColumn(array(
|
|
|
|
'name' => $discrColumnAnnot->name,
|
|
|
|
'type' => $discrColumnAnnot->type,
|
|
|
|
'length' => $discrColumnAnnot->length
|
|
|
|
));
|
2010-09-24 01:10:31 +04:00
|
|
|
} else {
|
|
|
|
$metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
|
2010-08-08 14:29:14 +04:00
|
|
|
}
|
2009-05-05 21:20:55 +04:00
|
|
|
|
2010-08-08 14:29:14 +04:00
|
|
|
// Evaluate DiscriminatorMap annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'])) {
|
|
|
|
$discrMapAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'];
|
|
|
|
$metadata->setDiscriminatorMap($discrMapAnnot->value);
|
|
|
|
}
|
|
|
|
}
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2010-08-08 14:29:14 +04:00
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
// Evaluate DoctrineChangeTrackingPolicy annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
|
|
|
|
$changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
|
2010-04-10 02:00:36 +04:00
|
|
|
$metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
|
2009-05-05 21:20:55 +04:00
|
|
|
}
|
|
|
|
|
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-10-07 22:53:30 +04:00
|
|
|
if ($metadata->isMappedSuperclass && ! $property->isPrivate()
|
|
|
|
||
|
|
|
|
$metadata->isInheritedField($property->name)
|
|
|
|
||
|
|
|
|
$metadata->isInheritedAssociation($property->name)) {
|
2009-04-09 22:12:48 +04:00
|
|
|
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();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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,
|
2010-02-02 00:48:27 +03:00
|
|
|
'columnDefinition' => $joinColumnAnnot->columnDefinition,
|
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[] = array(
|
|
|
|
'name' => $joinColumn->name,
|
|
|
|
'referencedColumnName' => $joinColumn->referencedColumnName,
|
|
|
|
'unique' => $joinColumn->unique,
|
|
|
|
'nullable' => $joinColumn->nullable,
|
|
|
|
'onDelete' => $joinColumn->onDelete,
|
2010-02-02 00:48:27 +03:00
|
|
|
'columnDefinition' => $joinColumn->columnDefinition,
|
2009-07-08 19:25:41 +04:00
|
|
|
);
|
|
|
|
}
|
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) {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::propertyTypeIsRequired($className, $property->getName());
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['type'] = $columnAnnot->type;
|
|
|
|
$mapping['length'] = $columnAnnot->length;
|
2009-09-04 22:35:40 +04:00
|
|
|
$mapping['precision'] = $columnAnnot->precision;
|
|
|
|
$mapping['scale'] = $columnAnnot->scale;
|
2009-02-02 14:55:50 +03:00
|
|
|
$mapping['nullable'] = $columnAnnot->nullable;
|
2009-09-08 22:12:01 +04:00
|
|
|
$mapping['unique'] = $columnAnnot->unique;
|
2009-12-07 15:47:23 +03:00
|
|
|
if ($columnAnnot->options) {
|
|
|
|
$mapping['options'] = $columnAnnot->options;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-29 14:23:13 +04:00
|
|
|
if (isset($columnAnnot->name)) {
|
|
|
|
$mapping['columnName'] = $columnAnnot->name;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-21 01:35:18 +03:00
|
|
|
if (isset($columnAnnot->columnDefinition)) {
|
|
|
|
$mapping['columnDefinition'] = $columnAnnot->columnDefinition;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-17 22:13:03 +04:00
|
|
|
if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
|
2009-08-24 21:06:12 +04:00
|
|
|
$metadata->setVersionMapping($mapping);
|
2009-07-17 22:13:03 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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')) {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::tableIdGeneratorNotImplemented($className);
|
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')) {
|
2010-08-07 21:33:54 +04:00
|
|
|
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
|
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
|
|
|
|
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;
|
2010-03-15 20:19:00 +03:00
|
|
|
$mapping['inversedBy'] = $oneToOneAnnot->inversedBy;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['cascade'] = $oneToOneAnnot->cascade;
|
2009-07-23 13:52:16 +04:00
|
|
|
$mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch);
|
2009-01-05 23:18:56 +03:00
|
|
|
$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;
|
2011-02-05 11:31:40 +03:00
|
|
|
$mapping['indexBy'] = $oneToManyAnnot->indexBy;
|
2009-07-23 13:52:16 +04:00
|
|
|
$mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch);
|
2010-02-14 22:38:22 +03:00
|
|
|
|
|
|
|
if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
|
|
|
|
$mapping['orderBy'] = $orderByAnnot->value;
|
|
|
|
}
|
|
|
|
|
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')) {
|
2010-08-07 21:33:54 +04:00
|
|
|
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
|
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
2009-02-07 20:02:13 +03:00
|
|
|
$mapping['cascade'] = $manyToOneAnnot->cascade;
|
2010-03-15 20:19:00 +03:00
|
|
|
$mapping['inversedBy'] = $manyToOneAnnot->inversedBy;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch);
|
2009-01-05 23:18:56 +03:00
|
|
|
$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();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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
|
|
|
);
|
2010-03-05 19:35:00 +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,
|
2010-02-02 00:48:27 +03:00
|
|
|
'columnDefinition' => $joinColumn->columnDefinition,
|
2009-07-08 19:25:41 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) {
|
|
|
|
$joinTable['inverseJoinColumns'][] = array(
|
|
|
|
'name' => $joinColumn->name,
|
|
|
|
'referencedColumnName' => $joinColumn->referencedColumnName,
|
|
|
|
'unique' => $joinColumn->unique,
|
|
|
|
'nullable' => $joinColumn->nullable,
|
|
|
|
'onDelete' => $joinColumn->onDelete,
|
2010-02-02 00:48:27 +03:00
|
|
|
'columnDefinition' => $joinColumn->columnDefinition,
|
2009-07-08 19:25:41 +04:00
|
|
|
);
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03: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;
|
2010-03-15 20:19:00 +03:00
|
|
|
$mapping['inversedBy'] = $manyToManyAnnot->inversedBy;
|
2009-02-07 20:02:13 +03:00
|
|
|
$mapping['cascade'] = $manyToManyAnnot->cascade;
|
2011-02-05 11:31:40 +03:00
|
|
|
$mapping['indexBy'] = $manyToManyAnnot->indexBy;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch);
|
2010-02-14 22:38:22 +03:00
|
|
|
|
|
|
|
if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
|
|
|
|
$mapping['orderBy'] = $orderByAnnot->value;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapManyToMany($mapping);
|
2009-10-09 18:27:35 +04:00
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-15 20:19:00 +03:00
|
|
|
// Evaluate @HasLifecycleCallbacks annotation
|
2009-08-26 21:14:03 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) {
|
2009-07-18 22:06:30 +04:00
|
|
|
foreach ($class->getMethods() as $method) {
|
2011-03-05 01:00:54 +03:00
|
|
|
// filter for the declaring class only, callbacks from parents will already be registered.
|
|
|
|
if ($method->isPublic() && $method->getDeclaringClass()->getName() == $class->name) {
|
2009-07-18 22:06:30 +04:00
|
|
|
$annotations = $this->_reader->getMethodAnnotations($method);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-05-23 00:10:13 +04:00
|
|
|
// Compatibility with Doctrine Common 3.x
|
|
|
|
if ($annotations && is_int(key($annotations))) {
|
|
|
|
foreach ($annotations as $annot) {
|
|
|
|
$annotations[get_class($annot)] = $annot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
|
|
|
|
}
|
2011-10-23 19:39:16 +04:00
|
|
|
|
|
|
|
if (isset($annotations['Doctrine\ORM\Mapping\PreFlush'])) {
|
|
|
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preFlush);
|
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 20:34:44 +03:00
|
|
|
|
|
|
|
/**
|
2010-01-30 00:24:29 +03:00
|
|
|
* Whether the class with the specified name is transient. Only non-transient
|
|
|
|
* classes, that is entities and mapped superclasses, should have their metadata loaded.
|
|
|
|
* A class is non-transient if it is annotated with either @Entity or
|
2009-05-29 14:23:13 +04:00
|
|
|
* @MappedSuperclass in the class doc block.
|
2009-02-05 20:34:44 +03:00
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTransient($className)
|
|
|
|
{
|
2009-07-24 15:33:38 +04:00
|
|
|
$classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className));
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-05-23 00:10:13 +04:00
|
|
|
// Compatibility with Doctrine Common 3.x
|
|
|
|
if ($classAnnotations && is_int(key($classAnnotations))) {
|
|
|
|
foreach ($classAnnotations as $annot) {
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\Entity) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\MappedSuperclass) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) &&
|
2009-09-04 22:35:40 +04:00
|
|
|
! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
|
2009-02-05 20:34:44 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-16 00:06:32 +03:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getAllClassNames()
|
2009-05-30 13:37:56 +04:00
|
|
|
{
|
2010-02-12 01:47:59 +03:00
|
|
|
if ($this->_classNames !== null) {
|
|
|
|
return $this->_classNames;
|
|
|
|
}
|
|
|
|
|
2010-04-11 12:52:53 +04:00
|
|
|
if (!$this->_paths) {
|
2010-04-10 19:35:18 +04:00
|
|
|
throw MappingException::pathRequired();
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
$classes = array();
|
|
|
|
$includedFiles = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
foreach ($this->_paths as $path) {
|
|
|
|
if ( ! is_dir($path)) {
|
2010-09-15 23:14:15 +04:00
|
|
|
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
|
2010-04-10 19:35:18 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-06-17 02:54:50 +04:00
|
|
|
$iterator = new \RegexIterator(
|
|
|
|
new \RecursiveIteratorIterator(
|
|
|
|
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
|
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY
|
|
|
|
),
|
2011-12-20 01:56:19 +04:00
|
|
|
'/^.+' . str_replace('.', '\.', $this->_fileExtension) . '$/i',
|
2011-06-17 02:54:50 +04:00
|
|
|
\RecursiveRegexIterator::GET_MATCH
|
2010-04-10 19:35:18 +04:00
|
|
|
);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
foreach ($iterator as $file) {
|
2011-06-17 02:54:50 +04:00
|
|
|
$sourceFile = realpath($file[0]);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
require_once $sourceFile;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
$includedFiles[] = $sourceFile;
|
2010-01-28 15:46:12 +03:00
|
|
|
}
|
2010-04-10 19:35:18 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
$declared = get_declared_classes();
|
2009-10-08 22:54:39 +04:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
foreach ($declared as $className) {
|
|
|
|
$rc = new \ReflectionClass($className);
|
|
|
|
$sourceFile = $rc->getFileName();
|
|
|
|
if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
|
|
|
|
$classes[] = $className;
|
2010-01-28 15:46:12 +03:00
|
|
|
}
|
2009-10-08 02:14:13 +04:00
|
|
|
}
|
2010-02-12 01:47:59 +03:00
|
|
|
|
|
|
|
$this->_classNames = $classes;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-28 15:46:12 +03:00
|
|
|
return $classes;
|
2009-05-30 13:37:56 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-08 13:01:22 +04:00
|
|
|
/**
|
|
|
|
* Attempts to resolve the fetch mode.
|
|
|
|
*
|
|
|
|
* @param string $className The class name
|
|
|
|
* @param string $fetchMode The fetch mode
|
|
|
|
* @return integer The fetch mode as defined in ClassMetadata
|
|
|
|
* @throws MappingException If the fetch mode is not valid
|
|
|
|
*/
|
|
|
|
private function getFetchMode($className, $fetchMode)
|
|
|
|
{
|
|
|
|
if(!defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
|
|
|
|
throw MappingException::invalidFetchMode($className, $fetchMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode);
|
|
|
|
}
|
2010-04-10 19:35:18 +04:00
|
|
|
/**
|
|
|
|
* Factory method for the Annotation Driver
|
2011-05-23 00:10:13 +04:00
|
|
|
*
|
2010-04-10 19:35:18 +04:00
|
|
|
* @param array|string $paths
|
|
|
|
* @param AnnotationReader $reader
|
|
|
|
* @return AnnotationDriver
|
|
|
|
*/
|
|
|
|
static public function create($paths = array(), AnnotationReader $reader = null)
|
|
|
|
{
|
|
|
|
if ($reader == null) {
|
|
|
|
$reader = new AnnotationReader();
|
|
|
|
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
|
|
|
|
}
|
|
|
|
return new self($reader, $paths);
|
|
|
|
}
|
2010-03-19 02:04:21 +03:00
|
|
|
}
|