【发布时间】:2018-04-08 03:26:25
【问题描述】:
我将编写一些命令来检查我的应用程序是否一切正常。
因为这些命令将由 cronjob 执行,所以我想将输出格式化为可在日志文件中利用。
为了在命令中的任何位置显示错误消息(在每个方法调用中不传递 $output),我将其设为类属性,它非常方便但看起来很糟糕,我知道它很糟糕但我不知道为什么。这是一个例子:
<?php
namespace CheckingBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class CheckingCommand
*
*/
class CheckingCommand extends Command
{
/**
* @var OutputInterface $output
*/
private $output;
protected function configure()
{
$this->setName('check:all');
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->checkSqlConnection();
}
protected function checkSqlConnection()
{
$myConnexion = null; //Try to connect to database
if (null === $myConnexion) {
$this->sendError('Cannot connect to MySQL database');
}
}
/**
* @param string $errorMessage
*/
protected function sendError($errorMessage)
{
$this->output->write(sprintf('%s <error>%s</error>', date('Y-m-d H:i:s'), $errorMessage));
}
}
有人能解释一下它为什么不好(如果是的话)吗?最好到处传递它:
$this->checkSqlConnection($output);
和
protected function checkSqlConnection(Output $output)
{
$myConnexion = null; //Try to connect to database
if (null === $myConnexion) {
$output->write('Cannot connect to MySQL database');
}
}
我应该在我的命令中使用 Exceptions 和 try/catch 并在 catch 中使用我的 sendError 方法吗?这可能是处理错误的好方法,但如果我想在方法中显示其他信息怎么办?
【问题讨论】:
-
这是相当主观的,但我猜它是“坏的”,因为格式化输出不是
command类的责任。你应该有一个OutputFormatter或其他东西来处理这个责任。
标签: php symfony console command