我已经构建了一个命令来触发通过互联网下载文件,但是由于这些文件需要由另一个组件处理,因此我们需要确保在过去 10 秒内已下载且未修改的每个文件都是正确的视频且未损坏/部分下载。
因此,我们需要找到一种方法来捕获 CTRL+C 或命令终止并清理任何尚未成功下载的适用文件。
这是我到目前为止通过使用symfony/consoleand尝试过的symfony/event-dispatcher:
#!/usr/bin/env php
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use ImportExport\Console\ImportCommand;
use Monolog\Logger;
$dotenv = new Dotenv\Dotenv(__DIR__ . '/../');
$dotenv->load();
$logger = new Logger('console');
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) {
// gets the command that has been executed
$command = $event->getCommand();
var_dump($command);
});
$application = new Application("Import-Export System", 'v0.1.0-ALPHA');
$application->add(new ImportCommand($logger));
$application->setDispatcher($dispatcher);
$application->run();
Run Code Online (Sandbox Code Playgroud)
但是,var_dump()如果我执行 CTRL+C,则永远不会在控制台中显示。
建议?
我想使用该security.interactive_login事件来更新我的用户的上次登录字段。
活动注册成功:
php bin/console debug:event-dispatcher security.interactive_login
Registered Listeners for "security.interactive_login" Event
===========================================================
------- ------------------------------------------------------------------------ ----------
Order Callable Priority
------- ------------------------------------------------------------------------ ----------
#1 App\EventSubscriber\UserLocaleSubscriber::onSecurityInteractiveLogin() 0
------- ------------------------------------------------------------------------ ----------
Run Code Online (Sandbox Code Playgroud)
但它落在Symfony 分析器中的Not Called Listens上。
这是事件订阅者:
class UserLocaleSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
/** @var User $user */
$user = $event->getAuthenticationToken()->getUser();
$user->setLastLoginAt(new DateTime());
$this->em->persist($user);
$this->em->flush();
}
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
]; …Run Code Online (Sandbox Code Playgroud) events symfony symfony-security symfony-eventdispatcher symfony5