Page MenuHomeDevCentral

Provide reflection bricks to build a service container
ClosedPublic

Authored by dereckson on Jun 19 2023, 23:05.
Tags
None
Referenced Files
F3786300: D3204.id8172.diff
Tue, Nov 26, 13:48
Unknown Object (File)
Sun, Nov 24, 11:52
Unknown Object (File)
Sat, Nov 23, 06:37
Unknown Object (File)
Fri, Nov 22, 23:22
Unknown Object (File)
Thu, Nov 21, 03:21
Unknown Object (File)
Wed, Nov 20, 22:00
Unknown Object (File)
Wed, Nov 20, 21:44
Unknown Object (File)
Wed, Nov 13, 19:42
Subscribers
None

Details

Summary

How to automatically build a controller with dependency injection?

A solution could be to get a services container with objects
of different types, and a controller with a constructor requiring
the needed services.

A working example of this solution can be found in AcmeApplication,
instantiated in CodeClassTest unit test.

A use case can be to automate in a MVC framework the controllers
instantiation.

This solution requires services to be first initialized and doesn't
allow other topologies like lazy loading directly, but the reflection
classes added can be use as a base to build custom services containers.

For example, a variant of CodeClass::newInstanceFromServices can be
used with a different callable to select and build the dependency
from the type in key.

The scope of this commit is to offer bricks required to create
a service container, not to be a dependency injection framework.

Test Plan

New unit tests offer full code coverage for added classes.

A proof of concept can be found in AcmeApplication and CodeClassTest.

A real-world test will be to build a service container in Zed.

Diff Detail

Repository
rKERUALD Keruald libraries development repository
Lint
Lint Errors
SeverityLocationCodeMessage
Erroromnitools/src/Reflection/CodeMethod.php:36PHPCS.E.Generic.Files.LineLength.MaxExceededGeneric.Files.LineLength.MaxExceeded
Warningomnitools/src/Reflection/CodeClass.php:83PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Warningomnitools/src/Reflection/CodeFunction.php:14PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Warningomnitools/src/Reflection/CodeMethod.php:23PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Warningomnitools/tests/Reflection/CodeFunctionTest.php:17PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Warningomnitools/tests/Reflection/CodeFunctionTest.php:24PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Warningomnitools/tests/Reflection/CodeMethodTest.php:13PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Unit
Tests Passed
Branch
reflection
Build Status
Buildable 5042
Build 5323: arc lint + arc unit