Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
14 / 14
ClassAutoloader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
14 / 14
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 loadClass
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
9 / 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(array($this, 'loadClass'));
        $this->namespace = $namespace;
        $this->nsLength = mb_strlen($namespace);
        $this->source_path = $source_path;
    }
    public function loadClass(string $class_name): bool
    {
        if (strpos($class_name, $this->namespace) !== 0) {
            return false;
        }
        $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;
        }
        return class_exists($class_name);
    }
}