DoctrineBundle.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\DoctrineBundle;
  11. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  12. use Symfony\Bundle\DoctrineBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\HttpKernel\Bundle\Bundle;
  15. use Doctrine\ORM\Version;
  16. use Doctrine\Common\Util\ClassUtils;
  17. /**
  18. * Bundle.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Jonathan H. Wage <jonwage@gmail.com>
  22. */
  23. class DoctrineBundle extends Bundle
  24. {
  25. private $autoloader;
  26. public function build(ContainerBuilder $container)
  27. {
  28. parent::build($container);
  29. $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  30. }
  31. public function boot()
  32. {
  33. // force Doctrine annotations to be loaded
  34. // should be removed when a better solution is found in Doctrine
  35. class_exists('Doctrine\ORM\Mapping\Driver\AnnotationDriver');
  36. // Register an autoloader for proxies to avoid issues when unserializing them
  37. // when the ORM is used.
  38. if ($this->container->hasParameter('doctrine.orm.proxy_namespace')) {
  39. $namespace = $this->container->getParameter('doctrine.orm.proxy_namespace');
  40. $dir = $this->container->getParameter('doctrine.orm.proxy_dir');
  41. $container =& $this->container;
  42. $this->autoloader = function($class) use ($namespace, $dir, &$container) {
  43. if (0 === strpos($class, $namespace)) {
  44. $className = substr($class, strlen($namespace) +1);
  45. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', '', $className).'.php';
  46. if (!file_exists($file) && $container->getParameter('kernel.debug')) {
  47. $registry = $container->get('doctrine');
  48. if (1 === Version::compare('2.2.0')) {
  49. $originalClassName = substr($className, 0, -5);
  50. } else {
  51. $originalClassName = ClassUtils::getRealClass($class);
  52. $originalClassName = str_replace('\\', '', $originalClassName);
  53. }
  54. // Tries to auto-generate the proxy file
  55. foreach ($registry->getEntityManagers() as $em) {
  56. if ($em->getConfiguration()->getAutoGenerateProxyClasses()) {
  57. $classes = $em->getMetadataFactory()->getAllMetadata();
  58. foreach ($classes as $class) {
  59. $name = str_replace('\\', '', $class->name);
  60. if ($name == $originalClassName) {
  61. $em->getProxyFactory()->generateProxyClasses(array($class));
  62. }
  63. }
  64. }
  65. }
  66. clearstatcache($file);
  67. }
  68. if (file_exists($file)) {
  69. require $file;
  70. }
  71. }
  72. };
  73. spl_autoload_register($this->autoloader);
  74. }
  75. }
  76. public function shutdown()
  77. {
  78. if (null !== $this->autoloader) {
  79. spl_autoload_unregister($this->autoloader);
  80. $this->autoloader = null;
  81. }
  82. }
  83. }