1<?php
2/**
3 *
4 * @category  Xamin
5 * @package   Handlebars
6 * @author    fzerorubigd <fzerorubigd@gmail.com>
7 * @author    Behrooz Shabani <everplays@gmail.com>
8 * @copyright 2012 (c) ParsPooyesh Co
9 * @copyright 2013 (c) Behrooz Shabani
10 * @copyright 2014 (c) Mardix
11 * @license   MIT
12 * @link      http://voodoophp.org/docs/handlebars
13 */
14
15namespace Handlebars;
16
17class Autoloader
18{
19
20    private $_baseDir;
21
22    /**
23     * Autoloader constructor.
24     *
25     * @param string $baseDir Handlebars library base directory default is
26     *                        __DIR__.'/..'
27     */
28    protected function __construct($baseDir = null)
29    {
30        if ($baseDir === null) {
31            $this->_baseDir = realpath(__DIR__ . '/..');
32        } else {
33            $this->_baseDir = rtrim($baseDir, '/');
34        }
35    }
36
37    /**
38     * Register a new instance as an SPL autoloader.
39     *
40     * @param string $baseDir Handlebars library base directory, default is
41     *                        __DIR__.'/..'
42     *
43     * @return \Handlebars\Autoloader Registered Autoloader instance
44     */
45    public static function register($baseDir = null)
46    {
47        $loader = new self($baseDir);
48        spl_autoload_register(array($loader, 'autoload'));
49
50        return $loader;
51    }
52
53    /**
54     * Autoload Handlebars classes.
55     *
56     * @param string $class class to load
57     *
58     * @return void
59     */
60    public function autoload($class)
61    {
62        if ($class[0] === '\\') {
63            $class = substr($class, 1);
64        }
65
66        if (strpos($class, 'Handlebars') !== 0) {
67            return;
68        }
69
70        $file = sprintf('%s/%s.php', $this->_baseDir, str_replace('\\', '/', $class));
71
72        if (is_file($file)) {
73            include $file;
74        }
75    }
76
77}
78