SQLiteProfilerStorage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Symfony\Component\HttpKernel\Profiler;
  3. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * SQLiteProfilerStorage stores profiling information in a SQLite database.
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class SQLiteProfilerStorage implements ProfilerStorageInterface
  18. {
  19. protected $store;
  20. protected $lifetime;
  21. /**
  22. * Constructor.
  23. *
  24. * @param string $store The path to the SQLite DB
  25. * @param integer $lifetime The lifetime to use for the purge
  26. */
  27. public function __construct($store, $lifetime = 86400)
  28. {
  29. $this->store = $store;
  30. $this->lifetime = (int) $lifetime;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function find($ip, $url, $limit)
  36. {
  37. $criteria = array();
  38. $args = array();
  39. if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
  40. $criteria[] = 'ip LIKE :ip';
  41. $args[':ip'] = '%'.$ip.'%';
  42. }
  43. if ($url) {
  44. $criteria[] = 'url LIKE :url ESCAPE "\"';
  45. $args[':url'] = '%'.addcslashes($url, '%_').'%';
  46. }
  47. $criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
  48. $db = $this->initDb();
  49. $tokens = $this->fetch($db, 'SELECT token, ip, url, time FROM data '.$criteria.' ORDER BY time DESC LIMIT '.((integer) $limit), $args);
  50. $this->close($db);
  51. return $tokens;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function read($token)
  57. {
  58. $db = $this->initDb();
  59. $args = array(':token' => $token);
  60. $data = $this->fetch($db, 'SELECT data, ip, url, time FROM data WHERE token = :token ORDER BY time DESC LIMIT 1', $args);
  61. $this->close($db);
  62. if (isset($data[0]['data'])) {
  63. return array($data[0]['data'], $data[0]['ip'], $data[0]['url'], $data[0]['time']);
  64. } else {
  65. return false;
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function write($token, $data, $ip, $url, $time)
  72. {
  73. $db = $this->initDb();
  74. $args = array(
  75. ':token' => $token,
  76. ':data' => $data,
  77. ':ip' => $ip,
  78. ':url' => $url,
  79. ':time' => $time,
  80. );
  81. $this->exec($db, 'INSERT INTO data (token, data, ip, url, time) VALUES (:token, :data, :ip, :url, :time)', $args);
  82. $this->cleanup();
  83. $this->close($db);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function purge()
  89. {
  90. $db = $this->initDb();
  91. $this->exec($db, 'DELETE FROM data');
  92. $this->close($db);
  93. }
  94. protected function cleanup()
  95. {
  96. $db = $this->initDb();
  97. $this->exec($db, 'DELETE FROM data WHERE time < :time', array(':time' => time() - $this->lifetime));
  98. $this->close($db);
  99. }
  100. /**
  101. * @throws \RuntimeException When neither of SQLite or PDO_SQLite extension is enabled
  102. */
  103. protected function initDb()
  104. {
  105. if (class_exists('SQLite3')) {
  106. $db = new \SQLite3($this->store, \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
  107. } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
  108. $db = new \PDO('sqlite:'.$this->store);
  109. } else {
  110. throw new \RuntimeException('You need to enable either the SQLite or PDO_SQLite extension for the profiler to run properly.');
  111. }
  112. $db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER)');
  113. $db->exec('CREATE INDEX IF NOT EXISTS data_data ON data (time)');
  114. $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON data (token)');
  115. return $db;
  116. }
  117. protected function exec($db, $query, array $args = array())
  118. {
  119. $stmt = $db->prepare($query);
  120. if ($db instanceof \SQLite3) {
  121. foreach ($args as $arg => $val) {
  122. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  123. }
  124. $res = $stmt->execute();
  125. $res->finalize();
  126. } else {
  127. foreach ($args as $arg => $val) {
  128. $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  129. }
  130. $stmt->execute();
  131. }
  132. }
  133. protected function fetch($db, $query, array $args = array())
  134. {
  135. $return = array();
  136. $stmt = $db->prepare($query);
  137. if ($db instanceof \SQLite3) {
  138. foreach ($args as $arg => $val) {
  139. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  140. }
  141. $res = $stmt->execute();
  142. while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
  143. $return[] = $row;
  144. }
  145. $res->finalize();
  146. $stmt->close();
  147. } else {
  148. foreach ($args as $arg => $val) {
  149. $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  150. }
  151. $stmt->execute();
  152. $return = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  153. }
  154. return $return;
  155. }
  156. protected function close($db)
  157. {
  158. if ($db instanceof \SQLite3) {
  159. $db->close();
  160. }
  161. }
  162. }