1: <?php
2:
3: namespace Guzzle\Service\Command\LocationVisitor;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6: use Guzzle\Service\Command\LocationVisitor\Request\RequestVisitorInterface;
7: use Guzzle\Service\Command\LocationVisitor\Response\ResponseVisitorInterface;
8:
9: 10: 11:
12: class VisitorFlyweight
13: {
14: 15: 16:
17: protected static $instance;
18:
19: 20: 21:
22: protected static $defaultMappings = array(
23: 'request.body' => 'Guzzle\Service\Command\LocationVisitor\Request\BodyVisitor',
24: 'request.header' => 'Guzzle\Service\Command\LocationVisitor\Request\HeaderVisitor',
25: 'request.json' => 'Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor',
26: 'request.postField' => 'Guzzle\Service\Command\LocationVisitor\Request\PostFieldVisitor',
27: 'request.postFile' => 'Guzzle\Service\Command\LocationVisitor\Request\PostFileVisitor',
28: 'request.query' => 'Guzzle\Service\Command\LocationVisitor\Request\QueryVisitor',
29: 'request.response_body' => 'Guzzle\Service\Command\LocationVisitor\Request\ResponseBodyVisitor',
30: 'request.xml' => 'Guzzle\Service\Command\LocationVisitor\Request\XmlVisitor',
31: 'response.body' => 'Guzzle\Service\Command\LocationVisitor\Response\BodyVisitor',
32: 'response.header' => 'Guzzle\Service\Command\LocationVisitor\Response\HeaderVisitor',
33: 'response.json' => 'Guzzle\Service\Command\LocationVisitor\Response\JsonVisitor',
34: 'response.reasonPhrase' => 'Guzzle\Service\Command\LocationVisitor\Response\ReasonPhraseVisitor',
35: 'response.statusCode' => 'Guzzle\Service\Command\LocationVisitor\Response\StatusCodeVisitor',
36: 'response.xml' => 'Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor'
37: );
38:
39: 40: 41:
42: protected $mappings;
43:
44: 45: 46:
47: protected $cache = array();
48:
49: 50: 51: 52: 53: 54:
55: public static function getInstance()
56: {
57: if (!self::$instance) {
58: self::$instance = new self();
59: }
60:
61: return self::$instance;
62: }
63:
64: 65: 66: 67: 68: 69:
70: public function __construct(array $mappings = null)
71: {
72: $this->mappings = $mappings === null ? self::$defaultMappings : $mappings;
73: }
74:
75: 76: 77: 78: 79: 80: 81:
82: public function getRequestVisitor($visitor)
83: {
84: return $this->getKey('request.' . $visitor);
85: }
86:
87: 88: 89: 90: 91: 92: 93:
94: public function getResponseVisitor($visitor)
95: {
96: return $this->getKey('response.' . $visitor);
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106:
107: public function addRequestVisitor($name, RequestVisitorInterface $visitor)
108: {
109: $this->cache['request.' . $name] = $visitor;
110:
111: return $this;
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121:
122: public function addResponseVisitor($name, ResponseVisitorInterface $visitor)
123: {
124: $this->cache['response.' . $name] = $visitor;
125:
126: return $this;
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136:
137: private function getKey($key)
138: {
139: if (!isset($this->cache[$key])) {
140: if (!isset($this->mappings[$key])) {
141: list($type, $name) = explode('.', $key);
142: throw new InvalidArgumentException("No {$type} visitor has been mapped for {$name}");
143: }
144: $this->cache[$key] = new $this->mappings[$key];
145: }
146:
147: return $this->cache[$key];
148: }
149: }
150: