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