Completed
Push — dev/6.0.0 ( 1f7b71...79f74e )
by Sudar
22:04 queued 09:43
created

BulkDeleteAutoloader::require_file()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php namespace BulkWP\BulkDelete;
2
3
/**
4
 * Autoloader for Bulk Delete, based on the PSR-4 general purpose implementation.
5
 *
6
 * @see http://www.php-fig.org/psr/psr-4/
7
 *
8
 * This differs from WordPress coding standard in the following ways.
9
 *
10
 * - Class name and directory names use Snake case.
11
 * - Use of namespaces.
12
 *
13
 * Given a foo-bar package of classes in the file system at the following
14
 * paths ...
15
 *
16
 *     /path/to/packages/foo-bar/
17
 *         src/
18
 *             Baz.php             # Foo\Bar\Baz
19
 *             Qux/
20
 *                 Quux.php        # Foo\Bar\Qux\Quux
21
 *         tests/
22
 *             BazTest.php         # Foo\Bar\BazTest
23
 *             Qux/
24
 *                 QuuxTest.php    # Foo\Bar\Qux\QuuxTest
25
 *
26
 * ... add the path to the class files for the \Foo\Bar\ namespace prefix
27
 * as follows:
28
 *
29
 *      <?php
30
 *      // instantiate the loader
31
 *      $loader = new \BulkWP\BulkDelete\BulkDeleteAutoloader;
32
 *
33
 *      // register the autoloader
34
 *      $loader->register();
35
 *
36
 *      // register the base directories for the namespace prefix
37
 *      $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
38
 *      $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests');
39
 *
40
 * The following line would cause the autoloader to attempt to load the
41
 * \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:
42
 *
43
 *      <?php
44
 *      new \Foo\Bar\Qux\Quux;
45
 *
46
 * The following line would cause the autoloader to attempt to load the
47
 * \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:
48
 *
49
 *      <?php
50
 *      new \Foo\Bar\Qux\QuuxTest;
51
 *
52
 * @since 6.0.0
53
 */
54
class BulkDeleteAutoloader {
55
	/**
56
	 * An associative array where the key is a namespace prefix and the value
57
	 * is an array of base directories for classes in that namespace.
58
	 *
59
	 * @var array
60
	 */
61
	protected $prefixes = array();
62
63
	/**
64
	 * An associative array containing the list files that needs to be autoloaded.
65
	 *
66
	 * @var array
67
	 */
68
	protected $files = array();
69
70
	protected $custom_class_map = array();
71
72
	/**
73
	 * Register loader with SPL autoloader stack.
74
	 *
75
	 * @return void
76
	 */
77
	public function register() {
78
		spl_autoload_register( array( $this, 'load_class' ) );
79
80
		// file exists check is already done in `add_file`.
81
		foreach ( $this->files as $file ) {
82
			$this->require_file( $file );
83
		}
84
	}
85
86
	/**
87
	 * Adds a base directory for a namespace prefix.
88
	 *
89
	 * @param string $prefix   The namespace prefix.
90
	 * @param string $base_dir A base directory for class files in the
91
	 *                         namespace.
92
	 * @param bool   $prepend  If true, prepend the base directory to the stack
93
	 *                         instead of appending it; this causes it to be searched first rather
94
	 *                         than last.
95
	 *
96
	 * @return void
97
	 */
98
	public function add_namespace( $prefix, $base_dir, $prepend = false ) {
99
		// normalize namespace prefix.
100
		$prefix = trim( $prefix, '\\' ) . '\\';
101
102
		// normalize the base directory with a trailing separator.
103
		$base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . '/';
104
105
		// initialize the namespace prefix array.
106
		if ( false === isset( $this->prefixes[ $prefix ] ) ) {
107
			$this->prefixes[ $prefix ] = array();
108
		}
109
110
		// retain the base directory for the namespace prefix.
111
		if ( $prepend ) {
112
			array_unshift( $this->prefixes[ $prefix ], $base_dir );
113
		} else {
114
			array_push( $this->prefixes[ $prefix ], $base_dir );
115
		}
116
	}
117
118
	/**
119
	 * Add a file to be autoloaded.
120
	 *
121
	 * @param string $filename File to be autoloaded.
122
	 */
123
	public function add_file( $filename ) {
124
		if ( ! in_array( $filename, $this->files, true ) ) {
125
			$this->files[] = $filename;
126
		}
127
	}
128
129
	/**
130
	 * Loads the class file for a given class name.
131
	 *
132
	 * @param string $class The fully-qualified class name.
133
	 *
134
	 * @return false|string The mapped file name on success, or boolean false on
135
	 *                      failure.
136
	 */
137
	public function load_class( $class ) {
138
		if ( array_key_exists( $class, $this->custom_class_map ) ) {
139
			$file_loaded = $this->require_file( $this->custom_class_map[ $class ] );
140
141
			if ( $file_loaded ) {
142
				return true;
143
			}
144
		}
145
146
		// the current namespace prefix.
147
		$prefix = $class;
148
149
		// work backwards through the namespace names of the fully-qualified class name to find a mapped file name.
150
		while ( false !== $pos = strrpos( $prefix, '\\' ) ) {
151
152
			// retain the trailing namespace separator in the prefix.
153
			$prefix = substr( $class, 0, $pos + 1 );
154
155
			// the rest is the relative class name.
156
			$relative_class = substr( $class, $pos + 1 );
157
158
			// try to load a mapped file for the prefix and relative class.
159
			$mapped_file = $this->load_mapped_file( $prefix, $relative_class );
160
			if ( $mapped_file !== false ) {
161
				return $mapped_file;
162
			}
163
164
			// remove the trailing namespace separator for the next iteration
165
			// of strrpos().
166
			$prefix = rtrim( $prefix, '\\' );
167
		}
168
169
		// never found a mapped file.
170
		return false;
171
	}
172
173
	/**
174
	 * Load the mapped file for a namespace prefix and relative class.
175
	 *
176
	 * @param string $prefix         The namespace prefix.
177
	 * @param string $relative_class The relative class name.
178
	 *
179
	 * @return false|string Boolean false if no mapped file can be loaded, or the
180
	 *                      name of the mapped file that was loaded.
181
	 */
182
	protected function load_mapped_file( $prefix, $relative_class ) {
183
		// are there any base directories for this namespace prefix?
184
		if ( false === isset( $this->prefixes[ $prefix ] ) ) {
185
			return false;
186
		}
187
188
		// look through base directories for this namespace prefix.
189
		foreach ( $this->prefixes[ $prefix ] as $base_dir ) {
190
191
			// replace the namespace prefix with the base directory,
192
			// replace namespace separators with directory separators
193
			// in the relative class name, append with .php.
194
			$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
195
196
			// if the mapped file exists, require it.
197
			if ( $this->require_file( $file ) ) {
198
				// yes, we're done.
199
				return $file;
200
			}
201
		}
202
203
		// never found it.
204
		return false;
205
	}
206
207
	/**
208
	 * If a file exists, require it from the file system.
209
	 *
210
	 * @param string $file The file to require.
211
	 *
212
	 * @return bool True if the file exists, false if not.
213
	 */
214
	protected function require_file( $file ) {
215
		if ( file_exists( $file ) ) {
216
			require_once $file;
217
218
			return true;
219
		}
220
221
		return false;
222
	}
223
224
	public function set_custom_mapping( $custom_class_map ) {
225
		$this->custom_class_map = $custom_class_map;
226
	}
227
}
228