Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Archiver / ArchiverManager.php
1 <?php
2
3 namespace Drupal\Core\Archiver;
4
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;
9
10 /**
11  * Provides an Archiver plugin manager.
12  *
13  * @see \Drupal\Core\Archiver\Annotation\Archiver
14  * @see \Drupal\Core\Archiver\ArchiverInterface
15  * @see plugin_api
16  */
17 class ArchiverManager extends DefaultPluginManager {
18
19   /**
20    * Constructs a ArchiverManager object.
21    *
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.
29    */
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');
34   }
35
36   /**
37    * {@inheritdoc}
38    */
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']);
43   }
44
45   /**
46    * {@inheritdoc}
47    */
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);
59         }
60       }
61     }
62   }
63
64 }