Skip to content

Instantly share code, notes, and snippets.

@ArnaudLigny
Created June 19, 2013 12:02
Show Gist options
  • Save ArnaudLigny/5813764 to your computer and use it in GitHub Desktop.
Save ArnaudLigny/5813764 to your computer and use it in GitHub Desktop.
Override Magento to handle PHP 5.3+ namespaces.
<?php
/**
* Retrieve class name by class group
*
* @param string $groupType currently supported model, block, helper
* @param string $classId slash separated class identifier, ex. group/class
* @param string $groupRootNode optional config path for group config
* @return string
*
* @override Override to handle PHP namespaces
*/
public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
{
if (empty($groupRootNode)) {
$groupRootNode = 'global/'.$groupType.'s';
}
$classArr = explode('/', trim($classId));
$group = $classArr[0];
$class = !empty($classArr[1]) ? $classArr[1] : null;
if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
return $this->_classNameCache[$groupRootNode][$group][$class];
}
$config = $this->_xml->global->{$groupType.'s'}->{$group};
// First - check maybe the entity class was rewritten
$className = null;
if (isset($config->rewrite->$class)) {
$className = (string)$config->rewrite->$class;
} else {
/**
* Backwards compatibility for pre-MMDB extensions.
* In MMDB release resource nodes <..._mysql4> were renamed to <..._resource>. So <deprecatedNode> is left
* to keep name of previously used nodes, that still may be used by non-updated extensions.
*/
if ($config->deprecatedNode) {
$deprecatedNode = $config->deprecatedNode;
$configOld = $this->_xml->global->{$groupType.'s'}->$deprecatedNode;
if (isset($configOld->rewrite->$class)) {
$className = (string) $configOld->rewrite->$class;
}
}
}
// Second - if entity is not rewritten then use class prefix to form class name
if (empty($className)) {
if (!empty($config)) {
$className = $config->getClassName();
}
if (empty($className)) {
$className = 'mage_'.$group.'_'.$groupType;
}
if (!empty($class)) {
if(strpos($className, '\\') !== false) {
$className .= '\\'.uc_words($class, '\\', '_');
} else {
$className .= '_'.$class;
}
}
$className = uc_words($className);
}
$this->_classNameCache[$groupRootNode][$group][$class] = $className;
return $className;
}
<?php
/**
* Create block object instance based on block type
*
* @param string $block
* @param array $attributes
* @return Mage_Core_Block_Abstract
*
* @override Override to handle PHP namespaces
*/
protected function _getBlockInstance($block, array $attributes=array())
{
if (is_string($block)) {
if (strpos($block, '/')!==false) {
if (!$block = Mage::getConfig()->getBlockClassName($block)) {
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
}
}
if (class_exists($block, false) || mageFindClassFile(str_replace('\\', DIRECTORY_SEPARATOR, $block))) {
$block = new $block($attributes);
}
}
if (!$block instanceof Mage_Core_Block_Abstract) {
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
}
return $block;
}
<?php
/**
* Load class source code
*
* @param string $class
*
* @override Override to handle PHP namespaces
*/
public function autoload($class)
{
if ($this->_collectClasses) {
$this->_arrLoadedClasses[self::$_scope][] = $class;
}
if ($this->_isIncludePathDefined) {
$classFile = $class;
} else if (strpos($class, "\\") !== false) {
$classFile = str_replace("\\", DIRECTORY_SEPARATOR, $class);
} else {
$classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
}
$classFile.= '.php';
return include_once $classFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment