Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
MongoDbProfilerStorage.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpKernel\Profiler;
013
014 @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);
015
016 /**
017 * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
018 * Use {@link FileProfilerStorage} instead.
019 */
020 class MongoDbProfilerStorage implements ProfilerStorageInterface
021 {
022 protected $dsn;
023 protected $lifetime;
024 private $mongo;
025
026 /**
027 * Constructor.
028 *
029 * @param string $dsn A data source name
030 * @param string $username Not used
031 * @param string $password Not used
032 * @param int $lifetime The lifetime to use for the purge
033 */
034 public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
035 {
036 $this->dsn = $dsn;
037 $this->lifetime = (int) $lifetime;
038 }
039
040 /**
041 * {@inheritdoc}
042 */
043 public function find($ip, $url, $limit, $method, $start = null, $end = null)
044 {
045 $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);
046
047 $tokens = array();
048 foreach ($cursor as $profile) {
049 $tokens[] = $this->getData($profile);
050 }
051
052 return $tokens;
053 }
054
055 /**
056 * {@inheritdoc}
057 */
058 public function purge()
059 {
060 $this->getMongo()->remove(array());
061 }
062
063 /**
064 * {@inheritdoc}
065 */
066 public function read($token)
067 {
068 $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
069
070 if (null !== $profile) {
071 $profile = $this->createProfileFromData($this->getData($profile));
072 }
073
074 return $profile;
075 }
076
077 /**
078 * {@inheritdoc}
079 */
080 public function write(Profile $profile)
081 {
082 $this->cleanup();
083
084 $record = array(
085 '_id' => $profile->getToken(),
086 'parent' => $profile->getParentToken(),
087 'data' => base64_encode(serialize($profile->getCollectors())),
088 'ip' => $profile->getIp(),
089 'method' => $profile->getMethod(),
090 'url' => $profile->getUrl(),
091 'time' => $profile->getTime(),
092 'status_code' => $profile->getStatusCode(),
093 );
094
095 $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
096
097 return (bool) (isset($result['ok']) ? $result['ok'] : $result);
098 }
099
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 }
266