Download the PHP package akaunting/laravel-mutable-observer without Composer
On this page you can find all versions of the php package akaunting/laravel-mutable-observer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-mutable-observer
Mutable Observer Package for Laravel
This package allows you to mute and unmute specific observer events at will. It ships with a trait that adds mutable methods to your observer classes, making it perfect for testing scenarios and preventing unwanted side effects.
Features
- ✅ Mute specific events - Target individual observer methods
- ✅ Mute all events - Silence entire observers with a single call
- ✅ Framework integration - Seamlessly works with Laravel's observer system
- ✅ Laravel 9-12 support - Compatible with all modern Laravel versions
- ✅ PHP 8.0-8.4 support - Uses modern PHP features like readonly properties
- ✅ Type-safe - Full type declarations for better IDE support
- ✅ Zero dependencies - Only requires Laravel framework
- ✅ Well tested - Comprehensive test coverage
Installation
Run the following command:
Usage
Basic Setup
Add the Mutable trait to your observer class:
Muting All Events
Perfect for testing scenarios where you want to disable all observer actions:
Muting Specific Events
Target individual observer methods while keeping others active:
Muting Multiple Events
Real-World Examples
Testing Scenario
Bulk Operations
Conditional Muting
Using Constants
API Reference
Mutable::mute($events = null)
Mutes the specified observer events.
Parameters:
$events(string|array|null) - The events to mute. Passnullor no arguments to mute all events.
Returns: void
Examples:
Mutable::unmute()
Unmutes all previously muted observer events.
Returns: void
Example:
Mutable::WILDCARD_EVENT
A constant representing the wildcard event that mutes all observer methods.
Value: '*'
Requirements
- PHP 8.0 or higher
- Laravel 9.0 or higher
Code Quality
This package maintains the highest code quality standards:
- ✅ PHPStan Level Max - Maximum static analysis level
- ✅ Psalm Level 1 - Strictest type checking
- ✅ PHP CS Fixer - PSR-12 compliant code style
- ✅ Infection - Mutation testing with 90%+ MSI
- ✅ 100% Test Coverage - Comprehensive test suite
- ✅ Strict Types - All files use
declare(strict_types=1)
Running Quality Checks
How It Works
The package uses a proxy pattern to intercept calls to your observer methods. When you call mute(), it registers a proxy in Laravel's service container that swallows the specified events. When you call unmute(), it removes the proxy and restores normal observer functionality.
Best Practices
-
Always unmute after muting - Especially in tests, use try-finally blocks:
-
Use setUp and tearDown in tests:
-
Be specific when possible - Mute only the events you need to disable:
- Document muted events - Add comments explaining why observers are muted:
Troubleshooting
Q: Observer still firing after muting?
A: Make sure the observer is registered through Laravel's observer system and the trait is properly imported.
Q: Can I mute observers globally?
A: Yes, mute them in a service provider's boot() method or middleware.
Q: Does this work with model events?
A: Yes, it works with any observer registered via Model::observe().
Changelog
Please see Releases for more information what has changed recently.
Contributing
Pull requests are more than welcome. You must follow the PSR coding standards.
Security
Please review our security policy on how to report security vulnerabilities.
Credits
- Cüneyt Şentürk
- Denis Duliçi
- Stephen Lewis
- All Contributors
License
The MIT License (MIT). Please see LICENSE for more information.