Prevent redundant autoloader registerations. Fixes #304

This commit is contained in:
Brendan Anderson
2017-01-02 10:22:37 -05:00
parent 2bdb23db94
commit 7cb5f3f08f
2 changed files with 32 additions and 1 deletions
+15 -1
View File
@@ -16,6 +16,14 @@ class Mustache_Autoloader
{
private $baseDir;
/**
* An array where the key is the baseDir and the key is an instance of this
* class.
*
* @var array
*/
static private $instances;
/**
* Autoloader constructor.
*
@@ -45,7 +53,13 @@ class Mustache_Autoloader
*/
public static function register($baseDir = null)
{
$loader = new self($baseDir);
$key = $baseDir ? $baseDir : 0;
if (!isset(self::$instances[$key])) {
self::$instances[$key] = new self($baseDir);
}
$loader = self::$instances[$key];
spl_autoload_register(array($loader, 'autoload'));
return $loader;