Using DI as a standalone component in the small part of the codebase I want to make some services visible outside this part and accessible as by interface class name.
As I know, I should use a container for this.
So when old part of the code (that can't use DI) wants to use service that implementation is configured by this part of the code: it should call $container->get(MyInterface:class)
.
The problem is that I receive:
The "App\MyInterface" service or alias has been removed or inlined when the container was compiled. You should either make it public or stop using the container directly and use dependency injection instead.
I can't use DI in other parts of the code. So I want to make some of my services public. This is my code:
services.yaml:
# ...
# autowire & autoconfigure: true
# catalogs included
App\MyInterface: '@my.configured.implementation'
my.configured.implementation:
class: App\MyInterfaceImplementation
Builder:
$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load('services.yaml');
//Make `MyInterface::class` public so
//$container->get(MyInterface::class) should work.
$containerBuilder->getDefinition(MyInterface::class)->setPublic(true);
$containerBuilder->compile(true);
When it comes to getting a definition I receive:
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "App\MyInterface"
How can I make this works? Or maybe there is better way to use one service by these two modules?