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