Explorar o código

[HttpKernel] added the possibility to define a parent token for a token in the profiler

Note that this is not used yet. I make this change now because the interface needs to be changed.
Fabien Potencier %!s(int64=14) %!d(string=hai) anos
pai
achega
8a8c733369

+ 19 - 7
src/Symfony/Component/HttpKernel/Profiler/Profiler.php

@@ -29,6 +29,7 @@ class Profiler
     protected $logger;
     protected $enabled;
     protected $token;
+    protected $parent;
     protected $data;
     protected $ip;
     protected $url;
@@ -104,7 +105,7 @@ class Profiler
      */
     public function export()
     {
-        $data = base64_encode(serialize(array($this->token, $this->collectors, $this->ip, $this->url, $this->time)));
+        $data = base64_encode(serialize(array($this->token, $this->parent, $this->collectors, $this->ip, $this->url, $this->time)));
 
         return $data;
     }
@@ -118,7 +119,7 @@ class Profiler
      */
     public function import($data)
     {
-        list($token, $collectors, $ip, $url, $time) = unserialize(base64_decode($data));
+        list($token, $parent, $collectors, $ip, $url, $time) = unserialize(base64_decode($data));
 
         if (false !== $this->storage->read($token)) {
             return false;
@@ -126,7 +127,7 @@ class Profiler
 
         $data = base64_encode(serialize($collectors));
 
-        $this->storage->write($token, $data, $ip, $url, $time);
+        $this->storage->write($token, $parent, $data, $ip, $url, $time);
 
         return $token;
     }
@@ -174,6 +175,16 @@ class Profiler
         return $this->empty;
     }
 
+    /**
+     * Returns the parent token.
+     *
+     * @return string The parent token
+     */
+    public function getParent()
+    {
+        return $this->parent;
+    }
+
     /**
      * Returns the IP.
      *
@@ -237,13 +248,14 @@ class Profiler
             $collector->collect($request, $response, $exception);
         }
 
-        $this->ip   = $request->server->get('REMOTE_ADDR');
-        $this->url  = $request->getUri();
-        $this->time = time();
+        $this->parent = '';
+        $this->ip     = $request->server->get('REMOTE_ADDR');
+        $this->url    = $request->getUri();
+        $this->time   = time();
 
         $data = base64_encode(serialize($this->collectors));
 
-        if (true === $this->storage->write($this->token, $data, $this->ip, $this->url, $this->time)) {
+        if (true === $this->storage->write($this->token, $this->parent, $data, $this->ip, $this->url, $this->time)) {
             $this->empty = false;
         } elseif (null !== $this->logger) {
             if (null !== $exception) {

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

@@ -43,15 +43,16 @@ interface ProfilerStorageInterface
     /**
      * Write data associated with the given token.
      *
-     * @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
+     * @param string  $token  A token
+     * @param string  $parent The parent 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
      *
      * @return Boolean Write operation successful
      */
-    function write($token, $data, $ip, $url, $time);
+    function write($token, $parent, $data, $ip, $url, $time);
 
     /**
      * Purges all data from the database.

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

@@ -81,11 +81,12 @@ class SqliteProfilerStorage implements ProfilerStorageInterface
     /**
      * {@inheritdoc}
      */
-    public function write($token, $data, $ip, $url, $time)
+    public function write($token, $parent, $data, $ip, $url, $time)
     {
         $db = $this->initDb();
         $args = array(
             ':token'        => $token,
+            ':parent'       => $parent,
             ':data'         => $data,
             ':ip'           => $ip,
             ':url'          => $url,
@@ -93,7 +94,7 @@ class SqliteProfilerStorage implements ProfilerStorageInterface
             ':created_at'   => time(),
         );
         try {
-            $this->exec($db, 'INSERT INTO data (token, data, ip, url, time, created_at) VALUES (:token, :data, :ip, :url, :time, :created_at)', $args);
+            $this->exec($db, 'INSERT INTO data (token, parent, data, ip, url, time, created_at) VALUES (:token, :parent, :data, :ip, :url, :time, :created_at)', $args);
             $this->cleanup();
             $status = true;
         } catch (\Exception $e) {
@@ -134,10 +135,11 @@ class SqliteProfilerStorage implements ProfilerStorageInterface
             throw new \RuntimeException('You need to enable either the SQLite or PDO_SQLite extension for the profiler to run properly.');
         }
 
-        $db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER, created_at INTEGER)');
+        $db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)');
         $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON data (created_at)');
         $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON data (ip)');
         $db->exec('CREATE INDEX IF NOT EXISTS data_url ON data (url)');
+        $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON data (parent)');
         $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON data (token)');
 
         return $db;