Skip to content

Commit f66164e

Browse files
committed
merged noelg/profiler
2 parents 4b07440 + b2fb5e0 commit f66164e

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

Profiler/PdoProfilerStorage.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,36 @@ public function find($ip, $url, $limit)
5353
$criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
5454

5555
$db = $this->initDb();
56-
$tokens = $this->fetch($db, 'SELECT token, ip, url, time FROM data '.$criteria.' ORDER BY time DESC LIMIT '.((integer) $limit), $args);
56+
$tokens = $this->fetch($db, 'SELECT token, ip, url, time, parent FROM data '.$criteria.' ORDER BY time DESC LIMIT '.((integer) $limit), $args);
5757
$this->close($db);
5858

5959
return $tokens;
6060
}
6161

62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function findChildren($token)
66+
{
67+
$db = $this->initDb();
68+
$args = array(':token' => $token);
69+
$tokens = $this->fetch($db, 'SELECT token FROM data WHERE parent = :token LIMIT 1', $args);
70+
$this->close($db);
71+
72+
return $tokens;
73+
}
74+
6275
/**
6376
* {@inheritdoc}
6477
*/
6578
public function read($token)
6679
{
6780
$db = $this->initDb();
6881
$args = array(':token' => $token);
69-
$data = $this->fetch($db, 'SELECT data, ip, url, time FROM data WHERE token = :token LIMIT 1', $args);
82+
$data = $this->fetch($db, 'SELECT data, parent, ip, url, time FROM data WHERE token = :token LIMIT 1', $args);
7083
$this->close($db);
7184
if (isset($data[0]['data'])) {
72-
return array($data[0]['data'], $data[0]['ip'], $data[0]['url'], $data[0]['time']);
85+
return array($data[0]['data'], $data[0]['parent'], $data[0]['ip'], $data[0]['url'], $data[0]['time']);
7386
}
7487

7588
return false;

Profiler/Profiler.php

+43-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Profiler
3535
protected $url;
3636
protected $time;
3737
protected $empty;
38+
protected $children;
3839

3940
/**
4041
* Constructor.
@@ -142,7 +143,7 @@ public function setToken($token)
142143
$this->token = $token;
143144

144145
if (false !== $items = $this->storage->read($token)) {
145-
list($data, $this->ip, $this->url, $this->time) = $items;
146+
list($data, $this->parent, $this->ip, $this->url, $this->time) = $items;
146147
$this->set(unserialize(base64_decode($data)));
147148

148149
$this->empty = false;
@@ -151,6 +152,30 @@ public function setToken($token)
151152
}
152153
}
153154

155+
/**
156+
* Sets the parent token
157+
*
158+
* @param string $parent The parent token
159+
*/
160+
public function setParent($parent)
161+
{
162+
$this->parent = $parent;
163+
}
164+
165+
/**
166+
* Returns an instance of the parent token
167+
*
168+
* @return Profiler
169+
*/
170+
public function getParentToken()
171+
{
172+
if (null !== $this->parent) {
173+
return $this->loadFromToken($this->parent);
174+
}
175+
176+
return null;
177+
}
178+
154179
/**
155180
* Gets the token.
156181
*
@@ -229,6 +254,23 @@ public function find($ip, $url, $limit)
229254
return $this->storage->find($ip, $url, $limit);
230255
}
231256

257+
/**
258+
* Finds children profilers.
259+
*
260+
* @return array An array of Profiler
261+
*/
262+
public function getChildren()
263+
{
264+
if (null === $this->children) {
265+
$this->children = array();
266+
foreach ($this->storage->findChildren($this->token) as $token) {
267+
$this->children[] = $this->loadFromToken($token['token']);
268+
}
269+
}
270+
271+
return $this->children;
272+
}
273+
232274
/**
233275
* Collects data for the given Response.
234276
*
@@ -248,7 +290,6 @@ public function collect(Request $request, Response $response, \Exception $except
248290
$collector->collect($request, $response, $exception);
249291
}
250292

251-
$this->parent = '';
252293
$this->ip = $request->server->get('REMOTE_ADDR');
253294
$this->url = $request->getUri();
254295
$this->time = time();

Profiler/ProfilerStorageInterface.php

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ interface ProfilerStorageInterface
2929
*/
3030
function find($ip, $url, $limit);
3131

32+
/**
33+
* Finds profiler tokens for the given parent token.
34+
*
35+
* @param string $token The parent token
36+
*
37+
* @return array An array of tokens
38+
*/
39+
function findChildren($token);
40+
3241
/**
3342
* Reads data associated with the given token.
3443
*

0 commit comments

Comments
 (0)