- Issue created by @donquixote
- Merge request !10399Issue #3490713: Introduce Drupal::serviceByClass(). → (Open) created by donquixote
- 🇷🇺Russia Chi
unless they use some kind of magic plugin that reads the services.yml files
For PhpStorm you can generate meta information about services.
drush gen phpstorm-meta
Psalm also reads it. Not sure about PhpStan. I suppose phpstan-drupal can help on this.
- Merge request !10401Draft: Issue #3490713: POC: Convert some Drupal::service() calls. → (Open) created by donquixote
- 🇩🇪Germany donquixote
For PhpStorm you can generate meta information about services.
Thanks!
In the past I tried the symfony plugin for phpstorm which was also supposed to do this.
But it slowed everything down.I think what I am proposing here is nicer, and more universally available.
- 🇩🇪Germany donquixote
I would like to add tests, but not sure where they would go.
- 🇩🇪Germany donquixote
For $container->getByClass() this is going to be more difficult, if we want to maintain full BC.
I was going to introduce a new interface ServiceByClassInterface and ConvenientContainerInterface, which would be implemented by our container classes.
But then in every place we need to check if the container we have is actually implementing that interface.
If instead we add the method directly to our ContainerInterface, any custom implementations will break which don't have this method.----
Alternatively we could introduce a function like this:
$time = service(TimeInterface::class, $container); assert($time instanceof TimeInterface);
so the same as above but we pass the $container object, so it is cleaner.
or
$time = ServiceFromClass::create($container)->getByClass(TimeInterface::class); assert($time instanceof TimeInterface);
or we have something that returns a closure.
$time = resolver($container)(TimeInterface::class); assert($time instanceof TimeInterface); $resolve = resolver($container); assert($resolve(TimeInterface::class) instanceof TimeInterface);
But currently a lot of the $container->get() calls should rather be replaced by some kind of autowire solution.
- 🇩🇪Germany donquixote
Seems we are suffering this one, 📌 [random test failure] BlockCacheTest Active
And also another random test failure which happened earlier.