diff --git a/src/Command/PluginManager.php b/src/Command/PluginManager.php index 65019b010..a320363c9 100644 --- a/src/Command/PluginManager.php +++ b/src/Command/PluginManager.php @@ -36,6 +36,7 @@ protected function generate(array &$vars, Assets $assets): void { $discovery_types = [ 'annotation' => 'Annotation', + 'attribute' => 'Attribute', 'yaml' => 'YAML', 'hook' => 'Hook', ]; @@ -53,6 +54,12 @@ protected function generate(array &$vars, Assets $assets): void { $assets->addFile('src/Plugin/{class_prefix}/Foo.php', 'annotation/src/Plugin/Example/Foo.php.twig'); break; + case 'attribute': + $assets->addFile('src/Attribute/{class_prefix}.php', 'attribute/src/Attribute/Example.php.twig'); + $assets->addFile('src/{class_prefix}PluginBase.php', 'attribute/src/ExamplePluginBase.php.twig'); + $assets->addFile('src/Plugin/{class_prefix}/Foo.php', 'attribute/src/Plugin/Example/Foo.php.twig'); + break; + case 'yaml': $assets->addFile('{machine_name}.{plugin_type|pluralize}.yml', 'yaml/model.examples.yml.twig'); $assets->addFile('src/{class_prefix}Default.php', 'yaml/src/ExampleDefault.php.twig'); diff --git a/templates/_plugin-manager/attribute/model.services.yml.twig b/templates/_plugin-manager/attribute/model.services.yml.twig new file mode 100644 index 000000000..65d61f802 --- /dev/null +++ b/templates/_plugin-manager/attribute/model.services.yml.twig @@ -0,0 +1,4 @@ +services: + plugin.manager.{{ plugin_type }}: + class: Drupal\{{ machine_name }}\{{ class_prefix }}PluginManager + parent: default_plugin_manager diff --git a/templates/_plugin-manager/attribute/src/Attribute/Example.php.twig b/templates/_plugin-manager/attribute/src/Attribute/Example.php.twig new file mode 100644 index 000000000..a2a3f66b5 --- /dev/null +++ b/templates/_plugin-manager/attribute/src/Attribute/Example.php.twig @@ -0,0 +1,23 @@ +pluginDefinition['label']; + } + +} diff --git a/templates/_plugin-manager/attribute/src/ExamplePluginManager.php.twig b/templates/_plugin-manager/attribute/src/ExamplePluginManager.php.twig new file mode 100644 index 000000000..f37893077 --- /dev/null +++ b/templates/_plugin-manager/attribute/src/ExamplePluginManager.php.twig @@ -0,0 +1,26 @@ +alterInfo('{{ plugin_type }}_info'); + $this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins'); + } + +} diff --git a/templates/_plugin-manager/attribute/src/Plugin/Example/Foo.php.twig b/templates/_plugin-manager/attribute/src/Plugin/Example/Foo.php.twig new file mode 100644 index 000000000..a045ec3f4 --- /dev/null +++ b/templates/_plugin-manager/attribute/src/Plugin/Example/Foo.php.twig @@ -0,0 +1,21 @@ +assertGeneratedFile('src/Plugin/Bar/Foo.php'); } + /** + * Test callback. + */ + public function testAttributeDiscovery(): void { + $input = [ + 'foo', + 'Foo', + 'bar', + 'Attribute', + ]; + $this->execute(PluginManager::class, $input); + + $expected_display = <<< 'TXT' + + Welcome to plugin-manager generator! + –––––––––––––––––––––––––––––––––––––– + + Module machine name: + ➤ + + Module name [Foo]: + ➤ + + Plugin type [foo]: + ➤ + + Discovery type [Annotation]: + [1] Annotation + [2] Attribute + [3] YAML + [4] Hook + ➤ + + The following directories and files have been created or updated: + ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– + • foo.info.yml + • foo.services.yml + • src/BarInterface.php + • src/BarPluginBase.php + • src/BarPluginManager.php + • src/Attribute/Bar.php + • src/Plugin/Bar/Foo.php + + TXT; + $this->assertDisplay($expected_display); + + $this->fixtureDir .= '/_attribute'; + $this->assertGeneratedFile('foo.services.yml'); + $this->assertGeneratedFile('src/BarInterface.php'); + $this->assertGeneratedFile('src/BarPluginBase.php'); + $this->assertGeneratedFile('src/BarPluginManager.php'); + $this->assertGeneratedFile('src/Attribute/Bar.php'); + $this->assertGeneratedFile('src/Plugin/Bar/Foo.php'); + } + /** * Test callback. */ @@ -94,8 +150,9 @@ public function testYamlDiscovery(): void { Discovery type [Annotation]: [1] Annotation - [2] YAML - [3] Hook + [2] Attribute + [3] YAML + [4] Hook ➤ The following directories and files have been created or updated: @@ -146,8 +203,9 @@ public function testHookDiscovery(): void { Discovery type [Annotation]: [1] Annotation - [2] YAML - [3] Hook + [2] Attribute + [3] YAML + [4] Hook ➤ The following directories and files have been created or updated: diff --git a/tests/functional/Generator/_plugin_manager/_attribute/foo.services.yml b/tests/functional/Generator/_plugin_manager/_attribute/foo.services.yml new file mode 100644 index 000000000..e26868ff1 --- /dev/null +++ b/tests/functional/Generator/_plugin_manager/_attribute/foo.services.yml @@ -0,0 +1,4 @@ +services: + plugin.manager.bar: + class: Drupal\foo\BarPluginManager + parent: default_plugin_manager diff --git a/tests/functional/Generator/_plugin_manager/_attribute/src/Attribute/Bar.php b/tests/functional/Generator/_plugin_manager/_attribute/src/Attribute/Bar.php new file mode 100644 index 000000000..5a35262f1 --- /dev/null +++ b/tests/functional/Generator/_plugin_manager/_attribute/src/Attribute/Bar.php @@ -0,0 +1,23 @@ +pluginDefinition['label']; + } + +} diff --git a/tests/functional/Generator/_plugin_manager/_attribute/src/BarPluginManager.php b/tests/functional/Generator/_plugin_manager/_attribute/src/BarPluginManager.php new file mode 100644 index 000000000..214909855 --- /dev/null +++ b/tests/functional/Generator/_plugin_manager/_attribute/src/BarPluginManager.php @@ -0,0 +1,24 @@ +alterInfo('bar_info'); + $this->setCacheBackend($cache_backend, 'bar_plugins'); + } + +} diff --git a/tests/functional/Generator/_plugin_manager/_attribute/src/Plugin/Bar/Foo.php b/tests/functional/Generator/_plugin_manager/_attribute/src/Plugin/Bar/Foo.php new file mode 100644 index 000000000..6dc49a1df --- /dev/null +++ b/tests/functional/Generator/_plugin_manager/_attribute/src/Plugin/Bar/Foo.php @@ -0,0 +1,19 @@ +