https://github.com/skybluesofa/laravel-wrapped-facade
Add before- and after- method calls to Facades.
https://github.com/skybluesofa/laravel-wrapped-facade
facade laravel
Last synced: 8 months ago
JSON representation
Add before- and after- method calls to Facades.
- Host: GitHub
- URL: https://github.com/skybluesofa/laravel-wrapped-facade
- Owner: skybluesofa
- License: mit
- Created: 2024-01-20T03:47:50.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-20T15:23:41.000Z (over 1 year ago)
- Last Synced: 2025-01-04T18:29:03.571Z (9 months ago)
- Topics: facade, laravel
- Language: PHP
- Homepage:
- Size: 48.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Wrapped-Facade
Add before- and after- method calls to Facades.## Purpose
This library creates the ability to add pre- and post- methods when calling Facade methods. You can find an example in the `tests` folder.
It is most useful purpose that I have found is to cache information instead of running a request.
## Laravel Version Compatibility
This package supports Laravel `v9` and `v10`
## Installation
```
composer require skybluesofa/laravel-wrapped-facade
```## Setup
### Install the Config File
#### Publish the Config File
Run the artisan command to publish the package's config file:
```
php artisan vendor:publish --tag=wrapped-facade-config
```_or_
#### Copy the Config File
Copy the `wrapped-facade.php` file from the `vendor/skybluesofa/laravel-wrapped-facade/config` folder to your application's `config` folder.## Using Wrapped Facades
Wrapped Facades work, out-of-the-box, exactly the same as standard (Laravel Facades)[https://laravel.com/docs/10.x/facades].
### Basic Facade Functionality
Lets start with the base `Fruits` class:
```
'Apple',
2 => 'Banana',
3 => 'Carrot',
4 => 'Durifruit',
5 => 'Eggplant',
];public function getAll(): array
{
return $this->data;
}
}```
And now we'll create a `SuperFruits` facade, though it could be named `Fruits`, if you wanted:
```
mostHatedFruit;return array_diff(
$results,
[$hatedFruit]
);
}
}
```Now when you run the facade code, you will still get a returned array of fruits and vegetables,
but it 'Banana' has been removed from the array.```
\App\Classes\SuperFruitsSuperFruits::getAll();// Returns an array of fruits, sans Banana
[
1 => 'Apple',
2 => 'Carrot',
3 => 'Durifruit',
4 => 'Eggplant',
]
```#### Example: Using Multiple Sideloaded Functionality
In this example, we're going to validate the results before we cache them.
We'll update the class to include a new 'postIndexValidate' method and a 'sideloadedMethodOrder' property:
```
validates()) {
throw new \RuntimeException('Fruit does not validate!');
}return $results;
}protected static function postIndex(array $args, mixed $results): mixed
{
// In this example , the user hates 'Banana'
$hatedFruit = Auth::user()->mostHatedFruit;return array_diff(
$results,
[$hatedFruit]
);
}
}
```We're going to assume that `FruitValidator::validates()` will return `false`. So now when you run the facade code, a `\RuntimeException` will be thrown.
```
\App\Classes\SuperFruitsSuperFruits::getAll();// RuntimeException('Fruit does not validate!');
```Please note that the `$sideloadedMethodOrder` array can be formatted in multiple ways.
All of these are equally valid:```
// If you don't care what order things are run in:
// Don't add the $sideloadedMethodOrder property at all, or:
protected static $sideloadedMethodOrder = [];// If you have only a few sideloaded methods, a simple array might be easiest.
// The array lists the specific sideloaded method names:
protected static $sideloadedMethodOrder = [
'postIndexValidate',
'postIndex',
];// If you have quite a few sideloaded methods, a nested array might be clearer.
// The first level of the array is the sideloaded key (+ ).
// The second level of the array lists the specific sideloaded method names:
protected static $sideloadedMethodOrder = [
'postIndex' => [
'postIndexValidate',
'postIndex',
],
];```