Giter Site home page Giter Site logo

laravel-hush's Introduction

PHP Version Support GitHub Workflow Status

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 [email protected]. 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.

laravel-hush's People

Contributors

kfriars avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

 avatar

Watchers

Dave Kellam avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.