embedpress
/
vendor
/
wpdevelopers
/
embera
/
src
/
Embera
/
ProviderCollection
/
ProviderCollectionAdapter.php
CustomProviderCollection.php
5 years ago
DefaultProviderCollection.php
5 years ago
ProviderCollectionAdapter.php
5 years ago
ProviderCollectionInterface.php
5 years ago
SlimProviderCollection.php
5 years ago
ProviderCollectionAdapter.php
208 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 = 'Embera\Provider\\' . $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 = 'Embera\Provider\\' . $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 |