Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-feed/src/Reader/Reader.php @ 2:5311817fb629
Theme updates
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 13:19:18 +0000 |
parents | c75dbcec494b |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
1:0b0e5f3b1e83 | 2:5311817fb629 |
---|---|
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 |
565 * @return void | 575 * @return void |
566 * @throws Exception\RuntimeException if unable to resolve Extension class | 576 * @throws Exception\RuntimeException if unable to resolve Extension class |
567 */ | 577 */ |
568 public static function registerExtension($name) | 578 public static function registerExtension($name) |
569 { | 579 { |
570 $feedName = $name . '\Feed'; | 580 if (! static::hasExtension($name)) { |
571 $entryName = $name . '\Entry'; | 581 throw new Exception\RuntimeException(sprintf( |
582 'Could not load extension "%s" using Plugin Loader.' | |
583 . ' Check prefix paths are configured and extension exists.', | |
584 $name | |
585 )); | |
586 } | |
587 | |
588 // Return early if already registered. | |
589 if (static::isRegistered($name)) { | |
590 return; | |
591 } | |
592 | |
572 $manager = static::getExtensionManager(); | 593 $manager = static::getExtensionManager(); |
573 if (static::isRegistered($name)) { | 594 |
574 if ($manager->has($feedName) || $manager->has($entryName)) { | 595 $feedName = $name . '\Feed'; |
575 return; | |
576 } | |
577 } | |
578 | |
579 if (!$manager->has($feedName) && !$manager->has($entryName)) { | |
580 throw new Exception\RuntimeException('Could not load extension: ' . $name | |
581 . ' using Plugin Loader. Check prefix paths are configured and extension exists.'); | |
582 } | |
583 if ($manager->has($feedName)) { | 596 if ($manager->has($feedName)) { |
584 static::$extensions['feed'][] = $feedName; | 597 static::$extensions['feed'][] = $feedName; |
585 } | 598 } |
599 | |
600 $entryName = $name . '\Entry'; | |
586 if ($manager->has($entryName)) { | 601 if ($manager->has($entryName)) { |
587 static::$extensions['entry'][] = $entryName; | 602 static::$extensions['entry'][] = $entryName; |
588 } | 603 } |
589 } | 604 } |
590 | 605 |
660 static::registerExtension('Atom'); | 675 static::registerExtension('Atom'); |
661 static::registerExtension('Slash'); | 676 static::registerExtension('Slash'); |
662 static::registerExtension('WellFormedWeb'); | 677 static::registerExtension('WellFormedWeb'); |
663 static::registerExtension('Thread'); | 678 static::registerExtension('Thread'); |
664 static::registerExtension('Podcast'); | 679 static::registerExtension('Podcast'); |
680 | |
681 // Added in 2.10.0; check for it conditionally | |
682 static::hasExtension('GooglePlayPodcast') | |
683 ? static::registerExtension('GooglePlayPodcast') | |
684 : trigger_error( | |
685 sprintf( | |
686 'Please update your %1$s\ExtensionManagerInterface implementation to add entries for' | |
687 . ' %1$s\Extension\GooglePlayPodcast\Entry and %1$s\Extension\GooglePlayPodcast\Feed.', | |
688 __NAMESPACE__ | |
689 ), | |
690 \E_USER_NOTICE | |
691 ); | |
665 } | 692 } |
666 | 693 |
667 /** | 694 /** |
668 * Utility method to apply array_unique operation to a multidimensional | 695 * Utility method to apply array_unique operation to a multidimensional |
669 * array. | 696 * array. |
680 foreach ($array as &$value) { | 707 foreach ($array as &$value) { |
681 $value = unserialize($value); | 708 $value = unserialize($value); |
682 } | 709 } |
683 return $array; | 710 return $array; |
684 } | 711 } |
712 | |
713 /** | |
714 * Does the extension manager have the named extension? | |
715 * | |
716 * This method exists to allow us to test if an extension is present in the | |
717 * extension manager. It may be used by registerExtension() to determine if | |
718 * the extension has items present in the manager, or by | |
719 * registerCoreExtension() to determine if the core extension has entries | |
720 * in the extension manager. In the latter case, this can be useful when | |
721 * adding new extensions in a minor release, as custom extension manager | |
722 * implementations may not yet have an entry for the extension, which would | |
723 * then otherwise cause registerExtension() to fail. | |
724 * | |
725 * @param string $name | |
726 * @return bool | |
727 */ | |
728 protected static function hasExtension($name) | |
729 { | |
730 $feedName = $name . '\Feed'; | |
731 $entryName = $name . '\Entry'; | |
732 $manager = static::getExtensionManager(); | |
733 | |
734 return $manager->has($feedName) || $manager->has($entryName); | |
735 } | |
685 } | 736 } |