Mercurial > hg > isophonics-drupal-site
comparison vendor/zendframework/zend-feed/src/Reader/Reader.php @ 12:7a779792577d
Update Drupal core to v8.4.5 (via Composer)
author | Chris Cannam |
---|---|
date | Fri, 23 Feb 2018 15:52:07 +0000 |
parents | 4c8ae668cc8c |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
11:bfffd8d7479a | 12:7a779792577d |
---|---|
10 namespace Zend\Feed\Reader; | 10 namespace Zend\Feed\Reader; |
11 | 11 |
12 use DOMDocument; | 12 use DOMDocument; |
13 use DOMXPath; | 13 use DOMXPath; |
14 use Zend\Cache\Storage\StorageInterface as CacheStorage; | 14 use Zend\Cache\Storage\StorageInterface as CacheStorage; |
15 use Zend\Feed\Reader\Exception\InvalidHttpClientException; | |
15 use Zend\Http as ZendHttp; | 16 use Zend\Http as ZendHttp; |
16 use Zend\Stdlib\ErrorHandler; | 17 use Zend\Stdlib\ErrorHandler; |
17 use Zend\Feed\Reader\Exception\InvalidHttpClientException; | |
18 | 18 |
19 /** | 19 /** |
20 */ | 20 */ |
21 class Reader implements ReaderImportInterface | 21 class Reader implements ReaderImportInterface |
22 { | 22 { |
220 $headers['If-Modified-Since'] = [$lastModified]; | 220 $headers['If-Modified-Since'] = [$lastModified]; |
221 } | 221 } |
222 } | 222 } |
223 $response = $client->get($uri, $headers); | 223 $response = $client->get($uri, $headers); |
224 if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 304) { | 224 if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 304) { |
225 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode()); | 225 throw new Exception\RuntimeException( |
226 'Feed failed to load, got response code ' . $response->getStatusCode() | |
227 ); | |
226 } | 228 } |
227 if ($response->getStatusCode() == 304) { | 229 if ($response->getStatusCode() == 304) { |
228 $responseXml = $data; | 230 $responseXml = $data; |
229 } else { | 231 } else { |
230 $responseXml = $response->getBody(); | 232 $responseXml = $response->getBody(); |
245 if ($data) { | 247 if ($data) { |
246 return static::importString($data); | 248 return static::importString($data); |
247 } | 249 } |
248 $response = $client->get($uri); | 250 $response = $client->get($uri); |
249 if ((int) $response->getStatusCode() !== 200) { | 251 if ((int) $response->getStatusCode() !== 200) { |
250 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode()); | 252 throw new Exception\RuntimeException( |
253 'Feed failed to load, got response code ' . $response->getStatusCode() | |
254 ); | |
251 } | 255 } |
252 $responseXml = $response->getBody(); | 256 $responseXml = $response->getBody(); |
253 $cache->setItem($cacheId, $responseXml); | 257 $cache->setItem($cacheId, $responseXml); |
254 return static::importString($responseXml); | 258 return static::importString($responseXml); |
255 } else { | 259 } else { |
256 $response = $client->get($uri); | 260 $response = $client->get($uri); |
257 if ((int) $response->getStatusCode() !== 200) { | 261 if ((int) $response->getStatusCode() !== 200) { |
258 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode()); | 262 throw new Exception\RuntimeException( |
263 'Feed failed to load, got response code ' . $response->getStatusCode() | |
264 ); | |
259 } | 265 } |
260 $reader = static::importString($response->getBody()); | 266 $reader = static::importString($response->getBody()); |
261 $reader->setOriginalSourceUri($uri); | 267 $reader->setOriginalSourceUri($uri); |
262 return $reader; | 268 return $reader; |
263 } | 269 } |
287 (is_object($response) ? get_class($response) : gettype($response)) | 293 (is_object($response) ? get_class($response) : gettype($response)) |
288 )); | 294 )); |
289 } | 295 } |
290 | 296 |
291 if ((int) $response->getStatusCode() !== 200) { | 297 if ((int) $response->getStatusCode() !== 200) { |
292 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode()); | 298 throw new Exception\RuntimeException( |
299 'Feed failed to load, got response code ' . $response->getStatusCode() | |
300 ); | |
293 } | 301 } |
294 $reader = static::importString($response->getBody()); | 302 $reader = static::importString($response->getBody()); |
295 $reader->setOriginalSourceUri($uri); | 303 $reader->setOriginalSourceUri($uri); |
296 return $reader; | 304 return $reader; |
297 } | 305 } |
305 * @throws Exception\RuntimeException | 313 * @throws Exception\RuntimeException |
306 */ | 314 */ |
307 public static function importString($string) | 315 public static function importString($string) |
308 { | 316 { |
309 $trimmed = trim($string); | 317 $trimmed = trim($string); |
310 if (!is_string($string) || empty($trimmed)) { | 318 if (! is_string($string) || empty($trimmed)) { |
311 throw new Exception\InvalidArgumentException('Only non empty strings are allowed as input'); | 319 throw new Exception\InvalidArgumentException('Only non empty strings are allowed as input'); |
312 } | 320 } |
313 | 321 |
314 $libxmlErrflag = libxml_use_internal_errors(true); | 322 $libxmlErrflag = libxml_use_internal_errors(true); |
315 $oldValue = libxml_disable_entity_loader(true); | 323 $oldValue = libxml_disable_entity_loader(true); |
323 } | 331 } |
324 } | 332 } |
325 libxml_disable_entity_loader($oldValue); | 333 libxml_disable_entity_loader($oldValue); |
326 libxml_use_internal_errors($libxmlErrflag); | 334 libxml_use_internal_errors($libxmlErrflag); |
327 | 335 |
328 if (!$status) { | 336 if (! $status) { |
329 // Build error message | 337 // Build error message |
330 $error = libxml_get_last_error(); | 338 $error = libxml_get_last_error(); |
331 if ($error && $error->message) { | 339 if ($error && $error->message) { |
332 $error->message = trim($error->message); | 340 $error->message = trim($error->message); |
333 $errormsg = "DOMDocument cannot parse XML: {$error->message}"; | 341 $errormsg = "DOMDocument cannot parse XML: {$error->message}"; |
382 public static function findFeedLinks($uri) | 390 public static function findFeedLinks($uri) |
383 { | 391 { |
384 $client = static::getHttpClient(); | 392 $client = static::getHttpClient(); |
385 $response = $client->get($uri); | 393 $response = $client->get($uri); |
386 if ($response->getStatusCode() !== 200) { | 394 if ($response->getStatusCode() !== 200) { |
387 throw new Exception\RuntimeException("Failed to access $uri, got response code " . $response->getStatusCode()); | 395 throw new Exception\RuntimeException( |
396 "Failed to access $uri, got response code " . $response->getStatusCode() | |
397 ); | |
388 } | 398 } |
389 $responseHtml = $response->getBody(); | 399 $responseHtml = $response->getBody(); |
390 $libxmlErrflag = libxml_use_internal_errors(true); | 400 $libxmlErrflag = libxml_use_internal_errors(true); |
391 $oldValue = libxml_disable_entity_loader(true); | 401 $oldValue = libxml_disable_entity_loader(true); |
392 $dom = new DOMDocument; | 402 $dom = new DOMDocument; |
393 $status = $dom->loadHTML(trim($responseHtml)); | 403 $status = $dom->loadHTML(trim($responseHtml)); |
394 libxml_disable_entity_loader($oldValue); | 404 libxml_disable_entity_loader($oldValue); |
395 libxml_use_internal_errors($libxmlErrflag); | 405 libxml_use_internal_errors($libxmlErrflag); |
396 if (!$status) { | 406 if (! $status) { |
397 // Build error message | 407 // Build error message |
398 $error = libxml_get_last_error(); | 408 $error = libxml_get_last_error(); |
399 if ($error && $error->message) { | 409 if ($error && $error->message) { |
400 $error->message = trim($error->message); | 410 $error->message = trim($error->message); |
401 $errormsg = "DOMDocument cannot parse HTML: {$error->message}"; | 411 $errormsg = "DOMDocument cannot parse HTML: {$error->message}"; |
423 { | 433 { |
424 if ($feed instanceof Feed\AbstractFeed) { | 434 if ($feed instanceof Feed\AbstractFeed) { |
425 $dom = $feed->getDomDocument(); | 435 $dom = $feed->getDomDocument(); |
426 } elseif ($feed instanceof DOMDocument) { | 436 } elseif ($feed instanceof DOMDocument) { |
427 $dom = $feed; | 437 $dom = $feed; |
428 } elseif (is_string($feed) && !empty($feed)) { | 438 } elseif (is_string($feed) && ! empty($feed)) { |
429 ErrorHandler::start(E_NOTICE|E_WARNING); | 439 ErrorHandler::start(E_NOTICE | E_WARNING); |
430 ini_set('track_errors', 1); | 440 ini_set('track_errors', 1); |
431 $oldValue = libxml_disable_entity_loader(true); | 441 $oldValue = libxml_disable_entity_loader(true); |
432 $dom = new DOMDocument; | 442 $dom = new DOMDocument; |
433 $status = $dom->loadXML($feed); | 443 $status = $dom->loadXML($feed); |
434 foreach ($dom->childNodes as $child) { | 444 foreach ($dom->childNodes as $child) { |
439 } | 449 } |
440 } | 450 } |
441 libxml_disable_entity_loader($oldValue); | 451 libxml_disable_entity_loader($oldValue); |
442 ini_restore('track_errors'); | 452 ini_restore('track_errors'); |
443 ErrorHandler::stop(); | 453 ErrorHandler::stop(); |
444 if (!$status) { | 454 if (! $status) { |
445 if (!isset($phpErrormsg)) { | 455 if (! isset($phpErrormsg)) { |
446 if (function_exists('xdebug_is_enabled')) { | 456 if (function_exists('xdebug_is_enabled')) { |
447 $phpErrormsg = '(error message not available, when XDebug is running)'; | 457 $phpErrormsg = '(error message not available, when XDebug is running)'; |
448 } else { | 458 } else { |
449 $phpErrormsg = '(error message not available)'; | 459 $phpErrormsg = '(error message not available)'; |
450 } | 460 } |
550 * | 560 * |
551 * @return ExtensionManagerInterface | 561 * @return ExtensionManagerInterface |
552 */ | 562 */ |
553 public static function getExtensionManager() | 563 public static function getExtensionManager() |
554 { | 564 { |
555 if (!isset(static::$extensionManager)) { | 565 if (! isset(static::$extensionManager)) { |
556 static::setExtensionManager(new StandaloneExtensionManager()); | 566 static::setExtensionManager(new StandaloneExtensionManager()); |
557 } | 567 } |
558 return static::$extensionManager; | 568 return static::$extensionManager; |
559 } | 569 } |
560 | 570 |
574 if ($manager->has($feedName) || $manager->has($entryName)) { | 584 if ($manager->has($feedName) || $manager->has($entryName)) { |
575 return; | 585 return; |
576 } | 586 } |
577 } | 587 } |
578 | 588 |
579 if (!$manager->has($feedName) && !$manager->has($entryName)) { | 589 if (! $manager->has($feedName) && ! $manager->has($entryName)) { |
580 throw new Exception\RuntimeException('Could not load extension: ' . $name | 590 throw new Exception\RuntimeException('Could not load extension: ' . $name |
581 . ' using Plugin Loader. Check prefix paths are configured and extension exists.'); | 591 . ' using Plugin Loader. Check prefix paths are configured and extension exists.'); |
582 } | 592 } |
583 if ($manager->has($feedName)) { | 593 if ($manager->has($feedName)) { |
584 static::$extensions['feed'][] = $feedName; | 594 static::$extensions['feed'][] = $feedName; |