I am developing a small application outside of the Symfony framework and I want to use the Symfony dependency injection component to automatically resolve my repositories which require \Doctrine\ORM\EntityManager as a parameter in their constructor.
I want to bind an existing instance to the dependency injection container - but I can't seem to figure this out from the documentation. It seems that the container is using reflection to get the class name of the instance and then create a new instance. The variable $entityManager is an instance of \Doctrine\ORM\EntityManager.
$container = new ContainerBuilder();
$container->register('doctrine.orm.entitymanager', $entityManager);
var_dump($container->get('doctrine.orm.entitymanager'));
The component throws a ReflectionException if I try to resolve the instance. What is the correct approach to this?
PHP Fatal error: Uncaught exception 'ReflectionException' with message 'Access to non-public constructor of class Doctrine\ORM\EntityManager' in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php:955
Stack trace:
#0 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(955): ReflectionClass->newInstanceArgs(Array)
#1 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(488): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'doctrine.orm.en...')
#2 /vagrant/sfs/test.php(12): Symfony\Component\DependencyInjection\ContainerBuilder->get('doctrine.orm.en...')
#3 {main}
thrown in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php on line 955
Fatal error: Uncaught exception 'ReflectionException' with message 'Access to non-public constructor of class Doctrine\ORM\EntityManager' in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php:955
Stack trace:
#0 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(955): ReflectionClass->newInstanceArgs(Array)
#1 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(488): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'doctrine.orm.en...')
#2 /vagrant/sfs/test.php(12): Symfony\Component\DependencyInjection\ContainerBuilder->get('doctrine.orm.en...')
#3 {main}
thrown in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php on line 955