PHP code example of jeffersongoncalves / laravel-zero-console

1. Go to this page and download the library: Download jeffersongoncalves/laravel-zero-console 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/ */

    

jeffersongoncalves / laravel-zero-console example snippets


use Illuminate\Console\Command;
use JeffersonGoncalves\LaravelZero\Console\FormatsOutput;

class ListCommand extends Command
{
    use FormatsOutput;

    public function handle(): int
    {
        // Renders a table, or "No results found." when $rows is empty.
        $this->renderTable(['ID', 'State', 'Updated'], $rows);

        $this->line($this->colorize('OPEN', $this->stateColor('OPEN'))); // <fg=blue>OPEN</>
        echo $this->formatDate('2024-01-02 03:04:05');                   // 2024-01-02 03:04
        echo $this->formatDate(null);                                    // -

        return self::SUCCESS;
    }
}

protected function stateColors(): array
{
    return [
        'SHIPPED' => 'magenta',
        'OPEN' => 'cyan',
    ];
}

use JeffersonGoncalves\LaravelZero\Console\HandlesApiErrors;

class FetchCommand extends Command
{
    use HandlesApiErrors;

    public function handle(): int
    {
        return $this->handleApiErrors(function () {
            $data = $this->api->fetch(); // may throw any Throwable

            $this->renderTable(['ID'], $data);

            return self::SUCCESS;
        });
    }
}

use JeffersonGoncalves\LaravelZero\Console\ResolvesPath;

$path = $this->resolvePath($this->argument('path')); // realpath, or cwd when empty
$cwd  = $this->resolveCwd();                          // realpath of getcwd()