1: <?php
2: /**
3: * Fluent-Logger-PHP
4: *
5: * Copyright (C) 2011 - 2012 Fluent-Logger-PHP Contributors
6: *
7: * Licensed under the Apache License, Version 2.0 (the "License");
8: * you may not use this file except in compliance with the License.
9: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: namespace Fluent;
20:
21: class Autoloader
22: {
23: const NAME_SPACE = 'Fluent';
24: protected static $base_dir;
25:
26: /**
27: * register Fluent basic autoloader
28: *
29: * @param string $dirname base directory path.
30: * @return void
31: */
32: public static function register($dirname = __DIR__)
33: {
34: self::$base_dir = $dirname;
35: spl_autoload_register(array(__CLASS__, "autoload"));
36: }
37:
38: /**
39: * unregister Fluent basic autoloader
40: *
41: * @return void
42: */
43: public static function unregister()
44: {
45: spl_autoload_unregister(array(__CLASS__, "autoload"));
46: }
47:
48: /**
49: * autoload basic implementation
50: *
51: * @param string $name class name
52: * @return boolean return true when load successful
53: */
54: public static function autoload($name)
55: {
56: $retval = false;
57: if (strpos($name,self::NAME_SPACE) === 0) {
58: $parts = explode("\\",$name);
59: array_shift($parts);
60:
61: $expected_path = join(DIRECTORY_SEPARATOR,array(self::$base_dir, join(DIRECTORY_SEPARATOR,$parts) . ".php"));
62: if (is_file($expected_path) && is_readable($expected_path)) {
63: require $expected_path;
64: $retval = true;
65: }
66: }
67:
68: return $retval;
69: }
70: }