ArchiverManager.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Plugin\DefaultPluginManager;
  7. /**
  8. * Provides an Archiver plugin manager.
  9. *
  10. * @see \Drupal\Core\Archiver\Annotation\Archiver
  11. * @see \Drupal\Core\Archiver\ArchiverInterface
  12. * @see plugin_api
  13. */
  14. class ArchiverManager extends DefaultPluginManager {
  15. /**
  16. * Constructs a ArchiverManager object.
  17. *
  18. * @param \Traversable $namespaces
  19. * An object that implements \Traversable which contains the root paths
  20. * keyed by the corresponding namespace to look for plugin implementations.
  21. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  22. * Cache backend instance to use.
  23. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  24. * The module handler to invoke the alter hook with.
  25. */
  26. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
  27. parent::__construct('Plugin/Archiver', $namespaces, $module_handler, 'Drupal\Core\Archiver\ArchiverInterface', 'Drupal\Core\Archiver\Annotation\Archiver');
  28. $this->alterInfo('archiver_info');
  29. $this->setCacheBackend($cache_backend, 'archiver_info_plugins');
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function createInstance($plugin_id, array $configuration = []) {
  35. $plugin_definition = $this->getDefinition($plugin_id);
  36. $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition, 'Drupal\Core\Archiver\ArchiverInterface');
  37. return new $plugin_class($configuration['filepath']);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getInstance(array $options) {
  43. $filepath = $options['filepath'];
  44. foreach ($this->getDefinitions() as $plugin_id => $definition) {
  45. foreach ($definition['extensions'] as $extension) {
  46. // Because extensions may be multi-part, such as .tar.gz,
  47. // we cannot use simpler approaches like substr() or pathinfo().
  48. // This method isn't quite as clean but gets the job done.
  49. // Also note that the file may not yet exist, so we cannot rely
  50. // on fileinfo() or other disk-level utilities.
  51. if (strrpos($filepath, '.' . $extension) === strlen($filepath) - strlen('.' . $extension)) {
  52. return $this->createInstance($plugin_id, $options);
  53. }
  54. }
  55. }
  56. }
  57. }