TimelineDataProvider.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\components;
  8. use yii\data\ArrayDataProvider;
  9. use yii\debug\panels\TimelinePanel;
  10. /**
  11. * TimelineDataProvider implements a data provider based on a data array.
  12. *
  13. * @property array $rulers This property is read-only.
  14. *
  15. * @author Dmitriy Bashkarev <dmitriy@bashkarev.com>
  16. * @since 2.0.7
  17. */
  18. class TimelineDataProvider extends ArrayDataProvider
  19. {
  20. /**
  21. * @var TimelinePanel
  22. */
  23. protected $panel;
  24. /**
  25. * TimelineDataProvider constructor.
  26. * @param TimelinePanel $panel
  27. * @param array $config
  28. */
  29. public function __construct(TimelinePanel $panel, $config = [])
  30. {
  31. $this->panel = $panel;
  32. parent::__construct($config);
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function prepareModels()
  38. {
  39. if (($models = $this->allModels) === null) {
  40. return [];
  41. }
  42. $child = [];
  43. foreach ($models as $key => &$model) {
  44. $model['timestamp'] *= 1000;
  45. $model['duration'] *= 1000;
  46. $model['child'] = 0;
  47. $model['css']['width'] = $this->getWidth($model);
  48. $model['css']['left'] = $this->getLeft($model);
  49. $model['css']['color'] = $this->getColor($model);
  50. foreach ($child as $id => $timestamp) {
  51. if ($timestamp > $model['timestamp']) {
  52. ++$models[$id]['child'];
  53. } else {
  54. unset($child[$id]);
  55. }
  56. }
  57. $child[$key] = $model['timestamp'] + $model['duration'];
  58. }
  59. return $models;
  60. }
  61. /**
  62. * Getting HEX color based on model duration
  63. * @param array $model
  64. * @return string
  65. */
  66. public function getColor($model)
  67. {
  68. $width = isset($model['css']['width']) ? $model['css']['width'] : $this->getWidth($model);
  69. foreach ($this->panel->colors as $percent => $color) {
  70. if ($width >= $percent) {
  71. return $color;
  72. }
  73. }
  74. return '#d6e685';
  75. }
  76. /**
  77. * Returns the offset left item, percentage of the total width
  78. * @param array $model
  79. * @return float
  80. */
  81. public function getLeft($model)
  82. {
  83. return $this->getTime($model) / ($this->panel->duration / 100);
  84. }
  85. /**
  86. * Returns item duration, milliseconds
  87. * @param array $model
  88. * @return float
  89. */
  90. public function getTime($model)
  91. {
  92. return $model['timestamp'] - $this->panel->start;
  93. }
  94. /**
  95. * Returns item width percent of the total width
  96. * @param array $model
  97. * @return float
  98. */
  99. public function getWidth($model)
  100. {
  101. return $model['duration'] / ($this->panel->duration / 100);
  102. }
  103. /**
  104. * Returns item, css class
  105. * @param array $model
  106. * @return string
  107. */
  108. public function getCssClass($model)
  109. {
  110. $class = 'time';
  111. $class .= (($model['css']['left'] > 15) && ($model['css']['left'] + $model['css']['width'] > 50)) ? ' right' : ' left';
  112. return $class;
  113. }
  114. /**
  115. * ruler items, key milliseconds, value offset left
  116. * @param int $line number of columns
  117. * @return array
  118. */
  119. public function getRulers($line = 10)
  120. {
  121. if ($line == 0) {
  122. return [];
  123. }
  124. $data = [0];
  125. $percent = ($this->panel->duration / 100);
  126. $row = $this->panel->duration / $line;
  127. $precision = $row > 100 ? -2 : -1;
  128. for ($i = 1; $i < $line; $i++) {
  129. $ms = round($i * $row, $precision);
  130. $data[$ms] = $ms / $percent;
  131. }
  132. return $data;
  133. }
  134. }