PHP code example of phlib / console-process

1. Go to this page and download the library: Download phlib/console-process library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phlib / console-process example snippets




declare(strict_types=1);

use Phlib\Console\Command\BackgroundCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyProcessCommand extends BackgroundCommand
{
    protected function configure(): void
    {
        $this->setName('my:process')
            ->setDescription('My background process.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('Doing important work!');
        return 0;
    }
}


class MyProcessCommand extends BackgroundCommand
{
    // ...
    
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $countDeleted = $this->deleteBatchOfRecords();
        if ($countDeleted === 0) {
            $output->writeln('All done!');
            $this->shutdown();
        }
        return 0;
    }
}

class MyProcessCommand extends BackgroundCommand
{
    // ...
    
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $isInputValid = $this->someValidationChecks($input);
        if ($isInputValid === false) {
            $output->writeln('Message explaining invalid input');
            return 1;
        }
        
        // do some work
        
        return 0;
    }
}

class MyProcessCommand extends BackgroundCommand
{
    // ...
    
    protected function onShutdown(InputInterface $input, OutputInterface $output): void
    {
        $output->writeln('onShutdown method called.');
    }
}



declare(strict_types=1);

use Phlib\Console\Command\DaemonCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyProcessCommand extends DaemonCommand
{
    protected function configure()
    {
        $this->setName('my:process')
            ->setDescription('My background process.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Doing important work!');
    }
}


use Symfony\Component\Console\Output\StreamOutput;

class MyProcessCommand extends DaemonCommand
{
    // ... 
    
    protected function createChildOutput()
    {
        return new MyOutputToLoggerClass();
    }
}

class MyProcessCommand extends DaemonCommand
{
    // ...

    protected function onBeforeDaemonize(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('onBeforeDaemonize method called.');
    }

    protected function onAfterDaemonizeChild(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('onAfterDaemonizeChild method called.');
    }

    protected function onAfterDaemonizeParent(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('onAfterDaemonizeParent method called.');
    }
    
    protected function onStart(InputInterface $input, OutputInterface $output): void
    {
        // Similar to `onAfterDaemonizeChild()` but also called if `--daemonize` option is not set.
        $output->writeln('onStart method called.');
    }
}