plank / laravel-hush
A targeted version of the Laravel Frameworks withoutEvents method.
Installs: 2 731
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- illuminate/contracts: ^10|^11
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
This package is auto-updated.
Last update: 2024-10-22 10:58:13 UTC
README
Laravel Hush
laravel-hush is a Laravel package that allows you to disable the observers and handlers for model events during the execution of a passed in closure. It functions as a more targetted version of the withoutEvents()
method that ships with Laravel.
Table of Contents
Installation
You can install the package via composer:
composer require plank/laravel-hush
Usage
The package ships with one trait: HushesHandlers
To enable the functionality on a model, simply include the trait on it.
It implements four methods:
withoutObserver(string $observer, Closure $callback)
This method will disable any handlers in the provided class for all of the models observable events during the exection of the callback.
Example assuming User
uses HushesHandlers
. In this example no handlers from the entire UserObserver
class will be called during the execution of the callback, for any observable event.
User::withoutObserver(UserObserver::class, function () { User::create(['name' => 'John Doe']); });
withoutObservers(array $observers, Closure $callback)
This method is the same as withoutObserver()
but allows you to pass in an array of observer classes to disable.
For example:
User::withoutObservers([UserObserver::class, ExpirableObserver::class], function () { User::create(['name' => 'John Doe']); });
withoutHandler(string $event, Closure $callback, array $classes = [])
This method will disable handlers for the provided event during the execution of the callback.
When no classes are provided, it will disable all handlers for the event.
When classes are provided (including classes where handlers have been added statically) it will only disable the registered handlers which are from the provided classes.
In the following example all created
event handlers would be disabled, however if there were creating
handlers registered for example, those event handlers would still run.
User::withoutHandler('created', function () { User::create(['name' => 'John Doe']); });
In the following example all created
event handlers defined in the User
model would be disabled, however if there was a created
handler in a UserObserver
class (or anywhere else), it would still handle the event.
User::withoutHandler('created', function () { User::create(['name' => 'John Doe']); }, [User::class]);
withoutHandlers(array $events, Closure $callback, array $classes = [])
This method is the same as withoutHandler()
but allows you to pass in an array of events to disable handlers for.
Credits
License
The MIT License (MIT). Please see License File for more information.
Security Vulnerabilities
If you discover a security vulnerability within siren, please send an e-mail to security@plankdesign.com. All security vulnerabilities will be promptly addressed.
Check Us Out!
Plank focuses on impactful solutions that deliver engaging experiences to our clients and their users. We're committed to innovation, inclusivity, and sustainability in the digital space. Learn more about our mission to improve the web.