nikic / FastRoute

Fast request router for PHP
Other
5.09k stars 446 forks source link

Consider callable factories for custom overrides? #280

Open pmjones opened 6 months ago

pmjones commented 6 months ago

Currently, the FastRoute class uses strings to indicate custom or override classes, then uses that string with a new call to create the override object. This means that the custom or override class cannot use an alternative constructor, which in turn limits additional or alternative functionality.

Allowing for the custom or override class to be specified as a string or callable, means that a callable can be invoked to return a fully-constructed object. This would let consumers add custom functionality to the custom or override class via a custom constructor. Cf. the as-yet-released CastRouteCollector which adds to the constructor parameters.

You could go further with this, and delegate object construction to a PSR-11 ContainerInterface, but that adds a dependency, and might over-complicate setup work.

pmjones commented 6 months ago

Thinking on this some more: enabling something more like "real" dependency injection might be preferable. As a proof-of-concept, see these three sequential PRs:

All tests pass, but I have not run the other QA tooling.

I understand that this rearrangement may not fit your vision for the project, so use/modify/discard as you see fit.

(p.s. I can see now that even RouteCollector might do well to get a with($routeParser) method, making it more like the other injectable objects.)