- Issue created by @mondrake
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
mondrake โ changed the visibility of the branch 3492391-explore-injecting-the to hidden.
- ๐บ๐ธUnited States nicxvan
Why not module_handler?
You can make it a hook, they are now event listeners and give a lot of the same benefits of event subscribers, they are now classes and are autowired.
I also think there's less boilerplate needed among other benefits.
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
Letโs see if we can make one more step and make the factory a decorator of the event dispatcher.
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
Added 2 CR stubs, one [#3494043] for the generic availability of the event dispatcher before container bootstrap, and one [#3494044] for its usage in
Database::Connection
. Will complete at later stage, doing at this stage has an high risk of getting stale information in it while reviews and tuning still needs to happen. - ๐ฎ๐นItaly mondrake ๐ฎ๐น
๐ Allow parsing and writing PHP constants and enums in YAML files Needs work would help, we could pass an enum parameter in the service definition file, which we cannot presently.
- ๐ณ๐ฑNetherlands daffie
Maybe we should do ๐ Allow parsing and writing PHP constants and enums in YAML files Needs work first?
- ๐ณ๐ฑNetherlands daffie
The CI pipeline is not happy. It fails for Drupal\KernelTests\Core\Database\DatabaseEventTest
- ๐ณ๐ฑNetherlands daffie
The MR looks good to me.
The IS and the CRs need to be updated. - ๐บ๐ธUnited States nicxvan
If you are trying to make this unified post bootstrap then why not have a utility class which you can call instead of the event dispatcher and does a fixed call before bootstrap and dispatches an event once the container is available?
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
@daffie thanks, if you do not mind I'd leave this to NR a bit longer to see if more reviews/input come. I will definitely do the CR and IS updates when we have settled on the final solution.
@nicxvan I am not sure I understand; feel free to open another issue branch and MR if you want to propose an alternative!
- ๐บ๐ธUnited States nicxvan
What I mean is you're jumping through all these hoops to get the invent dispatcher in place. But you're only calling a fixed output for that.
You should just replace this with a utility class that before bootstrap calls the static helper you're creating preBootstrapSubscribers to handle.
So the helper would call the static preBootstrapSubscribers if you are prebootstrap and then call the event dispatcher as it does now after.You get the benefits you're looking for with way less code.
- ๐บ๐ธUnited States nicxvan
nicxvan โ changed the visibility of the branch 3492391-early-fixed-event-dispatcher to hidden.
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
@nicxvan thanks, I like where MR 10607 is heading, especially in the part where the early subscribers to be registered with the early dispatcher are moved to the database API.
However, this is creating an (early) event dispatcher that is attached to the Connection class instead of being unique across the request. Not a big deal for now, right, but what if in the future more code would need to dispatch events early? Cache for instance may want to dispatch an event to be responded by a database listener (again, not the case know, but to prepare for possible future). I'd rather keep a singleton somewhere outside of the Connection class, so that we do not end up with vertical dispatchers not talking to each other. That's an architecture topic, tagging so we can have a review and guidance.
Then, I think we should use a closure also to register the early subscribers in place of the const array - so to allow passing instantiated objects that may require parameters (atm in both MRs the assumption is that the subscribers have no parameters in the construction).
- ๐จ๐ฆCanada Charlie ChX Negyesi ๐Canada
tl;dr: YAGNI.
Not a big deal for now, right, but what if in the future more code would need to dispatch events early?
When that future arrives, the fixed dispatcher could be moved into a trait.
I'd rather keep a singleton somewhere outside of the Connection class, so that we do not end up with vertical dispatchers not talking to each other.
Not sure why that's a problem. These are, in a sense not "real" event dispatchers in the senses that there's no mechanism to add subscribers to them -- they are constant anyways. They are merely a convenience for the consumer not to write a separate code path where it checks whether the subscriber is ready and if not then it calls the relevant code itself. This patch could be
public function dispatchEvent(DatabaseEvent $event, ?string $eventName = NULL): DatabaseEvent { if ($this->eventDispatcherCallable) { return ($this->eventDispatcherCallable)()->dispatch($event, $eventName);; } (new StatementExecutionSubscriber)->onStatementExecutionEnd($event); return $event; }
it's the exact same thing. It would be somewhat simpler to do that, too. Perhaps hardwiring the method besides the already hardwired class name is a problem and as with every problem it can be solved by adding some abstraction which in this case is this fixed event dispatcher. As proposed the fixed event dispatcher is so simple it's not worth considering the cost of it but the moment it becomes an elaborate mechanism itself the question could be raised whether this tiny amount of single responsibility principle violation is not worth it.
I think we should use a closure also to register the early subscribers in place of the const array
and what would supply such a closure? Maybe wait with that when there's a need? This is not like a normal core mechanism which needs to think about extensibility. The very point of the existence of this mechanism is the constant-ness.
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
#35 thanks for your comment.
Future in this case is not so far away, and I am actually working towards making it happen here.
If my proposal in ๐ Move the on-demand-table creation into the database API Needs work is accepted, we'll need a subscriber for database schema request events; if ๐ฑ [meta] Drop implicit autocommit on Transaction destruction Active goes on, I am suggesting to move the post-transaction callback execution from TransactionManager to an event subscriber that would react to a transaction commit/rollback/failure event - and that subscriber should live into the Cache API (
CacheTagsChecksumTrait
is the only consumer in core of post-transaction callbacks), hence the need to have a way to add early subscribers to the early dispatcher. - ๐จ๐ฆCanada Charlie ChX Negyesi ๐Canada
OK then would a trait work for you?
- ๐บ๐ธUnited States nicxvan
The trait approach is in: MR !10670 mergeable I am going to hide the fixed dispatcher MR for now because it is MR !10607 and that will be easy to confuse.
- ๐บ๐ธUnited States nicxvan
nicxvan โ changed the visibility of the branch 3492391-early-fixed-event-dispatcher2 to hidden.
- ๐ฎ๐นItaly mondrake ๐ฎ๐น
@ghost of drupal past, @nicxvan thanks - I still prefer my own approach than the trait one. Apart from the obvious, i.e. that since I wrote it I prefer it :), I think there are a couple of points that are unclear to me with the trait one:
1) IoC - IMO it's cleaner in the non-trait approach; each Connection object receives in the constructor a decorator of the EventDispatcher and does not have to care about it as it transit from pre-bootstrap to full container mode. This also makes simpler to write unit tests by mocking the event dispatcher passed to the Connection objects.
2) most important to me: unless I am mistaken, the trait will create an event dispatcher specific for the Connection object, whereas my idea is to have a singleton accessible by any class that would need pre-bootstrap event dispatching, with an orderly way to add early subscribers avoiding duplications.