surgiie / console
A base command and set of useful traits/support classes for Laravel or Laravel Zero commands.
Installs: 376
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Type:template
pkg:composer/surgiie/console
Requires
- php: ^8.1
- illuminate/console: ^10.0
- illuminate/validation: ^10.0
- laravel/prompts: ^0.1.11
- nunomaduro/termwind: ^1.3
- spatie/invade: ^1.1
- spatie/once: ^3.1
- surgiie/blade: ^3.0.0
- surgiie/transformer: ^0.3.0
- vlucas/phpdotenv: ^5.5
Requires (Dev)
- laravel/pint: ^1.2
- mockery/mockery: ^1.4.4
- pestphp/pest: ^1.21.3
- symfony/var-dumper: ^6.0
- dev-master
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.0
- v3.1.0
- v3.0.0
- v2.x-dev
- v2.2.0
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.17.0
- v0.16.0
- v0.15.3
- v0.15.2
- v0.15.1
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.0
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.0
- dev-bumps
- dev-disable-flag
- dev-improve-errors
- dev-fix-state-file-issue
- dev-throw-e
- dev-phar-fixes
- dev-try-catch
- dev-succeed
- dev-bring-back-loader
- dev-update-debug
- dev-remove-realpath
- dev-update-dep
- dev-remove-task
- dev-move-clear-lilne
- dev-remove-line-clear
- dev-spacing
- dev-handle-array-input
- dev-update-actions
- dev-tests
- dev-backup-command
- dev-exit-exception
- dev-fix-undefined-function-bug
- dev-check-pcntl
- dev-lang
- dev-fix-arbitrary-data-bug
This package is auto-updated.
Last update: 2024-02-26 17:55:27 UTC
README
A base command and set of useful support trait/classes for Laravel or Laravel Zero commands.
Installation
composer require surgiie/console
Features
Merged Data
All arguments and options are merged into a single $this->data collection, giving a fluent object to pull and work with option/arg data.
<?php namespace App\Console\Commands; use Surgiie\Console\Command; class ExampleCommand extends Command { public function handle() { $this->data->get("some-arg-or-option", 'default'); } }
Check if options were passed:
<?php namespace App\Console\Commands; use Surgiie\Console\Command; class ExampleCommand extends Command { protected $signature = "example {--iterations=}"; public function handle() { // check if the user passed the --iterations flag in the command call. if($this->optionWasPassed("iterations")){ } } }
Store values for performance into cache array
Helpful for caching instances into a array property if going to be called repeatedly.
protected function example() { // get a value or store it in the cache array if it doesnt exist return $this->fromArrayCache('example', fn () => new Example); }
Validation
Utilize Laravel Validation for Arguments & Options
<?php namespace App\Console\Commands; use Surgiie\Console\Command; class ExampleCommand extends Command { protected $signature = "example {--iterations=}"; public function rules() { return [ 'interations'=>'required|numeric' ]; } public function messages() { // custom validation messages return ['...']; } public function attributes() { // custom validation attributes return ['...']; } }
Arbitrary Options
To allow your command to accept arbitrary options not part of the command signature:
<?php namespace App\Console\Commands; use Surgiie\Console\Command; class ExampleCommand extends Command { public function arbitraryOptions() { return true; } public function handle() { // available if --something option is passed: $something = $this->arbitraryData->get("something") } }
Note Arbitrary options are parsed as is, without any validation or transformation, so ensure you run escapeshellarg or validate on any values that will be used in a shell command.
Argument & Option Transformation/Formatting
Transform, format, or sanitize input and arguments easily before handle is called, using a validation rule like syntax:
protected function transformers() : array { return [ 'some-option'=>['trim', 'ucwords'] ]; } protected function transformersAfterValidation() : array { return [ 'some-option'=>['strtoupper'] ]; }
*Note - For more, read the surgiie/tranformer readme docs.
Note - The base command performs some default tranformations before custom defined ones, they are as follows:
- All options with "date" in their name, are automatically converted to
\Carbon\Carboninstances.
Check Requirements
Provide a list of requirements before the handle is called:
public function requireSomethingOrFail(): string { // throw an exception: throw new FailedRequirementException("Failed to meet some requirment"); // or return an error string: return "Failed to meet some requirement"; } public function requirements(): array { return [ 'docker', //default for a string value checks if 'docker' is in $PATH with `which <value>` "requireSomethingOrFail", //unless the method exists on the class, it will call that instead function () { // can use callback that returns an error string $process = new Process(['docker', 'info']); $process->run(); return $process->getOutput() == '' ? 'Docker is not running' : ''; }, // can use also class constants or instances that have __invoke method. new Example, Example::class ]; }
Note If any of the methods above return an error string or raise FailedRequirementException, the handle method will not be called.
In addition, if you need custom logic to check if a string path is available, you can overwrite the following method:
/** * Check if a executable is in $PATH. * * @param string $requirement * @return string */ protected function checkWhichPath(string $requirement): string { $process = (new Process(['which', $requirement])); $process->run(); return $process->getOutput() == '' ? "This command requires $requirement." : ''; }
Render Files With Blade Engine:
An exented version of the blade engine is available to compile any textual file:
public function handle() { $contents = $this->render('/some/file', ['var'=>'example']); } // set a custom path for compiled/cached files. Default is /tmp/.compiled or tests/.compiled when running unit tests public function bladeCompiledPath(): string { return '/custom/directory'; }
Long Running Tasks
To give a better visual experience for long running tasks, you can use the runTask method:
$this->runTask("Doing stuff", function($task){ sleep(4); // simulating stuff. return true; // return whether task succeeded or not. }, spinner: true); // show spinner while task is running.
Note - In order to show a animated spinner, the pcntl PHP extension must be installed. When this extension is not available, a static version of the spinner will appear instead.
Custom Task Finished Text
When the task is completed, you can customize text shown the task has finished:
$this->runTask("Doing stuff", function($task){ sleep(4); // simulating stuff. }, finishedText: "Finished doing stuff");
Call Succeeded/Failed Functions Automatically
Automatically calls succeeded and failed based on handle exit code.
public function succeeded() { // called when handle int is 0 $this->components->info("It ran successfully"); } public function failed() { // called when handle int is 1 $this->components->info("It didnt run successfully"); }