Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

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