Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 14 |
| ClassAutoloader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
20.00 | |
0.00% |
0 / 14 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 5 |
|||
| loadClass | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 9 |
|||
| <?php | |
| namespace Luxian\Common; | |
| class ClassAutoloader | |
| { | |
| /** @var string */ | |
| private $namespace; | |
| /** @var int */ | |
| private $nsLength; | |
| /** @var string */ | |
| private $source_path; | |
| public function __construct(string $namespace, string $source_path) | |
| { | |
| spl_autoload_register([$this, 'loadClass']); | |
| $this->namespace = $namespace; | |
| $this->nsLength = mb_strlen($namespace); | |
| $this->source_path = $source_path; | |
| } | |
| public function loadClass(string $class_name): void | |
| { | |
| if (strpos($class_name, $this->namespace) !== 0) { | |
| return; | |
| } | |
| $dirSeparator = DIRECTORY_SEPARATOR; | |
| $shortClassName = substr($class_name, $this->nsLength); | |
| $relativePath = str_replace('\\', $dirSeparator, $shortClassName); | |
| $fullPath = $this->source_path . $dirSeparator . $relativePath . '.php'; | |
| if (file_exists($fullPath)) { | |
| include $fullPath; | |
| } | |
| } | |
| } |