PHP code example of glugox / actions

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

    

glugox / actions example snippets


use Glugox\Actions\Contracts\Action;
use Glugox\Actions\DTO\ActionContext;
use Glugox\Actions\Support\Progress;

class ExportUsers implements Action
{
    public function name(): string
    {
        return 'Export Users';
    }

    public function handle(ActionContext $ctx, Progress $progress): array
    {
        $users = \App\Models\User::query()->cursor();
        $count = \App\Models\User::count();
        $done = 0;

        foreach ($users as $u) {
            // ... export ...
            $done++;
            if ($done % 50 === 0) {
                $progress->update(intval($done / max($count,1) * 100), "Exported {$done}/{$count} users");
            }
        }

        return ['message' => 'Export done', 'file' => '/storage/exports/users.csv'];
    }
}

use Glugox\Actions\ActionRunner;

$runner = app(ActionRunner::class);
$run = $runner->run(new ExportUsers(), params: ['format' => 'csv'], userId: auth()->id());
// or queued:
$run = $runner->dispatch(new ExportUsers(), params: ['format' => 'csv'], userId: auth()->id());

Broadcast::channel('private-action-run.{id}', function ($user, $id) {
    // Check that the user can view this run id
    return true; // Replace with your policy
});
bash
composer vendor:publish --provider="Glugox\Actions\ActionServiceProvider" --tag=config
php artisan migrate