The example below works, and I see it used in some extensions, but it has no autocompletion and not catching typos.
Services.php: class TranslateServices implements ContainerInterface { public function getParsingPlaceholderFactory(): ParsingPlaceholderFactory { return $this->container->get( 'Translate:ParsingPlaceholderFactory' ); }
public function getTranslatablePageParser(): TranslatablePageParser { return $this->container->get( 'Translate:TranslatablePageParser' ); } }
ServiceWiring.php: return [ 'Translate:ParsingPlaceholderFactory' => function (): ParsingPlaceholderFactory { return new ParsingPlaceholderFactory(); },
'Translate:TranslatablePageParser' => function ( MediaWikiServices $services ) : TranslatablePageParser { return new TranslatablePageParser( $services->get( 'Translate:ParsingPlaceholderFactory' ) # <-------- ); }, ];
Do you see any downsides of using code like below instead?
'Translate:TranslatablePageParser' => function (): TranslatablePageParser { $services = TranslateServices::getInstance(); return new TranslatablePageParser( $services->getParsingPlaceholderFactory() ); },
I looked at other extensions and I noticed a lot of small differences among them: * Some extensions use static methods as opposed to wrapping the core service container * Some extensions use constants for service identifiers * Lots of different implementations of "To avoid name conflicts, the service names should be prefixed with the extension's name.": ** ExtensionService ** Extension.Service ** Extension:Service ** Extension_Service
Are we yet in a stage to agree on some (additional) conventions and document them somewhere? Maybe in https://www.mediawiki.org/wiki/Dependency_Injection
-Niklas