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

Php 通过插件在运行时更改Guzzle命令的参数值?,php,guzzle,Php,Guzzle,这是BaseOperation定义的一部分,带有一个强制参数(foo): “基本操作”=>数组( 'class'=>'My\Command\MyCustomCommand', 'httpMethod'=>'POST', “参数”=>数组( 'foo'=>数组( “必需”=>true, '位置'=>'查询' ) ) ) 在ChangeMethodPlugin插件中,我需要在运行时修改foo的值: class ChangeMethodPlugin实现EventSubscriberInterface

这是
BaseOperation
定义的一部分,带有一个强制参数(
foo
):

“基本操作”=>数组(
'class'=>'My\Command\MyCustomCommand',
'httpMethod'=>'POST',
“参数”=>数组(
'foo'=>数组(
“必需”=>true,
'位置'=>'查询'
)
)
)
ChangeMethodPlugin
插件中,我需要在运行时修改
foo
的值:

class ChangeMethodPlugin实现EventSubscriberInterface
{
公共静态函数getSubscribedEvents()
{
返回数组('command.before_send'=>'onbefore commandsend');
}
onBeforeCommandSend公共函数(事件$Event)
{
/**@var\Guzzle\Service\Command\CommandInterface$Command*/
$command=$event['command'];
//仅当测试配置为true时
if($command->getClient()->getConfig(ClientOptions::TEST)){
//仅当命令为MyCustomCommand时
if($command instanceof MyCustomCommand){
//这里我需要更改'foo'参数的值
}
}
}
}
我找不到任何内部或外部的方法


EDIT:将参数名称从“method”更改为“foo”,以避免与HTTP动词混淆。

您可以使用命令拥有的操作的
setHttpMethod()
方法,但您需要使用
命令。在准备
事件之前

<?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');
            }
        }
    }
}

您可以执行以下操作:

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


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

对不起,我的错,参数名称令人困惑。我说的不是HTTP方法本身,而是一个名为“method”的参数。我要编辑这个问题。。。