You can use middlewares to decorate any method calls on any object.
// Normal Call:
$myObj->myMethod();
// Decorated Call:
$myObj
->middlewares([...])
->myMethod():
composer require imanghafoori/laravel-middlewarize
Put the \Imanghafoori\Middlewarize\Middlewarable
trait on your class.
For example consider a simple repository class:
class UserRepository
{
use Middlewarable; // <---- Use "Middlewarable" trait on your class
public function find($id)
{
return User::find($id); // <---- we wanna cache it, right?
}
...
}
class CacheMiddleware
{
public function handle($data, $next, $key, $ttl)
{
// 1. This part runs before method call
if (Cache::has($key)) {
return Cache::get($key);
}
$value = $next($data); // <--- 2. Runs the actual method
Cache::put($key, $value, $ttl); // <-- 3. This part runs after method
return $value;
}
}
Since middlewares are resolved out of the laravel container, you can pass any abstract string as a middleware and bind it on the IOC:
public function boot()
{
app()->singleton('cacher', CacheMiddleware::class); // <---- Optional step
}
Cleaned controller will look like this:
public function show($id, UserRepository $repo)
{
$cachedUser = $repo
->middleware('cacher:fooKey,60')
->find($id);
}
Easy Peasy Yeah ?!
You totally separate the cache concern into a new class.
So let's compare...
Before utilizing middlewares our code was like this:
public function show($id, UserRepository $repo)
{
if (Cache::has('user.'.$id)) {
return Cache::get('user.'.$id); // <--- extra fluff around ->find($id)
}
$value = $repo->find($id); // <--- important method call here.
Cache::put('user.'.$id, $value, 60); // <--- extra fluff around ->find($id)
return $value;
}
public function show($id, UserRepository $repo)
{
$cachedUser = $repo
->middleware('cacher@MyHandle1:fooKey,60') // <--- Overrides default "handle" method name
->find($id);
}
public function show($id, UserRepository $repo)
{
$cachedUser = $repo->middleware(['middle1', 'middle2', 'middle3'])->find($id);
}
The order of execution is like that:
Start ===> ( middle1 -> middle2 -> middle_3 ( find ) middle_3 -> middle2 -> middle1 ) ===> result !!!
You wanna use facades to call the repo ?! No problem.
$cachedUser = UserRepositoryFacade::middleware('cacher:fooKey,60 seconds')->find($id);
You can also use objects as middlewares for more eloborated scenarios.
$obj = new CacheMiddleware('myCacheKey', etc...); // <---- you send depedencies to it.
$repo->middleware($obj)->find($id);
User::find($id); // <--- Sample static method call
User::middlewared('cache:key,10')->find($id); // <--- you can have a decorated call
// also you must put 'middlewarable' trait on User model.
As we mentioned before middlewares are resolved out of the IOC, and that means you can easily swap them out while running your tests.
class NullCacheMiddleware
{
public function handle($data, $next, $key, $ttl)
{
return $next($data); // <--- this "null middleware" does nothing.
}
}
public function testSomeThing()
{
app()->singleton('cacher', NullCacheMiddleware::class); // <--- this causes to replace the cache middleware
$this->get('/home');
}
Here we have neutralized the middleware to do "nothing" while the tests are running.
It is important to know if you throw an exception in your method, the post middlewares still execute and the value of $value = $next(data)
would be the thrown exception.
The exception is rethrown when all middlewares finished executing.
If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.
As always if you found this package useful and you want to encourage us to maintain and work on it. Just press the star button to declare your willing.
💎 A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.
💎 It allows to write expressive code to authorize, validate and authenticate.
💎 A minimal yet powerful package to give you opportunity to refactor your controllers.
💎 It allows you login with any password in local environment only.
💎 It allows you to decouple your eloquent models to reach a modular structure
Logic will get you from a to z, imagination will take you everywhere.
"Albert Einstein"