generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from oprypkhantc/macroable-check
Make Check macroable and allow chaining callables in if() and unless()
- Loading branch information
Showing
4 changed files
with
141 additions
and
32 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
docs/basic-usage/conditionally-running-or-modifying-checks.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
title: Conditionally running or modifying checks | ||
weight: 5 | ||
--- | ||
|
||
This package provides methods to run certain checks only when specified conditions are met. | ||
|
||
If you would like to conditionally run a check, you can use the `if` and `unless` methods. | ||
For more control, you can also use callables. They are evaluated every time a health check is run. | ||
|
||
```php | ||
use Spatie\Health\Facades\Health; | ||
use Spatie\Health\Checks\Checks\DebugModeCheck; | ||
use Spatie\Health\Checks\Checks\RedisCheck; | ||
|
||
Health::checks([ | ||
DebugModeCheck::new()->unless(app()->environment('local')), | ||
RedisCheck::new()->if(fn () => app(SomeHeavyService::class)->shouldCheckHealth()), | ||
]); | ||
``` | ||
|
||
## Custom condition methods | ||
|
||
You may find yourself repeating conditions for multiple checks. To avoid that, | ||
you can register a Laravel macro on a check with a custom condition method. | ||
|
||
```php | ||
use Spatie\Health\Facades\Health; | ||
use Spatie\Health\Checks\Check; | ||
use Spatie\Health\Checks\Checks\DebugModeCheck; | ||
use Spatie\Health\Checks\Checks\RedisCheck; | ||
|
||
Health::macro('ifEnvironment', fn (string|array $envs) => app()->environment($envs)); | ||
|
||
Health::checks([ | ||
DebugModeCheck::new()->ifEnvironment('production') | ||
]); | ||
``` | ||
|
||
## Chaining conditions | ||
|
||
Sometimes you need more than one condition on a check, so you may chain two or more of them | ||
simply by calling `if` or `unless` multiple times. They are evaluated in the order that | ||
you define them. | ||
|
||
```php | ||
use Spatie\Health\Facades\Health; | ||
use Spatie\Health\Checks\Checks\DebugModeCheck; | ||
use Spatie\Health\Checks\Checks\RedisCheck; | ||
|
||
Health::checks([ | ||
DebugModeCheck::new() | ||
->unless(app()->environment('local')) | ||
->if(fn () => app(SomeHeavyService::class)->shouldCheckHealth()), | ||
]); | ||
``` | ||
|
||
## Modifying checks on a condition | ||
|
||
You may want to slightly change check's configuration under a specific condition. You can do | ||
so using `when` and `doUnless` methods. In this example, a smaller memory limit is enforced | ||
on a local environment. | ||
|
||
```php | ||
use Spatie\Health\Facades\Health; | ||
use Spatie\Health\Checks\Checks\RedisMemoryUsageCheck; | ||
|
||
Health::checks([ | ||
RedisMemoryUsageCheck::new() | ||
->failWhenAboveMb(1000) | ||
->when( | ||
app()->environment('local'), | ||
fn (RedisMemoryUsageCheck $check) => $check->failWhenAboveMb(200) | ||
), | ||
]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters