Completed
Push — master ( c80630...f436f3 )
by Derek
03:35
created

Autoloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
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 string|false     The path to the class on success; otherwise false
66
     */
67 2
    protected function includeClass($class)
68
    {
69 2
        $this->logger->info("Setting up {$class} class for inclusion.");
70
71 2
        $package_found  = false;
72 2
        $package_loaded = false;
73
74 2
        foreach ($this->packages as $package => $sources) {
75 1
            if (strpos($class, $package) !== false) {
76 1
                $package_found = true;
77
78 1
                $package_strlen = strlen($package);
79
80 1
                $class_path = substr($class, $package_strlen);
81
82 1
                $this->logger->debug("Found package {$package} and class path {$class_path} in class {$class}");
83
84 1
                $package_loaded = $this->loadPackage($package, $class_path);
85 1
            }
86 2
        }
87
88 2
        if (!$package_found) {
89 1
            $this->logger->alert("No registered packages found for {$class}!");
90 1
        }
91
92 2
        return $package_loaded;
93
    }
94
95
    /**
96
     * Loads a package based on registered paths associated with the package.
97
     *
98
     * @param string $package_name  The name of the package in Vendor\Package format
99
     * @param string $class_name    The full, relative name of the class with the vendor and package names stripped
100
     *
101
     * @return false|string          The path to the class on success; otherwise false
102
     */
103 1
    protected function loadPackage($package_name, $class_name)
104
    {
105 1
        $this->logger->info("Working on loading {$class_name} from {$package_name}");
106
107 1
        $class_path = false;
108
109 1
        if (isset($this->packages[$package_name])) {
110 1
            $this->logger->debug("Package index of {$package_name} is set.");
111
112 1
            foreach ($this->packages[$package_name] as $path) {
113 1
                $converted_class = str_replace('\\', '/', $class_name);
114 1
                $class_path      = $path . $converted_class . ".php";
115
116 1
                $this->logger->debug("Class file path is {$class_path}");
117
118 1
                if (file_exists($class_path)) {
119
                    $this->logger->debug("File {$class_path} exists; including file");
120
121
                    /** @noinspection PhpIncludeInspection */
122
                    include $class_path;
123
124
                    $this->logger->info("Completed loading {$class_path}");
125
                } else {
126 1
                    $this->logger->debug("File {$class_path} does not exist");
127
128 1
                    $class_path = false;
129
                }
130 1
            }
131 1
        }
132
        
133 1
        return $class_path;
134
    }
135
136
    /**
137
     * Allows overriding the default file logger created at construction
138
     *
139
     * @param Logger $logger
140
     */
141 1
    public function setLogger(Logger $logger)
142
    {
143 1
        $this->logger = $logger;
144 1
    }
145
146
    /**
147
     * Gets the Logger set for the Autoloader
148
     *
149
     * @return Logger
150
     */
151 2
    public function getLogger()
152
    {
153 2
        return $this->logger;
154
    }
155
}
156