[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Profiler/ -> SqliteProfilerStorage.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  /**
  15   * SqliteProfilerStorage stores profiling information in a SQLite database.
  16   *
  17   * @author Fabien Potencier <fabien@symfony.com>
  18   */
  19  class SqliteProfilerStorage extends PdoProfilerStorage
  20  {
  21      /**
  22       * @throws \RuntimeException When neither of SQLite3 or PDO_SQLite extension is enabled
  23       */
  24      protected function initDb()
  25      {
  26          if (null === $this->db || $this->db instanceof \SQLite3) {
  27              if (0 !== strpos($this->dsn, 'sqlite')) {
  28                  throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Sqlite with an invalid dsn "%s". The expected format is "sqlite:/path/to/the/db/file".', $this->dsn));
  29              }
  30              if (class_exists('SQLite3')) {
  31                  $db = new \SQLite3(substr($this->dsn, 7, strlen($this->dsn)), \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
  32                  if (method_exists($db, 'busyTimeout')) {
  33                      // busyTimeout only exists for PHP >= 5.3.3
  34                      $db->busyTimeout(1000);
  35                  }
  36              } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
  37                  $db = new \PDO($this->dsn);
  38              } else {
  39                  throw new \RuntimeException('You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.');
  40              }
  41  
  42              $db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
  43              $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)');
  44              $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
  45              $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
  46              $db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
  47              $db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
  48              $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
  49              $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
  50  
  51              $this->db = $db;
  52          }
  53  
  54          return $this->db;
  55      }
  56  
  57      protected function exec($db, $query, array $args = array())
  58      {
  59          if ($db instanceof \SQLite3) {
  60              $stmt = $this->prepareStatement($db, $query);
  61              foreach ($args as $arg => $val) {
  62                  $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  63              }
  64  
  65              $res = $stmt->execute();
  66              if (false === $res) {
  67                  throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query));
  68              }
  69              $res->finalize();
  70          } else {
  71              parent::exec($db, $query, $args);
  72          }
  73      }
  74  
  75      protected function fetch($db, $query, array $args = array())
  76      {
  77          $return = array();
  78  
  79          if ($db instanceof \SQLite3) {
  80              $stmt = $this->prepareStatement($db, $query);
  81              foreach ($args as $arg => $val) {
  82                  $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  83              }
  84              $res = $stmt->execute();
  85              while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
  86                  $return[] = $row;
  87              }
  88              $res->finalize();
  89              $stmt->close();
  90          } else {
  91              $return = parent::fetch($db, $query, $args);
  92          }
  93  
  94          return $return;
  95      }
  96  
  97      /**
  98       * {@inheritdoc}
  99       */
 100      protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
 101      {
 102          $criteria = array();
 103          $args = array();
 104  
 105          if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
 106              $criteria[] = 'ip LIKE :ip';
 107              $args[':ip'] = '%'.$ip.'%';
 108          }
 109  
 110          if ($url) {
 111              $criteria[] = 'url LIKE :url ESCAPE "\"';
 112              $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
 113          }
 114  
 115          if ($method) {
 116              $criteria[] = 'method = :method';
 117              $args[':method'] = $method;
 118          }
 119  
 120          if (!empty($start)) {
 121              $criteria[] = 'time >= :start';
 122              $args[':start'] = $start;
 123          }
 124  
 125          if (!empty($end)) {
 126              $criteria[] = 'time <= :end';
 127              $args[':end'] = $end;
 128          }
 129  
 130          return array($criteria, $args);
 131      }
 132  
 133      protected function close($db)
 134      {
 135          if ($db instanceof \SQLite3) {
 136              $db->close();
 137          }
 138      }
 139  }


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