1

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

'BaseOperation' => array(
    'class' => 'My\Command\MyCustomCommand',
    'httpMethod' => 'POST',
    'parameters' => array(
        'foo' => array(
            'required' => true,
            'location' => 'query'
        )
    )
)

ChangeMethodPlugin插件里面我需要foo在运行时修改的值:

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

    public function onBeforeCommandSend(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 'foo' parameter
            }
        }
    }
}

我在里面找不到任何方法Parameteror AbstractCommand

编辑:参数名称从“方法”更改为“foo”以避免与 HTTP 动词混淆。

4

2 回答 2

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');
            }
        }
    }
}
于 2013-07-17T20:31:27.767 回答
0

您可以执行以下操作:

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

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

于 2014-02-26T21:56:01.157 回答