Completed
Push — master ( 677406...871e5e )
by Derek
02:41
created

Autoloader::includeClass()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

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

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
161
    {
162 6
        if ($this->logger instanceof Logger) {
163 2
            $this->logger->$level($message);
164
165 2
            $message_logged = true;
166 2
        } else {
167 4
            $message_logged = false;
168
        }
169
170 6
        return $message_logged;
171
    }
172
}
173