Browse Source

add CacheWarmer for route

Thomas Rabaix 11 years ago
parent
commit
7d1aebb93e
4 changed files with 57 additions and 49 deletions
  1. 0 48
      Command/DumpRoutesCommand.php
  2. 7 0
      Resources/config/route.xml
  3. 1 1
      Route/RoutesCache.php
  4. 49 0
      Route/RoutesCacheWarmUp.php

+ 0 - 48
Command/DumpRoutesCommand.php

@@ -1,48 +0,0 @@
-<?php
-
-/*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Sonata\AdminBundle\Command;
-
-use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Output\Output;
-
-class DumpRoutesCommand extends ContainerAwareCommand
-{
-    /**
-     * {@inheritDoc}
-     */
-    public function configure()
-    {
-        $this->setName('sonata:admin:dump-routes');
-        $this->setDescription('Dump route information to improve performance');
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function execute(InputInterface $input, OutputInterface $output)
-    {
-        $output->write("Starting dumping cache file");
-
-        $pool = $this->getContainer()->get('sonata.admin.pool');
-        $cache = $this->getContainer()->get('sonata.admin.route.cache');
-
-        foreach ($pool->getAdminServiceIds() as $id) {
-            $output->writeln(sprintf(' > Generate routes cache for <info>%s</info>', $id));
-            $routes = $cache->load($pool->getInstance($id));
-            $output->writeln(sprintf('   Load %d routes', count($routes)));
-        }
-
-        $output->write("done!");
-    }
-}

+ 7 - 0
Resources/config/route.xml

@@ -22,5 +22,12 @@
             <argument>%kernel.cache_dir%/sonata/admin</argument>
             <argument>%kernel.debug%</argument>
         </service>
+
+        <service id="sonata.admin.route.cache_warmup" class="Sonata\AdminBundle\Route\RoutesCacheWarmUp">
+            <argument type="service" id="sonata.admin.route.cache" />
+            <argument type="service" id="sonata.admin.pool" />
+
+            <tag name="kernel.cache_warmer" />
+        </service>
     </services>
 </container>

+ 1 - 1
Route/RoutesCache.php

@@ -44,7 +44,7 @@ class RoutesCache
      */
     public function load(AdminInterface $admin)
     {
-        $filename = $this->cacheFolder.'/'.md5($admin->getCode());
+        $filename = $this->cacheFolder.'/route_'.md5($admin->getCode());
 
         $cache = new ConfigCache($filename, $this->debug);
         if (!$cache->isFresh()) {

+ 49 - 0
Route/RoutesCacheWarmUp.php

@@ -0,0 +1,49 @@
+<?php
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Route;
+
+use Sonata\AdminBundle\Admin\Pool;
+use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
+
+class RoutesCacheWarmUp implements CacheWarmerInterface
+{
+    protected $cache;
+
+    protected $pool;
+
+    /**
+     * @param RoutesCache $cache
+     * @param Pool        $pool
+     */
+    public function __construct(RoutesCache $cache, Pool $pool)
+    {
+        $this->cache = $cache;
+        $this->pool = $pool;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function isOptional()
+    {
+        true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function warmUp($cacheDir)
+    {
+        foreach ($this->pool->getAdminServiceIds() as $id) {
+            $this->cache->load($this->pool->getInstance($id));
+        }
+    }
+}