Cache
5 years ago
Html
5 years ago
Http
5 years ago
Provider
4 years ago
ProviderCollection
5 years ago
Embera.php
4 years ago
Url.php
5 years ago
Embera.php
229 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Embera.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; |
| 14 | |
| 15 | use Embera\Http\HttpClient; |
| 16 | use Embera\Http\OembedClient; |
| 17 | use Embera\Http\HttpClientInterface; |
| 18 | use Embera\Html\IgnoreTags; |
| 19 | use Embera\Html\ResponsiveEmbeds; |
| 20 | use Embera\Provider\ProviderInterface; |
| 21 | use Embera\ProviderCollection\ProviderCollectionInterface; |
| 22 | use Embera\ProviderCollection\DefaultProviderCollection; |
| 23 | |
| 24 | /** |
| 25 | * Embera |
| 26 | * The Main Class of this library. |
| 27 | */ |
| 28 | class Embera |
| 29 | { |
| 30 | /** @var string Current Library Version */ |
| 31 | const VERSION = '2.0.16'; |
| 32 | |
| 33 | /** |
| 34 | * Constants describing how the library is |
| 35 | * going to handle fake responses. |
| 36 | * |
| 37 | * ALLOW_FAKE_RESPONSES is the default value. |
| 38 | */ |
| 39 | const ALLOW_FAKE_RESPONSES = 1; |
| 40 | const DISABLE_FAKE_RESPONSES = 2; |
| 41 | const ONLY_FAKE_RESPONSES = 3; |
| 42 | |
| 43 | /** @var array Configuration settings */ |
| 44 | protected $config = []; |
| 45 | |
| 46 | /** @var array Logged errors */ |
| 47 | protected $errors = []; |
| 48 | |
| 49 | /** @var array Closures to be used on oembed responses */ |
| 50 | protected $filters = []; |
| 51 | |
| 52 | /** @var ProviderCollectionInterface */ |
| 53 | protected $providerCollection; |
| 54 | |
| 55 | /** @var HttpClientInterface */ |
| 56 | protected $httpClient; |
| 57 | |
| 58 | /** |
| 59 | * Constructor |
| 60 | * |
| 61 | * @param array $config |
| 62 | * @param ProviderCollectionInterface $collection |
| 63 | * @param HttpClientInterface $httpClient |
| 64 | * @return void |
| 65 | */ |
| 66 | public function __construct(array $config = [], ProviderCollectionInterface $collection = null, HttpClientInterface $httpClient = null) |
| 67 | { |
| 68 | $this->config = array_merge([ |
| 69 | 'https_only' => false, |
| 70 | 'fake_responses' => self::ALLOW_FAKE_RESPONSES, |
| 71 | 'ignore_tags' => [ 'pre', 'code', 'a', 'img', 'iframe', 'oembed' ], |
| 72 | 'responsive' => false, |
| 73 | 'width' => 0, |
| 74 | 'height' => 0, |
| 75 | 'maxheight' => 0, |
| 76 | 'maxwidth' => 0, |
| 77 | ], $config); |
| 78 | |
| 79 | $this->config['maxwidth'] = max($this->config['width'], $this->config['maxwidth']); |
| 80 | $this->config['maxheight'] = max($this->config['height'], $this->config['maxheight']); |
| 81 | unset($this->config['height'], $this->config['width']); |
| 82 | |
| 83 | $this->providerCollection = $collection; |
| 84 | if (!$collection) { |
| 85 | $this->providerCollection = new DefaultProviderCollection($this->config); |
| 86 | } |
| 87 | |
| 88 | $this->httpClient = $httpClient; |
| 89 | if (!$httpClient) { |
| 90 | $this->httpClient = new HttpClient($this->config); |
| 91 | } |
| 92 | |
| 93 | // Set the config just in case. |
| 94 | $this->providerCollection->setConfig($this->config); |
| 95 | $this->httpClient->setConfig($this->config); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Embeds known/available services into the given text. |
| 100 | * |
| 101 | * @param string $text |
| 102 | * @return string |
| 103 | */ |
| 104 | public function autoEmbed($text) |
| 105 | { |
| 106 | if (is_string($text)) { |
| 107 | $table = []; |
| 108 | $providers = $this->getUrlData($text); |
| 109 | foreach ($providers as $url => $response) { |
| 110 | if (!empty($response['html'])) { |
| 111 | $table[$url] = $response['html']; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!empty($table)) { |
| 116 | |
| 117 | if (!empty($this->config['ignore_tags']) && strpos($text, '>') !== false) { |
| 118 | $ignoreTags = new IgnoreTags($this->config['ignore_tags']); |
| 119 | return $ignoreTags->replace($text, $table); |
| 120 | } |
| 121 | |
| 122 | return strtr($text, $table); |
| 123 | } |
| 124 | |
| 125 | } else { |
| 126 | $this->errors[] = 'For auto-embedding purposes, the input must be a string'; |
| 127 | } |
| 128 | |
| 129 | return $text; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the oembed response from the given data. |
| 134 | * |
| 135 | * @param array|string $urls An array with urls or a string with urls. |
| 136 | * @return array |
| 137 | */ |
| 138 | public function getUrlData($urls) |
| 139 | { |
| 140 | $return = []; |
| 141 | /** |
| 142 | * @var string $url |
| 143 | * @var ProviderInterface $provider |
| 144 | */ |
| 145 | foreach ($this->providerCollection->findProviders($urls) as $url => $provider) { |
| 146 | try { |
| 147 | if ( $provider->shouldSendRequest() ) { |
| 148 | $oembedClient = new OembedClient($this->config, $this->httpClient); |
| 149 | $response = $oembedClient->getResponseFrom($provider); |
| 150 | }else{ |
| 151 | $response = $provider->getStaticResponse(); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | if ($this->config['responsive'] && !$provider->hasResponsiveSupport()) { |
| 156 | $responsive = new ResponsiveEmbeds(); |
| 157 | $response = $responsive->transform($response); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | $return[$url] = $this->applyFilters($response); |
| 162 | |
| 163 | } catch (\Exception $e) { |
| 164 | $this->errors[] = $e->getMessage(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return array_filter($return); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Adds a filter to the oembed response |
| 173 | * |
| 174 | * @param callable $closure |
| 175 | * @return void |
| 176 | */ |
| 177 | public function addFilter(callable $closure) |
| 178 | { |
| 179 | $this->filters[] = $closure; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Applies registered filters/closures |
| 184 | * to the oembed response. |
| 185 | * |
| 186 | * @param array $response |
| 187 | * @return array |
| 188 | */ |
| 189 | protected function applyFilters(array $response) |
| 190 | { |
| 191 | foreach ($this->filters as $closure) { |
| 192 | $response = $closure($response); |
| 193 | } |
| 194 | |
| 195 | return $response; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Gets the last error found |
| 200 | * |
| 201 | * @return string |
| 202 | */ |
| 203 | public function getLastError() |
| 204 | { |
| 205 | return end($this->errors); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns an array with all the errors |
| 210 | * |
| 211 | * @return array |
| 212 | */ |
| 213 | public function getErrors() |
| 214 | { |
| 215 | return $this->errors; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Checks if there were errors |
| 220 | * |
| 221 | * @return bool |
| 222 | */ |
| 223 | public function hasErrors() |
| 224 | { |
| 225 | return (!empty($this->errors)); |
| 226 | } |
| 227 | |
| 228 | } |
| 229 |