SqliteProfilerStorage.php 4.7 KB

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