-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Drivers can now be configured #735
base: 1.11
Are you sure you want to change the base?
Conversation
1aed7e9
to
8c2a866
Compare
8c2a866
to
6eff4c4
Compare
/** @var class-string|null $class */ | ||
$class = self::$drivers[$type]['class'] ?? null; | ||
if (null !== $class) { | ||
$driver = new $class(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, That'd be better to use service instead of instantiating class, we can't have dependency injection in the drivers with your implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use a service because all the parameters created by the SyliusResourceBundle will have to be created during configuration. I don't know yet why this choice have been done, maybe there is a good reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lchrusciel WDYT?
use Sylius\Component\Resource\Metadata\MetadataInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class DriverProvider | ||
{ | ||
/** @var DriverInterface[] */ | ||
/** @var array<string, array<string, string>> */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with this construction, but will this work?
--- @var array<string, array<string, string>>
+++ @var array<string, array<string, class-string<DriverInterface>>>
Will it remove need to specify
/** @var class-string|null $class */
$class = self::$drivers[$type]['class'] ?? null;
and
/** @var DriverInterface $driver */
$driver = new $class();
later in the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes It works in this case, but the attribute php doc is here to define the generic type of a driver configuration. We should perhaps build a DriverMetadata
class which own this configuration to better type it (I have to check if @loic425 already did this)
return self::$drivers[$type]; | ||
} | ||
/** @var class-string|null $class */ | ||
$class = self::$drivers[$type]['class'] ?? null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is somewhat misleading as $class
is made nullable, while only $drivers[$type]
is nullable and $drivers[$type]['class']
should be asserted
if (isset(self::$drivers[$type])) {
Assert::notNull(self::$drivers[$type]['class'], sprintf('Class must be present for driver "%s" configuration', $type));
$class = self::$drivers[$type]['class'];
return new $class();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO this kind of assertion must be done into a DriverMetadata
class like I suggest earlier.
This PR allows to configure any drivers you want or even change the default one.
Here is the previous default configuration :
Now you can setup a little bit more info :
To keep backward compatibility, you still can set the config with a simple string array when you are using one the 3 available drivers :
doctrine/*
Right now this PR is using only the
class
config member, but we can have other things added here in the futur.Custom driver example
You are creating your own driver and wanted to register it in the list of available drivers, here is how to proceed :