Review main Abandoned

60343[WIP][FEATURE] Introduce PSR-14 based EventDispatcher as hook replacement

Benni Mack CR 0 V 0 Nightmare +2789 −255 updated 04/20/20, 20:01

Commit message

FYI: I'll keep this one open for migrating further hooks to PSR11/PSR14

The new PSR-14 standards recommendation is providing
a new OOP way to extend a framework in an interchangeable way.

There are four components to PSR-14:
- Listeners => actual code that hooks into a certain place
- Event => Any kind of object which defines what a listener can do (or just "listen")
- ListenerProvider => a flexible registration mechanism to register listeners
- Dispatcher => the main logic to be used throughout the framework

Due to its PSR-based nature, it is very close to various other EventDispatcher
systems provided by various frameworks.

For TYPO3 we decided to:
- Provide our own ListenerProvider that checks all active extensions for listeners
within EXT:my_extension/Configuration/EventListeners.php - similar to how
we've done that with PSR-15 middlewares. The ListenerProvider also uses
"makeInstance".
- Provider our own EventDispatcher which is set up at the VERY part of the
bootstrap process of TYPO3, and part of the PSR-11 Container.

The purpose of this change is to fully migrate away from
- Hooks
- Hooks that modify things
- Hooks which implement interfaces for hooks
- Signals/Slots from Extbase

and their special registration into one Object-Oriented way
on how to treat Events and Listeners.

The current change provides

1. A custom PackageListenerProvider
- with the mentioned registration concept
- which allows to do ordering based on DependencyOrderingService

2. A straightforward EventDispatcher implementation.

3. Events for all previous hooks in EXT:core.

4. Compatibility Listeners for all previous hooks, these
are usually loaded before other listeners.

5. A change to the Backend module "Configuration" to browse registered listeners

The aim for this patch is
- to be 100% compliant to the PSR-14 specification
- to change all EXT:core hooks as well to new Events (except DataHandler)
- to add further tests for the new functionality
- to add documentation for the new EventDispatcher system
- to change all EXT:core signal/slots to Events

Also, all limitations / changes need to be reflected.

Still todo:
- Add EventListeners for all existing hooks that have been migrated
- Migrate tests to use EventListeners instead of Signals/Hooks
- Add RST
- Add tests for listener provider

Review

  • Code review CR 0
  • Verified V 0

Files 56

Show 36 more files

Review log 2950

