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

  • AbstractCommand
  • ClosureCommand
  • DefaultRequestSerializer
  • DefaultResponseParser
  • OperationCommand
  • OperationResponseParser

Interfaces

  • CommandInterface
  • RequestSerializerInterface
  • ResponseClassInterface
  • ResponseParserInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: namespace Guzzle\Service\Command;
  4: 
  5: use Guzzle\Http\Message\RequestInterface;
  6: use Guzzle\Http\Url;
  7: use Guzzle\Parser\ParserRegistry;
  8: use Guzzle\Service\Command\LocationVisitor\Request\RequestVisitorInterface;
  9: use Guzzle\Service\Command\LocationVisitor\VisitorFlyweight;
 10: use Guzzle\Service\Description\OperationInterface;
 11: use Guzzle\Service\Description\Parameter;
 12: 
 13: /**
 14:  * Default request serializer that transforms command options and operation parameters into a request
 15:  */
 16: class DefaultRequestSerializer implements RequestSerializerInterface
 17: {
 18:     /**
 19:      * @var VisitorFlyweight $factory Visitor factory
 20:      */
 21:     protected $factory;
 22: 
 23:     /**
 24:      * @var self
 25:      */
 26:     protected static $instance;
 27: 
 28:     /**
 29:      * Get a cached default instance of the class
 30:      *
 31:      * @return self
 32:      * @codeCoverageIgnore
 33:      */
 34:     public static function getInstance()
 35:     {
 36:         if (!self::$instance) {
 37:             self::$instance = new self(VisitorFlyweight::getInstance());
 38:         }
 39: 
 40:         return self::$instance;
 41:     }
 42: 
 43:     /**
 44:      * @param VisitorFlyweight $factory Factory to use when creating visitors
 45:      */
 46:     public function __construct(VisitorFlyweight $factory)
 47:     {
 48:         $this->factory = $factory;
 49:     }
 50: 
 51:     /**
 52:      * Add a location visitor to the serializer
 53:      *
 54:      * @param string                   $location Location to associate with the visitor
 55:      * @param RequestVisitorInterface  $visitor  Visitor to attach
 56:      *
 57:      * @return self
 58:      */
 59:     public function addVisitor($location, RequestVisitorInterface $visitor)
 60:     {
 61:         $this->factory->addRequestVisitor($location, $visitor);
 62: 
 63:         return $this;
 64:     }
 65: 
 66:     /**
 67:      * {@inheritdoc}
 68:      */
 69:     public function prepare(CommandInterface $command)
 70:     {
 71:         $request = $this->createRequest($command);
 72:         // Keep an array of visitors found in the operation
 73:         $foundVisitors = array();
 74:         $operation = $command->getOperation();
 75: 
 76:         // Add arguments to the request using the location attribute
 77:         foreach ($operation->getParams() as $name => $arg) {
 78:             /** @var $arg \Guzzle\Service\Description\Parameter */
 79:             $location = $arg->getLocation();
 80:             // Skip 'uri' locations because they've already been processed
 81:             if ($location && $location != 'uri') {
 82:                 // Instantiate visitors as they are detected in the properties
 83:                 if (!isset($foundVisitors[$location])) {
 84:                     $foundVisitors[$location] = $this->factory->getRequestVisitor($location);
 85:                 }
 86:                 // Ensure that a value has been set for this parameter
 87:                 $value = $command->get($name);
 88:                 if ($value !== null) {
 89:                     // Apply the parameter value with the location visitor
 90:                     $foundVisitors[$location]->visit($command, $request, $arg, $value);
 91:                 }
 92:             }
 93:         }
 94: 
 95:         // Serialize additional parameters
 96:         if ($additional = $operation->getAdditionalParameters()) {
 97:             if ($visitor = $this->prepareAdditionalParameters($operation, $command, $request, $additional)) {
 98:                 $foundVisitors[$additional->getLocation()] = $visitor;
 99:             }
100:         }
101: 
102:         // Call the after method on each visitor found in the operation
103:         foreach ($foundVisitors as $visitor) {
104:             $visitor->after($command, $request);
105:         }
106: 
107:         return $request;
108:     }
109: 
110:     /**
111:      * Serialize additional parameters
112:      *
113:      * @param OperationInterface $operation  Operation that owns the command
114:      * @param CommandInterface   $command    Command to prepare
115:      * @param RequestInterface   $request    Request to serialize
116:      * @param Parameter          $additional Additional parameters
117:      *
118:      * @return null|RequestVisitorInterface
119:      */
120:     protected function prepareAdditionalParameters(
121:         OperationInterface $operation,
122:         CommandInterface $command,
123:         RequestInterface $request,
124:         Parameter $additional
125:     ) {
126:         if (!($location = $additional->getLocation())) {
127:             return;
128:         }
129: 
130:         $visitor = $this->factory->getRequestVisitor($location);
131: 
132:         foreach ($command->getAll() as $key => $value) {
133:             // Ignore values that are null or built-in command options
134:             if ($value !== null
135:                 && $key != 'command.headers'
136:                 && $key != 'command.response_processing'
137:                 && !$operation->hasParam($key)
138:             ) {
139:                 $additional->setName($key);
140:                 $visitor->visit($command, $request, $additional, $value);
141:             }
142:         }
143: 
144:         return $visitor;
145:     }
146: 
147:     /**
148:      * Create a request for the command and operation
149:      *
150:      * @param CommandInterface $command Command to create a request for
151:      *
152:      * @return RequestInterface
153:      */
154:     protected function createRequest(CommandInterface $command)
155:     {
156:         $operation = $command->getOperation();
157:         $client = $command->getClient();
158: 
159:         // If the command does not specify a template, then assume the base URL of the client
160:         if (!($uri = $operation->getUri())) {
161:             return $client->createRequest($operation->getHttpMethod(), $client->getBaseUrl());
162:         }
163: 
164:         // Get the path values and use the client config settings
165:         $variables = array();
166:         foreach ($operation->getParams() as $name => $arg) {
167:             if ($arg->getLocation() == 'uri') {
168:                 if ($command->hasKey($name)) {
169:                     $variables[$name] = $arg->filter($command->get($name));
170:                     if (!is_array($variables[$name])) {
171:                         $variables[$name] = (string) $variables[$name];
172:                     }
173:                 }
174:             }
175:         }
176: 
177:         // Merge the client's base URL with an expanded URI template
178:         return $client->createRequest(
179:             $operation->getHttpMethod(),
180:             (string) Url::factory($client->getBaseUrl())
181:                 ->combine(ParserRegistry::getInstance()->getParser('uri_template')->expand($uri, $variables))
182:         );
183:     }
184: }
185: 
php-coveralls API documentation generated by ApiGen 2.8.0