ProxyCacheWarmer.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Bridge\Doctrine\CacheWarmer;
  11. use Doctrine\Common\Persistence\ManagerRegistry;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. /**
  14. * The proxy generator cache warmer generates all entity proxies.
  15. *
  16. * In the process of generating proxies the cache for all the metadata is primed also,
  17. * since this information is necessary to build the proxies in the first place.
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. */
  21. class ProxyCacheWarmer implements CacheWarmerInterface
  22. {
  23. private $registry;
  24. public function __construct(ManagerRegistry $registry)
  25. {
  26. $this->registry = $registry;
  27. }
  28. /**
  29. * This cache warmer is not optional, without proxies fatal error occurs!
  30. *
  31. * @return false
  32. */
  33. public function isOptional()
  34. {
  35. return false;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function warmUp($cacheDir)
  41. {
  42. foreach ($this->registry->getManagers() as $em) {
  43. // we need the directory no matter the proxy cache generation strategy
  44. if (!is_dir($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
  45. if (false === @mkdir($proxyCacheDir, 0777, true)) {
  46. throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory "%s".', $proxyCacheDir));
  47. }
  48. } elseif (!is_writable($proxyCacheDir)) {
  49. throw new \RuntimeException(sprintf('The Doctrine Proxy directory "%s" is not writeable for the current system user.', $proxyCacheDir));
  50. }
  51. // if proxies are autogenerated we don't need to generate them in the cache warmer
  52. if ($em->getConfiguration()->getAutoGenerateProxyClasses()) {
  53. continue;
  54. }
  55. $classes = $em->getMetadataFactory()->getAllMetadata();
  56. $em->getProxyFactory()->generateProxyClasses($classes);
  57. }
  58. }
  59. }