|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bridge\Doctrine\CacheWarmer; |
|
13 |
|
14 use Symfony\Bridge\Doctrine\RegistryInterface; |
|
15 use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
|
16 |
|
17 /** |
|
18 * The proxy generator cache warmer generates all entity proxies. |
|
19 * |
|
20 * In the process of generating proxies the cache for all the metadata is primed also, |
|
21 * since this information is necessary to build the proxies in the first place. |
|
22 * |
|
23 * @author Benjamin Eberlei <kontakt@beberlei.de> |
|
24 */ |
|
25 class ProxyCacheWarmer implements CacheWarmerInterface |
|
26 { |
|
27 private $registry; |
|
28 |
|
29 /** |
|
30 * Constructor. |
|
31 * |
|
32 * @param RegistryInterface $registry A RegistryInterface instance |
|
33 */ |
|
34 public function __construct(RegistryInterface $registry) |
|
35 { |
|
36 $this->registry = $registry; |
|
37 } |
|
38 |
|
39 /** |
|
40 * This cache warmer is not optional, without proxies fatal error occurs! |
|
41 * |
|
42 * @return false |
|
43 */ |
|
44 public function isOptional() |
|
45 { |
|
46 return false; |
|
47 } |
|
48 |
|
49 public function warmUp($cacheDir) |
|
50 { |
|
51 foreach ($this->registry->getEntityManagers() as $em) { |
|
52 // we need the directory no matter the proxy cache generation strategy |
|
53 if (!file_exists($proxyCacheDir = $em->getConfiguration()->getProxyDir())) { |
|
54 if (false === @mkdir($proxyCacheDir, 0777, true)) { |
|
55 throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory "%s".', dirname($proxyCacheDir))); |
|
56 } |
|
57 } elseif (!is_writable($proxyCacheDir)) { |
|
58 throw new \RuntimeException(sprintf('The Doctrine Proxy directory "%s" is not writeable for the current system user.', $proxyCacheDir)); |
|
59 } |
|
60 |
|
61 // if proxies are autogenerated we don't need to generate them in the cache warmer |
|
62 if ($em->getConfiguration()->getAutoGenerateProxyClasses()) { |
|
63 continue; |
|
64 } |
|
65 |
|
66 $classes = $em->getMetadataFactory()->getAllMetadata(); |
|
67 |
|
68 $em->getProxyFactory()->generateProxyClasses($classes); |
|
69 } |
|
70 } |
|
71 } |