2011-07-23 19:05:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\HttpKernel\Profiler;
|
|
|
|
|
|
|
|
class MongoDbProfilerStorage implements ProfilerStorageInterface
|
|
|
|
{
|
|
|
|
protected $dsn;
|
2011-07-24 19:45:33 +01:00
|
|
|
private $mongo;
|
2011-07-23 19:05:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $dsn A data source name
|
|
|
|
*/
|
|
|
|
public function __construct($dsn)
|
|
|
|
{
|
|
|
|
$this->dsn = $dsn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds profiler tokens for the given criteria.
|
|
|
|
*
|
|
|
|
* @param string $ip The IP
|
|
|
|
* @param string $url The URL
|
|
|
|
* @param string $limit The maximum number of tokens to return
|
|
|
|
*
|
|
|
|
* @return array An array of tokens
|
|
|
|
*/
|
2011-07-24 19:45:33 +01:00
|
|
|
public function find($ip, $url, $limit)
|
2011-07-23 19:05:48 +01:00
|
|
|
{
|
2011-07-24 19:45:33 +01:00
|
|
|
$cursor = $this->getMongo()->find(array('ip' => $ip, 'url' => $url))->limit($limit);
|
2011-07-23 19:05:48 +01:00
|
|
|
$return = array();
|
|
|
|
foreach ($cursor as $profile) {
|
|
|
|
$return[] = $profile['token'];
|
|
|
|
}
|
2011-07-24 19:45:33 +01:00
|
|
|
|
2011-07-23 19:05:48 +01:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purges all data from the database.
|
|
|
|
*/
|
2011-07-24 19:45:33 +01:00
|
|
|
public function purge()
|
2011-07-23 19:05:48 +01:00
|
|
|
{
|
2011-07-24 19:45:33 +01:00
|
|
|
$this->getMongo()->remove(array());
|
2011-07-23 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads data associated with the given token.
|
|
|
|
*
|
|
|
|
* The method returns false if the token does not exists in the storage.
|
|
|
|
*
|
|
|
|
* @param string $token A token
|
|
|
|
*
|
|
|
|
* @return Profile The profile associated with token
|
|
|
|
*/
|
2011-07-24 19:45:33 +01:00
|
|
|
public function read($token)
|
2011-07-23 19:05:48 +01:00
|
|
|
{
|
2011-07-24 19:45:33 +01:00
|
|
|
$profile = $this->getMongo()->findOne(array('token' => $token));
|
|
|
|
|
2011-07-23 23:35:06 +01:00
|
|
|
return $profile !== null ? unserialize($profile['profile']) : null;
|
2011-07-23 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data associated with the given token.
|
|
|
|
*
|
|
|
|
* @param Profile $profile A Profile instance
|
|
|
|
*
|
|
|
|
* @return Boolean Write operation successful
|
|
|
|
*/
|
2011-07-24 19:45:33 +01:00
|
|
|
public function write(Profile $profile)
|
2011-07-23 19:05:48 +01:00
|
|
|
{
|
2011-07-24 19:45:33 +01:00
|
|
|
return $this->getMongo()->insert(array(
|
2011-09-04 13:38:29 +01:00
|
|
|
'token' => $profile->getToken(),
|
|
|
|
'ip' => $profile->getIp(),
|
|
|
|
'url' => $profile->getUrl() === null ? '' : $profile->getUrl(),
|
|
|
|
'profile' => serialize($profile)
|
|
|
|
));
|
2011-07-23 19:05:48 +01:00
|
|
|
}
|
2011-07-24 19:45:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal convenience method that returns the instance of the MongoDB Collection
|
|
|
|
*
|
|
|
|
* @return \MongoCollection
|
|
|
|
*/
|
|
|
|
protected function getMongo()
|
|
|
|
{
|
|
|
|
if ($this->mongo === null) {
|
|
|
|
$mongo = new \Mongo($this->dsn);
|
|
|
|
list($database, $collection,) = explode('/', substr(parse_url($this->dsn, PHP_URL_PATH), 1));
|
|
|
|
$this->mongo = $mongo->selectCollection($database, $collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->mongo;
|
|
|
|
}
|
2011-07-23 19:05:48 +01:00
|
|
|
}
|