[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/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  @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       * @param string $dsn      A data source name
  28       * @param string $username Not used
  29       * @param string $password Not used
  30       * @param int    $lifetime The lifetime to use for the purge
  31       */
  32      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  33      {
  34          $this->dsn = $dsn;
  35          $this->lifetime = (int) $lifetime;
  36      }
  37  
  38      /**
  39       * {@inheritdoc}
  40       */
  41      public function find($ip, $url, $limit, $method, $start = null, $end = null)
  42      {
  43          $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);
  44  
  45          $tokens = array();
  46          foreach ($cursor as $profile) {
  47              $tokens[] = $this->getData($profile);
  48          }
  49  
  50          return $tokens;
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      public function purge()
  57      {
  58          $this->getMongo()->remove(array());
  59      }
  60  
  61      /**
  62       * {@inheritdoc}
  63       */
  64      public function read($token)
  65      {
  66          $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
  67  
  68          if (null !== $profile) {
  69              $profile = $this->createProfileFromData($this->getData($profile));
  70          }
  71  
  72          return $profile;
  73      }
  74  
  75      /**
  76       * {@inheritdoc}
  77       */
  78      public function write(Profile $profile)
  79      {
  80          $this->cleanup();
  81  
  82          $record = array(
  83              '_id' => $profile->getToken(),
  84              'parent' => $profile->getParentToken(),
  85              'data' => base64_encode(serialize($profile->getCollectors())),
  86              'ip' => $profile->getIp(),
  87              'method' => $profile->getMethod(),
  88              'url' => $profile->getUrl(),
  89              'time' => $profile->getTime(),
  90              'status_code' => $profile->getStatusCode(),
  91          );
  92  
  93          $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
  94  
  95          return (bool) (isset($result['ok']) ? $result['ok'] : $result);
  96      }
  97  
  98      /**
  99       * Internal convenience method that returns the instance of the MongoDB Collection.
 100       *
 101       * @return \MongoCollection
 102       *
 103       * @throws \RuntimeException
 104       */
 105      protected function getMongo()
 106      {
 107          if (null !== $this->mongo) {
 108              return $this->mongo;
 109          }
 110  
 111          if (!$parsedDsn = $this->parseDsn($this->dsn)) {
 112              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));
 113          }
 114  
 115          list($server, $database, $collection) = $parsedDsn;
 116          $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\Mongo' : '\MongoClient';
 117          $mongo = new $mongoClass($server);
 118  
 119          return $this->mongo = $mongo->selectCollection($database, $collection);
 120      }
 121  
 122      /**
 123       * @return Profile
 124       */
 125      protected function createProfileFromData(array $data)
 126      {
 127          $profile = $this->getProfile($data);
 128  
 129          if ($data['parent']) {
 130              $parent = $this->getMongo()->findOne(array('_id' => $data['parent'], 'data' => array('$exists' => true)));
 131              if ($parent) {
 132                  $profile->setParent($this->getProfile($this->getData($parent)));
 133              }
 134          }
 135  
 136          $profile->setChildren($this->readChildren($data['token']));
 137  
 138          return $profile;
 139      }
 140  
 141      /**
 142       * @param string $token
 143       *
 144       * @return Profile[] An array of Profile instances
 145       */
 146      protected function readChildren($token)
 147      {
 148          $profiles = array();
 149  
 150          $cursor = $this->getMongo()->find(array('parent' => $token, 'data' => array('$exists' => true)));
 151          foreach ($cursor as $d) {
 152              $profiles[] = $this->getProfile($this->getData($d));
 153          }
 154  
 155          return $profiles;
 156      }
 157  
 158      protected function cleanup()
 159      {
 160          $this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime)));
 161      }
 162  
 163      /**
 164       * @param string $ip
 165       * @param string $url
 166       * @param string $method
 167       * @param int    $start
 168       * @param int    $end
 169       *
 170       * @return array
 171       */
 172      private function buildQuery($ip, $url, $method, $start, $end)
 173      {
 174          $query = array();
 175  
 176          if (!empty($ip)) {
 177              $query['ip'] = $ip;
 178          }
 179  
 180          if (!empty($url)) {
 181              $query['url'] = $url;
 182          }
 183  
 184          if (!empty($method)) {
 185              $query['method'] = $method;
 186          }
 187  
 188          if (!empty($start) || !empty($end)) {
 189              $query['time'] = array();
 190          }
 191  
 192          if (!empty($start)) {
 193              $query['time']['$gte'] = $start;
 194          }
 195  
 196          if (!empty($end)) {
 197              $query['time']['$lte'] = $end;
 198          }
 199  
 200          return $query;
 201      }
 202  
 203      /**
 204       * @return array
 205       */
 206      private function getData(array $data)
 207      {
 208          return array(
 209              'token' => $data['_id'],
 210              'parent' => isset($data['parent']) ? $data['parent'] : null,
 211              'ip' => isset($data['ip']) ? $data['ip'] : null,
 212              'method' => isset($data['method']) ? $data['method'] : null,
 213              'url' => isset($data['url']) ? $data['url'] : null,
 214              'time' => isset($data['time']) ? $data['time'] : null,
 215              'data' => isset($data['data']) ? $data['data'] : null,
 216              'status_code' => isset($data['status_code']) ? $data['status_code'] : null,
 217          );
 218      }
 219  
 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 array|null 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: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1