Procházet zdrojové kódy

[DoctrineMongoDBBundle] added a quick profiler panel

Kris Wallsmith před 14 roky
rodič
revize
e6bff045c9

+ 6 - 0
src/Symfony/Bundle/DoctrineMongoDBBundle/DataCollector/DoctrineMongoDBDataCollector.php

@@ -27,6 +27,7 @@ class DoctrineMongoDBDataCollector extends DataCollector
     public function collect(Request $request, Response $response, \Exception $exception = null)
     {
         $this->data['nb_queries'] = $this->logger->getNbQueries();
+        $this->data['queries'] = $this->logger->getQueries();
     }
 
     public function getQueryCount()
@@ -34,6 +35,11 @@ class DoctrineMongoDBDataCollector extends DataCollector
         return $this->data['nb_queries'];
     }
 
+    public function getQueries()
+    {
+        return $this->data['queries'];
+    }
+
     /**
      * {@inheritdoc}
      */

+ 23 - 1
src/Symfony/Bundle/DoctrineMongoDBBundle/Logger/DoctrineMongoDBLogger.php

@@ -12,6 +12,8 @@ use Symfony\Component\Yaml\Yaml;
  */
 class DoctrineMongoDBLogger
 {
+    const LOG_PREFIX = 'MongoDB query: ';
+
     protected $logger;
     protected $nbQueries;
 
@@ -26,7 +28,7 @@ class DoctrineMongoDBLogger
         ++$this->nbQueries;
 
         if (null !== $this->logger) {
-            $this->logger->info(static::formatQuery($query));
+            $this->logger->info(static::LOG_PREFIX.static::formatQuery($query));
         }
     }
 
@@ -35,6 +37,26 @@ class DoctrineMongoDBLogger
         return $this->nbQueries;
     }
 
+    public function getQueries()
+    {
+        $logger = $this->logger->getDebugLogger();
+
+        if (!$logger) {
+            return false;
+        }
+
+        $offset = strlen(static::LOG_PREFIX);
+        $mapper = function($log) use($offset)
+        {
+            if (0 === strpos($log['message'], DoctrineMongoDBLogger::LOG_PREFIX)) {
+                return substr($log['message'], $offset);
+            }
+        };
+
+        // map queries from logs, remove empty entries and re-index the array
+        return array_values(array_filter(array_map($mapper, $logger->getLogs())));
+    }
+
     /**
      * Formats the supplied query array recursively.
      * 

+ 1 - 0
src/Symfony/Bundle/DoctrineMongoDBBundle/Resources/views/Profiler/mongodb_menu.php

@@ -1,2 +1,3 @@
+<div class="count"><?php echo $data->getQueryCount() ?></div>
 <img style="margin: 0 5px 0 0; vertical-align: middle; width: 32px" alt="" src="<?php echo $view->get('assets')->getUrl('bundles/webprofiler/images/db.png') ?>" />
 Doctrine MongoDB

+ 12 - 1
src/Symfony/Bundle/DoctrineMongoDBBundle/Resources/views/Profiler/mongodb_panel.php

@@ -1,6 +1,17 @@
 <h2>Queries</h2>
 
-<?php if (0 == $data->getQueryCount()): ?>
+<?php if (false === $queries = $data->getQueries()): ?>
+    <em>Query logging is disabled.</em>
+<?php elseif (0 == $data->getQueryCount()): ?>
     <em>No queries.</em>
 <?php else: ?>
+    <ul class="alt">
+        <?php foreach ($queries as $i => $query): ?>
+            <li class="<?php echo $i % 2 ? 'odd' : 'even' ?>">
+                <div>
+                    <code><?php echo $query ?></code>
+                </div>
+            </li>
+        <?php endforeach; ?>
+    </ul>
 <?php endif; ?>