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;
|
|
|
|
|
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,
|
2009-10-05 08:11:29 +04:00
|
|
|
Doctrine\ORM\Mapping\ClassMetadataInfo,
|
2009-07-30 19:16:02 +04:00
|
|
|
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
|
|
|
*
|
2010-01-28 15:46:12 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @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
|
|
|
|
*/
|
2009-07-15 20:31:59 +04:00
|
|
|
private $_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;
|
2010-01-28 22:38:16 +03: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.
|
|
|
|
*
|
2010-01-28 15:46:12 +03:00
|
|
|
* @param $reader The AnnotationReader to use.
|
2010-01-28 22:38:16 +03:00
|
|
|
* @param string|array $paths One or multiple paths where mapping classes can be found.
|
2009-07-15 20:31:59 +04:00
|
|
|
*/
|
2010-01-28 22:38:16 +03:00
|
|
|
public function __construct(AnnotationReader $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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2009-10-05 08:11:29 +04: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
|
|
|
|
2009-07-30 19:16:02 +04:00
|
|
|
// Evaluate Entity annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
|
|
|
|
$entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
|
|
|
|
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
|
|
|
|
} else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
|
|
|
|
$metadata->isMappedSuperclass = true;
|
|
|
|
} else {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate DoctrineTable 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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate DiscriminatorColumn annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) {
|
|
|
|
$discrColumnAnnot = $classAnnotations['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-08-17 21:58:16 +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
|
|
|
|
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
|
|
|
'onUpdate' => $joinColumnAnnot->onUpdate,
|
|
|
|
'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
|
|
|
'onUpdate' => $joinColumn->onUpdate,
|
|
|
|
'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')) {
|
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;
|
2009-10-12 16:54:14 +04:00
|
|
|
$mapping['fetch'] = constant('Doctrine\ORM\Mapping\AssociationMapping::FETCH_' . $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;
|
2009-07-23 13:52:16 +04:00
|
|
|
$mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval;
|
2009-10-12 16:54:14 +04:00
|
|
|
$mapping['fetch'] = constant('Doctrine\ORM\Mapping\AssociationMapping::FETCH_' . $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')) {
|
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;
|
2009-10-12 16:54:14 +04:00
|
|
|
$mapping['fetch'] = constant('Doctrine\ORM\Mapping\AssociationMapping::FETCH_' . $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
|
|
|
'onUpdate' => $joinColumn->onUpdate,
|
|
|
|
'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
|
|
|
'onUpdate' => $joinColumn->onUpdate,
|
|
|
|
'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;
|
2009-10-12 16:54:14 +04:00
|
|
|
$mapping['fetch'] = constant('Doctrine\ORM\Mapping\AssociationMapping::FETCH_' . $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) {
|
|
|
|
if ($method->isPublic()) {
|
|
|
|
$annotations = $this->_reader->getMethodAnnotations($method);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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)) {
|
|
|
|
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath();
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
$iterator = new \RecursiveIteratorIterator(
|
|
|
|
new \RecursiveDirectoryIterator($path),
|
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY
|
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
foreach ($iterator as $file) {
|
|
|
|
if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
|
|
|
|
continue;
|
2009-10-08 02:14:13 +04:00
|
|
|
}
|
2010-04-10 19:35:18 +04:00
|
|
|
|
|
|
|
$sourceFile = realpath($file->getPathName());
|
|
|
|
require_once $sourceFile;
|
|
|
|
$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
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
/**
|
|
|
|
* Factory method for the Annotation Driver
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|