addNamespace example


  protected function getAnnotationReader() {
    if (!isset($this->annotationReader)) {
      $this->annotationReader = new SimpleAnnotationReader();

      // Add the namespaces from the main plugin annotation, like @EntityType.       $namespace = substr($this->pluginDefinitionAnnotationName, 0, strrpos($this->pluginDefinitionAnnotationName, '\\'));
      $this->annotationReader->addNamespace($namespace);

      // Register additional namespaces to be scanned for annotations.       foreach ($this->annotationNamespaces as $namespace) {
        $this->annotationReader->addNamespace($namespace);
      }
    }
    return $this->annotationReader;
  }

  /** * {@inheritdoc} */

    public function setOptions(array $options)
    {
        foreach ($options as $option => $value) {
            switch ($option) {
                case self::ESCAPE_FILTER:
                    $this->setDefaultEscapeFilter($value);
                    break;
                case self::INPUT_NAMESPACE:
                    $this->addNamespace($value);
                    break;
                case self::VALIDATOR_NAMESPACE:
                    if(is_string($value)) {
                        $value = array($value);
                    }

                    foreach($value AS $prefix) {
                        $this->addValidatorPrefixPath(
                                $prefix,
                                str_replace('_', DIRECTORY_SEPARATOR, $prefix)
                        );
                    }
$annots     = $parser->parse($docblock);

        $this->assertCount(1, $annots);
        $this->assertInstanceOf("Drupal\Tests\Component\Annotation\Doctrine\Ticket\Doctrine\ORM\Mapping\Entity", $annots[0]);
    }

    public function testIssueNamespaces()
    {
        $docblock   = "@Entity";
        $parser     = new DocParser();
        $parser->addNamespace("Drupal\Tests\Component\Annotation\Doctrine\Ticket\Doctrine\ORM");

        $annots     = $parser->parse($docblock);

        $this->assertCount(1, $annots);
        $this->assertInstanceOf("Drupal\Tests\Component\Annotation\Doctrine\Ticket\Doctrine\ORM\Entity", $annots[0]);
    }

    public function testIssueMultipleNamespaces()
    {
        $docblock   = "@Entity";
        $parser     = new DocParser();
        
$this->prefixes = [];
        $this->classmap = [];
        $this->files    = [];

        // We have to have one or the other, though we don't enforce the need         // to have both present in order to work.         if ($config->psr4 === [] && $config->classmap === []) {
            throw new InvalidArgumentException('Config array must contain either the \'psr4\' key or the \'classmap\' key.');
        }

        if ($config->psr4 !== []) {
            $this->addNamespace($config->psr4);
        }

        if ($config->classmap !== []) {
            $this->classmap = $config->classmap;
        }

        if ($config->files !== []) {
            $this->files = $config->files;
        }

        if (isset($config->helpers)) { // @phpstan-ignore-line


    /** * Adds a namespace in which we will look for annotations. * * @param string $namespace * * @return void */
    public function addNamespace($namespace)
    {
        $this->parser->addNamespace($namespace);
    }

    /** * {@inheritDoc} */
    public function getClassAnnotations(\ReflectionClass $class)
    {
        return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
    }

    /** * {@inheritDoc} */
/** * Ensure annotations can be ignored when namespaces are present. * * Drupal's DocParser should never use class_exists() on an ignored * annotation, including cases where namespaces are set. */
  public function testIgnoredAnnotationSkippedBeforeReflection() {
    $annotation = 'neverReflectThis';
    $parser = new DocParser();
    $parser->setIgnoredAnnotationNames([$annotation => TRUE]);
    $parser->addNamespace('\\Arbitrary\\Namespace');

    // Register our class loader which will fail if the parser tries to     // autoload disallowed annotations.     $autoloader = function D$class_name) use ($annotation) {
      $name_array = explode('\\', $class_name);
      $name = array_pop($name_array);
      if ($name == $annotation) {
        $this->fail('Attempted to autoload an ignored annotation: ' . $name);
      }
    };
    spl_autoload_register($autoloader, TRUE, TRUE);
    
parent::__construct($plugin_namespaces$plugin_definition_annotation_name$annotation_namespaces);
  }

  /** * {@inheritdoc} */
  protected function getAnnotationReader() {
    if (!isset($this->annotationReader)) {
      $reader = parent::getAnnotationReader();

      // Add the Core annotation classes like @Translation.       $reader->addNamespace('Drupal\Core\Annotation');
      $this->annotationReader = $reader;
    }
    return $this->annotationReader;
  }

  /** * {@inheritdoc} */
  protected function prepareAnnotationDefinition(AnnotationInterface $annotation$class) {
    parent::prepareAnnotationDefinition($annotation$class);

    


    /** * Adds a namespace in which we will look for annotations. * * @param string $namespace * * @return void */
    public function addNamespace($namespace)
    {
        $this->parser->addNamespace($namespace);
    }

    /** * {@inheritDoc} */
    public function getClassAnnotations(ReflectionClass $class)
    {
        return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
    }

    /** * {@inheritDoc} */
Home | Imprint | This part of the site doesn't use cookies.