Further modules included.
[yaffs-website] / web / modules / contrib / video / src / ProviderManager.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\video\ProviderManager.
6  */
7
8 namespace Drupal\video;
9
10 use Drupal\Component\Plugin\Mapper\MapperInterface;
11 use Drupal\Core\Cache\CacheBackendInterface;
12 use Drupal\Core\Extension\ModuleHandlerInterface;
13 use Drupal\Core\Plugin\DefaultPluginManager;
14
15 /**
16  * Gathers the provider plugins.
17  */
18 class ProviderManager extends DefaultPluginManager implements ProviderManagerInterface, MapperInterface {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
24     parent::__construct('Plugin/video/Provider', $namespaces, $module_handler, 'Drupal\video\ProviderPluginInterface', 'Drupal\video\Annotation\VideoEmbeddableProvider');
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function getProvidersOptionList() {
31     $options = [];
32     foreach ($this->getDefinitions() as $definition) {
33       $options[$definition['id']] = $definition['label'];
34     }
35     return $options;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function loadDefinitionsFromOptionList($options) {
42     $definitions = [];
43     // When no options are selected, all plugins are applicable.
44     if (count(array_keys($options, '0')) == count($options) || empty($options)) {
45       return $this->getDefinitions();
46     }
47     else {
48       foreach ($options as $provider_id => $enabled) {
49         if ($enabled) {
50           $definitions[$provider_id] = $this->getDefinition($provider_id);
51         }
52       }
53     }
54     return $definitions;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function loadApplicableDefinitionMatches(array $definitions, $user_input) {
61     foreach ($definitions as $definition) {
62       foreach($definition['regular_expressions'] as $reqular_expr){
63         if (preg_match($reqular_expr, $user_input, $matches)) {
64           return array('definition' => $definition, 'matches' => $matches);
65         }
66       }
67     }
68     return FALSE;
69   }
70   
71   /**
72    * {@inheritdoc}
73    */
74   public function loadProviderFromStream($stream, $file, $metadata = array(), $settings = array()) {
75     $definitions = $this->getDefinitions();
76     foreach ($definitions as $definition) {
77       if($definition['stream_wrapper'] == $stream){
78         return $definition ? $this->createInstance($definition['id'], ['file' => $file, 'metadata' => $metadata, 'settings' => $settings]) : FALSE;
79       }
80     }
81   }
82 }