Source of file MongoDbProfilerStorage.php
Size: 6,566 Bytes - Last Modified: 2013-07-17T08:22:21+02:00
/home/theseer/Downloads/Symfony/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 | <?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; protected $lifetime; private $mongo; /** * Constructor. * * @param string $dsn A data source name * @param string $username Not used * @param string $password Not used * @param integer $lifetime The lifetime to use for the purge */ public function __construct($dsn, $username = '', $password = '', $lifetime = 86400) { $this->dsn = $dsn; $this->lifetime = (int) $lifetime; } /** * {@inheritdoc} */ public function find($ip, $url, $limit, $method, $start = null, $end = null) { $cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit); $tokens = array(); foreach ($cursor as $profile) { $tokens[] = $this->getData($profile); } return $tokens; } /** * {@inheritdoc} */ public function purge() { $this->getMongo()->remove(array()); } /** * {@inheritdoc} */ public function read($token) { $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true))); if (null !== $profile) { $profile = $this->createProfileFromData($this->getData($profile)); } return $profile; } /** * {@inheritdoc} */ public function write(Profile $profile) { $this->cleanup(); $record = array( '_id' => $profile->getToken(), 'parent' => $profile->getParentToken(), 'data' => base64_encode(serialize($profile->getCollectors())), 'ip' => $profile->getIp(), 'method' => $profile->getMethod(), 'url' => $profile->getUrl(), 'time' => $profile->getTime() ); $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true)); return (boolean) (isset($result['ok']) ? $result['ok'] : $result); } /** * Internal convenience method that returns the instance of the MongoDB Collection * * @return \MongoCollection * * @throws \RuntimeException */ protected function getMongo() { if ($this->mongo === null) { if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) { $server = $matches[1].(!empty($matches[2]) ? '/'.$matches[2] : ''); $database = $matches[2]; $collection = $matches[3]; $mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient'; $mongo = new $mongoClass($server); $this->mongo = $mongo->selectCollection($database, $collection); } else { throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://[user:pass@]host/database/collection"', $this->dsn)); } } 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 Profile[] An array of Profile instances */ 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; } protected function cleanup() { $this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime))); } /** * @param string $ip * @param string $url * @param string $method * @param int $start * @param int $end * * @return array */ private function buildQuery($ip, $url, $method, $start, $end) { $query = array(); if (!empty($ip)) { $query['ip'] = $ip; } if (!empty($url)) { $query['url'] = $url; } if (!empty($method)) { $query['method'] = $method; } if (!empty($start) || !empty($end)) { $query['time'] = array(); } if (!empty($start)) { $query['time']['$gte'] = $start; } if (!empty($end)) { $query['time']['$lte'] = $end; } 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, 'method' => isset($data['method']) ? $data['method'] : 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->setMethod($data['method']); $profile->setUrl($data['url']); $profile->setTime($data['time']); $profile->setCollectors(unserialize(base64_decode($data['data']))); return $profile; } } |