Open imanghafoori1 opened 5 years ago
Things like repositories should be using an interface, which can then be decorated in the container binding itself:
use Illuminate\Contracts\Cache\Repository as CacheRepository;
$this->app->singleton(UserRepositoryContract::class, function ($app) {
// No need to modify underlying repository classes to add trait
return new CachingRepository(
$app->make(EloquentUserRepository::class);
$app->make(CacheRepository::class)
);
});
class UserController extends Controller
{
public function __construct(UserRepositoryContract $users)
{
// No need to litter client client code with middleware to use in repository
$this->users = $users;
}
}
How to share decorators in packages?? that decorator you mentioned is tightly coupled to a specific repo class.
If you decorate the repo interface at boot phase how do you decide to skip decorators in case you don't what them in a specific controller?
What is harmful about using a trait? Is creating interfaces and all the binding easier?
Before eagerly down vote, think it a second time.
Before eagerly down vote, think it a second time.
@imanghafoori1 Stop getting so defensive. I read your proposal, I disagreed. There was no rushing.
We can implement a simple trait to put on any class and then be able to decorate it's methods with laravel middlewares. Why limit the use of middlewares only to Http section ?
In the controller :
or even directly on eloquent models :
there are a lot of other use cases for middlewares. (like sanitization of inputs and outputs, logging, firing events,...)
The important part is that:
I have implemented the trait as a POC package and it works. The is just a simple wrapper around the Pipeline class which laravel uses for Routing stuff.
https://github.com/imanghafoori1/laravel-middlewarize