From ab23338fe9f1b3ed21455867f1c032d7b146ceb8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 2 Mar 2015 16:51:04 +0100 Subject: Initial Commit --- Mustache/Autoloader.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Mustache/Autoloader.php (limited to 'Mustache/Autoloader.php') diff --git a/Mustache/Autoloader.php b/Mustache/Autoloader.php new file mode 100644 index 0000000..707a0ff --- /dev/null +++ b/Mustache/Autoloader.php @@ -0,0 +1,69 @@ +baseDir = dirname(__FILE__).'/..'; + } else { + $this->baseDir = rtrim($baseDir, '/'); + } + } + + /** + * Register a new instance as an SPL autoloader. + * + * @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..') + * + * @return Mustache_Autoloader Registered Autoloader instance + */ + public static function register($baseDir = null) + { + $loader = new self($baseDir); + spl_autoload_register(array($loader, 'autoload')); + + return $loader; + } + + /** + * Autoload Mustache classes. + * + * @param string $class + */ + public function autoload($class) + { + if ($class[0] === '\\') { + $class = substr($class, 1); + } + + if (strpos($class, 'Mustache') !== 0) { + return; + } + + $file = sprintf('%s/%s.php', $this->baseDir, str_replace('_', '/', $class)); + if (is_file($file)) { + require $file; + } + } +} -- cgit v1.2.3-55-g7522