1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2013, Marcin Rosinski. (http://www.getsimpletools.com)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @description Efficient Autoloading Class, based on the SPL
33: * @copyright Copyright (c) 2013 Marcin Rosinski. (http://www.getsimpletools.com)
34: * @license http://www.opensource.org/licenses/bsd-license.php - BSD
35: * @version Ver: 2.0.15 2014-12-30 23:36
36: *
37: */
38:
39: namespace Simpletools\Autoload;
40:
41: class Loader
42: {
43: public static $loader;
44:
45: private $__namespaces = array();
46: private $__prefixes = array();
47: private $__fallbackAutoloader = false;
48:
49: public static function &getInstance($options=null)
50: {
51: if (self::$loader == NULL)
52: self::$loader = new self($options);
53:
54: return self::$loader;
55: }
56:
57: public function __construct($options=null)
58: {
59: spl_autoload_register(array($this,'__load'));
60: if($options) $this->setOptions($options);
61: }
62:
63: public function &setOptions($options)
64: {
65: if(isset($options['autoregister_simpletools']) && $options['autoregister_simpletools']===true)
66: {
67: $this->autoregisterSimpleTools();
68: }
69:
70: if(isset($options['prefixes']) && is_array($options['prefixes']))
71: {
72: $this->registerPrefixes($options['prefixes']);
73: }
74:
75: if(isset($options['namespaces']) && is_array($options['namespaces']))
76: {
77: $this->registerNamespaces($options['namespaces']);
78: }
79:
80: return $this;
81: }
82:
83: public function &autoloadSimpletools()
84: {
85: return $this->registerNamespaces(array('Simpletools'=>dirname(__FILE__)));
86: }
87:
88: public function &autoregisterSimpletools()
89: {
90: return $this->registerNamespaces(array('Simpletools'=>dirname(__FILE__)));
91: }
92:
93: public function ®isterNamespace($namespace,$path)
94: {
95: $this->__namespaces[$namespace] = realpath($path);
96:
97: return $this;
98: }
99:
100: public function ®isterNamespaces($namespaces)
101: {
102: foreach($namespaces as $namespace => $path)
103: {
104: $this->registerNamespace($namespace,$path);
105: }
106:
107: return $this;
108: }
109:
110: public function ®isterPrefix($prefix,$path)
111: {
112: $this->__prefixes[$prefix] = realpath($path);
113:
114: return $this;
115: }
116:
117: public function ®isterPrefixes($prefixes)
118: {
119: foreach($prefixes as $prefix => $path)
120: {
121: $this->registerPrefix($prefix,$path);
122: }
123:
124: return $this;
125: }
126:
127: private function __loadPrefix($class)
128: {
129: //print_r($this->__prefixes);
130: foreach($this->__prefixes as $prefix => $path)
131: {
132: if(strpos($class,$prefix)===0)
133: {
134: if(is_file($path.DIRECTORY_SEPARATOR.$class.'.php'))
135: {
136: return require($path.DIRECTORY_SEPARATOR.$class.'.php');
137: }
138: }
139: }
140:
141: $this->__fallbackAutoloader($class);
142: }
143:
144: private function __loadNamespace($class)
145: {
146: $dir = explode('\\',$class);
147: $file = array_pop($dir);
148: $dir = implode(DIRECTORY_SEPARATOR,$dir);
149:
150: foreach($this->__namespaces as $namespace => $path)
151: {
152: if(strpos($class,$namespace)===0)
153: {
154: $class = trim(str_replace(array($namespace,'\\'),array('',DIRECTORY_SEPARATOR),$class),DIRECTORY_SEPARATOR);
155:
156: if(is_file($path.'/'.$class.'.php'))
157: {
158: return require($path.'/'.$class.'.php');
159: }
160: }
161: }
162:
163: $this->__fallbackAutoloader($class);
164: }
165:
166: private function __load($class)
167: {
168: if(stripos($class,'\\')) $this->__loadNamespace($class);
169: else $this->__loadPrefix($class);
170:
171: //set_include_path(get_include_path().PATH_SEPARATOR.'/lib/');
172: //spl_autoload_extensions('.library.php');
173: //spl_autoload($class);
174: }
175:
176: public function setFallbackAutoloader($status)
177: {
178: $this->__fallbackAutoloader = (boolean) $status;
179: }
180:
181: public function isFallbackAutoloader()
182: {
183: return $this->__fallbackAutoloader;
184: }
185:
186: private function __fallbackAutoloader($class)
187: {
188: if(!$this->__fallbackAutoloader) return;
189:
190: spl_autoload($class);
191: }
192: }
193: ?>