Изменение значений параметров команд Guzzle во время выполнения через плагины?

Это (часть) определения 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
            }
        }
    }
}

Я не могу найти какой-либо метод внутри Parameter или AbstractCommand.

EDIT: имя параметра изменено на "foo" с "method", чтобы избежать путаницы с HTTP-глаголами.


person gremo    schedule 17.07.2013    source источник


Ответы (2)


Вы можете использовать метод 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');
            }
        }
    }
}
person Michael Dowling    schedule 17.07.2013
comment
Извините, мой плохой, имя параметра сбивает с толку. Я говорю не о самом методе HTTP, а о методе с именем параметра. Я отредактирую вопрос... - person gremo; 18.07.2013

Вы можете сделать что-то вроде следующего:

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

До тех пор, пока вы вводите новое значение «foo» в плагин, вы сможете выполнить то, что хотите сделать.

person Brandon W.    schedule 26.02.2014