123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace yii\debug\components;
- use yii\data\ArrayDataProvider;
- use yii\debug\panels\TimelinePanel;
- /**
- * TimelineDataProvider implements a data provider based on a data array.
- *
- * @property array $rulers This property is read-only.
- *
- * @author Dmitriy Bashkarev <dmitriy@bashkarev.com>
- * @since 2.0.7
- */
- class TimelineDataProvider extends ArrayDataProvider
- {
- /**
- * @var TimelinePanel
- */
- protected $panel;
- /**
- * TimelineDataProvider constructor.
- * @param TimelinePanel $panel
- * @param array $config
- */
- public function __construct(TimelinePanel $panel, $config = [])
- {
- $this->panel = $panel;
- parent::__construct($config);
- }
- /**
- * @inheritdoc
- */
- protected function prepareModels()
- {
- if (($models = $this->allModels) === null) {
- return [];
- }
- $child = [];
- foreach ($models as $key => &$model) {
- $model['timestamp'] *= 1000;
- $model['duration'] *= 1000;
- $model['child'] = 0;
- $model['css']['width'] = $this->getWidth($model);
- $model['css']['left'] = $this->getLeft($model);
- $model['css']['color'] = $this->getColor($model);
- foreach ($child as $id => $timestamp) {
- if ($timestamp > $model['timestamp']) {
- ++$models[$id]['child'];
- } else {
- unset($child[$id]);
- }
- }
- $child[$key] = $model['timestamp'] + $model['duration'];
- }
- return $models;
- }
- /**
- * Getting HEX color based on model duration
- * @param array $model
- * @return string
- */
- public function getColor($model)
- {
- $width = isset($model['css']['width']) ? $model['css']['width'] : $this->getWidth($model);
- foreach ($this->panel->colors as $percent => $color) {
- if ($width >= $percent) {
- return $color;
- }
- }
- return '#d6e685';
- }
- /**
- * Returns the offset left item, percentage of the total width
- * @param array $model
- * @return float
- */
- public function getLeft($model)
- {
- return $this->getTime($model) / ($this->panel->duration / 100);
- }
- /**
- * Returns item duration, milliseconds
- * @param array $model
- * @return float
- */
- public function getTime($model)
- {
- return $model['timestamp'] - $this->panel->start;
- }
- /**
- * Returns item width percent of the total width
- * @param array $model
- * @return float
- */
- public function getWidth($model)
- {
- return $model['duration'] / ($this->panel->duration / 100);
- }
- /**
- * Returns item, css class
- * @param array $model
- * @return string
- */
- public function getCssClass($model)
- {
- $class = 'time';
- $class .= (($model['css']['left'] > 15) && ($model['css']['left'] + $model['css']['width'] > 50)) ? ' right' : ' left';
- return $class;
- }
- /**
- * ruler items, key milliseconds, value offset left
- * @param int $line number of columns
- * @return array
- */
- public function getRulers($line = 10)
- {
- if ($line == 0) {
- return [];
- }
- $data = [0];
- $percent = ($this->panel->duration / 100);
- $row = $this->panel->duration / $line;
- $precision = $row > 100 ? -2 : -1;
- for ($i = 1; $i < $line; $i++) {
- $ms = round($i * $row, $precision);
- $data[$ms] = $ms / $percent;
- }
- return $data;
- }
- }
|