Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Fix issue with multiple autoloader declarations. See https://github.c…
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Oct 26, 2014
1 parent f25a63a commit 7c7f074
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 365 deletions.
363 changes: 181 additions & 182 deletions autoloader/InstantAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,214 +36,213 @@
* There might be several InstantAutoloaders deployed in a project.
* One is enough.
*/
if (class_exists("malkusch\autoloader\InstantAutoloader")) {
return;
if (! class_exists("malkusch\autoloader\InstantAutoloader", false)) {

}

/**
* An instant autoloader for shipping with project builds
*
* You can build a complete index of your project (by calling
* Autoloader::buildIndex()). If you use AutoloaderIndex_PHPArrayCode as
* index you can only use this instant autoloader for your project. This class
* has no further dependency on any other class. Just copy this class and the
* generated index anywhere into your project.
*
* Consider setting the AutoloaderIndexFilter_RelativePath filter.
*
* @category PHP
* @package Autoloader
* @author Markus Malkusch <[email protected]>
* @license http://php-autoloader.malkusch.de/en/license/ GPL 3
* @version Release: 1.12
* @link http://php-autoloader.malkusch.de/en/
* @see Autoloader::buildIndex()
* @see AutoloaderIndex_PHPArrayCode()
* @see AutoloaderIndexFilter_RelativePath()
*/
class InstantAutoloader
{

const
/**
* The name of the class constructor is classConstructor().
*/
CLASS_CONSTRUCTOR = 'classConstructor';

private
/**
* @var string
*/
$_basePath = "",
/**
* @var array
* An instant autoloader for shipping with project builds
*
* You can build a complete index of your project (by calling
* Autoloader::buildIndex()). If you use AutoloaderIndex_PHPArrayCode as
* index you can only use this instant autoloader for your project. This class
* has no further dependency on any other class. Just copy this class and the
* generated index anywhere into your project.
*
* Consider setting the AutoloaderIndexFilter_RelativePath filter.
*
* @category PHP
* @package Autoloader
* @author Markus Malkusch <[email protected]>
* @license http://php-autoloader.malkusch.de/en/license/ GPL 3
* @version Release: 1.12
* @link http://php-autoloader.malkusch.de/en/
* @see Autoloader::buildIndex()
* @see AutoloaderIndex_PHPArrayCode()
* @see AutoloaderIndexFilter_RelativePath()
*/
$_index = array();

/**
* Loads the generated index array
*
* The index must be a generated AutoloaderIndex_PHPArrayCode index.
*
* @param string $indexPath Path to the generated index
*/
public function __construct($indexPath)
class InstantAutoloader
{
$this->_index = require $indexPath;
}

/**
* Registers this autoloader at the autoloader stack
*
* @return void
*/
public function register()
{
// spl_autoload_register() disables __autoload(). This might be unwanted.
if (\function_exists("__autoload")) {
\spl_autoload_register("__autoload");
const
/**
* The name of the class constructor is classConstructor().
*/
CLASS_CONSTRUCTOR = 'classConstructor';

private
/**
* @var string
*/
$_basePath = "",
/**
* @var array
*/
$_index = array();

/**
* Loads the generated index array
*
* The index must be a generated AutoloaderIndex_PHPArrayCode index.
*
* @param string $indexPath Path to the generated index
*/
public function __construct($indexPath)
{
$this->_index = require $indexPath;
}

/**
* Registers this autoloader at the autoloader stack
*
* @return void
*/
public function register()
{
// spl_autoload_register() disables __autoload(). This might be unwanted.
if (\function_exists("__autoload")) {
\spl_autoload_register("__autoload");

}
\spl_autoload_register(array($this, "__autoload"));
}
\spl_autoload_register(array($this, "__autoload"));
}

/**
* Includes all class paths
*
* You can use this as an alternative to the autoload mechanism. This
* simply includes all classes without any autoloader.
*
* @return void
*/
public function requireAll()
{
foreach ($this->_index as $classPath) {
$this->_requirePath($classPath);
/**
* Includes all class paths
*
* You can use this as an alternative to the autoload mechanism. This
* simply includes all classes without any autoloader.
*
* @return void
*/
public function requireAll()
{
foreach ($this->_index as $classPath) {
$this->_requirePath($classPath);

}
}
}

/**
* Sets the base path for the class paths in the index
*
* @param string $basePath Base path for the class paths in the index
*
* @return void
*/
public function setBasePath($basePath)
{
$this->_basePath = $basePath;
}

/**
* Autoloader callback
*
* @param string $class Class name
*
* @return void
*/
public function __autoload($class)
{
$this->_normalizeClass($class);

/*
* spl_autoload_call() runs the complete stack,
* even though the class is already defined by
* a previously registered method.
/**
* Sets the base path for the class paths in the index
*
* @param string $basePath Base path for the class paths in the index
*
* @return void
*/
if (
\class_exists($class, false)
|| \interface_exists($class, false)
) {
return;
public function setBasePath($basePath)
{
$this->_basePath = $basePath;
}

/**
* Autoloader callback
*
* @param string $class Class name
*
* @return void
*/
public function __autoload($class)
{
$this->_normalizeClass($class);

/*
* spl_autoload_call() runs the complete stack,
* even though the class is already defined by
* a previously registered method.
*/
if (
\class_exists($class, false)
|| \interface_exists($class, false)
) {
return;

}
if (
\version_compare(PHP_VERSION, "5.4", '>=')
&& \trait_exists($class, false)
) {
return;

}
if (!\array_key_exists($class, $this->_index)) {
return;

}

$this->_requirePath($this->_index[$class]);

$this->_callClassConstructor($class, self::CLASS_CONSTRUCTOR);
}
if (
\version_compare(PHP_VERSION, "5.4", '>=')
&& \trait_exists($class, false)
) {
return;

}
if (! \array_key_exists($class, $this->_index)) {
return;
/**
* Requires a class path
*
* @param string $path Class path
*
* @return void
*/
private function _requirePath($path)
{
if (!empty($this->_basePath)) {
$path = $this->_basePath . DIRECTORY_SEPARATOR . $path;

}
require_once $path;
}

$this->_requirePath($this->_index[$class]);

$this->_callClassConstructor($class, self::CLASS_CONSTRUCTOR);
}

/**
* Requires a class path
*
* @param string $path Class path
*
* @return void
*/
private function _requirePath($path)
{
if (! empty($this->_basePath)) {
$path = $this->_basePath . DIRECTORY_SEPARATOR . $path;

/**
* Normalizes the reference of a class name with strtolower()
*
* Normalizing is needed as PHP is case insensitive.
*
* @param String &$class The reference of a class name
*
* @see strtolower().
* @return void
*/
private function _normalizeClass(&$class)
{
$class = \strtolower($class);
}
require_once $path;
}

/**
* Normalizes the reference of a class name with strtolower()
*
* Normalizing is needed as PHP is case insensitive.
*
* @param String &$class The reference of a class name
*
* @see strtolower().
* @return void
*/
private function _normalizeClass(&$class)
{
$class = \strtolower($class);
}

/**
* Calls the class constructor
*
* If the class $class has the method public static $constructor, it
* will be called.
*
* @param String $class A class which might have a class constructor
* @param String $constructorName the method name of the class constructor
*
* @return bool true if the class constructor was called
*/
public static function _callClassConstructor($class, $constructorName)
{
$reflectionClass = new \ReflectionClass($class);
if (! $reflectionClass->hasMethod($constructorName)) {
return false;
/**
* Calls the class constructor
*
* If the class $class has the method public static $constructor, it
* will be called.
*
* @param String $class A class which might have a class constructor
* @param String $constructorName the method name of the class constructor
*
* @return bool true if the class constructor was called
*/
public static function _callClassConstructor($class, $constructorName)
{
$reflectionClass = new \ReflectionClass($class);
if (!$reflectionClass->hasMethod($constructorName)) {
return false;

}

$constructor = $reflectionClass->getMethod($constructorName);
if (! $constructor->isStatic()) {
return false;
}

}

if (\version_compare(PHP_VERSION, "5.4", '>=') && $reflectionClass->isTrait()) {
return false;
$constructor = $reflectionClass->getMethod($constructorName);
if (!$constructor->isStatic()) {
return false;

}
}

if (\version_compare(PHP_VERSION, "5.4", '>=') && $reflectionClass->isTrait()) {
return false;

}

if ($constructor->getDeclaringClass()->getName() != $reflectionClass->getName()) {
return false;

if ($constructor->getDeclaringClass()->getName() != $reflectionClass->getName()) {
return false;
}

$constructor->invoke(null);
return true;
}

$constructor->invoke(null);
return true;
}

}
}
Loading

0 comments on commit 7c7f074

Please sign in to comment.