Skip to content
Browse files

Added Proxy Cache Warmer.

  • Loading branch information...
1 parent 1e76190 commit 6f543a75fb12e748cf939af0283ae6638b991fd3 @golovanov committed Jul 19, 2011
Showing with 78 additions and 2 deletions.
  1. +66 −0 CacheWarmer/ProxyCacheWarmer.php
  2. +12 −2 Resources/config/oxm.xml
View
66 CacheWarmer/ProxyCacheWarmer.php
@@ -0,0 +1,66 @@
+<?php
+
+
+namespace Doctrine\Bundle\OXMBundle\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 Igor Golovanov <igor.golovanov@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.oxm.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.oxm.auto_generate_proxy_classes') === true) {
+ return;
+ }
+
+ $xmlEntityManagers = $this->container->getParameter('doctrine.oxm.xml_entity_managers');
+ foreach ($xmlEntityManagers as $xmlEntityManagerName) {
+ $xem = $this->container->get(sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManagerName));
+ $classes = $xem->getMetadataFactory()->getAllMetadata();
+ $xem->getProxyFactory()->generateProxyClasses($classes);
+ }
+ }
+}
View
14 Resources/config/oxm.xml
@@ -27,14 +27,18 @@
<!-- metadata -->
<parameter key="doctrine.oxm.metadata.driver_chain.class">Doctrine\OXM\Mapping\Driver\DriverChain</parameter>
<parameter key="doctrine.oxm.metadata.annotation.class">Doctrine\OXM\Mapping\Driver\AnnotationDriver</parameter>
- <parameter key="doctrine.oxm.metadata.xml.class">Go\DoctrineOXMBundle\Mapping\Driver\XmlDriver</parameter>
- <parameter key="doctrine.oxm.metadata.yml.class">Go\DoctrineOXMBundle\Mapping\Driver\YamlDriver</parameter>
+ <parameter key="doctrine.oxm.metadata.xml.class">Doctrine\Bundle\OXMBundle\Mapping\Driver\XmlDriver</parameter>
+ <parameter key="doctrine.oxm.metadata.yml.class">Doctrine\Bundle\OXMBundle\Mapping\Driver\YamlDriver</parameter>
<!-- directories -->
<parameter key="doctrine.oxm.mapping_dirs" type="collection"></parameter>
<parameter key="doctrine.oxm.xml_mapping_dirs">%doctrine.oxm.mapping_dirs%</parameter>
<parameter key="doctrine.oxm.yml_mapping_dirs">%doctrine.oxm.mapping_dirs%</parameter>
<parameter key="doctrine.oxm.xml_entity_dirs" type="collection"></parameter>
+
+ <!-- proxy cache warmer -->
+ <parameter key="doctrine.oxm.proxy_cache_warmer.class">Doctrine\Bundle\OXMBundle\CacheWarmer\ProxyCacheWarmer</parameter>
+
</parameters>
@@ -59,6 +63,12 @@
<!-- cache -->
<service id="doctrine.oxm.cache.array" class="%doctrine.oxm.cache.array.class%" />
+
+ <!-- Cache Warmers -->
+ <service id="doctrine.oxm.proxy_cache_warmer" class="%doctrine.oxm.proxy_cache_warmer.class%" public="false">
+ <tag name="kernel.cache_warmer" />
+ <argument type="service" id="service_container" />
+ </service>
</services>
</container>

0 comments on commit 6f543a7

Please sign in to comment.
Something went wrong with that request. Please try again.