PluginProbe
MakeCommerce for WooCommerce / trunk
MakeCommerce for WooCommerce vtrunk
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / includes / autoloader.php

autoloader.php in MakeCommerce for WooCommerce trunk, at includes/autoloader.php

180 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce;
4
5 /**
6 * Autoloader. Loads all classes automatically so that we dont have to use require and include
7 *
8 * @since 3.0.0
9 */
10
11 class Autoloader
12 {
13 /**
14 * An associative array where the key is a namespace prefix and the value
15 * is an array of base directories for classes in that namespace.
16 *
17 * @var array
18 *
19 * @since 3.0.0
20 */
21 protected $prefixes = array();
22
23 /**
24 * Register loader with SPL autoloader stack.
25 *
26 * @return void
27 *
28 * @since 3.0.0
29 */
30 public function register() {
31
32 spl_autoload_register( array( $this, 'loadClass' ) );
33 }
34
35 /**
36 * Adds a base directory for a namespace prefix.
37 *
38 * @param string $prefix The namespace prefix.
39 * @param string $base_dir A base directory for class files in the
40 * namespace.
41 * @param bool $prepend If true, prepend the base directory to the stack
42 * instead of appending it; this causes it to be searched first rather
43 * than last.
44 * @return void
45 *
46 * @since 3.0.0
47 */
48 public function addNamespace( $prefix, $base_dir, $prepend = false ) {
49
50 // normalize namespace prefix
51 $prefix = trim( $prefix, '\\' ) . '\\';
52
53 // normalize the base directory with a trailing separator
54 $base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . '/';
55
56 // initialize the namespace prefix array
57 if ( isset( $this->prefixes[$prefix] ) === false ) {
58 $this->prefixes[$prefix] = array();
59 }
60
61 // retain the base directory for the namespace prefix
62 if ( $prepend ) {
63 array_unshift( $this->prefixes[$prefix], $base_dir );
64 } else {
65 array_push( $this->prefixes[$prefix], $base_dir );
66 }
67 }
68
69 /**
70 * Loads the class file for a given class name.
71 *
72 * @param string $class The fully-qualified class name.
73 * @return mixed The mapped file name on success, or boolean false on
74 * failure.
75 *
76 * @since 3.0.0
77 */
78 public function loadClass( $class ) {
79
80 // the current namespace prefix
81 $prefix = $class;
82
83 // work backwards through the namespace names of the fully-qualified
84 // class name to find a mapped file name
85 while ( false !== $pos = strrpos( $prefix, '\\' ) ) {
86
87 // retain the trailing namespace separator in the prefix
88 $prefix = substr( $class, 0, $pos + 1 );
89
90 // the rest is the relative class name
91 $relative_class = substr( $class, $pos + 1 );
92
93 // try to load a mapped file for the prefix and relative class
94 $mapped_file = $this->loadMappedFile( $prefix, $relative_class );
95 if ( $mapped_file ) {
96 return $mapped_file;
97 }
98
99 // remove the trailing namespace separator for the next iteration
100 // of strrpos()
101 $prefix = rtrim( $prefix, '\\' );
102 }
103
104 // never found a mapped file
105 return false;
106 }
107
108 /**
109 * Load the mapped file for a namespace prefix and relative class.
110 *
111 * @param string $prefix The namespace prefix.
112 * @param string $relative_class The relative class name.
113 * @return mixed Boolean false if no mapped file can be loaded, or the
114 * name of the mapped file that was loaded.
115 *
116 * @since 3.0.0
117 */
118 protected function loadMappedFile( $prefix, $relative_class ) {
119
120 // are there any base directories for this namespace prefix?
121 if ( isset( $this->prefixes[$prefix] ) === false ) {
122 return false;
123 }
124
125 // look through base directories for this namespace prefix
126 foreach ( $this->prefixes[$prefix] as $base_dir ) {
127
128 // replace the namespace prefix with the base directory,
129 // replace namespace separators with directory separators
130 // in the relative class name, append with .php
131 $file = $base_dir
132 . strtolower ( str_replace( '\\', '/', $relative_class ) )
133 . '.php';
134
135 // if the mapped file exists, require it
136 if ( $this->requireFile( $file ) ) {
137 // yes, we're done
138 return $file;
139 }
140
141 //No match above. Also try Class name as filename.
142 //Example: Shipping.php is in MakeCommerce namespace but not in shipping namespace to avoid MakeCommerce\Shipping\Shipping
143 $tmpRelativeClass = explode( '\\', $relative_class );
144 $tmpRelativeClass = end( $tmpRelativeClass );
145
146 $file = $base_dir
147 . strtolower ( str_replace( '\\', '/', $relative_class )
148 . "/" . $tmpRelativeClass )
149 . '.php';
150
151 if ( $this->requireFile( $file ) ) {
152 // yes, we're done
153 return $file;
154 }
155 }
156
157 // never found it
158 return false;
159 }
160
161 /**
162 * If a file exists, require it from the file system.
163 *
164 * @param string $file The file to require.
165 * @return bool True if the file exists, false if not.
166 *
167 * @since 3.0.0
168 */
169 protected function requireFile( $file ) {
170
171 if ( file_exists( $file ) ) {
172
173 require_once( $file );
174
175 return true;
176 }
177
178 return false;
179 }
180 }