flow-development-collection
flow-development-collection copied to clipboard
Full PHP8 typesafe injectable properties
Description
At the moment this is not possible with Flow 7.2:
class MonitoringCommandController extends CommandController
{
#[Flow\Inject(lazy: true)]
protected ?MyLazyService $myLazyService = null;
#[Flow\Inject(lazy: false)]
protected ?MyService $myService = null;
/**
* @var AnalyticsService|null
*/
#[Flow\Inject(lazy: false)]
protected ?MyAnotherService $myAnotherService = null;
/*code */
}
- The first example does not work because Flow uses another type as a placeholder to load the type on demand
- The second (as well as the first) example does not work because of the missing type annotation.
- The third example does not work because of the "|null" in the type annotation
My suggestion would be make the property Injection more robust fit for PHP8+
See also https://github.com/neos/flow-development-collection/issues/2114 - we need to change the way we build DependencyProxies
For classes with few dependencies, you can also work well with constructor injection, but this is not the case with larger classes.
I'll take a look when https://github.com/neos/flow-development-collection/pull/2701 is merged.
I just tested this with the code provided in #2701 and it just works. For testing I modified the test controller like so:
/**
* LoggerInterface doesn't have to be nullable, because the proxy class
* builder will not even try to use lazy injection, since it is an interface
**/
#[Flow\Inject(lazy: false)]
protected LoggerInterface $logger;
/**
* CacheManager must be nullable, because it can be injected lazily
*/
#[Flow\Inject(lazy: true)]
protected ?CacheManager $cacheManager;
/**
* @Flow\Inject
* @var TestSingleton|null
*/
#[Flow\Inject(lazy: true)]
protected ?TestSingleton $testSingleton;
https://github.com/neos/flow-development-collection/pull/2782 (included in 8.0) has made this work basically, with the limitation that typed properties are never lazy.