DoctrineBundle.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Bundle
  4. *
  5. * The code was originally distributed inside the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Doctrine\Bundle\DoctrineBundle;
  14. use Doctrine\Common\Util\ClassUtils;
  15. use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
  16. use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
  17. use Doctrine\Bundle\DoctrineBundle\Command\Proxy\RunSqlDoctrineCommand;
  18. use Symfony\Component\Console\Application;
  19. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  20. use Symfony\Component\DependencyInjection\ContainerBuilder;
  21. use Symfony\Component\HttpKernel\Bundle\Bundle;
  22. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
  23. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
  24. use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
  25. /**
  26. * Bundle.
  27. *
  28. * @author Fabien Potencier <fabien@symfony.com>
  29. * @author Jonathan H. Wage <jonwage@gmail.com>
  30. */
  31. class DoctrineBundle extends Bundle
  32. {
  33. private $autoloader;
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function build(ContainerBuilder $container)
  38. {
  39. parent::build($container);
  40. $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  41. if ($container->hasExtension('security')) {
  42. $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
  43. }
  44. $container->addCompilerPass(new DoctrineValidationPass('orm'));
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function boot()
  50. {
  51. // Register an autoloader for proxies to avoid issues when unserializing them
  52. // when the ORM is used.
  53. if ($this->container->hasParameter('doctrine.orm.proxy_namespace')) {
  54. $namespace = $this->container->getParameter('doctrine.orm.proxy_namespace');
  55. $dir = $this->container->getParameter('doctrine.orm.proxy_dir');
  56. // See https://github.com/symfony/symfony/pull/3419 for usage of
  57. // references
  58. $container =& $this->container;
  59. $this->autoloader = function($class) use ($namespace, $dir, &$container) {
  60. if (0 === strpos($class, $namespace)) {
  61. $fileName = str_replace('\\', '', substr($class, strlen($namespace) +1));
  62. $file = $dir.DIRECTORY_SEPARATOR.$fileName.'.php';
  63. if (!is_file($file) && $container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
  64. $originalClassName = ClassUtils::getRealClass($class);
  65. /** @var $registry Registry */
  66. $registry = $container->get('doctrine');
  67. // Tries to auto-generate the proxy file
  68. /** @var $em \Doctrine\ORM\EntityManager */
  69. foreach ($registry->getManagers() as $em) {
  70. if ($em->getConfiguration()->getAutoGenerateProxyClasses()) {
  71. $classes = $em->getMetadataFactory()->getAllMetadata();
  72. foreach ($classes as $classMetadata) {
  73. if ($classMetadata->name == $originalClassName) {
  74. $em->getProxyFactory()->generateProxyClasses(array($classMetadata));
  75. }
  76. }
  77. }
  78. }
  79. clearstatcache(true, $file);
  80. }
  81. if (file_exists($file)) {
  82. require $file;
  83. }
  84. }
  85. };
  86. spl_autoload_register($this->autoloader);
  87. }
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function shutdown()
  93. {
  94. if (null !== $this->autoloader) {
  95. spl_autoload_unregister($this->autoloader);
  96. $this->autoloader = null;
  97. }
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function registerCommands(Application $application)
  103. {
  104. // Use the default logic when the ORM is available.
  105. // This avoids listing all ORM commands by hand.
  106. if (class_exists('Doctrine\\ORM\\Version')) {
  107. parent::registerCommands($application);
  108. return;
  109. }
  110. // Register only the DBAL commands if the ORM is not available.
  111. $application->add(new CreateDatabaseDoctrineCommand());
  112. $application->add(new DropDatabaseDoctrineCommand());
  113. $application->add(new RunSqlDoctrineCommand());
  114. }
  115. }