[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Profiler/ -> MongoDbProfilerStorage.php (source)

   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  class MongoDbProfilerStorage implements ProfilerStorageInterface
  15  {
  16      protected $dsn;
  17      protected $lifetime;
  18      private $mongo;
  19  
  20      /**
  21       * Constructor.
  22       *
  23       * @param string $dsn      A data source name
  24       * @param string $username Not used
  25       * @param string $password Not used
  26       * @param int    $lifetime The lifetime to use for the purge
  27       */
  28      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  29      {
  30          $this->dsn = $dsn;
  31          $this->lifetime = (int) $lifetime;
  32      }
  33  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function find($ip, $url, $limit, $method, $start = null, $end = null)
  38      {
  39          $cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
  40  
  41          $tokens = array();
  42          foreach ($cursor as $profile) {
  43              $tokens[] = $this->getData($profile);
  44          }
  45  
  46          return $tokens;
  47      }
  48  
  49      /**
  50       * {@inheritdoc}
  51       */
  52      public function purge()
  53      {
  54          $this->getMongo()->remove(array());
  55      }
  56  
  57      /**
  58       * {@inheritdoc}
  59       */
  60      public function read($token)
  61      {
  62          $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
  63  
  64          if (null !== $profile) {
  65              $profile = $this->createProfileFromData($this->getData($profile));
  66          }
  67  
  68          return $profile;
  69      }
  70  
  71      /**
  72       * {@inheritdoc}
  73       */
  74      public function write(Profile $profile)
  75      {
  76          $this->cleanup();
  77  
  78          $record = array(
  79              '_id' => $profile->getToken(),
  80              'parent' => $profile->getParentToken(),
  81              'data' => base64_encode(serialize($profile->getCollectors())),
  82              'ip' => $profile->getIp(),
  83              'method' => $profile->getMethod(),
  84              'url' => $profile->getUrl(),
  85              'time' => $profile->getTime(),
  86          );
  87  
  88          $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
  89  
  90          return (bool) (isset($result['ok']) ? $result['ok'] : $result);
  91      }
  92  
  93      /**
  94       * Internal convenience method that returns the instance of the MongoDB Collection.
  95       *
  96       * @return \MongoCollection
  97       *
  98       * @throws \RuntimeException
  99       */
 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  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1