通过插件在运行时更改Guzzle命令的参数值?

时间:2013-07-17 17:01:05

标签: php guzzle

这是({1}}定义的(部分),带有一个必填参数(BaseOperation):

foo

'BaseOperation' => array( 'class' => 'My\Command\MyCustomCommand', 'httpMethod' => 'POST', 'parameters' => array( 'foo' => array( 'required' => true, 'location' => 'query' ) ) ) 内插件我需要在运行时修改ChangeMethodPlugin的值:

foo

我在ParameterAbstractCommand内找不到任何方法。

编辑:param名称从“方法”更改为“foo”,以避免与HTTP谓词混淆。

2 个答案:

答案 0 :(得分:0)

您可以使用命令所拥有的操作的setHttpMethod()方法,但您需要使用command.before_prepare事件。

<?php

class ChangeMethodPlugin implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array('command.before_prepare' => 'onBeforeCommandPrepare');
    }

    public function onBeforeCommandPrepare(Event $event)
    {
        /** @var \Guzzle\Service\Command\CommandInterface $command */
        $command = $event['command'];

        // Only if test configuration is true
        if ($command->getClient()->getConfig(ClientOptions::TEST)) {
            // Only if command is MyCustomCommand
            if ($command instanceof MyCustomCommand) {
                // Here I need to change the value of 'method' parameter
                $command->getOperation()->setHttpMethod('METHOD_NAME');
            }
        }
    }
}

答案 1 :(得分:0)

您可以执行以下操作:

$command->getRequest()->getQuery()->set('foo', 'bar');

只要你将新的'foo'值注入插件,你就应该能够完成你想要做的事情。