Test Failed
Push — master ( 36d69e...0393e5 )
by Stiofan
22:42
created

GD_Autoloader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * GeoDirectory Autoloader.
8
 *
9
 * @class 		GD_Autoloader
10
 * @version		1.0.0
11
 * @package		GeoDirectory/Classes
12
 * @category	Class
13
 * @author 		AyeCode
14
 */
15
class GD_Autoloader {
16
17
	/**
18
	 * Path to the includes directory.
19
	 *
20
	 * @var string
21
	 */
22
	private $include_path = '';
23
24
	/**
25
	 * The Constructor.
26
	 */
27
	public function __construct() {
28
		if ( function_exists( "__autoload" ) ) {
29
			spl_autoload_register( "__autoload" );
30
		}
31
32
		spl_autoload_register( array( $this, 'autoload' ) );
33
34
		$this->include_path = untrailingslashit( plugin_dir_path( GD_PLUGIN_FILE ) ) . '/includes/';
35
	}
36
37
	/**
38
	 * Take a class name and turn it into a file name.
39
	 *
40
	 * @param  string $class
41
	 * @return string
42
	 */
43
	private function get_file_name_from_class( $class ) {
44
		return 'class-' . str_replace( '_', '-', $class ) . '.php';
45
	}
46
47
	/**
48
	 * Include a class file.
49
	 *
50
	 * @param  string $path
51
	 * @return bool successful or not
52
	 */
53
	private function load_file( $path ) {
54
		if ( $path && is_readable( $path ) ) {
55
			include_once( $path );
56
			return true;
57
		}
58
		return false;
59
	}
60
61
	/**
62
	 * Auto-load WC classes on demand to reduce memory consumption.
63
	 *
64
	 * @param string $class
65
	 */
66
	public function autoload( $class ) {
67
		$class = strtolower( $class );
68
69
		if ( 0 !== strpos( $class, 'gd_' ) ) {
70
			return;
71
		}
72
73
		$file  = $this->get_file_name_from_class( $class );
74
		$path  = '';
75
76
		if ( strpos( $class, 'wc_addons_gateway_' ) === 0 ) {
77
			$path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 18 ) . '/';
78
		} elseif ( strpos( $class, 'gd_gateway_' ) === 0 ) {
79
			$path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 11 ) . '/';
80
		} elseif ( strpos( $class, 'gd_shipping_' ) === 0 ) {
81
			$path = $this->include_path . 'shipping/' . substr( str_replace( '_', '-', $class ), 12 ) . '/';
82
		} elseif ( strpos( $class, 'gd_shortcode_' ) === 0 ) {
83
			$path = $this->include_path . 'shortcodes/';
84
		} elseif ( strpos( $class, 'gd_meta_box' ) === 0 ) {
85
			$path = $this->include_path . 'admin/meta-boxes/';
86
		} elseif ( strpos( $class, 'gd_admin' ) === 0 ) {
87
			$path = $this->include_path . 'admin/';
88
		} elseif ( strpos( $class, 'gd_payment_token_' ) === 0 ) {
89
			$path = $this->include_path . 'payment-tokens/';
90
		} elseif ( strpos( $class, 'gd_log_handler_' ) === 0 ) {
91
			$path = $this->include_path . 'log-handlers/';
92
		}
93
94
		if ( empty( $path ) || ! $this->load_file( $path . $file ) ) {
95
			$this->load_file( $this->include_path . $file );
96
		}
97
	}
98
}
99
100
new GD_Autoloader();
101