Completed
Branchmaster (b31427)
created

Autoloader::_getFile()   A

↳ Parent: Autoloader

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 14
Code Lines 8

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 2
1
<?php
2
namespace Redaxscript;
3
4
/**
5
 * parent class to load required class files
6
 *
7
 * @since 3.0.0
8
 *
9
 * @package Redaxscript
10
 * @category Autoloader
11
 * @author Henry Ruhs
12
 */
13
14
class Autoloader
15
{
16
	/**
17
	 * array of the autoload
18
	 *
19
	 * @var array
20
	 */
21
22
	protected $_autoloadArray =
23
	[
24
		'Redaxscript' => 'includes',
25
		'Redaxscript\Modules' => 'modules',
26
		'libraries'
27
	];
28
29
	/**
30
	 * file extension
31
	 *
32
	 * @var string
33
	 */
34
35
	protected $_fileExtension = '.php';
36
37
	/**
38
	 * init the class
39
	 *
40
	 * @since 3.0.0
41
	 *
42
	 * @param mixed $autoload key or collection of the autoload
43
	 */
44
45 1
	public function init($autoload = null)
46
	{
47
		/* handle autoload */
48
49 1
		if (is_string($autoload))
50 1
		{
51
			$autoload =
52
			[
53
				$autoload
54 1
			];
55 1
		}
56 1
		if (is_array($autoload))
57 1
		{
58 1
			$this->_autoloadArray = array_merge($this->_autoloadArray, $autoload);
59 1
		}
60
61
		/* register autoload */
62
63 1
		spl_autoload_register(
64
		[
65 1
			__CLASS__,
66
			'_load'
67 1
		]);
68 1
	}
69
70
	/**
71
	 * load the class file
72
	 *
73
	 * @since 3.0.0
74
	 *
75
	 * @param string $className name of the class
76
	 */
77
78 71
	protected function _load($className = null)
79
	{
80 71
		foreach ($this->_autoloadArray as $namespace => $directory)
81
		{
82 71
			$file = $this->_getFile($className, $namespace);
83 71
			if (file_exists($directory . '/' . $file))
84 71
			{
85
				include_once($directory . '/' . $file);
86
			}
87
		}
88 71
	}
89 71
90 68
	/**
91 68
	 * get the file
92 71
	 *
93 71
	 * @since 3.0.0
94
	 *
95
	 * @param string $className name of the class
96
	 * @param string $namespace
97
	 *
98
	 * @return string
99
	 */
100
101
	protected function _getFile($className = null, $namespace = null)
102
	{
103
		$searchArray =
104
		[
105
			$namespace,
106
			'\\'
107
		];
108
		$replaceArray =
109
		[
110
			null,
111
			'/'
112
		];
113
		return str_replace($searchArray, $replaceArray, $className) . $this->_fileExtension;
114
	}
115
}
116