PHP code example of vectorial1024 / laravel-process-async

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

    

vectorial1024 / laravel-process-async example snippets


// define the task...
$target = "document.txt";
$task = new AsyncTask(function () use ($target) {
    file_put_contents($target, "Hello World!");
});

// if you are using interfaces, then it is just like this:
// $task = new AsyncTask(new WriteToFileTask($target, $message));

// then start it.
$task->start();

// the task is now run in another PHP process, and will not report back to this PHP process.

// start with the default time limit...
$task->start();

// start task with a different time limit...
$task->withTimeLimit(15)->start();

// ...or not have any limits at all (beware of orphaned processes!)
$task->withoutTimeLimit()->start();

// create a task with a specified task ID...
$task = new AsyncTask(function () {}, "customTaskID");

// will return a status object for immediate checking...
$status = $task->start();

// in case the task ID was not given, what is the generated task ID?
$taskID = $status->taskID;

// is that task still running?
$status->isRunning();

// when task IDs are known, task status objects can be recreated on-the-fly
$anotherStatus = new AsyncTaskStatus("customTaskID");

// you may obtain the task status in the usual way...
$fakeTask = new FakeAsyncTask(/* ... */, taskID: "TestingTask");
$fakeStatus = $fakeTask->start();

// ...or just construct it directly
$fakeStatusDirect = new FakeAsyncTaskStatus("TestingTask");
// both are the same
assert($fakeStatus == $fakeStatusDirect); // passes

// in your test code, fake task status can be used just like the normal task status:
$fakeStatus->isRunning(); // default returns true

// note: FakeAsyncTaskStatus defaults to "is running" when constructed
// to simulate "task ended", simply do:
$fakeStatus->fakeStopRunning();
// then, the following will return false
$fakeStatus->isRunning(); // returns false