Open Journal Systems  3.3.0
VisitorFlyweight.php
1 <?php
2 
4 
8 
13 {
15  protected static $instance;
16 
18  protected static $defaultMappings = array(
19  'request.body' => 'Guzzle\Service\Command\LocationVisitor\Request\BodyVisitor',
20  'request.header' => 'Guzzle\Service\Command\LocationVisitor\Request\HeaderVisitor',
21  'request.json' => 'Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor',
22  'request.postField' => 'Guzzle\Service\Command\LocationVisitor\Request\PostFieldVisitor',
23  'request.postFile' => 'Guzzle\Service\Command\LocationVisitor\Request\PostFileVisitor',
24  'request.query' => 'Guzzle\Service\Command\LocationVisitor\Request\QueryVisitor',
25  'request.response_body' => 'Guzzle\Service\Command\LocationVisitor\Request\ResponseBodyVisitor',
26  'request.responseBody' => 'Guzzle\Service\Command\LocationVisitor\Request\ResponseBodyVisitor',
27  'request.xml' => 'Guzzle\Service\Command\LocationVisitor\Request\XmlVisitor',
28  'response.body' => 'Guzzle\Service\Command\LocationVisitor\Response\BodyVisitor',
29  'response.header' => 'Guzzle\Service\Command\LocationVisitor\Response\HeaderVisitor',
30  'response.json' => 'Guzzle\Service\Command\LocationVisitor\Response\JsonVisitor',
31  'response.reasonPhrase' => 'Guzzle\Service\Command\LocationVisitor\Response\ReasonPhraseVisitor',
32  'response.statusCode' => 'Guzzle\Service\Command\LocationVisitor\Response\StatusCodeVisitor',
33  'response.xml' => 'Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor'
34  );
35 
37  protected $mappings;
38 
40  protected $cache = array();
41 
46  public static function getInstance()
47  {
48  if (!self::$instance) {
49  self::$instance = new self();
50  }
51 
53  }
54 
59  public function __construct(array $mappings = null)
60  {
61  $this->mappings = $mappings === null ? self::$defaultMappings : $mappings;
62  }
63 
71  public function getRequestVisitor($visitor)
72  {
73  return $this->getKey('request.' . $visitor);
74  }
75 
83  public function getResponseVisitor($visitor)
84  {
85  return $this->getKey('response.' . $visitor);
86  }
87 
96  public function addRequestVisitor($name, RequestVisitorInterface $visitor)
97  {
98  $this->cache['request.' . $name] = $visitor;
99 
100  return $this;
101  }
102 
111  public function addResponseVisitor($name, ResponseVisitorInterface $visitor)
112  {
113  $this->cache['response.' . $name] = $visitor;
114 
115  return $this;
116  }
117 
126  private function getKey($key)
127  {
128  if (!isset($this->cache[$key])) {
129  if (!isset($this->mappings[$key])) {
130  list($type, $name) = explode('.', $key);
131  throw new InvalidArgumentException("No {$type} visitor has been mapped for {$name}");
132  }
133  $this->cache[$key] = new $this->mappings[$key];
134  }
135 
136  return $this->cache[$key];
137  }
138 }
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\$mappings
$mappings
Definition: VisitorFlyweight.php:40
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight
Definition: VisitorFlyweight.php:12
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\__construct
__construct(array $mappings=null)
Definition: VisitorFlyweight.php:65
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\addRequestVisitor
addRequestVisitor($name, RequestVisitorInterface $visitor)
Definition: VisitorFlyweight.php:102
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\getRequestVisitor
getRequestVisitor($visitor)
Definition: VisitorFlyweight.php:77
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\$defaultMappings
static $defaultMappings
Definition: VisitorFlyweight.php:19
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\getInstance
static getInstance()
Definition: VisitorFlyweight.php:52
Guzzle\Service\Command\LocationVisitor\Response\ResponseVisitorInterface
Definition: ResponseVisitorInterface.php:12
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\addResponseVisitor
addResponseVisitor($name, ResponseVisitorInterface $visitor)
Definition: VisitorFlyweight.php:117
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\getResponseVisitor
getResponseVisitor($visitor)
Definition: VisitorFlyweight.php:89
Guzzle\Service\Command\LocationVisitor
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\$cache
$cache
Definition: VisitorFlyweight.php:46
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\$instance
static $instance
Definition: VisitorFlyweight.php:16
Guzzle\Service\Command\LocationVisitor\Request\RequestVisitorInterface
Definition: RequestVisitorInterface.php:12