-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autoloader.php
86 lines (77 loc) · 2.37 KB
/
Autoloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
/**
* Example autoload implementation which uses the generated class map configuration
*
* This file contains the class definition and the registration of the autoloader
* together for the sake of simplicity. This is up to you to separate the logic to
* your requirements/wishes.
*
* Usage:
* 1. Include this file, e.g. at the beginning of your scripts or in a bootstrap process
* <pre>
* require_once '{project_root}/vendor/purc/autoloader-class-map/src/Autoloader.php';
* // ... then use your classes ...
* </pre>
* 2. or define additional autoloader in your composer.json
* <pre>
* {
* "autoload": {
* "files": ["vendor/purc/autoloader-class-map/src/Autoloader.php"]
* }
* }
* </pre>
*
* @category Development
* @package AutoloaderClassMap
* @author Murat Purç <[email protected]>
* @copyright Murat Purç (http://www.purc.de)
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
*/
namespace Purc\AutoloaderClassMap;
use Exception;
/**
* Simple autoloader class
*/
class Autoloader
{
/**
* Class map configuration
* @var string[]
*/
protected $classMap;
/**
* Autoloader constructor, loads the passed class map file.
*
* @param string $classMapPath Path to class map file to include
* @return void
* @throws Exception if autoloader couldn't set class map configuration in initial call
*/
public function __construct(string $classMapPath)
{
// NOTE: Adapt the path to the class map configuration file or make it configurable!
$this->classMap = include_once($classMapPath);
if (!is_array($this->classMap) || empty($this->classMap)) {
throw new Exception(__CLASS__ . ": Couldn't load classmap configuration");
}
spl_autoload_register([$this, 'autoload']);
}
/**
* Autoload implementation, loads the passed class name from the class map.
*
* @param string $name The required class name
*/
public function autoload(string $name)
{
if (isset($this->classMap[$name])) {
require_once $this->classMap[$name];
}
}
}
(function() {
$file = getenv('PURC_AUTOLOADER_CLASS_MAP_FILE');
if (empty($file)) {
$file = $GLOBALS['PURC_AUTOLOADER_CLASS_MAP_FILE'] ?? '';
}
new Autoloader($file);
})();