haykel / laravel-action-maker
A Laravel package to generate Action classes with optional DB transactions and Pest tests.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/haykel/laravel-action-maker
Requires
- php: ^8.0
- illuminate/contracts: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0|^3.0
- phpunit/phpunit: ^10.0|^11.0
This package is auto-updated.
Last update: 2025-12-30 23:26:32 UTC
README
A Laravel package to generate Action classes with automatic transaction handling, custom namespaces, and Pest test generation.
Features
- Transactions by Default: Automatically wraps logic in
DB::transaction(configurable) - Smart Naming: Auto-appends configurable suffixes (e.g.,
CreateUser→CreateUserAction) - Folder Support: Easily organize actions into subfolders via
--folder - Test Generation: Generates a corresponding Pest unit test with
--with-test - Customizable: Publishable stubs and configuration
- Laravel 10, 11, & 12: Works with modern Laravel versions
- PHP 8.1+: Supports PHP 8.1, 8.2, and 8.3
Installation
You can install the package via composer:
composer require haykel/laravel-action-maker
The package will automatically register itself via Laravel's package auto-discovery.
Publish Configuration (Optional)
To customize the default behavior, publish the configuration file:
php artisan vendor:publish --tag=action-maker-config
This will create config/action-maker.php where you can customize:
- Default namespace for actions
- Class suffix
- Transaction behavior
- Test generation preferences
Publish Stubs (Optional)
To customize the generated code templates:
php artisan vendor:publish --tag=action-maker-stubs
This will publish stubs/action.stub and stubs/pest.stub to your project root.
Usage
Basic Action
php artisan make:action CreateUser
Result: app/Actions/CreateUserAction.php
<?php namespace App\Actions; use Illuminate\Support\Facades\DB; class CreateUserAction { public function execute() { return DB::transaction(function () { // TODO: Implement action logic here return true; }); } }
With Subfolder
php artisan make:action UpdateProfile --folder=User/Profile
Result: app/Actions/User/Profile/UpdateProfileAction.php
Without Transactions
php artisan make:action SendNotification --no-transaction
Result: Creates action without the DB::transaction wrapper.
With Pest Test
php artisan make:action DeleteUser --with-test
Result:
app/Actions/DeleteUserAction.phptests/Unit/Actions/DeleteUserActionTest.php
Combined Options
php artisan make:action ProcessPayment --folder=Billing/Payment --with-test --no-transaction
Configuration
The config/action-maker.php file contains the following options:
return [ // Default namespace (relative to App\) 'namespace' => 'Actions', // Suffix automatically appended to class names 'class_suffix' => 'Action', // Wrap execute() in DB::transaction by default 'use_transactions' => true, // Generate Pest tests by default (even without --with-test) 'generate_tests' => false, ];
Testing
This package includes comprehensive Pest tests. To run the tests:
composer install
composer test
For test coverage report:
composer test-coverage
Development Setup
If you want to contribute or modify this package locally:
- Clone the repository:
git clone https://github.com/HaykelRekik/laravel-action-maker.git
cd laravel-action-maker
- Install dependencies:
composer install
- Run tests:
composer test
Testing in a Local Laravel Project
To test the package in a local Laravel project before publishing:
- In your Laravel project's
composer.json, add a local repository:
{
"repositories": [
{
"type": "path",
"url": "../laravel-action-maker"
}
]
}
- Require the package:
composer require haykel/laravel-action-maker:@dev
- Test the commands:
php artisan make:action TestAction php artisan make:action AnotherAction --with-test --folder=User
Troubleshooting
Composer Dependency Conflicts
If you encounter dependency conflicts during installation, ensure your Laravel project meets the minimum requirements:
- PHP ^8.1|^8.2|^8.3
- Laravel ^10.0|^11.0|^12.0
The package uses illuminate/support and illuminate/contracts to ensure maximum compatibility.
Generated Classes Not Found
Run composer autoload dump after generating new classes:
composer dump-autoload
Credits
License
The MIT License (MIT). Please see License File for more information.