flow-development-collection icon indicating copy to clipboard operation
flow-development-collection copied to clipboard

Full PHP8 typesafe injectable properties

Open runner78 opened this issue 3 years ago • 5 comments

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+

runner78 avatar Oct 12 '21 12:10 runner78

See also https://github.com/neos/flow-development-collection/issues/2114 - we need to change the way we build DependencyProxies

albe avatar Oct 12 '21 13:10 albe

For classes with few dependencies, you can also work well with constructor injection, but this is not the case with larger classes.

runner78 avatar Oct 12 '21 14:10 runner78

I'll take a look when https://github.com/neos/flow-development-collection/pull/2701 is merged.

robertlemke avatar Feb 22 '22 17:02 robertlemke

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;

robertlemke avatar Feb 23 '22 17:02 robertlemke

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.

albe avatar Apr 03 '22 14:04 albe