Lemon-Framework / Lemon

🍋 A php microframework
https://lemon-framework.github.io/docs/
GNU General Public License v3.0
24 stars 8 forks source link

[Container] adds injectable classes #130

Closed tenmajkl closed 1 year ago

tenmajkl commented 1 year ago

This PR adds injectable classes to container, that have their biggest impact on Container::call, because till now, you could pass some values, which would be passed along with injected ones. Now, if param has type that implements \Lemon\Contracts\Kernel\Injectable, it would (instead of just passing the value) convert passed value into given type via Injectable::fromInjection static method.

Example

Route::get('/users/{user}', function(User $user) {
    return $user;
});

and User looks like this:

class User implements Injectable
{
    public static function fromInjection(Container $container, mixed $value): self
    {
        return $container->get(UserFactory::class)->get($value);
    }
}

It basicaly converts the value into provided type with container.