A Laravel package for generate action classes using Artisan Command.
Requires PHP 8.1+
Require using Composer.
composer require ceceply/action --devPut Ceceply\Action\Providers\ActionServiceProvider::class into list of service providers in config/app.php.
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
Ceceply\Action\Providers\ActionServiceProvider::class,
/*
* Application Service Providers...
*/
])->toArray(),In this package, an Action Class is a simple class that has only one task. Example, we have an Action class named CreatePayment. This class will has only one task, which is to create payments.
Also in this package, by default an Action class will implement interface. That interface will be automatically generated, before the action class generated. Example, if the Action class is named CreatePayment, the interface that class implements will be named CreatesPayments. You can customize the interface name later.
Run the following command to generate a new Action Class named CreatePayment.
php artisan make:action CreatePaymentIf that command executed, an Action Class with interface implemented by the class generated.
// app/Actions/Payment/Contracts/CreatesPayments.php
<?php
namespace App\Actions\Payment\Contracts;
use App\Models\Payment;
interface CreatesPayments
{
public function create(array $inputs);
}// app/Actions/Payment/CreatePayment.php
<?php
namespace App\Actions\Payment;
use App\Actions\Payment\Contracts\CreatesPayments;
use App\Models\Payment;
class CreatePayment implements CreatesPayments
{
public function create(array $inputs)
{
// TODO: create
}
}You can customize the method name when writing the command.
If the method name is
createorupdate, method will automatically have an array parameter namedinputs.
php artisan make:action Payment/CreatePayment --action=handleOr shorter.
php artisan make:action Payment/CreatePayment -ahandleOutput.
// app/Actions/Payment/Contracts/CreatesPayments.php
interface CreatesPayments
{
public function handle();
}
// app/Actions/Payment/CreatePayment.php
class CreatePayment implements CreatesPayments
{
public function handle()
{
// TODO: handle
}
}You can add a model to your Action Class. The model will be placed in first parameter of the action method.
php artisan make:action Payment/CreatePayment --model=PaymentOr shorter.
php artisan make:action Payment/CreatePayment -mPaymentOutput.
// app/Actions/Payment/Contracts/CreatesPayments.php
<?php
namespace App\Actions\Payment\Contracts;
use App\Models\Payment;
interface CreatesPayments
{
public function create(Payment $payment, array $inputs);
}// app/Actions/Payment/CreatePayment.php
<?php
namespace App\Actions\Payment;
use App\Actions\Payment\Contracts\CreatesPayments;
use App\Models\Payment;
class CreatePayment implements CreatesPayments
{
public function create(Payment $payment, array $inputs)
{
// TODO: create
}
}Instead of defining model manually, you can add --guess-model option when writing the command.
php artisan make:action Payment/CreatePayment --guess-modelOr shorter.
php artisan make:action Payment/CreatePayment -gLast word of the class name will be considered as the model name. If the last word is plural, it will be changed to singular first.
You can customize interface name when writing the command.
php artisan make:action Payment/CreatePayment --interface=Payment/Contracts/CreatePaymentContractsOr shorter.
php artisan make:action Payment/CreatePayment -iPayment/Contracts/CreatePaymentContractsYou can create an Action Class without implementing an interface.
php artisan make:action Payment/CreatePayment --without-interfaceOr shorter.
php artisan make:action Payment/CreatePayment -wCreate the class even if the class already exists.
php artisan make:action Payment/CreatePayment --forceOr shorter.
php artisan make:action Payment/CreatePayment -fCreate the interface and the class even if the class and the interface already exists.
php artisan make:action Payment/CreatePayment --force-bothOr shorter.
php artisan make:action Payment/CreatePayment -FIf you want to create the interface only, you can do that by using the make:iaction command. So only the interface generated.
php artisan make:iaction Payment/CreatesPaymentsEven if you create the interface only, you can still customize action method name, add model, guess model and force create interface.
php artisan make:iaction Payment/CreatesPayments -ahandlephp artisan make:iaction Payment/CreatesPayments -mPaymentphp artisan make:iaction Payment/CreatesPayments -gphp artisan make:iaction Payment/CreatesPayments -fThis package is an open-sourced software licensed under the MIT license.