Completed
Branch bootstrap-refactoring (b06695)
by Adam
03:28
created

Loader.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
4
namespace Genesis;
5
6
require_once __DIR__ . '/exceptions.php';
7
8
9
/**
10
 * @author Adam Bisek ([email protected])
11
 */
12
class Loader
13
{
14
15
	/** @var array */
16
	private $map;
17
18
19 1
	public function __construct(array $map = array('Genesis' => __DIR__))
20
	{
21 1
		$this->map = $map;
22 1
	}
23
24
25
	/**
26
	 * Register autoloader.
27
	 */
28 1
	public function register()
29
	{
30 1
		spl_autoload_register(array($this, 'tryLoad'));
31 1
		return $this;
32
	}
33
34
35
	/**
36
	 * @param  string
37
	 */
38 51
	private function tryLoad($type)
39
	{
40 51
		$type = ltrim($type, '\\');
41 51
		foreach ($this->map as $ns => $dir) {
42 51
			if ($this->stringStartsWith($type, $ns)) {
43 51
				$file = str_replace('\\', DIRECTORY_SEPARATOR, $type) . '.php';
44 51
				$file = ltrim(substr($file, strlen($ns) + 1));
45 51
				$path = substr($file, 0, strrpos($file, DIRECTORY_SEPARATOR));
46 51
				$file = str_replace($path, strtolower($path), $file);
47 51
				$file = $dir . DIRECTORY_SEPARATOR . $file;
48 51
				if (!is_file($file)) {
49 1
					throw new \RuntimeException("File '$file' does not exists.");
50
				}
51 50
				return require_once $file;
52
			}
53 1
		}
54 1
	}
55
56
57 51
	private function stringStartsWith($haystack, $needle)
58
	{
59 51
		return strncmp($haystack, $needle, strlen($needle)) === 0;
60
	}
61
62
}