-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite using native Attributes instead of annotations. #138
base: master
Are you sure you want to change the base?
Conversation
Cool! |
…ons (for clearer diff) - Updated docs - Implement v2 of psr/container
@Netiul if it makes things easier, I'd be fine supporting only named parameters. |
@shochdoerfer It's actually not very hard to support both. This is basically all that's needed, but it might be a bit confusing in userland to have both ways availabe... |
@shochdoerfer I've have updated the top post with a summary of the main changes and some points up for discussing. |
@Netiul awesome work! Thanks a lot for tackling this. Not allowing built-in types as return type alias makes sense to me. That seems to be a "bug" in the previous versions that I missed. Parameter validation also makes sense to me. I would even go that far and get rid of the positional logic and "just" keep the named parameter logic to reduce potential problems when parameters are re-ordered any types don't match anymore. Since PHP 8 gives us the ability to use named parameters it makes sense to make use of them. Not that I like them in code but for the configuration that sounds fine. @heiglandreas any other validations, you would like to see? I hear you are big Disco fan, so may have opinions :) |
For sure butwon't be able to voice them for another week. So if you don't want to wait that's fine with me 😉 |
@heiglandreas have you had a chance to think about this? |
For me the whole Parameter declaration is not 💯% clear as most of it seems to duplicate internal functionality.
So for me the only declaration that introduces something, that can not be declared via the function signature, would be the I would also only go for named parameters only. The key When there are multiple parameter declarations for the same Question: Can we use aliases from the DI within the configuration? $testConfig = [
'db' => '@sqlite',
];
$prodConfig = [
'db' => '@postgresql',
]; #[Configuration]
class MyConfiguration
{
#[Parameter(
name: 'db',
key: 'db',
)]
public function mySampleService(PDO $db) : SampleService
{
$service = new SampleService();
$service->setDatabase($test);
return $service;
}
#[Bean]
#[Alias(name: 'postgresql')]
public function productionDatabase(): PDO
{
// Define Postgresql service
}
#[Bean]
#[Alias(name: 'sqlite')]
public function testDatabase(): PDO
{
// Define SQLite-Service
}
} |
And I'm absolutely in favour of not allowing native types as type-aliases. |
@Netiul does the feedback make sense to you? :) |
Yes! Sorry for the late reply :) Thank for the feedback @heiglandreas. It makes totally sense to use the functions signature to declare defaults and what parameter is required. Funny how one can be easily fixated on an existing path 😆 About "last parameter with the same name wins", shouldn't we just hard fail at compile time? It's not really something that can only be detected at runtime. |
Yeah. failing at compile time makes more sense. There can only be one |
I will process the feedback shortly. I am currently stuck in work. |
This is an implementation of #131
Changes
Bean
Use the Bean attribute to define a bean and set their
singleton
,lazy
orscope
option. Use the Parameter and Alias attribute to define parameters and aliases.Alias
The purpose of the Alias attribute is to define a named alias. The name must be at least 1 character of length.
To define the return type as an alias use
#[TypeAlias]
. MultipleAlias
attribute are allowed, only 1TypeAlias
. BothAlias
andTypeAlias
only affect Bean definitions.BeanPostProcessor
To define a bean post processor apply
#[BeanPostProcessor]
attribute. Add#[Parameter]
attributes to define parameters.Parameter
Attribute a Bean or BeanPostProcessor with 1 or multiple Parameter attributes.
⚠️ This is the equivalent of
key
used to look up the value in the configuration array.name
in 0.x version.name
has now a different purpose:name
corresponds with the name of argument of the method definition.default
andrequired
act the same as in 0.x.Disco will use the feature named arguments of PHP 8, to pass the arguments to the method call.
About positional parameters
At this moment the
name
option is not mandatory. When omitted the Parameter becomes a positional argument. Disco will pass positional arguments to the method function followed by the named arguments as required by PHP, see Example #16.The mutual order of the positional parameters is of course important.
The base line is that the same rules and restrictions apply as when using and mixing named and positional arguments when calling the method yourself. Read more about it the PHP docs.
To be discussed
Builtin types as return type alias
In 0.x it was possible to define the return type alias when the actual return type was a builtin type like
string
,int
,float
,bool
,array
,object
,callable
oriterable
. This is now not possible anymore. An exception will be thrown. An example of an invalid configuration:Parameter validation
At compile time, should we validate configured parameters with the method signature? Possible validations:
name
option van the Parameter should correspond with the name of one of the argumentsname
should not be allowedAnything else?