php - 如何将我的所有 Artisan 命令输出发送到 slack?(使用 Laravel)
问题描述
我正在尝试使用 trait 扩展我的 Laravel Artisan 命令。该特征应捕获所有命令行输出并将其发送到 Slack。
我有使用这个包的“send messages to slack”部分。
但是我无法捕获控制台输出。这就是我所拥有的:
namespace App\Traits;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
trait NotifiesSlack
{
/**
* Execute the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return mixed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$consoleOutput = new BufferedOutput;
$call = $this->laravel->call([$this, 'handle']);
$this->notifySlack($consoleOutput->fetch());
return $call;
}
public function notifySlack(string $output)
{
\Slack::send($output);
}
}
我是否覆盖了正确的方法?还有其他方法可以从 Command 类捕获控制台输出吗?
欢迎任何帮助!提前致谢。
解决方案
您遇到无法通过 trait 覆盖方法的常见情况。这显然是因为该execute
方法已经在类本身中声明,呈现 trait useless。
一种快速简便的方法是简单地创建自己的抽象命令类,根据自己的喜好扩展Illuminate\Console\Command;
和覆盖该execute
方法;之后将您的可报告命令的抽象命令类用作base。
abstract class NotifiesSlackCommand extend Illuminate\Console\Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
...
}
}
以及需要发送到 Slack 的真实命令
class ProcessImagesCommand extends NotifiesSlackCommand {
public function handle() {/* do magic */}
}
推荐阅读
- c# - 启动中的服务注入
- c# - 当我点击它时,如何使文本框不可编辑?
- prometheus - 当我看到指标值增加时,为什么 increase() 只显示零值?
- php - 如何在 PHP(PDO) 中显示 dbms_output.put_line
- discord.py - 如何让我的机器人留在语音聊天中,直到它被告知离开
- node.js - cypress imap 阅读电子邮件 - 最佳实践
- reactjs - 为什么当我通过函数调用 axios 时它没有更新?
- selenium - 我有一个多语言网站需要提取 url 并需要检查国家特定代码是否存在
- scip - 为 VRP 选择屏障
- arrays - 如何使用 json 原始数据在 vuejs 中制作依赖的第二个列表?