PHP code example of yii1tech / async-cmd

1. Go to this page and download the library: Download yii1tech/async-cmd 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/ */

    

yii1tech / async-cmd example snippets




return [
    'components' => [
        \yii1tech\async\cmd\CommandDispatcher::class => [
            'class' => \yii1tech\async\cmd\CommandDispatcher::class,
        ],
        // ...
    ],
    // ...
];



use yii1tech\async\cmd\CommandDispatcher;

/** @var CommandDispatcher $dispatcher */
$dispatcher = Yii::app()->getComponent(CommandDispatcher::class);

// run Yii console command:
// executes: `php /path/to/project/yiic stats generate --date='2023-08-29'`
$dispatcher->create()
    ->yiic(StatsCommand::class, 'generate', ['date' => '2023-08-29']); 

// run arbitrary console command:
// executes: `curl -X POST -d 'param1=value1&param2=value2' http://example.com/api/notify`
$dispatcher->create()
    ->external('curl', [
        '-X' => 'POST',
        '-d' => 'param1=value1&param2=value2',
        'http://example.com/api/notify',
    ]);



use yii1tech\async\cmd\CommandDispatcher;

/** @var CommandDispatcher $dispatcher */
$dispatcher = Yii::app()->getComponent(CommandDispatcher::class);

$dispatcher->create()
    ->yiic(StatsCommand::class, 'generate', ['date' => '2023-08-29'])
    ->setOutputLog(Yii::app()->getRuntimePath() . '/stats-generate.log');



return [
    'components' => [
        'log' => [
            'class' => \CLogRouter::class,
            'routes' => [
                'fileRoute' => [
                    'class' => \CFileLogRoute::class,
                    'logFile' => 'async-cmd.log',
                    'levels' => 'info',
                    'categories' => 'yii1tech.async-cmd',
                ],
            ],
            // ...
        ],
        // ...
    ],
    // ...
];



return [
    'components' => [
        \yii1tech\async\cmd\CommandDispatcher::class => [
            'class' => \yii1tech\async\cmd\CommandDispatcher::class,
            'commandRunner' => [
                'class' => \yii1tech\async\cmd\ArrayCommandRunner::class,
            ],
        ],
        // ...
    ],
    // ...
];



use yii1tech\async\cmd\Command;
use yii1tech\async\cmd\CommandDispatcher;

class StatsGenerateLauncherTest extends TestCase
{
    public function testLaunchStatsGenerate(): void
    {
        $launcher = Yii::app()->getComponent(StatsGenerateLauncher::class);
        $launcher->launch(); // dispatches async command inside
        
        $dispatcher = Yii::app()->getComponent(CommandDispatcher::class);
        $runner = $dispatcher->getCommandRunner();
        $command = $runner->getLastCommand();
        
        // check if the async command has been dispatched with the correct parameters:
        $this->assertTrue($command instanceof Command);
        $this->assertSame(StatsCommand::class, $command->getCommandClass());
        $this->assertSame('generate', $command->getCommandAction());
    }
}