DoctrineBundle.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /**
  16. * Bundle.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Jonathan H. Wage <jonwage@gmail.com>
  20. */
  21. class DoctrineBundle extends Bundle
  22. {
  23. public function build(ContainerBuilder $container)
  24. {
  25. parent::build($container);
  26. $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  27. }
  28. public function boot()
  29. {
  30. // force Doctrine annotations to be loaded
  31. // should be removed when a better solution is found in Doctrine
  32. class_exists('Doctrine\ORM\Mapping\Driver\AnnotationDriver');
  33. // Register an autoloader for proxies to avoid issues when unserializing them
  34. // when the ORM is used.
  35. if ($this->container->hasParameter('doctrine.orm.proxy_namespace')) {
  36. $namespace = $this->container->getParameter('doctrine.orm.proxy_namespace');
  37. $dir = $this->container->getParameter('doctrine.orm.proxy_dir');
  38. $container =& $this->container;
  39. spl_autoload_register(function($class) use ($namespace, $dir, &$container) {
  40. if (0 === strpos($class, $namespace)) {
  41. $className = substr($class, strlen($namespace) +1);
  42. $file = $dir.DIRECTORY_SEPARATOR.$className.'.php';
  43. if (!file_exists($file) && $container->getParameter('kernel.debug')) {
  44. $originalClassName = substr($className, 0, -5);
  45. $registry = $container->get('doctrine');
  46. // Tries to auto-generate the proxy file
  47. foreach ($registry->getEntityManagers() as $em) {
  48. if ($em->getConfiguration()->getAutoGenerateProxyClasses()) {
  49. $classes = $em->getMetadataFactory()->getAllMetadata();
  50. foreach ($classes as $class) {
  51. $name = str_replace('\\', '', $class->name);
  52. if ($name == $originalClassName) {
  53. $em->getProxyFactory()->generateProxyClasses(array($class));
  54. }
  55. }
  56. }
  57. }
  58. clearstatcache($file);
  59. if (!file_exists($file)) {
  60. throw new \RuntimeException(sprintf('The proxy file "%s" does not exist. If you still have objects serialized in the session, you need to clear the session manually.', $file));
  61. }
  62. }
  63. require $file;
  64. }
  65. });
  66. }
  67. }
  68. }