Completed
Push — develop ( 813f17...3b34d4 )
by Aristeides
02:46
created

Kirki_Autoload::get_paths()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 1
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Kirki autoloader.
4
 * Handles locating and loading other class-files.
5
 *
6
 * @package     Kirki
7
 * @category    Core
8
 * @author      Aristeides Stathopoulos
9
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
10
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
11
 * @since       1.0
12
 */
13
14
/**
15
 * Autoloader class.
16
 *
17
 * @since 3.0.10
18
 */
19
class Kirki_Autoload {
20
21
	/**
22
	 * Cached paths.
23
	 *
24
	 * @access private
25
	 * @since 3.0.10
26
	 * @var array
27
	 */
28
	private $cached_paths = array();
29
30
	/**
31
	 * Class constructor.
32
	 *
33
	 * @access public
34
	 * @since 3.0.10
35
	 */
36
	public function __construct() {
37
38
		spl_autoload_register( array( $this, 'autoload' ) );
39
	}
40
41
	/**
42
	 * The Kirki class autoloader.
43
	 * Finds the path to a class that we're requiring and includes the file.
44
	 *
45
	 * @access protected
46
	 * @since 3.0.10
47
	 * @param string $class_name The name of the class we're trying to load.
48
	 */
49
	protected function autoload( $class_name ) {
50
51
		// Not a Kirki file, early exit.
52
		if ( 0 !== stripos( $class_name, 'Kirki' ) ) {
53
			return;
54
		}
55
56
		// Check if we've got it cached and ready.
57
		if ( isset( $this->cached_paths[ $class_name ] ) && file_exists( $this->cached_paths[ $class_name ] ) ) {
58
			include_once $this->cached_paths[ $class_name ];
59
			return;
60
		}
61
62
		$paths = $this->get_paths( $class_name );
63
64
		foreach ( $paths as $path ) {
65
			$path = wp_normalize_path( $path );
66
			if ( file_exists( $path ) ) {
67
				$this->cached_paths[ $class_name ] = $path;
68
				include_once $path;
69
				return;
70
			}
71
		}
72
	}
73
74
	/**
75
	 * Get an array of possible paths for the file.
76
	 *
77
	 * @access protected
78
	 * @since 3.0.10
79
	 * @param string $class_name The name of the class we're trying to load.
80
	 * @return array
81
	 */
82
	protected function get_paths( $class_name ) {
83
84
		$paths = array();
85
		// Build the filename.
86
		$filename = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
87
88
		// Break class-name is parts.
89
		$name_parts = explode( '_', str_replace( 'Kirki_', '', $class_name ) );
90
91
		// Handle modules loading.
92
		if ( isset( $name_parts[0] ) && 'Modules' === $name_parts[0] ) {
93
			$path  = dirname( __FILE__ ) . '/modules/';
94
			$path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Modules_', '', $class_name ) ) ) . '/';
95
			$paths[] = $path . $filename;
96
		}
97
98
		$paths[] = dirname( __FILE__ ) . '/core/' . $filename;
99
		$paths[] = dirname( __FILE__ ) . '/lib/' . $filename;
100
101
		$substr   = str_replace( 'Kirki_', '', $class_name );
102
		$exploded = explode( '_', $substr );
103
		$levels   = count( $exploded );
104
105
		$previous_path = '';
106
		for ( $i = 0; $i < $levels; $i++ ) {
107
			$paths[] = dirname( __FILE__ ) . '/' . $previous_path . strtolower( $exploded[ $i ] ) . '/' . $filename;
108
			$previous_path .= strtolower( $exploded[ $i ] ) . '/';
109
		}
110
		return $paths;
111
	}
112
}
113