Capturing Terminal Terminals / Outputs Using Symfony Console (CTRL + C)

I created a command that starts downloading files from the Internet, however, since these files must be processed by another component, we must make sure that every file that was downloaded and has not been modified in the last 10 seconds is the correct video and not damaged / partially downloaded .

For this reason, we need to find a way to catch CTRL + C or the completion of commands and clear any file that has not been successfully downloaded.

This is what I have tried so far, using 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();

However, it var_dump()never displays in the console if I do CTRL + C.

Suggestions?

+4
source share
1 answer

CTRL + C, SIGINT, , SIGTERM. http://php.net/manual/en/function.pcntl-signal.php pcntl_signal_dispatch, :

pcntl_signal(SIGINT,'sigIntHandler');

function sigIntHandler() {
  // Do some stuff
}

, . , , , , AbstractCommand sigIntHandler() :

pcntl_signal(SIGINT, [$this, 'sigIntHandler']);

pcntl_signal_dispatch(), , ( ).

+2

Source: https://habr.com/ru/post/1694164/


All Articles