1: <?php
2:
3: namespace Guzzle\Parser;
4:
5: /**
6: * Registry of parsers used by the application
7: */
8: class ParserRegistry
9: {
10: /**
11: * @var ParserRegistry Singleton instance
12: */
13: protected static $instance;
14:
15: /**
16: * @var array Array of parser instances
17: */
18: protected $instances = array();
19:
20: /**
21: * @var array Mapping of parser name to default class
22: */
23: protected $mapping = array(
24: 'message' => 'Guzzle\\Parser\\Message\\MessageParser',
25: 'cookie' => 'Guzzle\\Parser\\Cookie\\CookieParser',
26: 'url' => 'Guzzle\\Parser\\Url\\UrlParser',
27: 'uri_template' => 'Guzzle\\Parser\\UriTemplate\\UriTemplate',
28: );
29:
30: /**
31: * Get a singleton instance
32: *
33: * @return self
34: * @codeCoverageIgnore
35: */
36: public static function getInstance()
37: {
38: if (!self::$instance) {
39: self::$instance = new static;
40: }
41:
42: return self::$instance;
43: }
44:
45: /**
46: * Constructor used to apply the most performant parsers based on loaded extensions
47: */
48: public function __construct()
49: {
50: // Use the PECL URI template parser if available
51: if (extension_loaded('uri_template')) {
52: $this->mapping['uri_template'] = 'Guzzle\\Parser\\UriTemplate\\PeclUriTemplate';
53: }
54: }
55:
56: /**
57: * Get a parser by name from an instance
58: *
59: * @param string $name Name of the parser to retrieve
60: *
61: * @return mixed|null
62: */
63: public function getParser($name)
64: {
65: if (!isset($this->instances[$name])) {
66: if (!isset($this->mapping[$name])) {
67: return null;
68: }
69: $class = $this->mapping[$name];
70: $this->instances[$name] = new $class();
71: }
72:
73: return $this->instances[$name];
74: }
75:
76: /**
77: * Register a custom parser by name with the register
78: *
79: * @param string $name Name or handle of the parser to register
80: * @param mixed $parser Instantiated parser to register
81: */
82: public function registerParser($name, $parser)
83: {
84: $this->instances[$name] = $parser;
85: }
86:
87: /**
88: * Get a specific parser by handle name
89: *
90: * @param string $name Name of the parser to retrieve
91: *
92: * @return mixed|null Returns null if the parser is not found or cannot be instantiated
93: * @deprecated Will be removed in 3.1.0
94: * @codeCoverageIgnore
95: */
96: public static function get($name)
97: {
98: return self::getInstance()->getParser($name);
99: }
100:
101: /**
102: * Register a custom parser by name with the register
103: *
104: * @param string $name Name or handle of the parser to register
105: * @param mixed $parser Instantiated parser to register
106: * @deprecated Will be removed in 3.1.0
107: * @codeCoverageIgnore
108: */
109: public static function set($name, $parser)
110: {
111: self::getInstance()->registerParser($name, $parser);
112: }
113: }
114: