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: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170:
<?php
namespace Guzzle\Service\Command;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Service\Command\LocationVisitor\Request\RequestVisitorInterface;
use Guzzle\Service\Command\LocationVisitor\VisitorFlyweight;
use Guzzle\Service\Description\OperationInterface;
use Guzzle\Service\Description\Parameter;
class DefaultRequestSerializer implements RequestSerializerInterface
{
protected $factory;
protected static $instance;
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new self(VisitorFlyweight::getInstance());
}
return self::$instance;
}
public function __construct(VisitorFlyweight $factory)
{
$this->factory = $factory;
}
public function addVisitor($location, RequestVisitorInterface $visitor)
{
$this->factory->addRequestVisitor($location, $visitor);
return $this;
}
public function prepare(CommandInterface $command)
{
$request = $this->createRequest($command);
$foundVisitors = array();
$operation = $command->getOperation();
foreach ($operation->getParams() as $name => $arg) {
$location = $arg->getLocation();
if ($location && $location != 'uri') {
if (!isset($foundVisitors[$location])) {
$foundVisitors[$location] = $this->factory->getRequestVisitor($location);
}
$value = $command[$name];
if ($value !== null) {
$foundVisitors[$location]->visit($command, $request, $arg, $value);
}
}
}
if ($additional = $operation->getAdditionalParameters()) {
if ($visitor = $this->prepareAdditionalParameters($operation, $command, $request, $additional)) {
$foundVisitors[$additional->getLocation()] = $visitor;
}
}
foreach ($foundVisitors as $visitor) {
$visitor->after($command, $request);
}
return $request;
}
protected function prepareAdditionalParameters(
OperationInterface $operation,
CommandInterface $command,
RequestInterface $request,
Parameter $additional
) {
if (!($location = $additional->getLocation())) {
return;
}
$visitor = $this->factory->getRequestVisitor($location);
$hidden = $command[$command::HIDDEN_PARAMS];
foreach ($command->toArray() as $key => $value) {
if ($value !== null
&& !in_array($key, $hidden)
&& !$operation->hasParam($key)
) {
$additional->setName($key);
$visitor->visit($command, $request, $additional, $value);
}
}
return $visitor;
}
protected function createRequest(CommandInterface $command)
{
$operation = $command->getOperation();
$client = $command->getClient();
$options = $command[AbstractCommand::REQUEST_OPTIONS] ?: array();
if (!($uri = $operation->getUri())) {
return $client->createRequest($operation->getHttpMethod(), $client->getBaseUrl(), null, null, $options);
}
$variables = array();
foreach ($operation->getParams() as $name => $arg) {
if ($arg->getLocation() == 'uri') {
if (isset($command[$name])) {
$variables[$name] = $arg->filter($command[$name]);
if (!is_array($variables[$name])) {
$variables[$name] = (string) $variables[$name];
}
}
}
}
return $client->createRequest($operation->getHttpMethod(), array($uri, $variables), null, null, $options);
}
}