ArchiverManager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Core\Archiver;
  3. use Drupal\Component\Plugin\Factory\DefaultFactory;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\File\FileSystemInterface;
  7. use Drupal\Core\Plugin\DefaultPluginManager;
  8. /**
  9. * Provides an Archiver plugin manager.
  10. *
  11. * @see \Drupal\Core\Archiver\Annotation\Archiver
  12. * @see \Drupal\Core\Archiver\ArchiverInterface
  13. * @see plugin_api
  14. */
  15. class ArchiverManager extends DefaultPluginManager {
  16. /**
  17. * The file system service.
  18. *
  19. * @var \Drupal\Core\File\FileSystemInterface
  20. */
  21. protected $fileSystem;
  22. /**
  23. * Constructs a ArchiverManager object.
  24. *
  25. * @param \Traversable $namespaces
  26. * An object that implements \Traversable which contains the root paths
  27. * keyed by the corresponding namespace to look for plugin implementations.
  28. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  29. * Cache backend instance to use.
  30. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  31. * The module handler to invoke the alter hook with.
  32. * @param \Drupal\Core\File\FileSystemInterface $file_system
  33. * The file handler.
  34. */
  35. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system = NULL) {
  36. parent::__construct('Plugin/Archiver', $namespaces, $module_handler, 'Drupal\Core\Archiver\ArchiverInterface', 'Drupal\Core\Archiver\Annotation\Archiver');
  37. $this->alterInfo('archiver_info');
  38. $this->setCacheBackend($cache_backend, 'archiver_info_plugins');
  39. if (!isset($file_system)) {
  40. @trigger_error('Not defining the final $file_system argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.3 and will throw an error in drupal:10.0.0.', E_USER_DEPRECATED);
  41. $file_system = \Drupal::service('file_system');
  42. }
  43. $this->fileSystem = $file_system;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function createInstance($plugin_id, array $configuration = []) {
  49. $plugin_definition = $this->getDefinition($plugin_id);
  50. $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition, 'Drupal\Core\Archiver\ArchiverInterface');
  51. return new $plugin_class($this->fileSystem->realpath($configuration['filepath']));
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getInstance(array $options) {
  57. $filepath = $options['filepath'];
  58. foreach ($this->getDefinitions() as $plugin_id => $definition) {
  59. foreach ($definition['extensions'] as $extension) {
  60. // Because extensions may be multi-part, such as .tar.gz,
  61. // we cannot use simpler approaches like substr() or pathinfo().
  62. // This method isn't quite as clean but gets the job done.
  63. // Also note that the file may not yet exist, so we cannot rely
  64. // on fileinfo() or other disk-level utilities.
  65. if (strrpos($filepath, '.' . $extension) === strlen($filepath) - strlen('.' . $extension)) {
  66. return $this->createInstance($plugin_id, $options);
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Returns a string of supported archive extensions.
  73. *
  74. * @return string
  75. * A space-separated string of extensions suitable for use by the file
  76. * validation system.
  77. */
  78. public function getExtensions() {
  79. $valid_extensions = [];
  80. foreach ($this->getDefinitions() as $archive) {
  81. foreach ($archive['extensions'] as $extension) {
  82. foreach (explode('.', $extension) as $part) {
  83. if (!in_array($part, $valid_extensions)) {
  84. $valid_extensions[] = $part;
  85. }
  86. }
  87. }
  88. }
  89. return implode(' ', $valid_extensions);
  90. }
  91. }