IntegrationPass.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\DiExtraBundle\DependencyInjection\Compiler;
  18. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  19. use Symfony\Component\DependencyInjection\Reference;
  20. use Symfony\Component\DependencyInjection\Alias;
  21. use Symfony\Component\DependencyInjection\ContainerBuilder;
  22. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  23. /**
  24. * Integrates the bundle with external code.
  25. *
  26. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27. */
  28. class IntegrationPass implements CompilerPassInterface
  29. {
  30. public function process(ContainerBuilder $container)
  31. {
  32. // replace Symfony2's default controller resolver
  33. $container->setAlias('controller_resolver', new Alias('jms_di_extra.controller_resolver', false));
  34. if (true === $container->getParameter('jms_di_extra.doctrine_integration')) {
  35. $this->integrateWithDoctrine($container);
  36. }
  37. }
  38. /**
  39. * Integrates the DiAwareObjectManager with Doctrine.
  40. *
  41. * This is a bit trickier... mostly because Doctrine uses many factories,
  42. * and we cannot directly inject the EntityManager. We circumvent this
  43. * problem by renaming the original entity manager definition, and then
  44. * placing our definition in its place.
  45. *
  46. * Note that this also currently only supports the ORM, for the ODM flavors
  47. * a similar integration should be possible.
  48. *
  49. * @param ContainerBuilder $container
  50. */
  51. private function integrateWithDoctrine($container)
  52. {
  53. foreach ($container->getDefinitions() as $id => $definition) {
  54. if (!$definition instanceof DefinitionDecorator) {
  55. continue;
  56. }
  57. if ('doctrine.orm.entity_manager.abstract' !== $definition->getParent()) {
  58. continue;
  59. }
  60. $definition->setPublic(false);
  61. $container->setDefinition($id.'.delegate', $definition);
  62. $container->register($id, $container->getParameter('jms_di_extra.doctrine_integration.entity_manager.class'))
  63. ->setFile($container->getParameter('jms_di_extra.doctrine_integration.entity_manager.file'))
  64. ->addArgument(new Reference($id.'.delegate'))
  65. ->addArgument(new Reference('service_container'));
  66. }
  67. }
  68. }