vendor/gedmo/doctrine-extensions/src/Mapping/Driver/AbstractAnnotationDriver.php line 148

  1. <?php
  2. /*
  3.  * This file is part of the Doctrine Behavioral Extensions package.
  4.  * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Gedmo\Mapping\Driver;
  9. use Doctrine\Common\Annotations\Reader;
  10. use Doctrine\Deprecations\Deprecation;
  11. use Doctrine\Persistence\Mapping\ClassMetadata;
  12. use Doctrine\Persistence\Mapping\Driver\MappingDriver;
  13. /**
  14.  * This is an abstract class to implement common functionality
  15.  * for extension annotation mapping drivers.
  16.  *
  17.  * @author Derek J. Lambert <dlambert@dereklambert.com>
  18.  */
  19. abstract class AbstractAnnotationDriver implements AttributeDriverInterface
  20. {
  21.     /**
  22.      * Annotation reader instance
  23.      *
  24.      * @var Reader|AttributeReader|object
  25.      *
  26.      * @todo Remove the support for the `object` type in the next major release.
  27.      */
  28.     protected $reader;
  29.     /**
  30.      * Original driver if it is available
  31.      *
  32.      * @var MappingDriver
  33.      */
  34.     protected $_originalDriver;
  35.     /**
  36.      * List of types which are valid for extension
  37.      *
  38.      * @var string[]
  39.      */
  40.     protected $validTypes = [];
  41.     /**
  42.      * Set the annotation reader instance
  43.      *
  44.      * When originally implemented, `Doctrine\Common\Annotations\Reader` was not available,
  45.      * therefore this method may accept any object implementing these methods from the interface:
  46.      *
  47.      *     getClassAnnotations([reflectionClass])
  48.      *     getClassAnnotation([reflectionClass], [name])
  49.      *     getPropertyAnnotations([reflectionProperty])
  50.      *     getPropertyAnnotation([reflectionProperty], [name])
  51.      *
  52.      * @param Reader|AttributeReader|object $reader
  53.      *
  54.      * @return void
  55.      *
  56.      * @note Providing any object is deprecated, as of 4.0 an {@see AttributeReader} will be required
  57.      */
  58.     public function setAnnotationReader($reader)
  59.     {
  60.         if ($reader instanceof Reader) {
  61.             Deprecation::trigger(
  62.                 'gedmo/doctrine-extensions',
  63.                 'https://github.com/doctrine-extensions/DoctrineExtensions/pull/2772',
  64.                 'Annotations support is deprecated, migrate your application to use attributes and pass an instance of %s to the %s() method instead.',
  65.                 AttributeReader::class,
  66.                 __METHOD__
  67.             );
  68.         } elseif (!$reader instanceof AttributeReader) {
  69.             Deprecation::trigger(
  70.                 'gedmo/doctrine-extensions',
  71.                 'https://github.com/doctrine-extensions/DoctrineExtensions/pull/2558',
  72.                 'Providing an annotation reader which does not implement %s or is not an instance of %s to %s() is deprecated.',
  73.                 Reader::class,
  74.                 AttributeReader::class,
  75.                 __METHOD__
  76.             );
  77.         }
  78.         $this->reader $reader;
  79.     }
  80.     /**
  81.      * Passes in the mapping read by original driver
  82.      *
  83.      * @param MappingDriver $driver
  84.      *
  85.      * @return void
  86.      */
  87.     public function setOriginalDriver($driver)
  88.     {
  89.         $this->_originalDriver $driver;
  90.     }
  91.     /**
  92.      * @param ClassMetadata<object> $meta
  93.      *
  94.      * @return \ReflectionClass<object>
  95.      */
  96.     public function getMetaReflectionClass($meta)
  97.     {
  98.         return $meta->getReflectionClass();
  99.     }
  100.     /**
  101.      * @param ClassMetadata<object> $meta
  102.      * @param array<string, mixed>  $config
  103.      *
  104.      * @return void
  105.      */
  106.     public function validateFullMetadata(ClassMetadata $meta, array $config)
  107.     {
  108.     }
  109.     /**
  110.      * Checks if $field type is valid
  111.      *
  112.      * @param ClassMetadata<object> $meta
  113.      * @param string                $field
  114.      *
  115.      * @return bool
  116.      */
  117.     protected function isValidField($meta$field)
  118.     {
  119.         $mapping $meta->getFieldMapping($field);
  120.         return $mapping && in_array($mapping->type ?? $mapping['type'], $this->validTypestrue);
  121.     }
  122.     /**
  123.      * Try to find out related class name out of mapping
  124.      *
  125.      * @param ClassMetadata<object> $metadata the mapped class metadata
  126.      * @param string                $name     the related object class name
  127.      *
  128.      * @return string related class name or empty string if does not exist
  129.      *
  130.      * @phpstan-param class-string|string $name
  131.      *
  132.      * @phpstan-return class-string|''
  133.      */
  134.     protected function getRelatedClassName($metadata$name)
  135.     {
  136.         if (class_exists($name) || interface_exists($name)) {
  137.             return $name;
  138.         }
  139.         $refl $metadata->getReflectionClass();
  140.         $ns $refl->getNamespaceName();
  141.         $className $ns.'\\'.$name;
  142.         return class_exists($className) ? $className '';
  143.     }
  144. }