Download the PHP package skybluesofa/laravel-wrapped-facade without Composer
On this page you can find all versions of the php package skybluesofa/laravel-wrapped-facade. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download skybluesofa/laravel-wrapped-facade
More information about skybluesofa/laravel-wrapped-facade
Files in skybluesofa/laravel-wrapped-facade
Package laravel-wrapped-facade
Short Description Laravel facades with before/after method functionality
License MIT
Informations about the package laravel-wrapped-facade
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
Setup
Install the Config File
Publish the Config File
Run the artisan command to publish the package's config file:
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:
And now we'll create a SuperFruits
facade, though it could be named Fruits
, if you wanted:
When you run the facade code, you will get a returned array of fruits and vegetables.
Sideloading Functionality into the Facade
The difference is when you wish to add some new functionality to the method, before and/or after it gets run.
Example: Caching Results
We'll add a new attribute and a couple of methods:
Now when you run the facade code, you will still get a returned array of fruits and vegetables, but the next time it's accessed, it will be returned from the cache.
Example: Modifying Results
In this example, we're not going to cache the results. Instead we're going to filter the results based on the preferences of the logged in user.
We'll change the class to only have a 'postIndex' method:
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.
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:
We're going to assume that FruitValidator::validates()
will return false
. So now when you run the facade code, a \RuntimeException
will be thrown.
Please note that the $sideloadedMethodOrder
array can be formatted in multiple ways.
All of these are equally valid:
All versions of laravel-wrapped-facade with dependencies
laravel/framework Version ^10 || ^9.0
spatie/laravel-package-tools Version ^1.16