summaryrefslogtreecommitdiffstats
path: root/Mustache/Autoloader.php
diff options
context:
space:
mode:
authorSimon Rettberg2015-03-02 16:51:04 +0100
committerSimon Rettberg2015-03-02 16:51:04 +0100
commitab23338fe9f1b3ed21455867f1c032d7b146ceb8 (patch)
tree33d27fd848e25ae24a57b309348b5d73a811f223 /Mustache/Autoloader.php
downloadbwlp-webadmin-ab23338fe9f1b3ed21455867f1c032d7b146ceb8.tar.gz
bwlp-webadmin-ab23338fe9f1b3ed21455867f1c032d7b146ceb8.tar.xz
bwlp-webadmin-ab23338fe9f1b3ed21455867f1c032d7b146ceb8.zip
Initial Commit
Diffstat (limited to 'Mustache/Autoloader.php')
-rw-r--r--Mustache/Autoloader.php69
1 files changed, 69 insertions, 0 deletions
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 @@
+<?php
+
+/*
+ * This file is part of Mustache.php.
+ *
+ * (c) 2012 Justin Hileman
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Mustache class autoloader.
+ */
+class Mustache_Autoloader
+{
+
+ private $baseDir;
+
+ /**
+ * Autoloader constructor.
+ *
+ * @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..')
+ */
+ public function __construct($baseDir = null)
+ {
+ if ($baseDir === null) {
+ $this->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;
+ }
+ }
+}