public function testSetGetNameInLayout() { // basic setting/getting $this->assertEmpty($this->_block->getNameInLayout()); $name = uniqid('name'); $this->_block->setNameInLayout($name); $this->assertEquals($name, $this->_block->getNameInLayout()); // setting second time, along with the layout $layout = Mage::app()->getLayout(); $layout->createBlock('Mage_Core_Block_Template', $name); $block = $layout->getBlock($name); $this->assertInstanceOf('Mage_Core_Block_Abstract', $block); $block->setNameInLayout($name); $this->assertInstanceOf('Mage_Core_Block_Abstract', $layout->getBlock($name)); }
/** * Records information about new block creation * (non-PHPdoc) * @see Mage_Core_Model_Layout::_generateBlock() */ protected function _generateBlock($node, $parent) { $this->_collectedBlock = null; parent::_generateBlock($node, $parent); if ($this->_collectedBlock !== null) { $target = $this->_collectedBlock->getNameInLayout(); $params = array(); if (isset($node['as'])) { $params['alias'] = (string) $node['as']; } else { $params['alias'] = $target; } if (isset($node['class'])) { $params['type'] = (string) $node['class']; } elseif (isset($node['type'])) { $params['type'] = (string) $node['type']; } $params['class'] = get_class($this->_collectedBlock); $params['is_root'] = isset($node['output']); $this->record(self::ACTION_BLOCK_CREATED, $target, $params); if (isset($node['template'])) { $this->record(self::ACTION_BLOCK_ACTION, $target . '::setTemplate', array('template' => (string) $node['template'])); } } return $this; }
/** * Create placeholder object based on block information * * @param Mage_Core_Block_Abstract $block * @return Enterprise_PageCache_Model_Container_Placeholder */ public function getBlockPlaceholder($block) { $this->_initPlaceholders(); $type = $block->getType(); if (isset($this->_placeholders[$type])) { $placeholderData = false; foreach ($this->_placeholders[$type] as $placeholderInfo) { if (!empty($placeholderInfo['name'])) { if ($placeholderInfo['name'] == $block->getNameInLayout()) { $placeholderData = $placeholderInfo; } } else { $placeholderData = $placeholderInfo; } } if (!$placeholderData) { return false; } $placeholder = $placeholderData['code'] . ' container="' . $placeholderData['container'] . '"' . ' block="' . get_class($block) . '"'; $placeholder .= ' cache_id="' . $block->getCacheKey() . '"'; foreach ($block->getCacheKeyInfo() as $k => $v) { if (is_string($k) && !empty($k)) { $placeholder .= ' ' . $k . '="' . $v . '"'; } } $placeholder = Mage::getModel('enterprise_pagecache/container_placeholder', $placeholder); return $placeholder; } return false; }
/** * @param Mage_Core_Block_Abstract $block */ public function delayPrepareLayout($block, $sortOrder = 0) { if ($this->_delayedLayoutIsBeingProcessed || Mage::registry('m_page_is_being_rendered')) { $block->delayedPrepareLayout(); } else { $this->_delayPrepareLayoutBlocks[$block->getNameInLayout()] = compact('block', 'sortOrder'); } }
/** * Get block information * * @param Mage_Core_Block_Abstract $block * @param bool $fullInfo * @return array */ public function getBlockInfo(Mage_Core_Block_Abstract $block, $fullInfo = true) { $info = array('name' => $block->getNameInLayout(), 'alias' => $block->getBlockAlias()); if (!$fullInfo) { return $info; } $info['class'] = get_class($block); if ($this->getRemoteCallEnabled()) { $fileAndLine = Mage::helper('aoe_templatehints/classInfo')->findFileAndLine($info['class']); if ($fileAndLine) { $url = sprintf($this->getRemoteCallUrlTemplate(), $fileAndLine['file'], $fileAndLine['line']); $info['class'] = sprintf($this->getRemoteCallLinkTemplate(), $url, $info['class']); } } $info['module'] = $block->getModuleName(); if ($block instanceof Mage_Cms_Block_Block) { $info['cms-blockId'] = $block->getBlockId(); } if ($block instanceof Mage_Cms_Block_Page) { $info['cms-pageId'] = $block->getPage()->getIdentifier(); } $templateFile = $block->getTemplateFile(); if ($templateFile) { $info['template'] = $templateFile; if ($this->getRemoteCallEnabled()) { $url = sprintf($this->getRemoteCallUrlTemplate(), Mage::getBaseDir('design') . DS . $templateFile, 0); $info['template'] = sprintf($this->getRemoteCallLinkTemplate(), $url, $templateFile); } } // cache information $info['cache-status'] = self::TYPE_NOTCACHED; $cacheLifeTime = $block->getCacheLifetime(); if (!is_null($cacheLifeTime)) { $info['cache-lifetime'] = intval($cacheLifeTime) == 0 ? 'forever' : intval($cacheLifeTime) . ' sec'; $info['cache-key'] = $block->getCacheKey(); $info['cache-key-info'] = is_array($block->getCacheKeyInfo()) ? implode(', ', $block->getCacheKeyInfo()) : $block->getCacheKeyInfo(); $info['tags'] = implode(',', $block->getCacheTags()); $info['cache-status'] = self::TYPE_CACHED; } elseif ($this->isWithinCachedBlock($block)) { $info['cache-status'] = self::TYPE_IMPLICITLYCACHED; // not cached, but within cached } $info['methods'] = $this->getClassMethods(get_class($block)); return $info; }
/** * @param Mage_Core_Block_Abstract $block */ public function getBlockAlias($block) { if (($parent = $block->getParentBlock()) && $this->startsWith($block->getNameInLayout(), $parent->getNameInLayout() . '.')) { return substr($block->getNameInLayout(), strlen($parent->getNameInLayout() . '.')); } else { return $block->getNameInLayout(); } }
/** * Return a hash of the block layout XML in the current configuration, * this is used to identify a unique rendering of the block as we cache * all ESI requests * * @param Mage_Core_Block_Abstract $block */ public function getLayoutHash(Mage_Core_Block_Abstract $block) { $xml = $block->getLayout()->getNode(); $doc = new DOMDocument(); $doc->loadXML($xml->asXML()); $xpath = new DOMXpath($doc); $nodeList = $xpath->query("//block[@name='" . $block->getNameInLayout() . "']"); return sha1($doc->saveXML($nodeList->item(0))); }
/** * Add a block to registry, create new object if needed * * @param string|Mage_Core_Block_Abstract $block * @param string $name * @param string $parent * @param string $alias * @return Mage_Core_Block_Abstract */ public function addBlock($block, $name = '', $parent = '', $alias = '') { if (empty($name) && $block instanceof Mage_Core_Block_Abstract) { $name = $block->getNameInLayout(); } $name = $this->_createStructuralElement($name, self::TYPE_BLOCK, $name ?: (is_object($block) ? get_class($block) : $block)); if ($parent) { $this->_structure->setAsChild($name, $parent, $alias); } return $this->_createBlock($block, $name); }
/** * Set specified block as an anonymous child to specified container * * The block will be moved to the container from previous parent after all other elements * * @param Mage_Core_Block_Abstract $block * @param string $containerName * @return Mage_Backend_Controller_ActionAbstract */ private function _moveBlockToContainer(Mage_Core_Block_Abstract $block, $containerName) { $this->getLayout()->setChild($containerName, $block->getNameInLayout(), ''); return $this; }
/** * Insert child element into specified position * * By default inserts as first element into children list * * @param Mage_Core_Block_Abstract|string $element * @param string|int|null $siblingName * @param bool $after * @param string $alias * @return Mage_Core_Block_Abstract|bool */ public function insert($element, $siblingName = 0, $after = true, $alias = '') { $layout = $this->getLayout(); if (!$layout) { return false; } if ($element instanceof Mage_Core_Block_Abstract) { $elementName = $element->getNameInLayout(); } else { $elementName = $element; } $layout->setChild($this->_nameInLayout, $elementName, $alias); $layout->reorderChild($this->_nameInLayout, $elementName, $siblingName, $after); return $this; }