From 4ae7f8542f77e4eb063509e31a97d54860110a66 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Fri, 1 Mar 2024 14:26:17 +0000 Subject: [PATCH] Issue #3420985 by godotislate, larowlan, smustgrave, mstrelan: Convert Archiver discovery to attributes --- .../Core/Archiver/ArchiverInterface.php | 2 +- .../Drupal/Core/Archiver/ArchiverManager.php | 5 ++- .../Core/Archiver/Attribute/Archiver.php | 44 +++++++++++++++++++ .../system/src/Plugin/Archiver/Tar.php | 15 ++++--- .../system/src/Plugin/Archiver/Zip.php | 15 ++++--- .../Plugin/Archiver/UpdateTestArchiver.php | 14 +++--- 6 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 core/lib/Drupal/Core/Archiver/Attribute/Archiver.php diff --git a/core/lib/Drupal/Core/Archiver/ArchiverInterface.php b/core/lib/Drupal/Core/Archiver/ArchiverInterface.php index 979b93ac80bd..075c6f1da016 100644 --- a/core/lib/Drupal/Core/Archiver/ArchiverInterface.php +++ b/core/lib/Drupal/Core/Archiver/ArchiverInterface.php @@ -6,7 +6,7 @@ * Defines the common interface for all Archiver classes. * * @see \Drupal\Core\Archiver\ArchiverManager - * @see \Drupal\Core\Archiver\Annotation\Archiver + * @see \Drupal\Core\Archiver\Attribute\Archiver * @see plugin_api */ interface ArchiverInterface { diff --git a/core/lib/Drupal/Core/Archiver/ArchiverManager.php b/core/lib/Drupal/Core/Archiver/ArchiverManager.php index 08a6308a3edd..c33350f1eb9d 100644 --- a/core/lib/Drupal/Core/Archiver/ArchiverManager.php +++ b/core/lib/Drupal/Core/Archiver/ArchiverManager.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Archiver; use Drupal\Component\Plugin\Factory\DefaultFactory; +use Drupal\Core\Archiver\Attribute\Archiver; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\File\FileSystemInterface; @@ -11,7 +12,7 @@ /** * Provides an Archiver plugin manager. * - * @see \Drupal\Core\Archiver\Annotation\Archiver + * @see \Drupal\Core\Archiver\Attribute\Archiver * @see \Drupal\Core\Archiver\ArchiverInterface * @see plugin_api */ @@ -38,7 +39,7 @@ class ArchiverManager extends DefaultPluginManager { * The file handler. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) { - parent::__construct('Plugin/Archiver', $namespaces, $module_handler, 'Drupal\Core\Archiver\ArchiverInterface', 'Drupal\Core\Archiver\Annotation\Archiver'); + parent::__construct('Plugin/Archiver', $namespaces, $module_handler, 'Drupal\Core\Archiver\ArchiverInterface', Archiver::class, 'Drupal\Core\Archiver\Annotation\Archiver'); $this->alterInfo('archiver_info'); $this->setCacheBackend($cache_backend, 'archiver_info_plugins'); $this->fileSystem = $file_system; diff --git a/core/lib/Drupal/Core/Archiver/Attribute/Archiver.php b/core/lib/Drupal/Core/Archiver/Attribute/Archiver.php new file mode 100644 index 000000000000..438ba18f8319 --- /dev/null +++ b/core/lib/Drupal/Core/Archiver/Attribute/Archiver.php @@ -0,0 +1,44 @@ +<?php + +namespace Drupal\Core\Archiver\Attribute; + +use Drupal\Component\Plugin\Attribute\Plugin; +use Drupal\Core\StringTranslation\TranslatableMarkup; + +/** + * Defines an archiver attribute object. + * + * Plugin Namespace: Plugin\Archiver + * + * For a working example, see \Drupal\system\Plugin\Archiver\Zip + * + * @see \Drupal\Core\Archiver\ArchiverManager + * @see \Drupal\Core\Archiver\ArchiverInterface + * @see plugin_api + * @see hook_archiver_info_alter() + */ +#[\Attribute(\Attribute::TARGET_CLASS)] +class Archiver extends Plugin { + + /** + * Constructs an archiver plugin attribute object. + * + * @param string $id + * The archiver plugin ID. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title + * The human-readable name of the archiver plugin. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description + * The description of the archiver plugin. + * @param array $extensions + * An array of valid extensions for this archiver. + * @param string|null $deriver + * (optional) The deriver class. + */ + public function __construct( + public readonly string $id, + public readonly ?TranslatableMarkup $title = NULL, + public readonly ?TranslatableMarkup $description = NULL, + public readonly array $extensions = [], + public readonly ?string $deriver = NULL) {} + +} diff --git a/core/modules/system/src/Plugin/Archiver/Tar.php b/core/modules/system/src/Plugin/Archiver/Tar.php index 0b0594f73010..da2ac584a23d 100644 --- a/core/modules/system/src/Plugin/Archiver/Tar.php +++ b/core/modules/system/src/Plugin/Archiver/Tar.php @@ -2,17 +2,18 @@ namespace Drupal\system\Plugin\Archiver; +use Drupal\Core\Archiver\Attribute\Archiver; use Drupal\Core\Archiver\Tar as BaseTar; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Defines an archiver implementation for .tar files. - * - * @Archiver( - * id = "Tar", - * title = @Translation("Tar"), - * description = @Translation("Handles .tar files."), - * extensions = {"tar", "tgz", "tar.gz", "tar.bz2"} - * ) */ +#[Archiver( + id: 'Tar', + title: new TranslatableMarkup('Tar'), + description: new TranslatableMarkup('Handles .tar files.'), + extensions: ['tar', 'tgz', 'tar.gz', 'tar.bz2'] +)] class Tar extends BaseTar { } diff --git a/core/modules/system/src/Plugin/Archiver/Zip.php b/core/modules/system/src/Plugin/Archiver/Zip.php index 0bf695c5f61c..45c4c1358f30 100644 --- a/core/modules/system/src/Plugin/Archiver/Zip.php +++ b/core/modules/system/src/Plugin/Archiver/Zip.php @@ -2,19 +2,20 @@ namespace Drupal\system\Plugin\Archiver; +use Drupal\Core\Archiver\Attribute\Archiver; use Drupal\Core\Archiver\Zip as BaseZip; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Defines an archiver implementation for .zip files. * * @link http://php.net/zip - * - * @Archiver( - * id = "Zip", - * title = @Translation("Zip"), - * description = @Translation("Handles zip files."), - * extensions = {"zip"} - * ) */ +#[Archiver( + id: 'Zip', + title: new TranslatableMarkup('Zip'), + description: new TranslatableMarkup('Handles zip files.'), + extensions: ['zip'] +)] class Zip extends BaseZip { } diff --git a/core/modules/update/tests/modules/update_test/src/Plugin/Archiver/UpdateTestArchiver.php b/core/modules/update/tests/modules/update_test/src/Plugin/Archiver/UpdateTestArchiver.php index ffa4e46d7175..a63a1198fccc 100644 --- a/core/modules/update/tests/modules/update_test/src/Plugin/Archiver/UpdateTestArchiver.php +++ b/core/modules/update/tests/modules/update_test/src/Plugin/Archiver/UpdateTestArchiver.php @@ -3,16 +3,18 @@ namespace Drupal\update_test\Plugin\Archiver; use Drupal\Core\Archiver\ArchiverInterface; +use Drupal\Core\Archiver\Attribute\Archiver; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Defines a test archiver implementation. - * - * @Archiver( - * id = "update_test_archiver", - * title = @Translation("Update Test Archiver"), - * extensions = {"update-test-extension"} - * ) */ +#[Archiver( + id: 'update_test_archiver', + title: new TranslatableMarkup('Tar'), + description: new TranslatableMarkup('Update Test Archiver'), + extensions: ['update-test-extension'] +)] class UpdateTestArchiver implements ArchiverInterface { /** -- GitLab