|
| 1 | +<?php |
| 2 | + |
| 3 | +class Meanbee_CacheViewer_Model_Observer extends Mage_Core_Model_Abstract { |
| 4 | + |
| 5 | + const EVENT_BEFORE = 'core_block_abstract_to_html_before'; |
| 6 | + const EVENT_AFTER = 'core_block_abstract_to_html_after'; |
| 7 | + |
| 8 | + /** @var Meanbee_CacheViewer_Helper_Data */ |
| 9 | + protected $_helper; |
| 10 | + |
| 11 | + protected $_dispatch_start_time; |
| 12 | + |
| 13 | + public function _construct() { |
| 14 | + parent::_construct(); |
| 15 | + |
| 16 | + $this->_helper = Mage::helper('cacheviewer'); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Add cache block status |
| 21 | + * @param Varien_Event_Observer $observer |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function addBlockCacheStatuses(Varien_Event_Observer $observer) { |
| 25 | + $event = $observer->getEvent(); |
| 26 | + $block = $event->getBlock(); |
| 27 | + $transportObject = $event->getTransport(); |
| 28 | + |
| 29 | + if (!$this->_helper->isShowCacheStatusOnFrontend()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (in_array(get_class($block), array("Mage_Page_Block_Html", "Mage_Adminhtml_Block_Page"))) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Get cache status |
| 38 | + $cache = Mage::app()->getCache()->test(strtoupper($block->getCacheKey())); |
| 39 | + $lastModified = date(DATE_ATOM, time()); |
| 40 | + $blockName = get_class($block); |
| 41 | + $cacheClass = ""; |
| 42 | + |
| 43 | + |
| 44 | + // If cached, get last modified date and add cached class for css. |
| 45 | + if($cache) { |
| 46 | + $lastModified = date(DATE_ATOM, $cache); |
| 47 | + $cacheClass = " cacheviewer-block-cached"; |
| 48 | + } |
| 49 | + |
| 50 | + // Get transport object passed through event and wrap out hints around it. |
| 51 | + $html = $transportObject->getHtml(); |
| 52 | + $html = <<<HTML |
| 53 | +<div class="cacheviewer-container clearer"> |
| 54 | + <div class="cacheviewer-block{$cacheClass}"> |
| 55 | + <div class="cacheviewer-hints"> |
| 56 | + <span class="cacheviewer-lastmodified">{$lastModified}</span> |
| 57 | + <span class="cacheviewer-name">{$blockName}</span> |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + {$html} |
| 61 | +</div> |
| 62 | +HTML; |
| 63 | + |
| 64 | + $transportObject->setHtml($html); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Start the timer for tracking dispatch time. |
| 71 | + * Observe: controller_action_predispatch |
| 72 | + * |
| 73 | + * @param Varien_Event_Observer $observer |
| 74 | + */ |
| 75 | + public function startTimer(Varien_Event_Observer $observer) { |
| 76 | + $this->_dispatch_start_time = microtime(true); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Stop the timer tracking dispatch time and set the elapsed time in a cookie. |
| 81 | + * |
| 82 | + * @param Varien_Event_Observer $observer |
| 83 | + */ |
| 84 | + public function stopTimer(Varien_Event_Observer $observer) { |
| 85 | + $dispatch_finish_time = microtime(true); |
| 86 | + |
| 87 | + $elapsed_time = $dispatch_finish_time - $this->_dispatch_start_time; |
| 88 | + |
| 89 | + if ($this->_helper->isShowCacheStatusOnFrontend()) { |
| 90 | + Mage::getSingleton('core/cookie')->set("cacheviewer_dispatch_time", sprintf("%.3fs", $elapsed_time), 0, null, null, null, false); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments