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: 15:
16: class DefaultRequestSerializer implements RequestSerializerInterface
17: {
18: 19: 20:
21: protected $factory;
22:
23: 24: 25:
26: protected static $instance;
27:
28: 29: 30: 31: 32: 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: 45:
46: public function __construct(VisitorFlyweight $factory)
47: {
48: $this->factory = $factory;
49: }
50:
51: 52: 53: 54: 55: 56: 57: 58:
59: public function addVisitor($location, RequestVisitorInterface $visitor)
60: {
61: $this->factory->addRequestVisitor($location, $visitor);
62:
63: return $this;
64: }
65:
66: 67: 68:
69: public function prepare(CommandInterface $command)
70: {
71: $request = $this->createRequest($command);
72:
73: $foundVisitors = array();
74: $operation = $command->getOperation();
75:
76:
77: foreach ($operation->getParams() as $name => $arg) {
78:
79: $location = $arg->getLocation();
80:
81: if ($location && $location != 'uri') {
82:
83: if (!isset($foundVisitors[$location])) {
84: $foundVisitors[$location] = $this->factory->getRequestVisitor($location);
85: }
86:
87: $value = $command->get($name);
88: if ($value !== null) {
89:
90: $foundVisitors[$location]->visit($command, $request, $arg, $value);
91: }
92: }
93: }
94:
95:
96: if ($additional = $operation->getAdditionalParameters()) {
97: if ($visitor = $this->prepareAdditionalParameters($operation, $command, $request, $additional)) {
98: $foundVisitors[$additional->getLocation()] = $visitor;
99: }
100: }
101:
102:
103: foreach ($foundVisitors as $visitor) {
104: $visitor->after($command, $request);
105: }
106:
107: return $request;
108: }
109:
110: 111: 112: 113: 114: 115: 116: 117: 118: 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:
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: 149: 150: 151: 152: 153:
154: protected function createRequest(CommandInterface $command)
155: {
156: $operation = $command->getOperation();
157: $client = $command->getClient();
158:
159:
160: if (!($uri = $operation->getUri())) {
161: return $client->createRequest($operation->getHttpMethod(), $client->getBaseUrl());
162: }
163:
164:
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:
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: