1+ <?php
2+
3+ /**
4+ * CodeMommy AutoloadPHP
5+ * @author Candison November <www.kandisheng.com>
6+ */
7+
8+ namespace CodeMommy \AutoloadPHP ;
9+
10+ /**
11+ * Class Autoload
12+ * @package CodeMommy\AutoloadPHP
13+ */
14+ class Autoload
15+ {
16+ /**
17+ * Remove First Slash
18+ *
19+ * @param $string
20+ */
21+ private static function removeFirstSlash (&$ string )
22+ {
23+ if (substr ($ string , 0 , 1 ) == '/ ' ) {
24+ $ string = substr ($ string , 1 );
25+ }
26+ }
27+
28+ /**
29+ * Remove Last Slash
30+ *
31+ * @param $string
32+ */
33+ private static function removeLastSlash (&$ string )
34+ {
35+ if (substr ($ string , -1 ) == '/ ' ) {
36+ $ string = substr ($ string , 0 , -1 );
37+ }
38+ }
39+
40+ /**
41+ * Replace Slash
42+ *
43+ * @param $string
44+ */
45+ private static function replaceSlash (&$ string )
46+ {
47+ $ string = str_replace ('\\' , '/ ' , $ string );
48+ }
49+
50+ /**
51+ * Load
52+ *
53+ * @param $path
54+ * @param $namespaceRoot
55+ */
56+ public static function load ($ path , $ namespaceRoot )
57+ {
58+ spl_autoload_register (function ($ className ) use ($ path , $ namespaceRoot ) {
59+ self ::replaceSlash ($ path );
60+ self ::removeLastSlash ($ path );
61+ self ::replaceSlash ($ namespaceRoot );
62+ self ::removeFirstSlash ($ namespaceRoot );
63+ self ::removeLastSlash ($ namespaceRoot );
64+ self ::replaceSlash ($ className );
65+ self ::removeFirstSlash ($ className );
66+ self ::removeLastSlash ($ className );
67+ if (substr ($ className , 0 , strlen ($ namespaceRoot )) == $ namespaceRoot ) {
68+ $ className = substr ($ className , strlen ($ namespaceRoot ));
69+ self ::removeFirstSlash ($ className );
70+ }
71+ $ file = $ path . '/ ' . $ className . '.php ' ;
72+ if (is_file ($ file )) {
73+ require_once ($ file );
74+ }
75+ });
76+ }
77+
78+ /**
79+ * File
80+ *
81+ * @param $path
82+ * @param $name
83+ */
84+ public static function file ($ path , $ name )
85+ {
86+ spl_autoload_register (function ($ className ) use ($ path , $ name ) {
87+ self ::replaceSlash ($ name );
88+ self ::removeFirstSlash ($ name );
89+ self ::removeLastSlash ($ name );
90+ self ::replaceSlash ($ className );
91+ self ::removeFirstSlash ($ className );
92+ self ::removeLastSlash ($ className );
93+ if ($ name == $ className ) {
94+ if (is_file ($ path )) {
95+ require_once ($ path );
96+ }
97+ }
98+ });
99+ }
100+ }
0 commit comments