4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Profiler;
14 @trigger_error('The '.__NAMESPACE__.'\MongoDbProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
17 * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
18 * Use {@link FileProfilerStorage} instead.
20 class MongoDbProfilerStorage implements ProfilerStorageInterface
29 * @param string $dsn A data source name
30 * @param string $username Not used
31 * @param string $password Not used
32 * @param int $lifetime The lifetime to use for the purge
34 public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
37 $this->lifetime = (int) $lifetime;
43 public function find($ip, $url, $limit, $method, $start = null, $end = null)
45 $cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time', 'status_code'))->sort(array('time' => -1))->limit($limit);
48 foreach ($cursor as $profile) {
49 $tokens[] = $this->getData($profile);
58 public function purge()
60 $this->getMongo()->remove(array());
66 public function read($token)
68 $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
70 if (null !== $profile) {
71 $profile = $this->createProfileFromData($this->getData($profile));
80 public function write(Profile $profile)
85 '_id' => $profile->getToken(),
86 'parent' => $profile->getParentToken(),
87 'data' => base64_encode(serialize($profile->getCollectors())),
88 'ip' => $profile->getIp(),
89 'method' => $profile->getMethod(),
90 'url' => $profile->getUrl(),
91 'time' => $profile->getTime(),
92 'status_code' => $profile->getStatusCode(),
95 $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
97 return (bool) (isset($result['ok']) ? $result['ok'] : $result);
101 * Internal convenience method that returns the instance of the MongoDB Collection.
103 * @return \MongoCollection
105 * @throws \RuntimeException
107 protected function getMongo()
109 if (null !== $this->mongo) {
113 if (!$parsedDsn = $this->parseDsn($this->dsn)) {
114 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));
117 list($server, $database, $collection) = $parsedDsn;
118 $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\Mongo' : '\MongoClient';
119 $mongo = new $mongoClass($server);
121 return $this->mongo = $mongo->selectCollection($database, $collection);
129 protected function createProfileFromData(array $data)
131 $profile = $this->getProfile($data);
133 if ($data['parent']) {
134 $parent = $this->getMongo()->findOne(array('_id' => $data['parent'], 'data' => array('$exists' => true)));
136 $profile->setParent($this->getProfile($this->getData($parent)));
140 $profile->setChildren($this->readChildren($data['token']));
146 * @param string $token
148 * @return Profile[] An array of Profile instances
150 protected function readChildren($token)
154 $cursor = $this->getMongo()->find(array('parent' => $token, 'data' => array('$exists' => true)));
155 foreach ($cursor as $d) {
156 $profiles[] = $this->getProfile($this->getData($d));
162 protected function cleanup()
164 $this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime)));
170 * @param string $method
176 private function buildQuery($ip, $url, $method, $start, $end)
185 $query['url'] = $url;
188 if (!empty($method)) {
189 $query['method'] = $method;
192 if (!empty($start) || !empty($end)) {
193 $query['time'] = array();
196 if (!empty($start)) {
197 $query['time']['$gte'] = $start;
201 $query['time']['$lte'] = $end;
212 private function getData(array $data)
215 'token' => $data['_id'],
216 'parent' => isset($data['parent']) ? $data['parent'] : null,
217 'ip' => isset($data['ip']) ? $data['ip'] : null,
218 'method' => isset($data['method']) ? $data['method'] : null,
219 'url' => isset($data['url']) ? $data['url'] : null,
220 'time' => isset($data['time']) ? $data['time'] : null,
221 'data' => isset($data['data']) ? $data['data'] : null,
222 'status_code' => isset($data['status_code']) ? $data['status_code'] : null,
231 private function getProfile(array $data)
233 $profile = new Profile($data['token']);
234 $profile->setIp($data['ip']);
235 $profile->setMethod($data['method']);
236 $profile->setUrl($data['url']);
237 $profile->setTime($data['time']);
238 $profile->setCollectors(unserialize(base64_decode($data['data'])));
246 * @return null|array Array($server, $database, $collection)
248 private function parseDsn($dsn)
250 if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
254 $server = $matches[1];
255 $database = $matches[2];
256 $collection = $matches[3];
257 preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
259 if ('' == $matchesServer[5] && '' != $matches[2]) {
260 $server .= '/'.$matches[2];
263 return array($server, $database, $collection);