-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassLoader.php
52 lines (44 loc) · 1.18 KB
/
ClassLoader.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
<?php
namespace Oro\Component\PhpUtils;
/**
* A simple and fast implementation of the class loader
* that can be used to map one namespace to one path.
*/
class ClassLoader
{
private string $namespacePrefix;
private string $path;
public function __construct(string $namespacePrefix, string $path)
{
$this->namespacePrefix = $namespacePrefix;
$this->path = $path . DIRECTORY_SEPARATOR;
}
/**
* Registers this class loader on the SPL autoload stack.
*/
public function register(): void
{
spl_autoload_register([$this, 'loadClass']);
}
/**
* Removes this class loader from the SPL autoload stack.
*/
public function unregister(): void
{
spl_autoload_unregister([$this, 'loadClass']);
}
/**
* Loads the given class.
*/
public function loadClass(string $className): bool
{
if (!str_starts_with($className, $this->namespacePrefix)) {
return false;
}
$file = $this->path . str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
if (false === @include $file) {
return false;
}
return true;
}
}