forked from doctrine/DoctrineMongoDBBundle
/
ProxyCacheWarmer.php
76 lines (67 loc) · 2.58 KB
/
ProxyCacheWarmer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
/**
* The proxy generator cache warmer generates all document proxies.
*
* In the process of generating proxies the cache for all the metadata is primed also,
* since this information is necessary to build the proxies in the first place.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class ProxyCacheWarmer implements CacheWarmerInterface
{
/**
* @var Container
*/
private $container;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* This cache warmer is not optional, without proxies fatal error occurs!
*
* @return false
*/
public function isOptional()
{
return false;
}
public function warmUp($cacheDir)
{
// we need the directory no matter the proxy cache generation strategy.
$proxyCacheDir = $this->container->getParameter('doctrine.odm.mongodb.proxy_dir');
if (!file_exists($proxyCacheDir)) {
if (false === @mkdir($proxyCacheDir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory (%s)', dirname($proxyCacheDir)));
}
} else if (!is_writable($proxyCacheDir)) {
throw new \RuntimeException(sprintf('Doctrine Proxy directory (%s) is not writeable for the current system user.', $proxyCacheDir));
}
// if proxies are autogenerated we don't need to generate them in the cache warmer.
if ($this->container->getParameter('doctrine.odm.mongodb.auto_generate_proxy_classes') === true) {
return;
}
$documentManagers = $this->container->getParameter('doctrine.odm.mongodb.document_managers');
foreach ($documentManagers as $documentManagerName) {
$dm = $this->container->get(sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName));
/* @var $dm Doctrine\ODM\MongoDB\DocumentManager */
$classes = $dm->getMetadataFactory()->getAllMetadata();
$dm->getProxyFactory()->generateProxyClasses($classes);
}
}
}