Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-kernel / Profiler / MongoDbProfilerStorage.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Profiler;
13
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);
15
16 /**
17  * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
18  *             Use {@link FileProfilerStorage} instead.
19  */
20 class MongoDbProfilerStorage implements ProfilerStorageInterface
21 {
22     protected $dsn;
23     protected $lifetime;
24     private $mongo;
25
26     /**
27      * Constructor.
28      *
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
33      */
34     public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
35     {
36         $this->dsn = $dsn;
37         $this->lifetime = (int) $lifetime;
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     public function find($ip, $url, $limit, $method, $start = null, $end = null)
44     {
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);
46
47         $tokens = array();
48         foreach ($cursor as $profile) {
49             $tokens[] = $this->getData($profile);
50         }
51
52         return $tokens;
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function purge()
59     {
60         $this->getMongo()->remove(array());
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     public function read($token)
67     {
68         $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
69
70         if (null !== $profile) {
71             $profile = $this->createProfileFromData($this->getData($profile));
72         }
73
74         return $profile;
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     public function write(Profile $profile)
81     {
82         $this->cleanup();
83
84         $record = array(
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(),
93         );
94
95         $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
96
97         return (bool) (isset($result['ok']) ? $result['ok'] : $result);
98     }
99
100     /**
101      * Internal convenience method that returns the instance of the MongoDB Collection.
102      *
103      * @return \MongoCollection
104      *
105      * @throws \RuntimeException
106      */
107     protected function getMongo()
108     {
109         if (null !== $this->mongo) {
110             return $this->mongo;
111         }
112
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));
115         }
116
117         list($server, $database, $collection) = $parsedDsn;
118         $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\Mongo' : '\MongoClient';
119         $mongo = new $mongoClass($server);
120
121         return $this->mongo = $mongo->selectCollection($database, $collection);
122     }
123
124     /**
125      * @param array $data
126      *
127      * @return Profile
128      */
129     protected function createProfileFromData(array $data)
130     {
131         $profile = $this->getProfile($data);
132
133         if ($data['parent']) {
134             $parent = $this->getMongo()->findOne(array('_id' => $data['parent'], 'data' => array('$exists' => true)));
135             if ($parent) {
136                 $profile->setParent($this->getProfile($this->getData($parent)));
137             }
138         }
139
140         $profile->setChildren($this->readChildren($data['token']));
141
142         return $profile;
143     }
144
145     /**
146      * @param string $token
147      *
148      * @return Profile[] An array of Profile instances
149      */
150     protected function readChildren($token)
151     {
152         $profiles = array();
153
154         $cursor = $this->getMongo()->find(array('parent' => $token, 'data' => array('$exists' => true)));
155         foreach ($cursor as $d) {
156             $profiles[] = $this->getProfile($this->getData($d));
157         }
158
159         return $profiles;
160     }
161
162     protected function cleanup()
163     {
164         $this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime)));
165     }
166
167     /**
168      * @param string $ip
169      * @param string $url
170      * @param string $method
171      * @param int    $start
172      * @param int    $end
173      *
174      * @return array
175      */
176     private function buildQuery($ip, $url, $method, $start, $end)
177     {
178         $query = array();
179
180         if (!empty($ip)) {
181             $query['ip'] = $ip;
182         }
183
184         if (!empty($url)) {
185             $query['url'] = $url;
186         }
187
188         if (!empty($method)) {
189             $query['method'] = $method;
190         }
191
192         if (!empty($start) || !empty($end)) {
193             $query['time'] = array();
194         }
195
196         if (!empty($start)) {
197             $query['time']['$gte'] = $start;
198         }
199
200         if (!empty($end)) {
201             $query['time']['$lte'] = $end;
202         }
203
204         return $query;
205     }
206
207     /**
208      * @param array $data
209      *
210      * @return array
211      */
212     private function getData(array $data)
213     {
214         return array(
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,
223         );
224     }
225
226     /**
227      * @param array $data
228      *
229      * @return Profile
230      */
231     private function getProfile(array $data)
232     {
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'])));
239
240         return $profile;
241     }
242
243     /**
244      * @param string $dsn
245      *
246      * @return null|array Array($server, $database, $collection)
247      */
248     private function parseDsn($dsn)
249     {
250         if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
251             return;
252         }
253
254         $server = $matches[1];
255         $database = $matches[2];
256         $collection = $matches[3];
257         preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
258
259         if ('' == $matchesServer[5] && '' != $matches[2]) {
260             $server .= '/'.$matches[2];
261         }
262
263         return array($server, $database, $collection);
264     }
265 }