DrupalDataCollectorTrait.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Drupal\webprofiler\DataCollector;
  3. /**
  4. * Class DrupalDataCollectorTrait
  5. */
  6. trait DrupalDataCollectorTrait {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public function reset() {
  11. $this->data = array();
  12. }
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function getPanelSummary() {
  17. return NULL;
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function hasPanel() {
  23. return TRUE;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getLibraries() {
  29. return [];
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getDrupalSettings() {
  35. return [];
  36. }
  37. /**
  38. * @return mixed
  39. */
  40. public function getData() {
  41. return $this->data;
  42. }
  43. /**
  44. * @param $class
  45. * @param $method
  46. *
  47. * @return array
  48. */
  49. public function getMethodData($class, $method) {
  50. $class = is_object($class) ? get_class($class) : $class;
  51. $data = [];
  52. try {
  53. $reflectedMethod = new \ReflectionMethod($class, $method);
  54. $data = [
  55. 'class' => $class,
  56. 'method' => $method,
  57. 'file' => $reflectedMethod->getFilename(),
  58. 'line' => $reflectedMethod->getStartLine(),
  59. ];
  60. } catch (\ReflectionException $re) {
  61. // TODO: handle the exception.
  62. } finally {
  63. return $data;
  64. }
  65. }
  66. /**
  67. * @param $value
  68. *
  69. * @return int|string
  70. */
  71. private function convertToBytes($value) {
  72. if ('-1' === $value) {
  73. return -1;
  74. }
  75. $value = strtolower($value);
  76. $max = strtolower(ltrim($value, '+'));
  77. if (0 === strpos($max, '0x')) {
  78. $max = intval($max, 16);
  79. }
  80. elseif (0 === strpos($max, '0')) {
  81. $max = intval($max, 8);
  82. }
  83. else {
  84. $max = intval($max);
  85. }
  86. switch (substr($value, -1)) {
  87. case 't':
  88. $max *= 1024;
  89. break;
  90. case 'g':
  91. $max *= 1024;
  92. break;
  93. case 'm':
  94. $max *= 1024;
  95. break;
  96. case 'k':
  97. $max *= 1024;
  98. break;
  99. }
  100. return $max;
  101. }
  102. }