Fixed the find() method and changed the way the profile data is stored.
This commit is contained in:
parent
beeec5eee2
commit
365e73aa71
@ -37,13 +37,14 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
||||
*/
|
||||
public function find($ip, $url, $limit)
|
||||
{
|
||||
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url))->sort(array('time' => -1))->limit($limit);
|
||||
$return = array();
|
||||
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url), array('_id', 'parent', 'ip', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
|
||||
|
||||
$tokens = array();
|
||||
foreach ($cursor as $profile) {
|
||||
$return[] = $profile['_id'];
|
||||
$tokens[] = $this->getData($profile);
|
||||
}
|
||||
|
||||
return $return;
|
||||
return $tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,9 +66,13 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
||||
*/
|
||||
public function read($token)
|
||||
{
|
||||
$profile = $this->getMongo()->findOne(array('_id' => $token));
|
||||
$profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
|
||||
|
||||
return $profile !== null ? unserialize($profile['profile']) : null;
|
||||
if (null !== $profile) {
|
||||
$profile = $this->createProfileFromData($this->getData($profile));
|
||||
}
|
||||
|
||||
return $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,13 +84,16 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
||||
*/
|
||||
public function write(Profile $profile)
|
||||
{
|
||||
return $this->getMongo()->insert(array(
|
||||
$record = array(
|
||||
'_id' => $profile->getToken(),
|
||||
'parent' => $profile->getParent() ? $profile->getParent()->getToken() : null,
|
||||
'data' => serialize($profile->getCollectors()),
|
||||
'ip' => $profile->getIp(),
|
||||
'url' => $profile->getUrl() === null ? '' : $profile->getUrl(),
|
||||
'time' => $profile->getTime(),
|
||||
'profile' => serialize($profile)
|
||||
));
|
||||
'url' => $profile->getUrl(),
|
||||
'time' => $profile->getTime()
|
||||
);
|
||||
|
||||
return $this->getMongo()->insert(array_filter($record, function ($v) { return !empty($v); }));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,6 +117,42 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
||||
return $this->mongo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return Profile
|
||||
*/
|
||||
protected function createProfileFromData(array $data)
|
||||
{
|
||||
$profile = $this->getProfile($data);
|
||||
|
||||
if ($data['parent']) {
|
||||
$parent = $this->getMongo()->findOne(array('_id' => $data['parent'], 'data' => array('$exists' => true)));
|
||||
if ($parent) {
|
||||
$profile->setParent($this->getProfile($this->getData($parent)));
|
||||
}
|
||||
}
|
||||
|
||||
$profile->setChildren($this->readChildren($data['token']));
|
||||
|
||||
return $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
* @return array
|
||||
*/
|
||||
protected function readChildren($token)
|
||||
{
|
||||
$profiles = array();
|
||||
|
||||
$cursor = $this->getMongo()->find(array('parent' => $token, 'data' => array('$exists' => true)));
|
||||
foreach ($cursor as $d) {
|
||||
$profiles[] = $this->getProfile($this->getData($d));
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ip
|
||||
* @param string $url
|
||||
@ -128,4 +172,35 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
private function getData(array $data)
|
||||
{
|
||||
return array(
|
||||
'token' => $data['_id'],
|
||||
'parent' => isset($data['parent']) ? $data['parent'] : null,
|
||||
'ip' => isset($data['ip']) ? $data['ip'] : null,
|
||||
'url' => isset($data['url']) ? $data['url'] : null,
|
||||
'time' => isset($data['time']) ? $data['time'] : null,
|
||||
'data' => isset($data['data']) ? $data['data'] : null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return Profile
|
||||
*/
|
||||
private function getProfile(array $data)
|
||||
{
|
||||
$profile = new Profile($data['token']);
|
||||
$profile->setIp($data['ip']);
|
||||
$profile->setUrl($data['url']);
|
||||
$profile->setTime($data['time']);
|
||||
$profile->setCollectors(unserialize($data['data']));
|
||||
|
||||
return $profile;
|
||||
}
|
||||
}
|
||||
|
@ -85,9 +85,9 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
$records = self::$storage->find('', '', 3);
|
||||
$this->assertEquals(count($records), 3, '->find() returns all previously added records');
|
||||
$this->assertEquals($records[0], 'time_2', '->find() returns records ordered by time in descendant order');
|
||||
$this->assertEquals($records[1], 'time_1', '->find() returns records ordered by time in descendant order');
|
||||
$this->assertEquals($records[2], 'time_0', '->find() returns records ordered by time in descendant order');
|
||||
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns records ordered by time in descendant order');
|
||||
$this->assertEquals($records[1]['token'], 'time_1', '->find() returns records ordered by time in descendant order');
|
||||
$this->assertEquals($records[2]['token'], 'time_0', '->find() returns records ordered by time in descendant order');
|
||||
self::$storage->purge();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user