Skip to content

Instantly share code, notes, and snippets.

@astorm
Created December 14, 2011 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astorm/1477619 to your computer and use it in GitHub Desktop.
Save astorm/1477619 to your computer and use it in GitHub Desktop.
Old Magento Loader from an Earlier Alpha
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Magento
* @package Magento_Loader
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Global class creator
*
* @param string $class
* @return object
*/
function createObject($class)
{
$parameters = func_get_args();
array_shift($parameters);
$parameters = count($parameters) > 0 ? $parameters : null;
return Magento_Loader::getInstance()->createObject($class, $parameters);
}
/**
* Call static methods of specific class
*
* @param string $class class name
* @param string $method method name
* @return mixed
*/
function callStatic($class, $method)
{
$parameters = func_get_args();
array_shift($parameters);
array_shift($parameters);
$parameters = count($parameters) > 0 ? $parameters : null;
return Magento_Loader::getInstance()->callMethod($class, $method, $parameters);
}
/**
* Classes auto loader
*/
class Magento_Loader
{
/**
* Namespaces separator
*/
const NS_SEPARATOR = '\\';
/**
* Singleton
*
* @var Magento_Loader
*/
protected static $_instance;
/**
* System root directory
*
* @var string
*/
protected $_rootDir;
/**
* Association between class names and files
*
* @var array
*/
protected $_filesMap = array();
/**
* Class overriding map
*
* @var array
*/
protected $_classesMap = array();
/**
* Information about loaded classes
*
* @var array
*/
protected $_loadedClasses;
/**
* Class constructor
*/
public function __construct()
{
spl_autoload_register(array($this, 'autoload'));
$this->_rootDir = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR;
}
/**
* Loader root directory getter
*
* @return string
*/
public function getRootDir()
{
return $this->_rootDir;
}
/**
* Ability to use loader sith singleton implementation
*
* @return Magento_Autoload
*/
public static function getInstance()
{
if (!self::$_instance) {
self::$_instance = new Magento_Loader();
}
return self::$_instance;
}
/**
* Auto load class file
*
* @param string $class class name
*/
public function autoload($class)
{
if (isset($this->_filesMap[$class])) {
$classFile = $this->_rootDir . $this->_filesMap[$class];
} else {
$classFile = str_replace(array('_', self::NS_SEPARATOR), DIRECTORY_SEPARATOR, $class) . '.php';
}
require $classFile;
}
/**
* Add additional include path
*
* @param string|array $path specific path(s) started from system root folder
*/
public function addIncludePath($path)
{
if (is_array($path)) {
foreach ($path as $key => $value) {
$path[$key] = $this->_rootDir . $value;
}
} else {
$path = array($this->_rootDir . $path);
}
$path[] = get_include_path();
set_include_path(implode(PATH_SEPARATOR, $path));
}
/**
* Add classes files declaration to the map. New map will override existing values if such was defined before.
*
* @param array $map
*/
public function addFilesMap(array $map)
{
$this->_filesMap = array_merge($this->_filesMap, $map);
return $this;
}
/**
* Add classes overriding map. New map will override existing values if such was defined before.
*
* @param array $map
*/
public function addClassesMap(array $map)
{
$this->_classesMap = array_merge($this->_classesMap, $map);
return $this;
}
/**
* Create instance of particular class
*
* @param string $class class name
* @param null|array $parameters constructor parameters
* @return object
*/
public function createObject($class, $parameters=null)
{
$class = $this->_getClass($class);
if (null === $parameters) {
$object = new $class;
} else {
/* Switch based implementation can be used for instances with disabled reflections */
$object = new ReflectionClass($this->_getClass($class));
$object = $object->newInstanceArgs($parameters);
}
return $object;
}
/**
* Call class static method
*
* @param string $class class name
* @param string $method method name
* @param null|array $parameters method parameters
* @return mixed
*/
public function callMethod($class, $method, $parameters=null)
{
if (null === $parameters) {
$result = call_user_func(array($this->_getClass($class), $method));
} else {
$result = call_user_func_array(array($this->_getClass($class), $method), $parameters);
}
return $result;
}
/**
* Check override declarations and return class name
*
* @param string $class
* @return string
*/
protected function _getClass($class)
{
return isset($this->_classesMap[$class]) ? $this->_classesMap[$class] : $class;
}
}
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Magento
* @package Magento_Loader
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Global class creator
*
* @param string $class
* @return object
*/
function createObject($class)
{
$parameters = func_get_args();
array_shift($parameters);
$parameters = count($parameters) > 0 ? $parameters : null;
return Magento_Loader::getInstance()->createObject($class, $parameters);
}
/**
* Call static methods of specific class
*
* @param string $class class name
* @param string $method method name
* @return mixed
*/
function callStatic($class, $method)
{
$parameters = func_get_args();
array_shift($parameters);
array_shift($parameters);
$parameters = count($parameters) > 0 ? $parameters : null;
return Magento_Loader::getInstance()->callMethod($class, $method, $parameters);
}
/**
* Classes auto loader
*/
class Magento_Loader
{
/**
* Namespaces separator
*/
const NS_SEPARATOR = '\\';
/**
* Singleton
*
* @var Magento_Loader
*/
protected static $_instance;
/**
* System root directory
*
* @var string
*/
protected $_rootDir;
/**
* Association between class names and files
*
* @var array
*/
protected $_filesMap = array();
/**
* Class overriding map
*
* @var array
*/
protected $_classesMap = array();
/**
* Information about loaded classes
*
* @var array
*/
protected $_loadedClasses;
/**
* Class constructor
*/
public function __construct()
{
spl_autoload_register(array($this, 'autoload'));
$this->_rootDir = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR;
}
/**
* Loader root directory getter
*
* @return string
*/
public function getRootDir()
{
return $this->_rootDir;
}
/**
* Ability to use loader sith singleton implementation
*
* @return Magento_Autoload
*/
public static function getInstance()
{
if (!self::$_instance) {
self::$_instance = new Magento_Loader();
}
return self::$_instance;
}
/**
* Auto load class file
*
* @param string $class class name
*/
public function autoload($class)
{
if (isset($this->_filesMap[$class])) {
$classFile = $this->_rootDir . $this->_filesMap[$class];
} else {
$classFile = str_replace(array('_', self::NS_SEPARATOR), DIRECTORY_SEPARATOR, $class) . '.php';
}
require $classFile;
}
/**
* Add additional include path
*
* @param string|array $path specific path(s) started from system root folder
*/
public function addIncludePath($path)
{
if (is_array($path)) {
foreach ($path as $key => $value) {
$path[$key] = $this->_rootDir . $value;
}
} else {
$path = array($this->_rootDir . $path);
}
$path[] = get_include_path();
set_include_path(implode(PATH_SEPARATOR, $path));
}
/**
* Add classes files declaration to the map. New map will override existing values if such was defined before.
*
* @param array $map
*/
public function addFilesMap(array $map)
{
$this->_filesMap = array_merge($this->_filesMap, $map);
return $this;
}
/**
* Add classes overriding map. New map will override existing values if such was defined before.
*
* @param array $map
*/
public function addClassesMap(array $map)
{
$this->_classesMap = array_merge($this->_classesMap, $map);
return $this;
}
/**
* Create instance of particular class
*
* @param string $class class name
* @param null|array $parameters constructor parameters
* @return object
*/
public function createObject($class, $parameters=null)
{
$class = $this->_getClass($class);
if (null === $parameters) {
$object = new $class;
} else {
/* Switch based implementation can be used for instances with disabled reflections */
$object = new ReflectionClass($this->_getClass($class));
$object = $object->newInstanceArgs($parameters);
}
return $object;
}
/**
* Call class static method
*
* @param string $class class name
* @param string $method method name
* @param null|array $parameters method parameters
* @return mixed
*/
public function callMethod($class, $method, $parameters=null)
{
if (null === $parameters) {
$result = call_user_func(array($this->_getClass($class), $method));
} else {
$result = call_user_func_array(array($this->_getClass($class), $method), $parameters);
}
return $result;
}
/**
* Check override declarations and return class name
*
* @param string $class
* @return string
*/
protected function _getClass($class)
{
return isset($this->_classesMap[$class]) ? $this->_classesMap[$class] : $class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment