MysqlProfilerStorage.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * A ProfilerStorage for Mysql
  13. *
  14. * @author Jan Schumann <js@schumann-it.com>
  15. */
  16. class MysqlProfilerStorage extends PdoProfilerStorage
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function initDb()
  22. {
  23. if (null === $this->db) {
  24. if (0 !== strpos($this->dsn, 'mysql')) {
  25. throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Mysql with an invalid dsn "%s". The expected format is "mysql:dbname=database_name;host=host_name".', $this->dsn));
  26. }
  27. if (!class_exists('PDO') || !in_array('mysql', \PDO::getAvailableDrivers(), true)) {
  28. throw new \RuntimeException('You need to enable PDO_Mysql extension for the profiler to run properly.');
  29. }
  30. $db = new \PDO($this->dsn, $this->username, $this->password);
  31. $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url), KEY (parent))');
  32. $this->db = $db;
  33. }
  34. return $this->db;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
  40. {
  41. $criteria = array();
  42. $args = array();
  43. if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
  44. $criteria[] = 'ip LIKE :ip';
  45. $args[':ip'] = '%'.$ip.'%';
  46. }
  47. if ($url) {
  48. $criteria[] = 'url LIKE :url';
  49. $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
  50. }
  51. if ($method) {
  52. $criteria[] = 'method = :method';
  53. $args[':method'] = $method;
  54. }
  55. if (!empty($start)) {
  56. $criteria[] = 'time >= :start';
  57. $args[':start'] = $start;
  58. }
  59. if (!empty($end)) {
  60. $criteria[] = 'time <= :end';
  61. $args[':end'] = $end;
  62. }
  63. return array($criteria, $args);
  64. }
  65. }