You are here

abstract class AnnotationDriver in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/AnnotationDriver.php \Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver

The AnnotationDriver reads the mapping metadata from docblock annotations.

@since 2.2 @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>

Hierarchy

Expanded class hierarchy of AnnotationDriver

1 file declares its use of AnnotationDriver
AnnotationDriverTest.php in vendor/doctrine/common/tests/Doctrine/Tests/Common/Persistence/Mapping/AnnotationDriverTest.php

File

vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/AnnotationDriver.php, line 34

Namespace

Doctrine\Common\Persistence\Mapping\Driver
View source
abstract class AnnotationDriver implements MappingDriver {

  /**
   * The AnnotationReader.
   *
   * @var AnnotationReader
   */
  protected $reader;

  /**
   * The paths where to look for mapping files.
   *
   * @var array
   */
  protected $paths = array();

  /**
   * The paths excluded from path where to look for mapping files.
   *
   * @var array
   */
  protected $excludePaths = array();

  /**
   * The file extension of mapping documents.
   *
   * @var string
   */
  protected $fileExtension = '.php';

  /**
   * Cache for AnnotationDriver#getAllClassNames().
   *
   * @var array|null
   */
  protected $classNames;

  /**
   * Name of the entity annotations as keys.
   *
   * @var array
   */
  protected $entityAnnotationClasses = array();

  /**
   * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
   * docblock annotations.
   *
   * @param AnnotationReader  $reader The AnnotationReader to use, duck-typed.
   * @param string|array|null $paths  One or multiple paths where mapping classes can be found.
   */
  public function __construct($reader, $paths = null) {
    $this->reader = $reader;
    if ($paths) {
      $this
        ->addPaths((array) $paths);
    }
  }

  /**
   * Appends lookup paths to metadata driver.
   *
   * @param array $paths
   *
   * @return void
   */
  public function addPaths(array $paths) {
    $this->paths = array_unique(array_merge($this->paths, $paths));
  }

  /**
   * Retrieves the defined metadata lookup paths.
   *
   * @return array
   */
  public function getPaths() {
    return $this->paths;
  }

  /**
   * Append exclude lookup paths to metadata driver.
   *
   * @param array $paths
   */
  public function addExcludePaths(array $paths) {
    $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
  }

  /**
   * Retrieve the defined metadata lookup exclude paths.
   *
   * @return array
   */
  public function getExcludePaths() {
    return $this->excludePaths;
  }

  /**
   * Retrieve the current annotation reader
   *
   * @return AnnotationReader
   */
  public function getReader() {
    return $this->reader;
  }

  /**
   * Gets the file extension used to look for mapping files under.
   *
   * @return string
   */
  public function getFileExtension() {
    return $this->fileExtension;
  }

  /**
   * Sets 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;
  }

  /**
   * Returns 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 an annotation
   * from the {@see AnnotationDriver::entityAnnotationClasses}.
   *
   * @param string $className
   *
   * @return boolean
   */
  public function isTransient($className) {
    $classAnnotations = $this->reader
      ->getClassAnnotations(new \ReflectionClass($className));
    foreach ($classAnnotations as $annot) {
      if (isset($this->entityAnnotationClasses[get_class($annot)])) {
        return false;
      }
    }
    return true;
  }

  /**
   * {@inheritDoc}
   */
  public function getAllClassNames() {
    if ($this->classNames !== null) {
      return $this->classNames;
    }
    if (!$this->paths) {
      throw MappingException::pathRequired();
    }
    $classes = array();
    $includedFiles = array();
    foreach ($this->paths as $path) {
      if (!is_dir($path)) {
        throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
      }
      $iterator = new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY), '/^.+' . preg_quote($this->fileExtension) . '$/i', \RecursiveRegexIterator::GET_MATCH);
      foreach ($iterator as $file) {
        $sourceFile = $file[0];
        if (!preg_match('(^phar:)i', $sourceFile)) {
          $sourceFile = realpath($sourceFile);
        }
        foreach ($this->excludePaths as $excludePath) {
          $exclude = str_replace('\\', '/', realpath($excludePath));
          $current = str_replace('\\', '/', $sourceFile);
          if (strpos($current, $exclude) !== false) {
            continue 2;
          }
        }
        require_once $sourceFile;
        $includedFiles[] = $sourceFile;
      }
    }
    $declared = get_declared_classes();
    foreach ($declared as $className) {
      $rc = new \ReflectionClass($className);
      $sourceFile = $rc
        ->getFileName();
      if (in_array($sourceFile, $includedFiles) && !$this
        ->isTransient($className)) {
        $classes[] = $className;
      }
    }
    $this->classNames = $classes;
    return $classes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotationDriver::$classNames protected property Cache for AnnotationDriver#getAllClassNames().
AnnotationDriver::$entityAnnotationClasses protected property Name of the entity annotations as keys. 1
AnnotationDriver::$excludePaths protected property The paths excluded from path where to look for mapping files.
AnnotationDriver::$fileExtension protected property The file extension of mapping documents.
AnnotationDriver::$paths protected property The paths where to look for mapping files.
AnnotationDriver::$reader protected property The AnnotationReader.
AnnotationDriver::addExcludePaths public function Append exclude lookup paths to metadata driver.
AnnotationDriver::addPaths public function Appends lookup paths to metadata driver.
AnnotationDriver::getAllClassNames public function Gets the names of all mapped classes known to this driver. Overrides MappingDriver::getAllClassNames
AnnotationDriver::getExcludePaths public function Retrieve the defined metadata lookup exclude paths.
AnnotationDriver::getFileExtension public function Gets the file extension used to look for mapping files under.
AnnotationDriver::getPaths public function Retrieves the defined metadata lookup paths.
AnnotationDriver::getReader public function Retrieve the current annotation reader
AnnotationDriver::isTransient public function Returns whether the class with the specified name is transient. Only non-transient classes, that is entities and mapped superclasses, should have their metadata loaded. Overrides MappingDriver::isTransient
AnnotationDriver::setFileExtension public function Sets the file extension used to look for mapping files under.
AnnotationDriver::__construct public function Initializes a new AnnotationDriver that uses the given AnnotationReader for reading docblock annotations.
MappingDriver::loadMetadataForClass public function Loads the metadata for the specified class into the provided container. 5