Show 1625 earlier messages
  1. Benni Mack uploaded patch set 1 03/26/19, 16:01

  2. TYPO3com voted on patch set 1 V -1 03/26/19, 16:04 Completed build in 02m 22s on Tue, 26 Mar, 05:04 PM Test Summary: 3450 of 39251 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-32976

  3. Mathias Brodala commented on patch set 1 03/27/19, 08:42

    This must be EventListeners according to the code which makes more sense, too.
  4. Markus Poerschke commented on patch set 1 03/30/19, 21:40

    This looks very promising to me! :) I left some ideas and concerns I saw. Hopefully this is ok with you. :)
    I would change this line to: "a flexible registration mechanism to register listeners"

    Unresolved

    Actually a dispatcher can also be used in the domain / business logic. Actually the text could also be "copied" from here: https://www.php-fig.org/psr/psr-14/#definitions
    Change "it’s" to "its".
    "to be 100% compliant to the PSR-14 recommendation"

    AbstractUserAuthentication.php:822

    I understand why the "foreach" was moved to the event listeners, but by doing that you have to pass the emitter to the event. That means that each listener must depend on the interface of the emitter. The aim should be that the listeners only depend on the event that is passed. If we have a look at the recommendation: «Events are objects that act as the unit of communication between an Emitter and appropriate Listeners.» Now we are talking to the emitter directly. So the event class cannot serve the purpose of beeing the unit of communication between emitter and listeners. «It is RECOMMENDED, but NOT REQUIRED, that Event objects support lossless serialization and deserialization;» Mostly emitters will not be able to be serialized, because of their complex dependencies (e.g. database connection etc.). Furthermore the event should be „any arbitrary PHP object“. I suggest to move back the foreach and add a deprecation warning if the array is not empty. Disatvantage: the order cannot be set correctly anymore. Either the legacy hooks are executed first or the new events. But anyhow the hooks are executed in a block so that should not be a big problem.

    AbstractUserAuthentication.php:822 Unresolved

    I think the emitter of an event should not be added to the event as an attribute. I guess that in a long term it makes sense to make the properties of this class protected? So adding the relevant values as separate properties of the event.

    BackendUserAuthentication.php:547

    When I first saw this lines, I was asking myself what "evt" is standing for. I think renaming the variable to "event" is appropiate.

    BackendUserAuthentication.php:1393

    I like the idea that you can pass the query builder to add custom constraints etc.

    BackendUserAuthentication.php:1395

    If the QueryBuilder is passed to the event, the constraints can be added to the query builder directly. There is no need to add the constrains to the event first and then to add them to the query builder.

    BackendUserAuthentication.php:1876

    If the emitter is the first argument of the constructor it will be very hard to remove it again later.

    HookListeners.php:343 Unresolved

    Missing class import.

    PackageListenerProvider.php:70

    Why do you use "json_encode" here? Shouldn’t the cache take care of propper serialization?

    PackageListenerProvider.php:89

    The listeners are called in a wrong order. When I add a listener for an interface but specify that it should be called before a listener that is registered for a concrete class it will be still executed after it. Therefore the listeners may not be grouped by the "event class" in the property "listeners". They must be placed in flat array instead. $listeners = [ [ 'target' => 'The\ListenerClass::method', 'eventClass' => 'The\Event\ClassName' ], ]; $eventClasses = [get_class($event)]; array_push($eventClasses, class_parents($event), class_implements($event)); foreach ($listeners as $listener) { if (in_array($listener['eventClass'], $eventClasses)) { yield $this->getCallableFromTarget($listener['target']); } }

    PackageListenerProvider.php:147

    If a string syntax like this is callable why not using the old TypoScript syntax "->"? How can a static method beeing called?

    locallang.xlf:40

    How about dropping the word "Hooks" and add "(PSR-14)" instead. This is how it was done for the HTTP Middlewares.
  5. Mathias Brodala commented on patch set 1 04/01/19, 07:15

    PackageListenerProvider.php:148

    Wouldn't it be easier to move this top the top? The goal of this method is to return a callable after all ...

    PackageListenerProvider.php:152

    This sounds confusing. Better: Target "%s" is not callable
  6. Benni Mack commented on patch set 1 04/08/19, 22:58

    Reply Unresolved

    yup.

    Reply Unresolved

    Ack

    Reply Unresolved

    yup.

    Reply Unresolved

    > "to be 100% compliant to the PSR-14 recommendation" Yeah, makes sense

    AbstractUserAuthentication.php:822 Reply

    Yeah, keeping the hooks here in place instead of creating "Legacy Listeners" is another way of handling it. Felt easier for me when starting to migrate that I'm sure I covered all hooks properly. I'm also considering activating the hooks only via a feature flag to improve perf. as a "strict event listeners" mode. However I don't really care on how to phase the hooks out as long as we're sure we have everything covered and have a clean code base at some point. As for serialization, this part was added to the spec due to the fact that people can use async / deferred calls to the events. At this point, TYPO3 does not support real and proper serialization so it is recommended but we don't do that by default yet. `Furthermore the event should be „any arbitrary PHP object“.` What do you mean with this?

    BackendUserAuthentication.php:547 Reply

    no probs, that was taken from JS (where people also use $e for "Event"). I've seen other implementations that call e.g. "return $dispatcher->dispatch(...)->getQueryConstraint();" directly.

    BackendUserAuthentication.php:1395 Reply

    question is who is responsible for doing this "logic" - adding the constraints to the queryBuilder - then? The constraints from the top of this method should be added, but not overridden then, right?

    BackendUserAuthentication.php:1876 Reply Unresolved

    true.

    PackageListenerProvider.php:70 Reply

    unserialize() could lead to security issues and unneeded side-effects. At this point we use json_* functions so it is very clear that the data that we use in this ListenerProvider needs a strict PHP array without any magic callables or anonynmous classes.

    PackageListenerProvider.php:89 Reply Unresolved

    nice one. thanks! I will def. add tests for that.

    PackageListenerProvider.php:147 Reply

    syntax is the same as for backend routes and all array-registered syntax we're using since TYPO3 v7. the "::" is actually closer to other systems (e.g. Symfony) where-as `->` is TYPO3 specific.

    PackageListenerProvider.php:148 Reply

    Hey, I have to check this again - I had this in TYPO3 v7 when we added this logic for Backend Routing Dispatching. AFAIK if we'd do this then Line 141-145 would not be executed without using GeneralUtility::makeInstance().

    PackageListenerProvider.php:152 Reply Unresolved

    ACK

    locallang.xlf:40 Reply

    no prob.
  7. Benni Mack uploaded patch set 2 04/12/19, 18:17

  8. TYPO3com voted on patch set 2 V -1 04/12/19, 18:24 Completed build in 04m 38s on Fri, 12 Apr, 08:24 PM Test Summary: 4876 of 39263 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-33221

  9. Markus Poerschke commented on patch set 2 04/12/19, 20:31

    AbstractUserAuthentication.php:822 Reply

    In my understanding the part "arbitrary PHP object" (quoted from the specification) plays together with the possibility of serialization. Also I unterstand it that the emitter should not be passed. But: in this case it might be nessecary to do so. Comparing to events in JavaScript/HTML, the "target" is also passed to the event. I would leave it as it is in this change. Regarding the serialization: actually that would make everything even more complex. If some one needs asynchronous work, they can copy the event and persistent it in their own data format.

    PackageListenerProvider.php:70 Reply

    Sounds reasonable to me.

    PackageListenerProvider.php:147 Reply

    Keep the stuff uniform makes sense!

    PackageListenerProvider.php:148 Reply

    Ack.
  10. Markus Poerschke commented on patch set 2 04/12/19, 20:33

    BackendUserAuthentication.php:547 Reply

    Done

    locallang.xlf:40 Reply

    Done
  11. Benni Mack uploaded patch set 3 04/14/19, 09:18

  12. TYPO3com voted on patch set 3 V -1 04/14/19, 09:21 Completed build in 02m 30s on Sun, 14 Apr, 11:21 AM Test Summary: 4124 of 39263 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-33245

  13. Markus Poerschke commented on patch set 3 04/14/19, 15:56

    I just had another look. Looks good so far. I found two classes where, I would not use the event listener.

    BackendUserAuthentication.php:1395 Reply Unresolved

    It could be handled like the QueryRestrictionInterface: there only the expression builder is passed to the method that will then return a CompositeExpression.

    HookListeners.php:83 Unresolved

    Accodring to the standard the listeners should return void. The return value is currently not used. „A Listener SHOULD have a void return, and SHOULD type hint that return explicitly. A Dispatcher MUST ignore return values from Listeners.“ The correct method signautre should be: public function onAbstractUserAuthenticationPostUserLookUp(AfterUserLookUpEvent $event): void

    RteHtmlParser.php:213 Unresolved

    I think this is not a use case for event listeners. I would not transform this to use event listeners, because only one single class per "$cmd" should be called. Especially the logic "if ($evt->isPropagationStopped()) {" looks confusing to me. Why aren’t the other commands implemented as listeners? If they would be implemented as listerners too, then "$value = $event->getValue()" could be called in any cases and no special condition is needed. My suggestion: do not use event dispatcher here and keep the old implementation. --- Idea how to solve this issue (not scope of this change): I think in the future each case of the "switch($cmd)" might be moved to a dedicated class. There will be a class you can register objects of type "TransformerInterface" for each "$cmd". Maybe a it could be implemented like this: $transformersRegistry = GeneralUtility::makeInstance(TransformersRegistry::class); if ($transformersRegistry->hasTransformer($cmd)) { $value = $transformersRegistry->getTransformer($cmd)->transform($value); } The concrete transformers could be registered using tags within the dependency container (PSR-11).

    RteHtmlParser.php:241 Unresolved

    Same as above. Do not use event listener and keep original implementation.

    TypoScriptParser.php:601 Unresolved

    See comment in RteHtmlParser.php. Leave the old implementation for now.
  14. Benni Mack uploaded patch set 4 05/07/19, 09:50

  15. Benni Mack uploaded patch set 5 05/07/19, 09:55

  16. TYPO3com voted on patch set 4 V -1 05/07/19, 10:40 Completed build in 42m 59s on Tue, 7 May, 12:40 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-33767

  17. TYPO3com voted on patch set 5 V -1 05/07/19, 10:54 Completed build in 14m 13s on Tue, 7 May, 12:54 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-33768

  18. Benni Mack uploaded patch set 6 05/20/19, 20:11

  19. TYPO3com voted on patch set 6 V -1 05/20/19, 20:13 Completed build in 02m 37s on Mon, 20 May, 10:13 PM Test Summary: 3801 of 39295 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-33951

  20. Benni Mack uploaded patch set 7 05/24/19, 20:42

  21. TYPO3com voted on patch set 7 V -1 05/24/19, 20:45 Completed build in 02m 23s on Fri, 24 May, 10:45 PM Test Summary: 5509 of 39332 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34034

  22. Benni Mack uploaded patch set 8 05/25/19, 21:29

  23. TYPO3com voted on patch set 8 V -1 05/25/19, 21:32 Completed build in 02m 36s on Sat, 25 May, 11:32 PM Test Summary: 7903 of 39332 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34044

  24. Benni Mack uploaded patch set 9 05/25/19, 22:02

  25. TYPO3com voted on patch set 9 V -1 05/25/19, 22:05 Completed build in 02m 36s on Sun, 26 May, 12:05 AM Test Summary: 4465 of 39332 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34045

  1. Benni Mack uploaded patch set 10 05/25/19, 22:28

  2. TYPO3com voted on patch set 10 V -1 05/25/19, 22:31 Completed build in 02m 44s on Sun, 26 May, 12:31 AM Test Summary: 9786 of 46732 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34046

  3. Benni Mack uploaded patch set 11 05/27/19, 15:24

  4. TYPO3com voted on patch set 11 V -1 05/27/19, 15:34 Completed build in 09m 08s on Mon, 27 May, 05:34 PM Test Summary: 3353 of 46809 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34068

  5. Benni Mack uploaded patch set 12 05/29/19, 19:13

  6. TYPO3com voted on patch set 12 V -1 05/29/19, 19:24 Completed build in 10m 28s on Wed, 29 May, 09:24 PM Test Summary: 2708 of 46809 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34090

  7. Benni Mack uploaded patch set 13 05/29/19, 20:19

  8. TYPO3com voted on patch set 13 V -1 05/29/19, 20:31 Completed build in 11m 39s on Wed, 29 May, 10:31 PM Test Summary: 2824 of 46809 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34094

  9. Benni Mack uploaded patch set 14 07/13/19, 13:30

  10. TYPO3com voted on patch set 14 V -1 07/13/19, 13:42 Completed build in 11m 32s on Sat, 13 Jul, 03:42 PM Test Summary: 2981 of 47361 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34994

  11. Benni Mack uploaded patch set 15 07/13/19, 14:20

  12. TYPO3com voted on patch set 15 V -1 07/13/19, 14:31 Completed build in 10m 55s on Sat, 13 Jul, 04:31 PM Test Summary: 2691 of 47361 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-34997

  13. Benni Mack uploaded patch set 16 07/25/19, 09:13

  14. TYPO3com voted on patch set 16 V -1 07/25/19, 09:19 Completed build in 05m 35s on Thu, 25 Jul, 11:19 AM Test Summary: 9800 of 47197 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-35317

  15. Benni Mack uploaded patch set 17 11/09/19, 21:37

  16. TYPO3com voted on patch set 17 V -1 11/09/19, 21:38 Completed build in 22s on Sat, 9 Nov, 09:38 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-38238

  17. Benni Mack uploaded patch set 18 11/15/19, 20:22

  18. TYPO3com voted on patch set 18 V -1 11/15/19, 20:24 Completed build in 31s on Fri, 15 Nov, 08:24 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-38405

  19. Benni Mack uploaded patch set 19 11/22/19, 11:33

  20. TYPO3com voted on patch set 19 V -1 11/22/19, 11:43 Completed build in 09m 42s on Fri, 22 Nov, 11:43 AM Test Summary: 4302 of 48429 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-38617

  21. Benni Mack rebased onto patch set 20 11/22/19, 16:30

  22. TYPO3com voted on patch set 20 V -1 11/22/19, 16:45 Completed build in 12m 35s on Fri, 22 Nov, 04:45 PM Test Summary: 3266 of 48448 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-38665

  23. Benni Mack uploaded patch set 21 12/03/19, 14:43

  24. TYPO3com voted on patch set 21 V -1 12/03/19, 14:55 Completed build in 09m 58s on Tue, 3 Dec, 02:55 PM Test Summary: 3902 of 48711 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-39078

  25. Benni Mack abandoned the change 04/20/20, 20:01