vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadata.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 /*
       
     3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
     4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
     5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
     6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
     7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
     8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
     9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    14  *
       
    15  * This software consists of voluntary contributions made by many individuals
       
    16  * and is licensed under the LGPL. For more information, see
       
    17  * <http://www.doctrine-project.org>.
       
    18  */
       
    19 
       
    20 namespace Doctrine\ORM\Mapping;
       
    21 
       
    22 use ReflectionClass, ReflectionProperty;
       
    23 
       
    24 /**
       
    25  * A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
       
    26  * of an entity and it's associations.
       
    27  * 
       
    28  * Once populated, ClassMetadata instances are usually cached in a serialized form.
       
    29  *
       
    30  * <b>IMPORTANT NOTE:</b>
       
    31  *
       
    32  * The fields of this class are only public for 2 reasons:
       
    33  * 1) To allow fast READ access.
       
    34  * 2) To drastically reduce the size of a serialized instance (private/protected members
       
    35  *    get the whole class name, namespace inclusive, prepended to every property in
       
    36  *    the serialized representation).
       
    37  *
       
    38  * @author Roman Borschel <roman@code-factory.org>
       
    39  * @author Jonathan H. Wage <jonwage@gmail.com>
       
    40  * @since 2.0
       
    41  */
       
    42 class ClassMetadata extends ClassMetadataInfo
       
    43 {
       
    44     /**
       
    45      * The ReflectionProperty instances of the mapped class.
       
    46      *
       
    47      * @var array
       
    48      */
       
    49     public $reflFields = array();
       
    50     
       
    51     /**
       
    52      * The prototype from which new instances of the mapped class are created.
       
    53      * 
       
    54      * @var object
       
    55      */
       
    56     private $_prototype;
       
    57 
       
    58     /**
       
    59      * Initializes a new ClassMetadata instance that will hold the object-relational mapping
       
    60      * metadata of the class with the given name.
       
    61      *
       
    62      * @param string $entityName The name of the entity class the new instance is used for.
       
    63      */
       
    64     public function __construct($entityName)
       
    65     {
       
    66         $this->reflClass = new ReflectionClass($entityName);
       
    67         $this->namespace = $this->reflClass->getNamespaceName();
       
    68         $this->table['name'] = $this->reflClass->getShortName();
       
    69         parent::__construct($this->reflClass->getName()); // do not use $entityName, possible case-problems
       
    70     }
       
    71 
       
    72     /**
       
    73      * Gets the ReflectionPropertys of the mapped class.
       
    74      *
       
    75      * @return array An array of ReflectionProperty instances.
       
    76      */
       
    77     public function getReflectionProperties()
       
    78     {
       
    79         return $this->reflFields;
       
    80     }
       
    81 
       
    82     /**
       
    83      * Gets a ReflectionProperty for a specific field of the mapped class.
       
    84      *
       
    85      * @param string $name
       
    86      * @return ReflectionProperty
       
    87      */
       
    88     public function getReflectionProperty($name)
       
    89     {
       
    90         return $this->reflFields[$name];
       
    91     }
       
    92 
       
    93     /**
       
    94      * Gets the ReflectionProperty for the single identifier field.
       
    95      *
       
    96      * @return ReflectionProperty
       
    97      * @throws BadMethodCallException If the class has a composite identifier.
       
    98      */
       
    99     public function getSingleIdReflectionProperty()
       
   100     {
       
   101         if ($this->isIdentifierComposite) {
       
   102             throw new \BadMethodCallException("Class " . $this->name . " has a composite identifier.");
       
   103         }
       
   104         return $this->reflFields[$this->identifier[0]];
       
   105     }
       
   106     
       
   107     /**
       
   108      * Validates & completes the given field mapping.
       
   109      *
       
   110      * @param array $mapping  The field mapping to validated & complete.
       
   111      * @return array  The validated and completed field mapping.
       
   112      * 
       
   113      * @throws MappingException
       
   114      */
       
   115     protected function _validateAndCompleteFieldMapping(array &$mapping)
       
   116     {
       
   117         parent::_validateAndCompleteFieldMapping($mapping);
       
   118 
       
   119         // Store ReflectionProperty of mapped field
       
   120         $refProp = $this->reflClass->getProperty($mapping['fieldName']);
       
   121         $refProp->setAccessible(true);
       
   122         $this->reflFields[$mapping['fieldName']] = $refProp;
       
   123     }
       
   124 
       
   125     /**
       
   126      * Extracts the identifier values of an entity of this class.
       
   127      * 
       
   128      * For composite identifiers, the identifier values are returned as an array
       
   129      * with the same order as the field order in {@link identifier}.
       
   130      *
       
   131      * @param object $entity
       
   132      * @return array
       
   133      */
       
   134     public function getIdentifierValues($entity)
       
   135     {
       
   136         if ($this->isIdentifierComposite) {
       
   137             $id = array();
       
   138             foreach ($this->identifier as $idField) {
       
   139                 $value = $this->reflFields[$idField]->getValue($entity);
       
   140                 if ($value !== null) {
       
   141                     $id[$idField] = $value;
       
   142                 }
       
   143             }
       
   144             return $id;
       
   145         } else {
       
   146             $value = $this->reflFields[$this->identifier[0]]->getValue($entity);
       
   147             if ($value !== null) {
       
   148                 return array($this->identifier[0] => $value);
       
   149             }
       
   150             return array();
       
   151         }
       
   152     }
       
   153 
       
   154     /**
       
   155      * Populates the entity identifier of an entity.
       
   156      *
       
   157      * @param object $entity
       
   158      * @param mixed $id
       
   159      * @todo Rename to assignIdentifier()
       
   160      */
       
   161     public function setIdentifierValues($entity, array $id)
       
   162     {
       
   163         foreach ($id as $idField => $idValue) {
       
   164             $this->reflFields[$idField]->setValue($entity, $idValue);
       
   165         }
       
   166     }
       
   167 
       
   168     /**
       
   169      * Sets the specified field to the specified value on the given entity.
       
   170      *
       
   171      * @param object $entity
       
   172      * @param string $field
       
   173      * @param mixed $value
       
   174      */
       
   175     public function setFieldValue($entity, $field, $value)
       
   176     {
       
   177         $this->reflFields[$field]->setValue($entity, $value);
       
   178     }
       
   179 
       
   180     /**
       
   181      * Gets the specified field's value off the given entity.
       
   182      *
       
   183      * @param object $entity
       
   184      * @param string $field
       
   185      */
       
   186     public function getFieldValue($entity, $field)
       
   187     {
       
   188         return $this->reflFields[$field]->getValue($entity);
       
   189     }
       
   190 
       
   191     /**
       
   192      * Stores the association mapping.
       
   193      *
       
   194      * @param AssociationMapping $assocMapping
       
   195      */
       
   196     protected function _storeAssociationMapping(array $assocMapping)
       
   197     {
       
   198         parent::_storeAssociationMapping($assocMapping);
       
   199 
       
   200         // Store ReflectionProperty of mapped field
       
   201         $sourceFieldName = $assocMapping['fieldName'];
       
   202 
       
   203         $refProp = $this->reflClass->getProperty($sourceFieldName);
       
   204         $refProp->setAccessible(true);
       
   205         $this->reflFields[$sourceFieldName] = $refProp;
       
   206     }
       
   207 
       
   208     /**
       
   209      * Creates a string representation of this instance.
       
   210      *
       
   211      * @return string The string representation of this instance.
       
   212      * @todo Construct meaningful string representation.
       
   213      */
       
   214     public function __toString()
       
   215     {
       
   216         return __CLASS__ . '@' . spl_object_hash($this);
       
   217     }
       
   218     
       
   219     /**
       
   220      * Determines which fields get serialized.
       
   221      *
       
   222      * It is only serialized what is necessary for best unserialization performance.
       
   223      * That means any metadata properties that are not set or empty or simply have
       
   224      * their default value are NOT serialized.
       
   225      * 
       
   226      * Parts that are also NOT serialized because they can not be properly unserialized:
       
   227      *      - reflClass (ReflectionClass)
       
   228      *      - reflFields (ReflectionProperty array)
       
   229      * 
       
   230      * @return array The names of all the fields that should be serialized.
       
   231      */
       
   232     public function __sleep()
       
   233     {
       
   234         // This metadata is always serialized/cached.
       
   235         $serialized = array(
       
   236             'associationMappings',
       
   237             'columnNames', //TODO: Not really needed. Can use fieldMappings[$fieldName]['columnName']
       
   238             'fieldMappings',
       
   239             'fieldNames',
       
   240             'identifier',
       
   241             'isIdentifierComposite', // TODO: REMOVE
       
   242             'name',
       
   243             'namespace', // TODO: REMOVE
       
   244             'table',
       
   245             'rootEntityName',
       
   246             'idGenerator', //TODO: Does not really need to be serialized. Could be moved to runtime.
       
   247         );
       
   248 
       
   249         // The rest of the metadata is only serialized if necessary.
       
   250         if ($this->changeTrackingPolicy != self::CHANGETRACKING_DEFERRED_IMPLICIT) {
       
   251             $serialized[] = 'changeTrackingPolicy';
       
   252         }
       
   253 
       
   254         if ($this->customRepositoryClassName) {
       
   255             $serialized[] = 'customRepositoryClassName';
       
   256         }
       
   257 
       
   258         if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE) {
       
   259             $serialized[] = 'inheritanceType';
       
   260             $serialized[] = 'discriminatorColumn';
       
   261             $serialized[] = 'discriminatorValue';
       
   262             $serialized[] = 'discriminatorMap';
       
   263             $serialized[] = 'parentClasses';
       
   264             $serialized[] = 'subClasses';
       
   265         }
       
   266 
       
   267         if ($this->generatorType != self::GENERATOR_TYPE_NONE) {
       
   268             $serialized[] = 'generatorType';
       
   269             if ($this->generatorType == self::GENERATOR_TYPE_SEQUENCE) {
       
   270                 $serialized[] = 'sequenceGeneratorDefinition';
       
   271             }
       
   272         }
       
   273 
       
   274         if ($this->isMappedSuperclass) {
       
   275             $serialized[] = 'isMappedSuperclass';
       
   276         }
       
   277 
       
   278         if ($this->containsForeignIdentifier) {
       
   279             $serialized[] = 'containsForeignIdentifier';
       
   280         }
       
   281 
       
   282         if ($this->isVersioned) {
       
   283             $serialized[] = 'isVersioned';
       
   284             $serialized[] = 'versionField';
       
   285         }
       
   286 
       
   287         if ($this->lifecycleCallbacks) {
       
   288             $serialized[] = 'lifecycleCallbacks';
       
   289         }
       
   290 
       
   291         if ($this->namedQueries) {
       
   292             $serialized[] = 'namedQueries';
       
   293         }
       
   294 
       
   295         if ($this->isReadOnly) {
       
   296             $serialized[] = 'isReadOnly';
       
   297         }
       
   298 
       
   299         return $serialized;
       
   300     }
       
   301 
       
   302     /**
       
   303      * Restores some state that can not be serialized/unserialized.
       
   304      * 
       
   305      * @return void
       
   306      */
       
   307     public function __wakeup()
       
   308     {
       
   309         // Restore ReflectionClass and properties
       
   310         $this->reflClass = new ReflectionClass($this->name);
       
   311 
       
   312         foreach ($this->fieldMappings as $field => $mapping) {
       
   313             if (isset($mapping['declared'])) {
       
   314                 $reflField = new ReflectionProperty($mapping['declared'], $field);
       
   315             } else {
       
   316                 $reflField = $this->reflClass->getProperty($field);
       
   317             }
       
   318             $reflField->setAccessible(true);
       
   319             $this->reflFields[$field] = $reflField;
       
   320         }
       
   321 
       
   322         foreach ($this->associationMappings as $field => $mapping) {
       
   323             if (isset($mapping['declared'])) {
       
   324                 $reflField = new ReflectionProperty($mapping['declared'], $field);
       
   325             } else {
       
   326                 $reflField = $this->reflClass->getProperty($field);
       
   327             }
       
   328 
       
   329             $reflField->setAccessible(true);
       
   330             $this->reflFields[$field] = $reflField;
       
   331         }
       
   332     }
       
   333     
       
   334     /**
       
   335      * Creates a new instance of the mapped class, without invoking the constructor.
       
   336      * 
       
   337      * @return object
       
   338      */
       
   339     public function newInstance()
       
   340     {
       
   341         if ($this->_prototype === null) {
       
   342             $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
       
   343         }
       
   344         return clone $this->_prototype;
       
   345     }
       
   346 }