QueryCounter.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Elgg\Database;
  3. /**
  4. * Count queries performed
  5. *
  6. * Do not use directly. Use _elgg_db_get_query_counter().
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Database
  12. * @since 1.9.0
  13. */
  14. class QueryCounter {
  15. /**
  16. * @var int
  17. */
  18. protected $initial;
  19. /**
  20. * @var \Elgg\Database
  21. */
  22. protected $db;
  23. /**
  24. * Constructor
  25. *
  26. * @param \Elgg\Database $db Elgg's database
  27. */
  28. public function __construct(\Elgg\Database $db) {
  29. $this->db = $db;
  30. $this->initial = $db->getQueryCount();
  31. }
  32. /**
  33. * Get the number of queries performed since the object was constructed
  34. *
  35. * @return int # of queries
  36. */
  37. public function getDelta() {
  38. return $this->db->getQueryCount() - $this->initial;
  39. }
  40. /**
  41. * Create header X-ElggQueryDelta-* with the delta
  42. *
  43. * @see getDelta()
  44. *
  45. * @param string $key Key to add to HTTP header name
  46. *
  47. * @return void
  48. */
  49. public function setDeltaHeader($key = 'Default') {
  50. $delta = $this->getDelta();
  51. header("X-ElggQueryDelta-$key: $delta", true);
  52. }
  53. /**
  54. * Get SCRIPT element which sends the delta to console.log
  55. *
  56. * @see getDelta()
  57. *
  58. * @param string $key Key to display in console log
  59. *
  60. * @return string markup of SCRIPT element
  61. */
  62. public function getDeltaScript($key = 'Default') {
  63. $delta = $this->getDelta();
  64. $msg = json_encode("ElggQueryDelta-$key: $delta");
  65. return "<script>console.log($msg)</script>";
  66. }
  67. }