Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

MongoDbProfilerStorage.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 6.85 KiB


 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  class MongoDbProfilerStorage implements ProfilerStorageInterface
 015  {
 016      protected $dsn;
 017      protected $lifetime;
 018      private $mongo;
 019   
 020      /**
 021       * Constructor.
 022       *
 023       * @param string  $dsn      A data source name
 024       * @param string  $username Not used
 025       * @param string  $password Not used
 026       * @param int     $lifetime The lifetime to use for the purge
 027       */
 028      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
 029      {
 030          $this->dsn = $dsn;
 031          $this->lifetime = (int) $lifetime;
 032      }
 033   
 034      /**
 035       * {@inheritdoc}
 036       */
 037      public function find($ip, $url, $limit, $method, $start = null, $end = null)
 038      {
 039          $cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
 040   
 041          $tokens = array();
 042          foreach ($cursor as $profile) {
 043              $tokens[] = $this->getData($profile);
 044          }
 045   
 046          return $tokens;
 047      }
 048   
 049      /**
 050       * {@inheritdoc}
 051       */
 052      public function purge()
 053      {
 054          $this->getMongo()->remove(array());
 055      }
 056   
 057      /**
 058       * {@inheritdoc}
 059       */
 060      public function read($token)
 061      {
 062          $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
 063   
 064          if (null !== $profile) {
 065              $profile = $this->createProfileFromData($this->getData($profile));
 066          }
 067   
 068          return $profile;
 069      }
 070   
 071      /**
 072       * {@inheritdoc}
 073       */
 074      public function write(Profile $profile)
 075      {
 076          $this->cleanup();
 077   
 078          $record = array(
 079              '_id' => $profile->getToken(),
 080              'parent' => $profile->getParentToken(),
 081              'data' => base64_encode(serialize($profile->getCollectors())),
 082              'ip' => $profile->getIp(),
 083              'method' => $profile->getMethod(),
 084              'url' => $profile->getUrl(),
 085              'time' => $profile->getTime(),
 086          );
 087   
 088          $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
 089   
 090          return (bool) (isset($result['ok']) ? $result['ok'] : $result);
 091      }
 092   
 093      /**
 094       * Internal convenience method that returns the instance of the MongoDB Collection
 095       *
 096       * @return \MongoCollection
 097       *
 098       * @throws \RuntimeException
 099       */
 100      protected function getMongo()
 101      {
 102          if (null !== $this->mongo) {
 103              return $this->mongo;
 104          }
 105   
 106          if (!$parsedDsn = $this->parseDsn($this->dsn)) {
 107              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));
 108          }
 109   
 110          list($server, $database, $collection) = $parsedDsn;
 111          $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\Mongo' : '\MongoClient';
 112          $mongo = new $mongoClass($server);
 113   
 114          return $this->mongo = $mongo->selectCollection($database, $collection);
 115      }
 116   
 117      /**
 118       * @param array $data
 119       *
 120       * @return Profile
 121       */
 122      protected function createProfileFromData(array $data)
 123      {
 124          $profile = $this->getProfile($data);
 125   
 126          if ($data['parent']) {
 127              $parent = $this->getMongo()->findOne(array('_id' => $data['parent'], 'data' => array('$exists' => true)));
 128              if ($parent) {
 129                  $profile->setParent($this->getProfile($this->getData($parent)));
 130              }
 131          }
 132   
 133          $profile->setChildren($this->readChildren($data['token']));
 134   
 135          return $profile;
 136      }
 137   
 138      /**
 139       * @param string $token
 140       *
 141       * @return Profile[] An array of Profile instances
 142       */
 143      protected function readChildren($token)
 144      {
 145          $profiles = array();
 146   
 147          $cursor = $this->getMongo()->find(array('parent' => $token, 'data' => array('$exists' => true)));
 148          foreach ($cursor as $d) {
 149              $profiles[] = $this->getProfile($this->getData($d));
 150          }
 151   
 152          return $profiles;
 153      }
 154   
 155      protected function cleanup()
 156      {
 157          $this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime)));
 158      }
 159   
 160      /**
 161       * @param string $ip
 162       * @param string $url
 163       * @param string $method
 164       * @param int    $start
 165       * @param int    $end
 166       *
 167       * @return array
 168       */
 169      private function buildQuery($ip, $url, $method, $start, $end)
 170      {
 171          $query = array();
 172   
 173          if (!empty($ip)) {
 174              $query['ip'] = $ip;
 175          }
 176   
 177          if (!empty($url)) {
 178              $query['url'] = $url;
 179          }
 180   
 181          if (!empty($method)) {
 182              $query['method'] = $method;
 183          }
 184   
 185          if (!empty($start) || !empty($end)) {
 186              $query['time'] = array();
 187          }
 188   
 189          if (!empty($start)) {
 190              $query['time']['$gte'] = $start;
 191          }
 192   
 193          if (!empty($end)) {
 194              $query['time']['$lte'] = $end;
 195          }
 196   
 197          return $query;
 198      }
 199   
 200      /**
 201       * @param array $data
 202       *
 203       * @return array
 204       */
 205      private function getData(array $data)
 206      {
 207          return array(
 208              'token' => $data['_id'],
 209              'parent' => isset($data['parent']) ? $data['parent'] : null,
 210              'ip' => isset($data['ip']) ? $data['ip'] : null,
 211              'method' => isset($data['method']) ? $data['method'] : null,
 212              'url' => isset($data['url']) ? $data['url'] : null,
 213              'time' => isset($data['time']) ? $data['time'] : null,
 214              'data' => isset($data['data']) ? $data['data'] : null,
 215          );
 216      }
 217   
 218      /**
 219       * @param array $data
 220       *
 221       * @return Profile
 222       */
 223      private function getProfile(array $data)
 224      {
 225          $profile = new Profile($data['token']);
 226          $profile->setIp($data['ip']);
 227          $profile->setMethod($data['method']);
 228          $profile->setUrl($data['url']);
 229          $profile->setTime($data['time']);
 230          $profile->setCollectors(unserialize(base64_decode($data['data'])));
 231   
 232          return $profile;
 233      }
 234   
 235      /**
 236       * @param string $dsn
 237       *
 238       * @return null|array Array($server, $database, $collection)
 239       */
 240      private function parseDsn($dsn)
 241      {
 242          if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
 243              return;
 244          }
 245   
 246          $server = $matches[1];
 247          $database = $matches[2];
 248          $collection = $matches[3];
 249          preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
 250   
 251          if ('' == $matchesServer[5] && '' != $matches[2]) {
 252              $server .= '/'.$matches[2];
 253          }
 254   
 255          return array($server, $database, $collection);
 256      }
 257  }
 258