1: <?php
2: /**
3: * Fluent-PHP-Logger
4: *
5: * Copyright (C) 2011 Shuhei Tanuma
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: /**
22: * Fluent Autoloader.
23: *
24: * @author Shuhei Tanuma <stanuma@zynga.com>
25: */
26: class Autoloader
27: {
28: const NAME_SPACE = 'Fluent';
29: protected static $base_dir;
30:
31: /**
32: * register Fluent basic autoloader
33: *
34: * @param string $dirname base directory path.
35: * @return void
36: */
37: public static function register($dirname = __DIR__)
38: {
39: self::$base_dir = $dirname;
40: spl_autoload_register(array(__CLASS__, "autoload"));
41: }
42:
43: /**
44: * unregister Fluent basic autoloader
45: *
46: * @return void
47: */
48: public static function unregister()
49: {
50: spl_autoload_unregister(array(__CLASS__, "autoload"));
51: }
52:
53: /**
54: * autoload basic implementation
55: *
56: * @param string class name
57: * @return boolean return true when load successful
58: */
59: public static function autoload($name)
60: {
61: $retval = false;
62: if (strpos($name,self::NAME_SPACE) === 0) {
63: $parts = explode("\\",$name);
64: array_shift($parts);
65:
66: $expected_path = join(DIRECTORY_SEPARATOR,array(self::$base_dir, join(DIRECTORY_SEPARATOR,$parts) . ".php"));
67: if (is_file($expected_path) && is_readable($expected_path)) {
68: require $expected_path;
69: $retval = true;
70: }
71: }
72:
73: return $retval;
74: }
75: }