ProxyCacheWarmer.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\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. * @return string[] A list of files to preload on PHP 7.4+
  41. */
  42. public function warmUp(string $cacheDir)
  43. {
  44. $files = [];
  45. foreach ($this->registry->getManagers() as $em) {
  46. // we need the directory no matter the proxy cache generation strategy
  47. if (!is_dir($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
  48. if (false === @mkdir($proxyCacheDir, 0777, true)) {
  49. throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory "%s".', $proxyCacheDir));
  50. }
  51. } elseif (!is_writable($proxyCacheDir)) {
  52. throw new \RuntimeException(sprintf('The Doctrine Proxy directory "%s" is not writeable for the current system user.', $proxyCacheDir));
  53. }
  54. // if proxies are autogenerated we don't need to generate them in the cache warmer
  55. if ($em->getConfiguration()->getAutoGenerateProxyClasses()) {
  56. continue;
  57. }
  58. $classes = $em->getMetadataFactory()->getAllMetadata();
  59. $em->getProxyFactory()->generateProxyClasses($classes);
  60. foreach (scandir($proxyCacheDir) as $file) {
  61. if (!is_dir($file = $proxyCacheDir.'/'.$file)) {
  62. $files[] = $file;
  63. }
  64. }
  65. }
  66. return $files;
  67. }
  68. }