Open KKSzymanowski opened 3 years ago
Ability to set a property of an object to some config value when the object is constructed by the Container. API would look something like this:
class FooController extends Controller { #[Config('app.url')] protected $appUrl; #[Config('foo.bar', 'some default value')] protected $fooBar; }
Proof of concept:
Create a Config attribute:
#[Attribute(Attribute::TARGET_PROPERTY)] class Config { public function __construct($key, $default = null) { } }
Illuminate\Container\Container::build()
- return $reflector->newInstanceArgs($instances); + $object = $reflector->newInstanceArgs($instances); + + foreach($reflector->getProperties() as $property) { + foreach($property->getAttributes() as $attribute) { + if($attribute->getName() == Config::class) { + $property->setAccessible(true); + $property->setValue($object, call_user_func_array('config', $attribute->getArguments())); + } + } + } + + return $object;
What do you think?
@taylorotwell Can I get your opinion about this?
Ability to set a property of an object to some config value when the object is constructed by the Container. API would look something like this:
Proof of concept:
Create a Config attribute:
Illuminate\Container\Container::build()
What do you think?