ParserRegistry.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Guzzle\Parser;
  3. /**
  4. * Registry of parsers used by the application
  5. */
  6. class ParserRegistry
  7. {
  8. /** @var ParserRegistry Singleton instance */
  9. protected static $instance;
  10. /** @var array Array of parser instances */
  11. protected $instances = array();
  12. /** @var array Mapping of parser name to default class */
  13. protected $mapping = array(
  14. 'message' => 'Guzzle\\Parser\\Message\\MessageParser',
  15. 'cookie' => 'Guzzle\\Parser\\Cookie\\CookieParser',
  16. 'url' => 'Guzzle\\Parser\\Url\\UrlParser',
  17. 'uri_template' => 'Guzzle\\Parser\\UriTemplate\\UriTemplate',
  18. );
  19. /**
  20. * @return self
  21. * @codeCoverageIgnore
  22. */
  23. public static function getInstance()
  24. {
  25. if (!self::$instance) {
  26. self::$instance = new static;
  27. }
  28. return self::$instance;
  29. }
  30. public function __construct()
  31. {
  32. // Use the PECL URI template parser if available
  33. if (extension_loaded('uri_template')) {
  34. $this->mapping['uri_template'] = 'Guzzle\\Parser\\UriTemplate\\PeclUriTemplate';
  35. }
  36. }
  37. /**
  38. * Get a parser by name from an instance
  39. *
  40. * @param string $name Name of the parser to retrieve
  41. *
  42. * @return mixed|null
  43. */
  44. public function getParser($name)
  45. {
  46. if (!isset($this->instances[$name])) {
  47. if (!isset($this->mapping[$name])) {
  48. return null;
  49. }
  50. $class = $this->mapping[$name];
  51. $this->instances[$name] = new $class();
  52. }
  53. return $this->instances[$name];
  54. }
  55. /**
  56. * Register a custom parser by name with the register
  57. *
  58. * @param string $name Name or handle of the parser to register
  59. * @param mixed $parser Instantiated parser to register
  60. */
  61. public function registerParser($name, $parser)
  62. {
  63. $this->instances[$name] = $parser;
  64. }
  65. }