Completed
Push — master ( b8fb8b...1f1253 )
by Derek
02:22
created

Autoloader::updateLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
namespace Subreality\Dilmun\Nabu;
3
4
use Subreality\Dilmun\LoggedClassTrait;
5
6
/**
7
 * Nabu was the god of writing and scribes and was the keeper of the Tablets of
8
 * Destiny, in which the fate of humankind was recorded.
9
 *
10
 * @author derek
11
 */
12
class Autoloader
13
{
14
    use LoggedClassTrait;
15
16
    protected $packages = array();
17
18
    /**
19
     * Automatically initializes logging using the System handler
20
     */
21 2
    public function __construct()
22
    {
23 2
    }
24
25
    /**
26
     * Registers the includeClass function as an autoloader.
27
     *
28
     * @see Autoloader::includeClass
29
     */
30 2
    public function register()
31
    {
32 2
        spl_autoload_register(array($this,"includeClass"));
33 2
    }
34
35
    /**
36
     * @param $package
37
     * @param $path
38
     */
39 6
    public function registerPackages($package, $path)
40
    {
41 6
        $this->updateLog("info", "Preparing {$package} package for registration.");
42 6
        $this->updateLog("debug", "Parameters: package = {$package}, path = {$path}");
43
44 6
        $package = trim($package, "\\");
45 6
        $package = $package . "\\";
46
47 6
        $path = rtrim($path, "/");
48 6
        $path = $path . "/";
49
50 6
        $this->updateLog("debug", "Registering index {$package} with {$path}");
51 6
        $this->packages[$package][] = $path;
52
53 6
        $this->updateLog("info", "Registered index {$package} as {$path}");
54 6
    }
55
56
    /**
57
     * Includes the file corresponding with the class name to be autoloaded.
58
     *
59
     * @param callable $class   class name as defined by PHP autoloading
60
     *
61
     * @return string|false     The path to the class on success; otherwise false
62
     */
63 3
    protected function includeClass($class)
64
    {
65 3
        $this->updateLog("info", "Setting up {$class} class for inclusion.");
66
67 3
        $package_found  = false;
68 3
        $package_loaded = false;
69
70 3
        foreach ($this->packages as $package => $sources) {
71 2
            if (strpos($class, $package) !== false) {
72 2
                $package_found = true;
73
74 2
                $package_length = strlen($package);
75
76 2
                $class_path = substr($class, $package_length);
77
78 2
                $this->updateLog("debug", "Found package {$package} and class path {$class_path} in class {$class}");
79
80 2
                $package_loaded = $this->loadPackage($package, $class_path);
81 2
            }
82 3
        }
83
84 3
        if (!$package_found) {
85 1
            $this->updateLog("alert", "No registered packages found for {$class}!");
86 1
        }
87
88 3
        return $package_loaded;
89
    }
90
91
    /**
92
     * Loads a package based on registered paths associated with the package.
93
     *
94
     * @param string $package_name  The name of the package in Vendor\Package format
95
     * @param string $class_name    The full, relative name of the class with the vendor and package names stripped
96
     *
97
     * @return false|string          The path to the class on success; otherwise false
98
     */
99 2
    protected function loadPackage($package_name, $class_name)
100
    {
101 2
        $this->updateLog("info", "Working on loading {$class_name} from {$package_name}");
102
103 2
        $class_path = false;
104
105 2
        if (isset($this->packages[$package_name])) {
106 2
            $this->updateLog("debug", "Package index of {$package_name} is set.");
107
108 2
            foreach ($this->packages[$package_name] as $path) {
109 2
                $converted_class = str_replace('\\', '/', $class_name);
110 2
                $class_path      = $path . $converted_class . ".php";
111
112 2
                $this->updateLog("debug", "Class file path is {$class_path}");
113
114 2
                if (file_exists($class_path)) {
115 1
                    $this->updateLog("debug", "File {$class_path} exists; including file");
116
117
                    /** @noinspection PhpIncludeInspection */
118 1
                    include $class_path;
119
120 1
                    $this->updateLog("info", "Completed loading {$class_path}");
121 1
                } else {
122 2
                    $this->updateLog("debug", "File {$class_path} does not exist");
123
124 2
                    $class_path = false;
125
                }
126 2
            }
127 2
        }
128
        
129 2
        return $class_path;
130
    }
131
}
132