All Projects → victor-falcon → laravel-task

victor-falcon / laravel-task

Licence: MIT license
A simple way to trigger tasks in a clean and simple way

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-task

Private Composer Installer
Composer install helper outsourcing sensitive keys from the package URL into environment variables
Stars: ✭ 168 (+833.33%)
Mutual labels:  packagist
packagist-mirror-docker
🐋📦✂️📋📦 Docker image of packagist mirror
Stars: ✭ 28 (+55.56%)
Mutual labels:  packagist
generator-composer
🐘 Yeoman (http://yeoman.io) generator for a PHP Composer project
Stars: ✭ 16 (-11.11%)
Mutual labels:  packagist
Asset Packagist
Asset Packagist
Stars: ✭ 235 (+1205.56%)
Mutual labels:  packagist
HabboAPI
A PHP wrapper for the (undocumented) Habbo API
Stars: ✭ 34 (+88.89%)
Mutual labels:  packagist
firebase-php
Firebase Realtime Database PHP Wrapper
Stars: ✭ 41 (+127.78%)
Mutual labels:  packagist
Ansible Role Composer
Ansible Role - Composer PHP Dependency Manager
Stars: ✭ 149 (+727.78%)
Mutual labels:  packagist
yii2-fullcalendar-scheduler
Yii 2 component for easy fullcalendar scheduler integration
Stars: ✭ 24 (+33.33%)
Mutual labels:  packagist
silk
A modern API for WordPress.
Stars: ✭ 62 (+244.44%)
Mutual labels:  packagist
drupal8-vagrant
Simple Drupal 8 Development Environment
Stars: ✭ 59 (+227.78%)
Mutual labels:  packagist
Satis
Simple static Composer repository generator - For a full private Composer repo use Private Packagist
Stars: ✭ 2,722 (+15022.22%)
Mutual labels:  packagist
artisan-gui
Simple but yet powerful library for running almost all artisan commands.
Stars: ✭ 334 (+1755.56%)
Mutual labels:  packagist
Container
🚀 PHP Service Container with fast and cachable dependency injection.
Stars: ✭ 28 (+55.56%)
Mutual labels:  packagist
Artisan Gui
Simple but yet powerful library for running almost all artisan commands.
Stars: ✭ 226 (+1155.56%)
Mutual labels:  packagist
packagist
🐳 Dockette out-of-box Packagist (Nginx / Solr 6 / PHP 7.1+FPM)
Stars: ✭ 32 (+77.78%)
Mutual labels:  packagist
Hprose Php
Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP
Stars: ✭ 1,952 (+10744.44%)
Mutual labels:  packagist
zx-ip-address
Deprecated
Stars: ✭ 96 (+433.33%)
Mutual labels:  packagist
laravel-stripe-connect
🦓 Stripe Connect binding for Laravel
Stars: ✭ 73 (+305.56%)
Mutual labels:  packagist
composer-localdev-plugin
Composer Plugin for local development
Stars: ✭ 31 (+72.22%)
Mutual labels:  packagist
rippled-php
A PHP library for rippled (XRP Ledger) communication.
Stars: ✭ 33 (+83.33%)
Mutual labels:  packagist

Laravel Task

GitHub Workflow Status Packagist Packagist Packagist

🇪🇸 Documentación en español aqui

Table of content:

Installation

Install via composer

# PHP >= 8
composer require victor-falcon/laravel-task
# Previous PHP versions
composer require victor-falcon/laravel-task:1.1.4

Usage

1. Basic usage

Create a simple task using:

artisan task:make Shop/CreateUserShop

You can pass Shop/CreateUserShop to create the class in a sub-folder or just the task name. The default path is app/Tasks.

<?php

declare(strict_types=1);

namespace App\Tasks\Shop;

use VictorFalcon\LaravelTask\Task;
use VictorFalcon\LaravelTask\Taskable;

final class CreateUserShop implements Task
{
	use Taskable;

	private User $user;

	public function __construct(User $user)
	{
		$this->user = $user;
	}

	public function handle(ShopCreator $creator): Shop
	{
		// Create your shop
	}
}

and trigger it:

$shop = CreateUserShop::trigger($user);

2. With validation

If you want, you can pass validated data to your tasks using the Laravel validator. For example, If you need to validate a user creation you can do something like this.

final class CreateUser implements Task
{
	public function rules(): array
	{
		return [
			'name' => 'required|string|min:5',
			'email' => 'required|email|unique:users,email',
		];
	}

	public function handle(): User
	{
		return User::create($this->data);
	}
}

And then you can trigger your task with extra data using:

CreateUser::trigger()->withValid([
	'name' => 'Víctor Falcón',
	'email' => '[email protected]',
]);

You can customize the messages with the method messages(): array in your task or add custom attributes with customAttributes(): array.

If you want to customize the errors bag name of the validator just define the string $errorBag property in your class.

3. With authorization

Sometimes you need to check if the user triggering the task is authorized or not. You can do that by adding a simple authorize(): bool method to your task. If this method returns false an AuthorizationException will thrown on before execution.

public function authorize(): bool
{
	return $this->user()->can('create', Product::class);
}

In any task you can access to the current logged user with $this->user() or, if you want, you can pass a user object by doing:

CreateProduct::trigger()->by($user);

4. Recover response

By default, each task is executed without returning any value. So if you want to recover the result of a task you must call the result() method.

// This will return the result of the `handle` method inside our task
$product = CreateProduct::trigger()->withValid($data)->result();

Generate IDE Help

In order to make more easy to write and use your task you can generate a _ide_helper_tasks.php file automatically with the artisan task:ide-help command.

Update config

If you want, you can publish the package config to customize, for example, where do you want you task to be store at:

artisan vendor:publish --tag=laravel-task

Credits

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].