Browse Source

[HttpKernel] added import/export to Profiler

Fabien Potencier 14 years ago
parent
commit
8c6478dab9

+ 26 - 4
src/Symfony/Component/HttpKernel/Profiler/Profiler.php

@@ -89,6 +89,28 @@ class Profiler
         return $profiler;
     }
 
+    public function export()
+    {
+        $unpack = unpack('H*', serialize(array($this->token, $this->collectors, $this->ip, $this->url, $this->time)));
+
+        return $unpack[1];
+    }
+
+    public function import($data)
+    {
+        list($token, $collectors, $ip, $url, $time) = unserialize(pack('H*', $data));
+
+        if (false !== $this->storage->read($token)) {
+            return false;
+        }
+
+        $unpack = unpack('H*', serialize($this->collectors));
+
+        $this->storage->write($token, $unpack[1], $ip, $url, $time);
+
+        return $token;
+    }
+
     /**
      * Sets the token.
      *
@@ -99,8 +121,8 @@ class Profiler
         $this->token = $token;
 
         if (false !== $items = $this->storage->read($token)) {
-            list($collectors, $this->ip, $this->url, $this->time) = $items;
-            $this->setCollectors($collectors);
+            list($data, $this->ip, $this->url, $this->time) = $items;
+            $this->setCollectors(unserialize(pack('H*', $data)));
 
             $this->empty = false;
         } else {
@@ -200,9 +222,9 @@ class Profiler
         $this->url  = $request->getUri();
         $this->time = time();
 
+        $unpack = unpack('H*', serialize($this->collectors));
         try {
-            $this->storage->write($this->token, $this->collectors, $this->ip, $this->url, $this->time);
-
+            $this->storage->write($this->token, $unpack[1], $this->ip, $this->url, $this->time);
             $this->empty = false;
         } catch (\Exception $e) {
             if (null !== $this->logger) {

+ 7 - 9
src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php

@@ -2,8 +2,6 @@
 
 namespace Symfony\Component\HttpKernel\Profiler;
 
-use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
-
 /*
  * This file is part of the Symfony framework.
  *
@@ -38,18 +36,18 @@ interface ProfilerStorageInterface
      *
      * @param string $token A token
      *
-     * @return DataCollectorInterface[] An array of DataCollectorInterface instance
+     * @return string The data associated with token
      */
     function read($token);
 
     /**
      * Reads data associated with the given token.
      *
-     * @param string                   $token A token
-     * @param DataCollectorInterface[] $collectors An array of DataCollectorInterface instances
-     * @param string                   $ip    An IP
-     * @param string                   $url   An URL
-     * @param integer                  $time  The time of the data
+     * @param string  $token A token
+     * @param string  $data  The data associated with token
+     * @param string  $ip    An IP
+     * @param string  $url   An URL
+     * @param integer $time  The time of the data
      */
-    function write($token, $collectors, $ip, $url, $time);
+    function write($token, $data, $ip, $url, $time);
 }

+ 3 - 5
src/Symfony/Component/HttpKernel/Profiler/SQLiteProfilerStorage.php

@@ -69,7 +69,7 @@ class SQLiteProfilerStorage implements ProfilerStorageInterface
         $data = $this->fetch($db, 'SELECT data, ip, url, time FROM data WHERE token = :token ORDER BY time DESC LIMIT 1', $args);
         $this->close($db);
         if (isset($data[0]['data'])) {
-            return array(unserialize(pack('H*', $data[0]['data'])), $data[0]['ip'], $data[0]['url'], $data[0]['time']);
+            return array($data[0]['data'], $data[0]['ip'], $data[0]['url'], $data[0]['time']);
         } else {
             return false;
         }
@@ -78,11 +78,8 @@ class SQLiteProfilerStorage implements ProfilerStorageInterface
     /**
      * {@inheritdoc}
      */
-    public function write($token, $collectors, $ip, $url, $time)
+    public function write($token, $data, $ip, $url, $time)
     {
-        $unpack = unpack('H*', serialize($collectors));
-        $data = $unpack[1];
-
         $db = $this->initDb();
         $args = array(
             ':token' => $token,
@@ -125,6 +122,7 @@ class SQLiteProfilerStorage implements ProfilerStorageInterface
 
         $db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER)');
         $db->exec('CREATE INDEX IF NOT EXISTS data_data ON data (time)');
+        $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON data (token)');
 
         return $db;
     }