PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / trunk
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more vtrunk
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / wpdevelopers / embera / src / Embera / ProviderCollection / ProviderCollectionAdapter.php
embedpress / vendor / wpdevelopers / embera / src / Embera / ProviderCollection Last commit date
CustomProviderCollection.php 5 years ago DefaultProviderCollection.php 9 months ago ProviderCollectionAdapter.php 9 months ago ProviderCollectionInterface.php 9 months ago SlimProviderCollection.php 9 months ago
ProviderCollectionAdapter.php
225 lines
1 <?php
2 /**
3 * ProviderCollectionAdapter.php
4 *
5 * @package Embera
6 * @author Michael Pratt <yo@michael-pratt.com>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Embera\ProviderCollection;
14
15 use ReflectionClass;
16
17 /**
18 * This is the adapter which has the main logic for
19 * the provider collection behaviour.
20 *
21 * Its main function is to accept urls as input and return
22 * matching providers.
23 */
24 abstract class ProviderCollectionAdapter implements ProviderCollectionInterface
25 {
26 /** @var array Configuration Settings */
27 protected $config = [];
28
29 /** @var array Hosts with wildcards, calculated at runtime */
30 protected $wildCardHosts = [];
31
32 /** @var array Massive array with the mapping of host -> provider relation. */
33 protected $providers = [];
34
35 /**
36 * Alias for the setConfig method
37 * @param array $config
38 */
39 public function __construct(array $config = [])
40 {
41 $this->setConfig($config);
42 }
43
44 /** inline {@inheritdoc} */
45 public function setConfig(array $config = [])
46 {
47 $this->config = array_merge([
48 'https_only' => false,
49 'url_extractor_regex' => '~\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_))~i',
50 ], $config);
51 }
52
53 /** inline {@inheritdoc} */
54 public function addProvider($host, $class)
55 {
56 $host = preg_replace('~^(?:www)\.~i', '', strtolower($host));
57 $this->providers[$host] = $class;
58 }
59
60 /** inline {@inheritdoc} */
61 public function filter($providerName)
62 {
63 if (is_callable($providerName)) {
64 $list = array_filter($this->providers, $providerName);
65 } else {
66 $list = array_filter($this->providers, static function ($class) use ($providerName) {
67 return (strtolower($providerName) == strtolower($class));
68 });
69 }
70
71 $newCollection = new static($this->config);
72 $newCollection->setProviderList($list);
73
74 return $newCollection;
75 }
76
77 /** inline {@inheritdoc} */
78 public function findProviders($data)
79 {
80 $urls = $this->extractUrls($data);
81 if (empty($urls)) {
82 return [];
83 }
84
85 $this->wildCardHosts = array_filter(array_keys($this->providers), static function($key) {
86 return (strpos($key, '*') !== false);
87 });
88
89 return $this->extractProviders(array_unique($urls));
90 }
91
92 /** inline {@inheritdoc} */
93 public function setProviderList(array $list)
94 {
95 $this->providers = $list;
96 }
97
98 /** inline {@inheritdoc} */
99 public function registerProvider($names, $prefix = true)
100 {
101 foreach ((array) $names as $name) {
102 if ($prefix) {
103 $name = $this->getProviderClassFqn($name);
104 }
105
106 $hosts = $name::getHosts();
107 foreach ($hosts as $h) {
108 $this->providers[$h] = $name;
109 }
110 }
111 }
112
113 /**
114 * Extract the urls from a given text or array
115 *
116 * @param mixed $data
117 * @return array with found urls
118 */
119 protected function extractUrls($data)
120 {
121 $regex = $this->config['url_extractor_regex'];
122
123 if (is_array($data)) {
124 return array_filter($data, static function ($arr) use ($regex) {
125 return preg_match($regex, $arr);
126 });
127 }
128
129 if (preg_match_all($regex, $data, $matches)) {
130 return array_filter($matches['0']);
131 }
132
133 return [];
134 }
135
136 /**
137 * Returns an array with supported providers
138 *
139 * @param array $urls An array with urls
140 * @return array An Array with loaded services
141 */
142 protected function extractProviders(array $urls = [])
143 {
144 $return = [];
145 foreach ($urls as $u) {
146 if ($provider = $this->getProvider($u)) {
147 $return[$u] = $provider;
148 }
149 }
150
151 return $return;
152 }
153
154 /**
155 * Gets a normalized host for the given $url
156 *
157 * @param string $url
158 * @return mixed The provider Object
159 */
160 protected function getProvider($url)
161 {
162 if ($data = parse_url($url)) {
163 $data = array_merge(['host' => ''], $data); // Just in case we didnt find a host
164 $host = preg_replace('~^(?:www|player)\.~i', '', strtolower($data['host']));
165 if (isset($this->providers[$host])) {
166 return $this->initializeProvider($this->providers[$host], $url);
167 } else if (isset($this->providers['*.' . $host])) {
168 return $this->initializeProvider($this->providers['*.' . $host], $url);
169 }
170
171 foreach ($this->wildCardHosts as $value) {
172 $regex = strtr(preg_quote($value, '~'), ['\*' => '(?:.*)']);
173 if (preg_match('~' . $regex . '~i', $host)) {
174 $this->providers[$host] = $this->providers[$value];
175 return $this->initializeProvider($this->providers[$value], $url);
176 }
177 }
178 }
179
180 return false;
181 }
182
183 /**
184 * Initializes a provider from the given $class and $url
185 *
186 * @param string $class The name of the class
187 * @param string $url The associated url
188 * @return object|bool
189 */
190 protected function initializeProvider($class, $url)
191 {
192 if (strpos($class, '\\') === false) {
193 $class = $this->getProviderClassFqn($class);
194 }
195
196 $reflection = new ReflectionClass($class);
197 $provider = $reflection->newInstance($url, $this->config);
198
199 if (!$provider->validateUrl($provider->getUrl(false)) ||
200 ($this->config['https_only'] && !$provider->hasHttpsSupport())) {
201 return false;
202 }
203
204 return $provider;
205 }
206
207 /**
208 * Get the full qualified class name for a given provider name. E.g. `YouTube` => `Embera\Provider\YouTube`
209 *
210 * @param string $name
211 * @return string
212 */
213 protected function getProviderClassFqn($name)
214 {
215 $providerFqn = explode('\\', __NAMESPACE__);
216 array_pop($providerFqn);
217
218 $providerFqn[] = 'Provider';
219 $providerFqn[] = $name;
220
221 return implode('\\', $providerFqn);
222 }
223
224 }
225