如何从console参数设置config参数?

时间:2016-08-22 09:55:52

标签: symfony

我是Symfony的新手。

我尝试通过控制台参数更改Monolog输出格式化程序' format = json'。

简而言之,我想以这样的方式运行任何控制台命令:

window.open(‘http://example.com’, ‘_system’);   Loads in the system browser 
window.open(‘http://example.com’, ‘_blank’);    Loads in the InAppBrowser
window.open(‘http://example.com’, ‘_blank’, ‘location=no’); Loads in the InAppBrowser with no location bar
window.open(‘http://example.com’, ‘_self’); Loads in the Cordova web view 

...以请求的格式获取LoggerInterface的输出。

例如,我在配置中设置了默认格式化程序:

app/console my_command --format=json # xml / txt / my own

当我创建一些MyEventListener :: onConsoleCommand(as described here)时,我无法更改参数包因为它已经被编译:"无法在冻结的ParameterBag上调用set()。&#34 ;

Up2 :在这种情况下,我的服务配置如下所示:

monolog:
    handlers:
        console:
            type:   console
            channels: [!event, !doctrine]
            formatter: json_formatter
services:
    json_formatter:
        class: Monolog\Formatter\JsonFormatter

换句话说,我可以在初始文件中注册控制台选项:

services:
    kernel.listener.command_dispatch:
        class: My\Listener\MyEventListener
        autowire: true
        tags:
            - { name: kernel.event_listener, event: console.command }

但是我无法找到一种方法来更改Container中的参数包。因为$ application-> getKernel() - > getContainer()仍为空。

那么,如何从控制台输入中更改Symfony2参数?

或者,也许我可以使用一些环境参数?但是如何在YAML配置中获取环境变量?

谢谢。

UP3: 我已经用这样的环境变量实现了目标:

# app/console
$loader = require __DIR__.'/autoload.php';
# ...
$application->getDefinition()->addOption(
    new InputOption(
        'formatter',
        'f',
        InputOption::VALUE_OPTIONAL,
        'The logs output formatter',
        'json_formatter'
    )
);

2 个答案:

答案 0 :(得分:1)

修改应用程序的每个已注册命令的命令参数的唯一一点是处理在执行任何命令之前触发之前触发的CommandEvents::COMMAND。因此,您可以修改其参数并按照here所述进行读取。此外,此时您已编译容器,此时无法修改服务的定义。但是你可以得到任何服务。

所以我认为你最终可以使用以下处理程序:

class LogFormatterEventListener
{
    private $container;
    private $consoleHandler;

    public function __construct(ContainerInterface $container, HandlerInterface $consoleHandler)
    {
        $this->container = $container;
        $this->consoleHandler = $consoleHandler;
    }

    public function onConsoleCommand(ConsoleCommandEvent $event)
    {
        $inputDefinition = $event->getCommand()->getApplication()->getDefinition();

        $inputDefinition->addOption(
            new InputOption('logformat', null, InputOption::VALUE_OPTIONAL, 'Format of your logs', null)
        );

        // merge the application's input definition
        $event->getCommand()->mergeApplicationDefinition();

        $input = new ArgvInput();

        // we use the input definition of the command
        $input->bind($event->getCommand()->getDefinition());

        $formatter = $input->getOption('logformat');
        if ($formatter /** && $this->container->has($formatter) **/) {
            $this->consoleHandler->setFormatter(
                $this->container->get($formatter);
            );
        }
    }
}

答案 1 :(得分:0)

这是替代解决方案(与常见兼容):

<强>配置:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
    If ProgressBar1.Value = ProgressBar1.Maximum Then
        Timer1.Stop()
        ProgressBar1.Value = ProgressBar1.Minimum
        If Form1.Label4.Text = "Unregistered" Then
            MsgBox("Exampletext", MsgBoxStyle.Information)
            Me.Hide()
        Else
            MsgBox("Exampletext1", MsgBoxStyle.Information)
            Hide()
        End If
    End If 'You need to add the End If here
End Sub

执行命令:

monolog:
    handlers:
        console:
            type:   console
            channels: [!event, !doctrine]
            formatter: "%log.formatter%"
services:
    json_formatter:
        class: Monolog\Formatter\JsonFormatter