/** * {@inheritdoc} */ public function prepare(CommandInterface $command) { $request = $this->createRequest($command); // Keep an array of visitors found in the operation $foundVisitors = array(); // Add arguments to the request using the location attribute foreach ($command->getOperation()->getParams() as $name => $arg) { /** @var $arg \Guzzle\Service\Description\Parameter */ if ($location = $arg->getLocation()) { // Skip 'uri' locations because they've already been processed if ($location == 'uri') { continue; } // Instantiate visitors as they are detected in the properties if (!isset($foundVisitors[$location])) { $foundVisitors[$location] = $this->factory->getRequestVisitor($location); } // Ensure that a value has been set for this parameter $value = $command->get($name); if ($value !== null) { // Apply the parameter value with the location visitor $foundVisitors[$location]->visit($command, $request, $arg, $value); } } } // Call the after method on each visitor found in the operation foreach ($foundVisitors as $visitor) { $visitor->after($command, $request); } return $request; }
public function testAllowsAddingVisitors() { $f = new VisitorFlyweight(); $j1 = new JsonRequestVisitor(); $j2 = new JsonResponseVisitor(); $f->addRequestVisitor('json', $j1); $f->addResponseVisitor('json', $j2); $this->assertSame($j1, $f->getRequestVisitor('json')); $this->assertSame($j2, $f->getResponseVisitor('json')); }
/** * Serialize additional parameters * * @param OperationInterface $operation Operation that owns the command * @param CommandInterface $command Command to prepare * @param RequestInterface $request Request to serialize * @param Parameter $additional Additional parameters * * @return null|RequestVisitorInterface */ protected function prepareAdditionalParameters(OperationInterface $operation, CommandInterface $command, RequestInterface $request, Parameter $additional) { if (!($location = $additional->getLocation())) { return; } $visitor = $this->factory->getRequestVisitor($location); foreach ($command->getAll() as $key => $value) { // Ignore values that are null or built-in command options if ($value !== null && $key != 'command.headers' && $key != 'command.response_processing' && !$operation->hasParam($key)) { $additional->setName($key); $visitor->visit($command, $request, $additional, $value); } } return $visitor; }
/** * Serialize additional parameters * * @param OperationInterface $operation Operation that owns the command * @param CommandInterface $command Command to prepare * @param RequestInterface $request Request to serialize * @param Parameter $additional Additional parameters * * @return null|RequestVisitorInterface */ 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) { // Ignore values that are null or built-in command options if ($value !== null && !in_array($key, $hidden) && !$operation->hasParam($key)) { $additional->setName($key); $visitor->visit($command, $request, $additional, $value); } } return $visitor; }