plank/laravel-hush

A targeted version of the Laravel Frameworks withoutEvents method.

v1.0.1 2024-04-22 22:18 UTC

This package is auto-updated.

Last update: 2024-04-29 08:42:39 UTC


README

hush.png

PHP Version Support GitHub Workflow Status 68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f636f7665726167652f706c616e6b2f6c61726176656c2d687573683f636f6c6f723d253233666639333736266c6162656c3d74657374253230636f766572616765266c6f676f3d636f64652d636c696d617465266c6f676f436f6c6f723d253233666666 68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f706c616e6b2f6c61726176656c2d687573683f636f6c6f723d253233353238636666266c6162656c3d6d61696e7461696e61626c696c697479266c6f676f3d636f64652d636c696d617465266c6f676f436f6c6f723d253233666666

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!

68747470733a2f2f706c616e6b2e636f2f6f70656e2d736f757263652f62616e6e6572

 

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.