ProxyCacheWarmer.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  25. * Constructor.
  26. *
  27. * @param ManagerRegistry $registry A ManagerRegistry instance
  28. */
  29. public function __construct(ManagerRegistry $registry)
  30. {
  31. $this->registry = $registry;
  32. }
  33. /**
  34. * This cache warmer is not optional, without proxies fatal error occurs!
  35. *
  36. * @return false
  37. */
  38. public function isOptional()
  39. {
  40. return false;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function warmUp($cacheDir)
  46. {
  47. foreach ($this->registry->getManagers() as $em) {
  48. // we need the directory no matter the proxy cache generation strategy
  49. if (!is_dir($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
  50. if (false === @mkdir($proxyCacheDir, 0777, true)) {
  51. throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory "%s".', $proxyCacheDir));
  52. }
  53. } elseif (!is_writable($proxyCacheDir)) {
  54. throw new \RuntimeException(sprintf('The Doctrine Proxy directory "%s" is not writeable for the current system user.', $proxyCacheDir));
  55. }
  56. // if proxies are autogenerated we don't need to generate them in the cache warmer
  57. if ($em->getConfiguration()->getAutoGenerateProxyClasses()) {
  58. continue;
  59. }
  60. $classes = $em->getMetadataFactory()->getAllMetadata();
  61. $em->getProxyFactory()->generateProxyClasses($classes);
  62. }
  63. }
  64. }