Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Log.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class Log
12 {
16  protected static $instance;
17 
21  protected $currentItem = null;
22 
26  protected $data = [];
27 
31  protected $roots = [];
32 
36  protected $used = [];
37 
41  protected $stats = ['total' => 0, 'used' => 0, 'unused' => 0];
42 
46  public function __construct()
47  {
48  register_shutdown_function([$this, 'display']);
49  }
50 
56  public static function getInstance()
57  {
58  if (!self::$instance) {
59  self::$instance = new Log();
60  }
61  return self::$instance;
62  }
63 
70  public function startCreating($class)
71  {
72  $this->stats['total']++;
73  $parent = empty($this->currentItem) ? null : $this->currentItem;
74  $item = new Item($class, $parent);
75 
76  if (!$parent) {
77  $this->roots[] = $item;
78  }
79 
80  $this->currentItem = $item;
81  }
82 
89  public function stopCreating($object)
90  {
91  $this->currentItem->setHash(spl_object_hash($object));
92  $this->currentItem = $this->currentItem->getParent();
93  }
94 
101  public function add($object)
102  {
103  $this->stats['total']++;
104  if ($this->currentItem) {
105  $item = new Item(get_class($object), $this->currentItem);
106  $this->currentItem->addChild($item);
107  } else {
108  $item = new Item(get_class($object), null);
109  $this->roots[] = $item;
110  }
111  $item->setHash(spl_object_hash($object));
112  }
113 
120  public function invoked($object)
121  {
122  $this->used[spl_object_hash($object)] = 1;
123  }
124 
130  public function display()
131  {
132  $this->stats['used'] = count($this->used);
133  $this->stats['unused'] = $this->stats['total'] - $this->stats['used'];
134  echo '<table border="1" cellspacing="0" cellpadding="2">',
135  '<thead><tr><th>',
136  "Creation chain (Red items are never used) Total: {$this->stats['total']}\n",
137  "Used: {$this->stats['used']} Not used: {$this->stats['unused']}",
138  '</th></tr></thead>',
139  '<tbody>',
140  '<tr><th>Instance class</th></tr>';
141  foreach ($this->roots as $root) {
142  $this->displayItem($root);
143  }
144  echo '</tbody></table>';
145  }
146 
154  protected function displayItem(Item $item, $level = 0)
155  {
156  $colorStyle = isset($this->used[$item->getHash()]) ? '' : ' style="color:red" ';
157 
158  echo "<tr><td $colorStyle>" . str_repeat('ยท&nbsp;', $level) . $item->getClass()
159  . ' - ' . $item->getHash() . '</td></tr>';
160 
161  foreach ($item->getChildren() as $child) {
162  $this->displayItem($child, $level + 1);
163  }
164  }
165 }
displayItem(Item $item, $level=0)
Definition: Log.php:154
$_option $_optionId $class
Definition: date.phtml:13