Download the PHP package kirschbaum-development/laravel-actions without Composer
On this page you can find all versions of the php package kirschbaum-development/laravel-actions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kirschbaum-development/laravel-actions
More information about kirschbaum-development/laravel-actions
Files in kirschbaum-development/laravel-actions
Package laravel-actions
Short Description A Laravel package for handling actions and events
License MIT
Homepage https://github.com/kirschbaum-development/laravel-actions
Informations about the package laravel-actions
Laravel Actions
A package for handling simple actions with eventing.
Laravel Actions are simple job-like classes that don't interact with the queue. Actions are great to leverage when you have some simple functionality that you need to reuse.
But why would you want to use Actions you ask? Actions are great when you have small bits of code that you want to extract into small, testable classes. Actions can also take the place of queued jobs when you don't want or need that kind of power, or if you need the results of the code right now.
But the real power is with eventing.
This package exposes two events during your Action:
- Before the Action begins
- After the Action completes
The special sauce here is that you get to tell the Action which events you want triggered!
Requirements
PHP Version | Laravel Actions Version |
---|---|
^6.0, ^7.0 |
^0.1.0 |
^8.0 |
^0.2.0 |
Installation
Creating and Preparing the Action
Create a new Action with artisan command:
This will create a new Action class at app/Actions/ChuckNorris.php
. You are, of course, free to move the action wherever you want. Just make sure you update the namespace!
There are two public properties ready for your events: $before
and $after
. You can use one or both of these, and you can remove either of them if you don't use them in your Action class.
Next place your required arguments within the __construct()
method and your Action code within the __invoke()
method. You are free to return anything you might need from the invokeable method. Now we're ready to use the Action!
Usage
There are two different ways we can call our newly created Action.
From the Action
We can call one of three methods on the Action itself as long as the class is using the CanAct
trait.
The $data
is passed into the Action's constructor. You can pass as many arguments as needed in your use case.
The second two methods, actWhen
and actUnless
require a condition as the first variable. These work like other Laravel methods such as throw_if()
and throw_unless()
. Finally, you can pass as many arguments as needed for your Action after the condition.
Facade
The package also has a facade. Here's the syntax:
The usage is nearly identical to calling the methods directly on the Action as mentioned in the section above. The benefit here is that you can easily test actions using Action::shouldReceive('act')
, Action::shouldReceive('actWhen')
or Action::shouldReceive('actUnless')
.
Helpers
The package also has a few handy helpers to get Chuck in action. Here's the syntax:
Dependency Injection
You can even inject Actions as a dependencies inside your application!
Handling Failures
We all know Chuck Norris isn't going to fail us, but he isn't the only one using this... Handling failures is pretty easy with Actions. Out of the box, any exceptions thrown by your Action classes get handled by Laravel's exception handler. If you'd rather implement your own logic during a failure, add a failed()
method to your Action. It's that easy! You can return data from your failed()
method if you choose as well.
Hashtag #BAM!
Custom Exceptions
Another option for handling failures is to tell the Action to throw its own exception. If you don't need the extra overhead of writing your own failed()
method, you can just tell your Action to throw a custom exception. It's as simple as just defining the exception you want thrown from the Action.
Auto-Discovery and Configuration
Out of the box, actions are automatically discovered and bound to Laravel's container, which allows for easier testing of your actions. If you need to add a custom path if you are placing your actions somewhere other than app/Actions
, make sure to publish the configs.
If you want to disable auto-discovery, publish the config and return an empty array from the paths
key.
Testing
Have no fear. Testing all of this is very straightforward. There are two approaches to testing built in.
Testing Facades
If you are using Facades to implement your Actions, you can use the standard shouldReceive()
method directly from the Facade.
Mocking
If you are using helpers, the CanAct
trait, or dependency injection, you can easily mock the Action
class with Laravel's mocking tools.
Because actions are bound into Laravel's container by default, you can test specific actions as well.
Last thoughts
If for some reason you'd prefer not to use the cool Eventing system, Facades, Mocking, etc., that's fine. Just call your Action like this:
This will bypass all the magic and call the invoke method automagically, letting Chuck do his thing without anyone knowing, but why? ;)
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] or [email protected] instead of using the issue tracker.
Credits
- Brandon Ferens
- Inspired in part by Luke Downing's Laracon Online Winter '22 talk.
Sponsorship
Development of this package is sponsored by Kirschbaum, a developer driven company focused on problem solving, team building, and community. Learn more about us or join us!
License
The MIT License (MIT). Please see License File for more information.