[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/Storage/ -> PdoStorage.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\Storage;
  12  
  13  use PDO;
  14  
  15  /**
  16   * Stores collected data into a database using PDO
  17   */
  18  class PdoStorage implements StorageInterface
  19  {
  20      protected $pdo;
  21  
  22      protected $tableName;
  23  
  24      protected $sqlQueries = array(
  25          'save' => "INSERT INTO %tablename% (id, data, meta_utime, meta_datetime, meta_uri, meta_ip, meta_method) VALUES (?, ?, ?, ?, ?, ?, ?)",
  26          'get' => "SELECT data FROM %tablename% WHERE id = ?",
  27          'find' => "SELECT data FROM %tablename% %where% ORDER BY meta_datetime DESC LIMIT %limit% OFFSET %offset%",
  28          'clear' => "DELETE FROM %tablename%"
  29      );
  30  
  31      /**
  32       * @param \PDO $pdo The PDO instance
  33       * @param string $tableName
  34       * @param array $sqlQueries
  35       */
  36      public function __construct(PDO $pdo, $tableName = 'phpdebugbar', array $sqlQueries = array())
  37      {
  38          $this->pdo = $pdo;
  39          $this->tableName = $tableName;
  40          $this->setSqlQueries($sqlQueries);
  41      }
  42  
  43      /**
  44       * Sets the sql queries to be used
  45       *
  46       * @param array $queries
  47       */
  48      public function setSqlQueries(array $queries)
  49      {
  50          $this->sqlQueries = array_merge($this->sqlQueries, $queries);
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      public function save($id, $data)
  57      {
  58          $sql = $this->getSqlQuery('save');
  59          $stmt = $this->pdo->prepare($sql);
  60          $meta = $data['__meta'];
  61          $stmt->execute(array($id, serialize($data), $meta['utime'], $meta['datetime'], $meta['uri'], $meta['ip'], $meta['method']));
  62      }
  63  
  64      /**
  65       * {@inheritdoc}
  66       */
  67      public function get($id)
  68      {
  69          $sql = $this->getSqlQuery('get');
  70          $stmt = $this->pdo->prepare($sql);
  71          $stmt->execute(array($id));
  72          if (($data = $stmt->fetchColumn(0)) !== false) {
  73              return unserialize($data);
  74          }
  75          return null;
  76      }
  77  
  78      /**
  79       * {@inheritdoc}
  80       */
  81      public function find(array $filters = array(), $max = 20, $offset = 0)
  82      {
  83          $where = array();
  84          $params = array();
  85          foreach ($filters as $key => $value) {
  86              $where[] = "meta_$key = ?";
  87              $params[] = $value;
  88          }
  89          if (count($where)) {
  90              $where = " WHERE " . implode(' AND ', $where);
  91          } else {
  92              $where = '';
  93          }
  94  
  95          $sql = $this->getSqlQuery('find', array(
  96              'where' => $where,
  97              'offset' => $offset,
  98              'limit' => $max
  99          ));
 100  
 101          $stmt = $this->pdo->prepare($sql);
 102          $stmt->execute($params);
 103  
 104          $results = array();
 105          foreach ($stmt->fetchAll() as $row) {
 106              $data = unserialize($row['data']);
 107              $results[] = $data['__meta'];
 108              unset($data);
 109          }
 110          return $results;
 111      }
 112  
 113      /**
 114       * {@inheritdoc}
 115       */
 116      public function clear()
 117      {
 118          $this->pdo->exec($this->getSqlQuery('clear'));
 119      }
 120  
 121      /**
 122       * Get a SQL Query for a task, with the variables replaced
 123       *
 124       * @param  string $name
 125       * @param  array  $vars
 126       * @return string
 127       */
 128      protected function getSqlQuery($name, array $vars = array())
 129      {
 130          $sql = $this->sqlQueries[$name];
 131          $vars = array_merge(array('tablename' => $this->tableName), $vars);
 132          foreach ($vars as $k => $v) {
 133              $sql = str_replace("%$k%", $v, $sql);
 134          }
 135          return $sql;
 136      }
 137  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer