|
1 | | -# fraction |
| 1 | +# Fraction for Laravel |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <img src="./arts/logo.png" alt="Fraction for Laravel" width="150"> |
| 5 | +</p> |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +- [About](#about) |
| 10 | +- [Installation](#installation) |
| 11 | +- [Using](#using) |
| 12 | + - [Default](#default) |
| 13 | + - [Queued](#queued) |
| 14 | + - [Deferred](#deferred) |
| 15 | + - [Hooks](#hooks) |
| 16 | +- [Contribuing](#contribuing) |
| 17 | +- [License](#license) |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## About |
| 22 | + |
| 23 | +There's no denying that the "Action Pattern" in the Laravel ecosystem is extremely useful and widely used. However, action classes require "too much content" to do basic things. Let's review a basic action class: |
| 24 | + |
| 25 | +```php |
| 26 | +namespace App\Actions; |
| 27 | + |
| 28 | +use App\Models\User; |
| 29 | + |
| 30 | +class CreateUser |
| 31 | +{ |
| 32 | + public function handle(array $data): User |
| 33 | + { |
| 34 | + return User::create($data); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +We have a namespace, a class, a method, a return type, a model import, an array as a parameter... all of this to create a user. It's overkill for such a simple task, isn't it? |
| 40 | +The solution is the **Fraction for Laravel** package. It allows you to write actions in a simpler and more direct way, without the need for all this structure. See what the same example would look like with Fraction: |
| 41 | + |
| 42 | +```php |
| 43 | +// app/Actions/CreateUser.php |
| 44 | + |
| 45 | +execute('create user', function (array $data) { |
| 46 | + return User::create($data); |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +```php |
| 51 | +namespace App\Http\Controllers; |
| 52 | + |
| 53 | +class CreateUserController extends Controller |
| 54 | +{ |
| 55 | + public function store(Request $request) |
| 56 | + { |
| 57 | + // ... |
| 58 | + |
| 59 | + $user = run('create user', $request->all()); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +With Fraction for Laravel, we focused on simplifying action creation while maintaining code clarity and readability and **focusing on what really matters: the action logic.** No classes, no namespaces, no fluff. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Installation |
| 69 | + |
| 70 | +```bash |
| 71 | +composer require fraction/fraction |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## Using |
| 77 | + |
| 78 | +Primeiro de tudo, você deve criar as ações, e para isso você pode usar o comando `make:action` do Artisan: |
| 79 | + |
| 80 | +```bash |
| 81 | +php artisan make:action CreateUser |
| 82 | +``` |
| 83 | + |
| 84 | +Essa ação irá gerar um arquivo em `app/Actions/CreateUser.php` com o seguinte conteúdo: |
| 85 | + |
| 86 | +```php |
| 87 | +execute('create user', function () { |
| 88 | + // ... |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +Por padrão, o namespace utilizado para criação e leitura de ações é `App\Actions`, mas você pode alterar isso no arquivo de configuração `config/fraction.php`: |
| 93 | + |
| 94 | +```bash |
| 95 | +php artisan vendor:publish --tag=fraction-config |
| 96 | +``` |
| 97 | + |
| 98 | +### Default |
| 99 | + |
| 100 | +Para executar uma ação, você pode usar a função `run`: |
| 101 | + |
| 102 | +```php |
| 103 | +run('create user'); |
| 104 | +``` |
| 105 | +Opcionalmente você pode passar argumentos para a ação: |
| 106 | + |
| 107 | +```php |
| 108 | +use App\Models\User; |
| 109 | + |
| 110 | +execute('create user', function (User $user) { |
| 111 | + // ... |
| 112 | +}); |
| 113 | + |
| 114 | +// ... |
| 115 | + |
| 116 | +run('create user', auth()->user()); |
| 117 | +``` |
| 118 | + |
| 119 | +Como as ações são totalmente resolvidas pelo container do Laravel, você pode contar com a resolução de dependências do Laravel para injetar qualquer dependência necessária na ação. Por exemplo, se você quiser injetar uma instância de `Illuminate\Http\Request`: |
| 120 | + |
| 121 | +```php |
| 122 | +use Illuminate\Http\Request; |
| 123 | + |
| 124 | +execute('create user', function (Request $request) { |
| 125 | + // ... |
| 126 | +}); |
| 127 | + |
| 128 | +// ... |
| 129 | + |
| 130 | +run('create user'); |
| 131 | +``` |
0 commit comments