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