SQLiteProfilerStorage.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Profiler;
  11. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  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 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. ':created_at' => time(),
  81. );
  82. $this->exec($db, 'INSERT INTO data (token, data, ip, url, time, created_at) VALUES (:token, :data, :ip, :url, :time, :created_at)', $args);
  83. $this->cleanup();
  84. $this->close($db);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function purge()
  90. {
  91. $db = $this->initDb();
  92. $this->exec($db, 'DELETE FROM data');
  93. $this->close($db);
  94. }
  95. protected function cleanup()
  96. {
  97. $db = $this->initDb();
  98. $this->exec($db, 'DELETE FROM data WHERE created_at < :time', array(':time' => time() - $this->lifetime));
  99. $this->close($db);
  100. }
  101. /**
  102. * @throws \RuntimeException When neither of SQLite or PDO_SQLite extension is enabled
  103. */
  104. protected function initDb()
  105. {
  106. if (class_exists('SQLite3')) {
  107. $db = new \SQLite3($this->store, \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
  108. } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
  109. $db = new \PDO('sqlite:'.$this->store);
  110. } else {
  111. throw new \RuntimeException('You need to enable either the SQLite or PDO_SQLite extension for the profiler to run properly.');
  112. }
  113. $db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER, created_at INTEGER)');
  114. $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON data (created_at)');
  115. $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON data (ip)');
  116. $db->exec('CREATE INDEX IF NOT EXISTS data_url ON data (url)');
  117. $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON data (token)');
  118. return $db;
  119. }
  120. protected function exec($db, $query, array $args = array())
  121. {
  122. $stmt = $db->prepare($query);
  123. if ($db instanceof \SQLite3) {
  124. foreach ($args as $arg => $val) {
  125. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  126. }
  127. $res = $stmt->execute();
  128. $res->finalize();
  129. } else {
  130. foreach ($args as $arg => $val) {
  131. $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  132. }
  133. $stmt->execute();
  134. }
  135. }
  136. protected function fetch($db, $query, array $args = array())
  137. {
  138. $return = array();
  139. $stmt = $db->prepare($query);
  140. if ($db instanceof \SQLite3) {
  141. foreach ($args as $arg => $val) {
  142. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  143. }
  144. $res = $stmt->execute();
  145. while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
  146. $return[] = $row;
  147. }
  148. $res->finalize();
  149. $stmt->close();
  150. } else {
  151. foreach ($args as $arg => $val) {
  152. $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  153. }
  154. $stmt->execute();
  155. $return = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  156. }
  157. return $return;
  158. }
  159. protected function close($db)
  160. {
  161. if ($db instanceof \SQLite3) {
  162. $db->close();
  163. }
  164. }
  165. }