I am currently working on a command in a bundle (symfony version: 6.4) where i need to get the instance of a service without the possibility of injecting the service. I only have the classname of the service in a variable as follows:
if (!empty($remoteService = $reflection->getAttributes(GetDataFromRemoteService::class))) {
$serviceFqcn = $remoteService[0]->getArguments()['serviceFqcn'];
$method = $remoteService[0]->getArguments()['method'];
if (class_exists($serviceFqcn)) {
if (method_exists($serviceFqcn, $method)) {
$container = new ContainerBuilder;
dd($container->get($serviceFqcn));
}
}
}
This code is returning an error:
You have requested a non-existent service "AppServiceSomeService"
I cannot just instantiate the service cause it would be too complicated as i have like 30 services like these and they all have injected dependencies. The easiest would be to add the service to the container an retrieve an instance of the service from the container.
The services could be private or public, synthetic or not, lazy or not, i cannot know beforehand.
How can i achieve this at lowest cost.