Drivers can now be configured#735
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.
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.
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.
| final class DriverProvider | ||
| { | ||
| /** @var DriverInterface[] */ | ||
| /** @var array<string, array<string, string>> */ |
There was a problem hiding this comment.
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.
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.
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.
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
classconfig 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 :