AnnotatedClassDiscovery example


    elseif (is_array($definition) && isset($definition['class'])) {
      $definition['class'] = ltrim($definition['class'], '\\');
    }
  }

  /** * {@inheritdoc} */
  protected function getDiscovery() {
    if (!$this->discovery) {
      $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
      $this->discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
    }
    return $this->discovery;
  }

  /** * {@inheritdoc} */
  protected function getFactory() {
    if (!$this->factory) {
      $this->factory = new ContainerFactory($this$this->pluginInterface);
    }
parent::__construct('Plugin/CKEditor5Plugin', $namespaces$module_handler, CKEditor5PluginInterface::class, CKEditor5Plugin::class);

    $this->alterInfo('ckeditor5_plugin_info');
    $this->setCacheBackend($cache_backend, 'ckeditor5_plugins');
  }

  /** * {@inheritdoc} */
  protected function getDiscovery() {
    if (!$this->discovery) {
      $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
      $discovery = new YamlDiscoveryDecorator($discovery, 'ckeditor5', $this->moduleHandler->getModuleDirectories());
      // Note: adding translatable properties here is impossible because it only       // supports top-level properties.       // @see \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition::label()       $discovery = new AnnotationBridgeDecorator($discovery$this->pluginDefinitionAnnotationName);
      $discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
      $this->discovery = $discovery;
    }
    return $this->discovery;
  }

  
'color' => 'pink',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\ExtendingNonInstalledClass',
        'provider' => 'plugin_test',
      ],
    ];

    $base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
    $base_directory2 = $this->root . '/core/modules/system/tests/modules/plugin_test_extended/src';
    $namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory, 'Drupal\plugin_test_extended' => $base_directory2]);

    $annotation_namespaces = ['Drupal\plugin_test\Plugin\Annotation', 'Drupal\plugin_test_extended\Plugin\Annotation'];
    $this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/fruit', $namespaces, 'Drupal\Component\Annotation\Plugin', $annotation_namespaces);
    $this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $namespaces);
  }

}
/** * Tests that entity type labels use sentence-case. */
  public function testEntityLabelCasing() {
    $base_directory = $this->root . '/core/modules/';
    $modules = scandir($base_directory);
    $paths = [];
    foreach ($modules as $module) {
      $paths["\Drupal\\{$module}\Entity"] = $base_directory . $module . '/src/';
    }
    $namespaces = new \ArrayObject($paths);
    $discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
    $definitions = $discovery->getDefinitions();

    foreach ($definitions as $definition) {
      /** @var \Drupal\Core\Entity\EntityType $definition */

      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $label = $definition->getLabel();
      $collection_label = $definition->getCollectionLabel();

      $label_string = $label->getUntranslatedString();
      $collection_label_string = $collection_label->getUntranslatedString();

      

  public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, TranslationInterface $string_translation, ClassResolverInterface $class_resolver, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
    parent::__construct('Entity', $namespaces$module_handler, 'Drupal\Core\Entity\EntityInterface');

    $this->setCacheBackend($cache, 'entity_type', ['entity_types']);
    $this->alterInfo('entity_type');

    $this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
    $this->stringTranslation = $string_translation;
    $this->classResolver = $class_resolver;
    $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
  }

  /** * {@inheritdoc} */
  public function processDefinition(&$definition$plugin_id) {
    /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
    parent::processDefinition($definition$plugin_id);

    
'id' => 'extending_non_installed_class',
        'label' => 'A plugin whose class is extending from a non-installed module class',
        'color' => 'pink',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\ExtendingNonInstalledClass',
        'provider' => 'plugin_test',
      ],
    ];

    $base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
    $namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory]);

    $this->discovery = new AnnotatedClassDiscovery('', $namespaces);
    $empty_namespaces = new \ArrayObject();
    $this->emptyDiscovery = new AnnotatedClassDiscovery('', $empty_namespaces);
  }

}
'example_2' => [
        'id' => 'example_2',
        'custom' => 'Paul',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\custom_annotation\Example2',
        'provider' => 'plugin_test',
      ],
    ];

    $base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
    $root_namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory]);

    $this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/custom_annotation', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
    $this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
  }

}

  protected function providerExists($provider) {
    return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);
  }

  /** * {@inheritdoc} */
  protected function getDiscovery() {
    if (!$this->discovery) {
      $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
      $discovery = new YamlDiscoveryDecorator($discovery, 'layouts', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories());
      $discovery
        ->addTranslatableProperty('label')
        ->addTranslatableProperty('description')
        ->addTranslatableProperty('category');
      $discovery = new AnnotationBridgeDecorator($discovery$this->pluginDefinitionAnnotationName);
      $discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
      $this->discovery = $discovery;
    }
    return $this->discovery;
  }

  
// Ensure the file cache is disabled.     FileCacheFactory::setConfiguration([FileCacheFactory::DISABLE_CACHE => TRUE]);
    // Ensure that FileCacheFactory has a prefix.     FileCacheFactory::setPrefix('prefix');
  }

  /** * @covers ::__construct * @covers ::getPluginNamespaces */
  public function testGetPluginNamespaces() {
    $discovery = new AnnotatedClassDiscovery(['com/example' => [__DIR__]]);

    $reflection = new \ReflectionMethod($discovery, 'getPluginNamespaces');
    $result = $reflection->invoke($discovery);
    $this->assertEquals(['com/example' => [__DIR__]]$result);
  }

  /** * @covers ::getDefinitions * @covers ::prepareAnnotationDefinition * @covers ::getAnnotationReader */
  
// Make a directory for discovery.     $url = vfsStream::url('root');
    mkdir($url . '/DrupalTest');

    // Create a class docblock with our annotation.     $php_file = "<?php\nnamespace DrupalTest;\n/**\n";
    $php_file .= " * @$annotation\n";
    $php_file .= " */\nclass TestClass {}";
    file_put_contents($url . '/DrupalTest/TestClass.php', $php_file);

    // Create an AnnotatedClassDiscovery object referencing the virtual file.     $discovery = new AnnotatedClassDiscovery(
      ['\\DrupalTest\\TestClass' => [vfsStream::url('root/DrupalTest')]], '\\DrupalTest\\Component\\Annotation\\'
    );

    // Register our class loader which will fail if the annotation reader tries     // to autoload disallowed annotations.     $class_loader = function D$class_name) use ($annotation) {
      $name_array = explode('\\', $class_name);
      $name = array_pop($name_array);
      if ($name == $annotation) {
        $this->fail('Attempted to autoload a non-plugin annotation: ' . $name);
      }
    };
/** * Tests that getDefinitions() retrieves the file cache correctly. * * @covers ::getDefinitions */
  public function testGetDefinitions() {
    // Path to the classes which we'll discover and parse annotation.     $discovery_path = __DIR__ . '/Fixtures';
    // File path that should be discovered within that directory.     $file_path = $discovery_path . '/PluginNamespace/DiscoveryTest1.php';

    $discovery = new AnnotatedClassDiscovery(['com\example' => [$discovery_path]]);
    $this->assertEquals([
      'discovery_test_1' => [
        'id' => 'discovery_test_1',
        'class' => 'com\example\PluginNamespace\DiscoveryTest1',
      ],
    ]$discovery->getDefinitions());

    // Gain access to the file cache so we can change it.     $ref_file_cache = new \ReflectionProperty($discovery, 'fileCache');
    /** @var \Drupal\Component\FileCache\FileCacheInterface $file_cache */
    $file_cache = $ref_file_cache->getValue($discovery);
    
Home | Imprint | This part of the site doesn't use cookies.