Problem/Motivation
While extending route attribute discovery to forms in #3584793: Use PHP attributes for form route discovery, we encountered an issue where one of the forms in layout_builder extends a from class from field_ui, despite that the layout_buildermodule does not have a dependency on field_ui. In configurations where layout_buider is installed but field_ui is not, there will be an exception thrown when checking class_exists on that layout builder form class. This exception is caught, and the class is skipped from being reflected to discover any routes definitions in it. In PHP 8.5, this exception handling works when a class is either extending a missing class, implementing a missing interface, or using a missing trait.
However, in PHP version <= 8.4, using class_exists or reflection on a class that uses a missing trait does not throw an exception; PHP fatal errors in that case, and this can not be caught. This was addressed for plugin attribute discovery in #3502913: Add a fallback classloader that can handle missing traits for attribute discovery. While there are no instances of missing traits in Controller or Form namespaces in core, this is something that could come up in contrib or custom modules, so we should likely extend or copy this functionality for route discovery.
It may also be a good idea to see if the missing trait handling can be extracted from being exclusive to plugin discovery, so that it can be used for route discovery and hook discovery. (Since Hook is a new namespace, missing traits have not been an issue so far, but it is a potential issue.) Also, as attribute autodiscovery may be extended to other things like services in #3422359: Directory based automatic service creation, it would be convenient to have for Drupal 11.
Comments
Comment #2
godotislate