| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Components; |
| 4 |
|
| 5 |
use Photonic_Plugin\Core\Photonic; |
| 6 |
use Photonic_Plugin\Layouts\Core_Layout; |
| 7 |
use Photonic_Plugin\Platforms\Base; |
| 8 |
|
| 9 |
class Stack_Trace implements Printable { |
| 10 |
public $events = []; |
| 11 |
|
| 12 |
public function add_to_first_open_event($new_event) { |
| 13 |
$found = false; |
| 14 |
foreach ($this->events as $id => $event) { |
| 15 |
if (isset($event['start']) && !isset($event['end'])) { |
| 16 |
// Ongoing event. Need to add to this. |
| 17 |
$found = true; |
| 18 |
if (!isset($event['children'])) { |
| 19 |
$children = new Stack_Trace(); |
| 20 |
} |
| 21 |
else { |
| 22 |
$children = $event['children']; |
| 23 |
} |
| 24 |
$children->add_to_first_open_event($new_event); |
| 25 |
$event['children'] = $children; |
| 26 |
$this->events[$id] = $event; |
| 27 |
} |
| 28 |
if ($found) { |
| 29 |
break; |
| 30 |
} |
| 31 |
} |
| 32 |
if (!$found) { |
| 33 |
$this->events[] = [ |
| 34 |
'event' => $new_event, |
| 35 |
'start' => microtime(true), |
| 36 |
]; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function pop_from_first_open_event(): bool { |
| 41 |
$found = false; |
| 42 |
foreach ($this->events as $id => $event) { |
| 43 |
if (isset($event['start']) && !isset($event['end'])) { |
| 44 |
// Ongoing event. Need to pop this or its open child |
| 45 |
$found = true; |
| 46 |
$found_child = false; |
| 47 |
if (isset($event['children'])) { |
| 48 |
/** @var Stack_Trace $children */ |
| 49 |
$children = $event['children']; |
| 50 |
$found_child = $children->pop_from_first_open_event(); |
| 51 |
$event['children'] = $children; |
| 52 |
} |
| 53 |
if (!$found_child) { |
| 54 |
$event['end'] = microtime(true); |
| 55 |
$event['time'] = $event['end'] - $event['start']; |
| 56 |
} |
| 57 |
$this->events[$id] = $event; |
| 58 |
} |
| 59 |
if ($found) { |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
return $found; |
| 64 |
} |
| 65 |
|
| 66 |
private function get_nested_element($indent = "\t"): string { |
| 67 |
$ret = ''; |
| 68 |
foreach ($this->events as $trace) { |
| 69 |
$trace_items = []; |
| 70 |
foreach ($trace as $key => $trace_item) { |
| 71 |
if ('children' !== $key) { |
| 72 |
$trace_items[] = strtoupper(substr($key, 0, 1)) . substr($key, 1) . ': ' . $trace_item; |
| 73 |
} |
| 74 |
} |
| 75 |
$ret .= $indent . implode(', ', $trace_items) . "\n"; |
| 76 |
if (!empty($trace['children'])) { |
| 77 |
/** @var Stack_Trace $children */ |
| 78 |
$children = $trace['children']; |
| 79 |
$ret .= $children->get_nested_element($indent . "\t"); |
| 80 |
} |
| 81 |
} |
| 82 |
return $ret; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
public function html(Base $module, ?Core_Layout $layout = null, $print = false): string { |
| 87 |
$ret = "<!--\n"; |
| 88 |
$ret .= "Stats for Platform: {$module->provider}, Gallery: {$module->gallery_index}, Library: {$layout->get_library()}\n"; |
| 89 |
$ret .= $this->get_nested_element(); |
| 90 |
$ret .= "-->\n"; |
| 91 |
|
| 92 |
if ($print) { |
| 93 |
echo wp_kses($ret, Photonic::$safe_tags); |
| 94 |
} |
| 95 |
|
| 96 |
return wp_kses($ret, Photonic::$safe_tags); |
| 97 |
} |
| 98 |
} |
| 99 |
|