[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Profiler/ -> PdoProfilerStorage.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__.'\PdoProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  15  
  16  /**
  17   * Base PDO storage for profiling information in a PDO database.
  18   *
  19   * @author Fabien Potencier <fabien@symfony.com>
  20   * @author Jan Schumann <js@schumann-it.com>
  21   *
  22   * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  23   *             Use {@link FileProfilerStorage} instead.
  24   */
  25  abstract class PdoProfilerStorage implements ProfilerStorageInterface
  26  {
  27      protected $dsn;
  28      protected $username;
  29      protected $password;
  30      protected $lifetime;
  31      protected $db;
  32  
  33      /**
  34       * @param string $dsn      A data source name
  35       * @param string $username The username for the database
  36       * @param string $password The password for the database
  37       * @param int    $lifetime The lifetime to use for the purge
  38       */
  39      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  40      {
  41          $this->dsn = $dsn;
  42          $this->username = $username;
  43          $this->password = $password;
  44          $this->lifetime = (int) $lifetime;
  45      }
  46  
  47      /**
  48       * {@inheritdoc}
  49       */
  50      public function find($ip, $url, $limit, $method, $start = null, $end = null)
  51      {
  52          if (null === $start) {
  53              $start = 0;
  54          }
  55  
  56          if (null === $end) {
  57              $end = time();
  58          }
  59  
  60          list($criteria, $args) = $this->buildCriteria($ip, $url, $start, $end, $limit, $method);
  61  
  62          $criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
  63  
  64          $db = $this->initDb();
  65          $tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent, status_code FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
  66          $this->close($db);
  67  
  68          return $tokens;
  69      }
  70  
  71      /**
  72       * {@inheritdoc}
  73       */
  74      public function read($token)
  75      {
  76          $db = $this->initDb();
  77          $args = array(':token' => $token);
  78          $data = $this->fetch($db, 'SELECT data, parent, ip, method, url, time FROM sf_profiler_data WHERE token = :token LIMIT 1', $args);
  79          $this->close($db);
  80          if (isset($data[0]['data'])) {
  81              return $this->createProfileFromData($token, $data[0]);
  82          }
  83      }
  84  
  85      /**
  86       * {@inheritdoc}
  87       */
  88      public function write(Profile $profile)
  89      {
  90          $db = $this->initDb();
  91          $args = array(
  92              ':token' => $profile->getToken(),
  93              ':parent' => $profile->getParentToken(),
  94              ':data' => base64_encode(serialize($profile->getCollectors())),
  95              ':ip' => $profile->getIp(),
  96              ':method' => $profile->getMethod(),
  97              ':url' => $profile->getUrl(),
  98              ':time' => $profile->getTime(),
  99              ':created_at' => time(),
 100              ':status_code' => $profile->getStatusCode(),
 101          );
 102  
 103          try {
 104              if ($this->has($profile->getToken())) {
 105                  $this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at, status_code = :status_code WHERE token = :token', $args);
 106              } else {
 107                  $this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at, status_code) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at, :status_code)', $args);
 108              }
 109              $this->cleanup();
 110              $status = true;
 111          } catch (\Exception $e) {
 112              $status = false;
 113          }
 114  
 115          $this->close($db);
 116  
 117          return $status;
 118      }
 119  
 120      /**
 121       * {@inheritdoc}
 122       */
 123      public function purge()
 124      {
 125          $db = $this->initDb();
 126          $this->exec($db, 'DELETE FROM sf_profiler_data');
 127          $this->close($db);
 128      }
 129  
 130      /**
 131       * Build SQL criteria to fetch records by ip and url.
 132       *
 133       * @param string $ip     The IP
 134       * @param string $url    The URL
 135       * @param string $start  The start period to search from
 136       * @param string $end    The end period to search to
 137       * @param string $limit  The maximum number of tokens to return
 138       * @param string $method The request method
 139       *
 140       * @return array An array with (criteria, args)
 141       */
 142      abstract protected function buildCriteria($ip, $url, $start, $end, $limit, $method);
 143  
 144      /**
 145       * Initializes the database.
 146       *
 147       * @throws \RuntimeException When the requested database driver is not installed
 148       */
 149      abstract protected function initDb();
 150  
 151      protected function cleanup()
 152      {
 153          $db = $this->initDb();
 154          $this->exec($db, 'DELETE FROM sf_profiler_data WHERE created_at < :time', array(':time' => time() - $this->lifetime));
 155          $this->close($db);
 156      }
 157  
 158      protected function exec($db, $query, array $args = array())
 159      {
 160          $stmt = $this->prepareStatement($db, $query);
 161  
 162          foreach ($args as $arg => $val) {
 163              $stmt->bindValue($arg, $val, \is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
 164          }
 165          $success = $stmt->execute();
 166          if (!$success) {
 167              throw new \RuntimeException(sprintf('Error executing query "%s"', $query));
 168          }
 169      }
 170  
 171      protected function prepareStatement($db, $query)
 172      {
 173          try {
 174              $stmt = $db->prepare($query);
 175          } catch (\Exception $e) {
 176              $stmt = false;
 177          }
 178  
 179          if (false === $stmt) {
 180              throw new \RuntimeException('The database cannot successfully prepare the statement');
 181          }
 182  
 183          return $stmt;
 184      }
 185  
 186      protected function fetch($db, $query, array $args = array())
 187      {
 188          $stmt = $this->prepareStatement($db, $query);
 189  
 190          foreach ($args as $arg => $val) {
 191              $stmt->bindValue($arg, $val, \is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
 192          }
 193          $stmt->execute();
 194  
 195          return $stmt->fetchAll(\PDO::FETCH_ASSOC);
 196      }
 197  
 198      protected function close($db)
 199      {
 200      }
 201  
 202      protected function createProfileFromData($token, $data, $parent = null)
 203      {
 204          $profile = new Profile($token);
 205          $profile->setIp($data['ip']);
 206          $profile->setMethod($data['method']);
 207          $profile->setUrl($data['url']);
 208          $profile->setTime($data['time']);
 209          $profile->setCollectors(unserialize(base64_decode($data['data'])));
 210  
 211          if (!$parent && !empty($data['parent'])) {
 212              $parent = $this->read($data['parent']);
 213          }
 214  
 215          if ($parent) {
 216              $profile->setParent($parent);
 217          }
 218  
 219          $profile->setChildren($this->readChildren($token, $profile));
 220  
 221          return $profile;
 222      }
 223  
 224      /**
 225       * Reads the child profiles for the given token.
 226       *
 227       * @param string $token  The parent token
 228       * @param string $parent The parent instance
 229       *
 230       * @return Profile[] An array of Profile instance
 231       */
 232      protected function readChildren($token, $parent)
 233      {
 234          $db = $this->initDb();
 235          $data = $this->fetch($db, 'SELECT token, data, ip, method, url, time FROM sf_profiler_data WHERE parent = :token', array(':token' => $token));
 236          $this->close($db);
 237  
 238          if (!$data) {
 239              return array();
 240          }
 241  
 242          $profiles = array();
 243          foreach ($data as $d) {
 244              $profiles[] = $this->createProfileFromData($d['token'], $d, $parent);
 245          }
 246  
 247          return $profiles;
 248      }
 249  
 250      /**
 251       * Returns whether data for the given token already exists in storage.
 252       *
 253       * @param string $token The profile token
 254       *
 255       * @return string
 256       */
 257      protected function has($token)
 258      {
 259          $db = $this->initDb();
 260          $tokenExists = $this->fetch($db, 'SELECT 1 FROM sf_profiler_data WHERE token = :token LIMIT 1', array(':token' => $token));
 261          $this->close($db);
 262  
 263          return !empty($tokenExists);
 264      }
 265  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1