PHP code example of vanilla / garden-progress

1. Go to this page and download the library: Download vanilla/garden-progress 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/ */

    

vanilla / garden-progress example snippets


use Garden\Progress\Progress;

$progress = new Progress();

$step1 = $progress->step("step1");

// Optional but completion is generally more easily tracked if you know how much work there is to do.
$step1->setTotal(count($thingsToDo));

// Track a bunch of work at once.
try {
    $thing->doWorkOnItems($thingsToDo);
    $step1->incrementSuccess(count($thingsToDo));
} catch (Exception $failed) {
    // Handle your error.
    $step1->incrementFailed(count($thingsToDo));
}

// Track specific IDs.
foreach ($thingsToDo as $thingID) {
    try {
        $thing->doThing($thingID);
        $step1->trackSuccessID($thingID);
    } catch (Throwable $e) {
        $step1->trackFailedID($thingID, $e);
    }
}

// Access current completion
$progress->step("step1")->completion->countFailed;
$progress->step("step1")->completion->countTotal;
$progress->step("step1")->completion->countSuccess;

$progress->step("step1")->transientData->successIDs;
$progress->step("step1")->transientData->failedIDs;
$progress->step("step1")->transientData->errorsByID[$someID]["message"] ?? null;

use Garden\Progress\Progress;

// You dump it down to an array/json
$progress = new Progress();
$progress
    ->step("step1")
    ->setTotal(10)
    ->incrementSuccess(10);
$progress
    ->step("step2")
    ->setTotal(20)
    ->incrementFailed(20);
$json = json_encode($progress);

// And pull it back out again.
$fromJson = Progress::fromArray(json_decode($progress, true));

use Garden\Progress\Progress;

$progress1 = new Progress();
$progress1->step("step1");

$progress2 = new Progress();
$progress2->step("step2");

$progress3 = new Progress();
$progress3->step("step2");
$progress3->step("step3");

$merged = $progress1->merge($progress2, $progress3);
// All these were merged together.
$merged->step("step1");
$merged->step("step2");
$merged->step("step3");