Log.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Profiler;
  7. use Magento\Framework\ObjectManager\Profiler\Tree\Item as Item;
  8. /**
  9. * Class Log
  10. */
  11. class Log
  12. {
  13. /**
  14. * @var self
  15. */
  16. protected static $instance;
  17. /**
  18. * @var Item
  19. */
  20. protected $currentItem = null;
  21. /**
  22. * @var array
  23. */
  24. protected $data = [];
  25. /**
  26. * @var array
  27. */
  28. protected $roots = [];
  29. /**
  30. * @var array
  31. */
  32. protected $used = [];
  33. /**
  34. * @var array
  35. */
  36. protected $stats = ['total' => 0, 'used' => 0, 'unused' => 0];
  37. /**
  38. * Constructor
  39. */
  40. public function __construct()
  41. {
  42. register_shutdown_function([$this, 'display']);
  43. }
  44. /**
  45. * Retrieve instance
  46. *
  47. * @return Log
  48. */
  49. public static function getInstance()
  50. {
  51. if (!self::$instance) {
  52. self::$instance = new Log();
  53. }
  54. return self::$instance;
  55. }
  56. /**
  57. * Log object instantiation
  58. *
  59. * @param string $class
  60. * @return void
  61. */
  62. public function startCreating($class)
  63. {
  64. $this->stats['total']++;
  65. $parent = empty($this->currentItem) ? null : $this->currentItem;
  66. $item = new Item($class, $parent);
  67. if (!$parent) {
  68. $this->roots[] = $item;
  69. }
  70. $this->currentItem = $item;
  71. }
  72. /**
  73. * Log object instantiation
  74. *
  75. * @param mixed $object
  76. * @return void
  77. */
  78. public function stopCreating($object)
  79. {
  80. $this->currentItem->setHash(spl_object_hash($object));
  81. $this->currentItem = $this->currentItem->getParent();
  82. }
  83. /**
  84. * Monitor object
  85. *
  86. * @param mixed $object
  87. * @return void
  88. */
  89. public function add($object)
  90. {
  91. $this->stats['total']++;
  92. if ($this->currentItem) {
  93. $item = new Item(get_class($object), $this->currentItem);
  94. $this->currentItem->addChild($item);
  95. } else {
  96. $item = new Item(get_class($object), null);
  97. $this->roots[] = $item;
  98. }
  99. $item->setHash(spl_object_hash($object));
  100. }
  101. /**
  102. * Object was invoked
  103. *
  104. * @param mixed $object
  105. * @return void
  106. */
  107. public function invoked($object)
  108. {
  109. $this->used[spl_object_hash($object)] = 1;
  110. }
  111. /**
  112. * Display results
  113. *
  114. * @return void
  115. */
  116. public function display()
  117. {
  118. $this->stats['used'] = count($this->used);
  119. $this->stats['unused'] = $this->stats['total'] - $this->stats['used'];
  120. echo '<table border="1" cellspacing="0" cellpadding="2">',
  121. '<thead><tr><th>',
  122. "Creation chain (Red items are never used) Total: {$this->stats['total']}\n",
  123. "Used: {$this->stats['used']} Not used: {$this->stats['unused']}",
  124. '</th></tr></thead>',
  125. '<tbody>',
  126. '<tr><th>Instance class</th></tr>';
  127. foreach ($this->roots as $root) {
  128. $this->displayItem($root);
  129. }
  130. echo '</tbody></table>';
  131. }
  132. /**
  133. * Display item
  134. *
  135. * @param Item $item
  136. * @param int $level
  137. * @return void
  138. */
  139. protected function displayItem(Item $item, $level = 0)
  140. {
  141. $colorStyle = isset($this->used[$item->getHash()]) ? '' : ' style="color:red" ';
  142. echo "<tr><td $colorStyle>" . str_repeat('·&nbsp;', $level) . $item->getClass()
  143. . ' - ' . $item->getHash() . '</td></tr>';
  144. foreach ($item->getChildren() as $child) {
  145. $this->displayItem($child, $level + 1);
  146. }
  147. }
  148. }