Completed
Push — master ( b0efab...7373cc )
by Derek
02:07
created

Autoloader::includeClass()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 32
ccs 21
cts 21
cp 1
rs 8.5806
cc 4
eloc 18
nc 6
nop 1
crap 4
1
<?php
2
namespace Subreality\Dilmun\Nabu;
3
4
use Subreality\Dilmun\Nabu\LoggerHandler\System;
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
    protected $logger;
15
16
    protected $packages = array();
17
18
    /**
19
     * Automatically initializes logging using the System handler
20
     */
21 2
    public function __construct()
22
    {
23 2
        $system_logger = new System();
24 2
        $system_logger->initialize();
25
        
26 2
        $this->logger = new Logger($system_logger);
27 2
    }
28
29
    /**
30
     * Registers the includeClass function as an autoloader.
31
     *
32
     * @see Autoloader::includeClass
33
     */
34 2
    public function register()
35
    {
36 2
        spl_autoload_register(array($this,"includeClass"));
37 2
    }
38
39
    /**
40
     * @param $package
41
     * @param $path
42
     */
43 6
    public function registerPackages($package, $path)
44
    {
45 6
        $this->logger->info("Preparing {$package} package for registration.");
46 6
        $this->logger->debug("Parameters: package = {$package}, path = {$path}");
47
48 6
        $package = trim($package, "\\");
49 6
        $package = $package . "\\";
50
51 6
        $path = rtrim($path, "/");
52 6
        $path = $path . "/";
53
54 6
        $this->logger->debug("Registering index {$package} with {$path}");
55 6
        $this->packages[$package][] = $path;
56
57 6
        $this->logger->info("Registered index {$package} as {$path}");
58 6
    }
59
60
    /**
61
     * Includes the file corresponding with the class name to be autoloaded.
62
     *
63
     * @param callable $class   class name as defined by PHP autoloading
64
     *
65
     * @return false|string      The path to the class on success; otherwise false
66
     *                          otherwise.
67
     */
68 3
    protected function includeClass($class)
69
    {
70 3
        $this->logger->info("Setting up {$class} class for inclusion.");
71 3
        $this->logger->debug("Parameter => class: {$class}");
72
73
        //Find the vendor based on the first backslash
74 3
        $vendor_end = strpos($class, '\\');
75 3
        $this->logger->debug("The vendor name in {$class} ends at position {$vendor_end}");
76
77 3
        $package_found  = false;
78 3
        $package_loaded = false;
79
80 3
        foreach ($this->packages as $package => $sources) {
81 2
            if (strpos($class, $package) !== false) {
82 2
                $package_found = true;
83 2
                $this->logger->debug("Found package {$package} in {$class}!");
84
85 2
                $package_strlen = strlen($package);
86
87 2
                $class_path = substr($class, $package_strlen);
88 2
                $this->logger->debug("Pulled out class path {$class_path}");
89
90 2
                $package_loaded = $this->loadPackage($package, $class_path);
91 2
            }
92 3
        }
93
94 3
        if (!$package_found) {
95 1
            $this->logger->alert("No registered packages found for {$class}!");
96 1
        }
97
98 3
        return $package_loaded;
99
    }
100
101
    /**
102
     * Loads a package based on registered paths associated with the package.
103
     *
104
     * @param string $package_name  The name of the package in Vendor\Package format
105
     * @param string $class_name    The full, relative name of the class with the vendor and package names stripped
106
     *
107
     * @return false|string          The path to the class on success; otherwise false
108
     */
109 2
    protected function loadPackage($package_name, $class_name)
110
    {
111 2
        $this->logger->info("Working on loading class via file include.");
112 2
        $this->logger->debug("Parameters: package_name = {$package_name}, class_name = {$class_name}");
113
114 2
        $path_loaded = false;
115
116 2
        if (isset($this->packages[$package_name])) {
117 2
            $this->logger->debug("Package index of {$package_name} is set.");
118
119 2
            foreach ($this->packages[$package_name] as $path) {
120 2
                $converted_class = str_replace('\\', '/', $class_name);
121 2
                $class_path      = $path . $converted_class . ".php";
122
123 2
                $this->logger->debug("Set up class path as {$class_path}");
124
125 2
                if (file_exists($class_path)) {
126 1
                    $this->logger->debug("File {$class_path} exists");
127
128 1
                    $this->logger->debug("Including " . $class_path);
129
                    /** @noinspection PhpIncludeInspection */
130 1
                    include $class_path;
131
132 1
                    $path_loaded = $class_path;
133
134 1
                    $this->logger->info("Completed loading {$path_loaded}");
135 1
                } else {
136 2
                    $this->logger->debug("File {$class_path} does not exist given {$package_name} package");
137
138 2
                    $path_loaded = false;
139
                }
140 2
            }
141 2
        }
142
        
143 2
        $this->logger->debug("loadPackage is returning {$path_loaded}");
144 2
        return $path_loaded;
145
    }
146
147
    /**
148
     * Allows overriding the default file logger created at construction
149
     *
150
     * @param Logger $logger
151
     */
152 1
    public function setLogger(Logger $logger)
153
    {
154 1
        $this->logger = $logger;
155 1
    }
156
157
    /**
158
     * Gets the Logger set for the Autoloader
159
     *
160
     * @return Logger
161
     */
162 2
    public function getLogger()
163
    {
164 2
        return $this->logger;
165
    }
166
}
167