This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php

105 lines
2.8 KiB
PHP
Raw Normal View History

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;
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
*/
public function find($ip, $url, $limit)
2011-07-23 19:05:48 +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-23 19:05:48 +01:00
return $return;
}
/**
* Purges all data from the database.
*/
public function purge()
2011-07-23 19:05:48 +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
*/
public function read($token)
2011-07-23 19:05:48 +01:00
{
$profile = $this->getMongo()->findOne(array('token' => $token));
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
*/
public function write(Profile $profile)
2011-07-23 19:05:48 +01:00
{
return $this->getMongo()->insert(array(
2011-07-23 19:05:48 +01:00
'token' => $profile->getToken(),
'ip' => $profile->getIp(),
'url' => $profile->getUrl() === null ? '' : $profile->getUrl(),
'profile' => serialize($profile)
));
}
/**
* 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
}