Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • VisitorFlyweight
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  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:  * Flyweight factory used to instantiate request and response visitors
 11:  */
 12: class VisitorFlyweight
 13: {
 14:     /**
 15:      * @var self Singleton instance of self
 16:      */
 17:     protected static $instance;
 18: 
 19:     /**
 20:      * @var array Default array of mappings of location names to classes
 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:      * @var array Array of mappings of location names to classes
 41:      */
 42:     protected $mappings;
 43: 
 44:     /**
 45:      * @var array Cache of instantiated visitors
 46:      */
 47:     protected $cache = array();
 48: 
 49:     /**
 50:      * Get a cached instance of the flyweight factory
 51:      *
 52:      * @return self
 53:      * @codeCoverageIgnore
 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:      * Create a new flyweight
 66:      *
 67:      * @param array $mappings Array mapping request.name and response.name to location visitor classes. Leave null to
 68:      *                        use the default values.
 69:      */
 70:     public function __construct(array $mappings = null)
 71:     {
 72:         $this->mappings = $mappings === null ? self::$defaultMappings : $mappings;
 73:     }
 74: 
 75:     /**
 76:      * Get an instance of a request visitor by location name
 77:      *
 78:      * @param string $visitor Visitor name
 79:      *
 80:      * @return RequestVisitorInterface
 81:      */
 82:     public function getRequestVisitor($visitor)
 83:     {
 84:         return $this->getKey('request.' . $visitor);
 85:     }
 86: 
 87:     /**
 88:      * Get an instance of a response visitor by location name
 89:      *
 90:      * @param string $visitor Visitor name
 91:      *
 92:      * @return ResponseVisitorInterface
 93:      */
 94:     public function getResponseVisitor($visitor)
 95:     {
 96:         return $this->getKey('response.' . $visitor);
 97:     }
 98: 
 99:     /**
100:      * Add a response visitor to the factory by name
101:      *
102:      * @param string                  $name    Name of the visitor
103:      * @param RequestVisitorInterface $visitor Visitor to add
104:      *
105:      * @return self
106:      */
107:     public function addRequestVisitor($name, RequestVisitorInterface $visitor)
108:     {
109:         $this->cache['request.' . $name] = $visitor;
110: 
111:         return $this;
112:     }
113: 
114:     /**
115:      * Add a response visitor to the factory by name
116:      *
117:      * @param string                   $name    Name of the visitor
118:      * @param ResponseVisitorInterface $visitor Visitor to add
119:      *
120:      * @return self
121:      */
122:     public function addResponseVisitor($name, ResponseVisitorInterface $visitor)
123:     {
124:         $this->cache['response.' . $name] = $visitor;
125: 
126:         return $this;
127:     }
128: 
129:     /**
130:      * Get a visitor by key value name
131:      *
132:      * @param string $key Key name to retrieve
133:      *
134:      * @return mixed
135:      * @throws InvalidArgumentException
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: 
php-coveralls API documentation generated by ApiGen 2.8.0