Completed
Push — master ( 431343...34be94 )
by Derek
02:18
created

Autoloader::getLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Subreality\Dilmun\Nabu;
3
4
use Subreality\Dilmun\Nabu\LoggerHandler\File;
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 a File handler
20
     */
21 3
    public function __construct()
22
    {
23 3
        $logger_handler = new File("autoloader.log", DILMUN_LOGS_DIR);
24 3
        $logger_handler->initialize();
25
26 3
        $this->logger = new Logger($logger_handler);
27 3
    }
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 5
    public function registerPackages($package, $path)
44
    {
45 5
        $this->logger->info("Preparing {$package} package for registration.");
46 5
        $this->logger->debug("Parameters: package = {$package}, path = {$path}");
47
48 5
        $package = trim($package, "\\");
49 5
        $package = $package . "\\";
50
51 5
        $path = rtrim($path, "/");
52 5
        $path = $path . "/";
53
54 5
        $this->logger->debug("Registering index {$package} with {$path}");
55 5
        $this->packages[$package][] = $path;
56
57 5
        $this->logger->info("Registered index {$package} as {$path}");
58 5
    }
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 bool|string      The path to the class on success; otherwise false
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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
        //Maybe we'll need this one day...
78
        //$vendor_name = substr($class, 0, $vendor_end);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
80 3
        $package_found = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
81 3
        $package_loaded = false;
82
83 3
        foreach ($this->packages as $package => $sources) {
84 2
            if (strpos($class, $package) !== false) {
85 2
                $package_found = true;
86 2
                $this->logger->debug("Found package {$package} in {$class}!");
87
88 2
                $package_string_length = strlen($package);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $package_string_length exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
89
90 2
                $class_path = substr($class, $package_string_length);
91 2
                $this->logger->debug("Pulled out class path {$class_path}");
92
93 2
                $package_loaded = $this->loadPackage($package, $class_path);
94 2
            }
95 3
        }
96
97 3
        if (!$package_found) {
98 1
            $this->logger->alert("No registered packages found for {$class}!");
99 1
        }
100
101 3
        return $package_loaded;
102
    }
103
104
    /**
105
     * Loads a package based on registered paths associated with the package.
106
     *
107
     * @param string $package_name  The name of the package in Vendor\Package format
108
     * @param string $class_name    The full, relative name of the class with the vendor and package names stripped
109
     *
110
     * @return bool|string          The path to the class on success; otherwise false
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
111
     */
112 2
    protected function loadPackage($package_name, $class_name)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $package_name is not named in camelCase.

This check marks parameter names that have not been 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 string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $class_name is not named in camelCase.

This check marks parameter names that have not been 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 string becomes databaseConnectionString.

Loading history...
113
    {
114 2
        $this->logger->info("Working on loading class via file include.");
115 2
        $this->logger->debug("Parameters: package_name = {$package_name}, class_name = {$class_name}");
116
117 2
        $path_loaded = false;
118
119 2
        if (isset($this->packages[$package_name])) {
120 2
            $this->logger->debug("Package index of {$package_name} is set.");
121
122 2
            foreach ($this->packages[$package_name] as $path) {
123 2
                $converted_class = str_replace('\\', '/', $class_name);
124 2
                $class_path = $path . $converted_class . ".php";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
125 2
                $this->logger->debug("Set up class path as {$class_path}");
126
127 2
                if (file_exists($class_path)) {
128 1
                    $this->logger->debug("File {$class_path} exists");
129
130 1
                    $this->logger->debug("Including " . $class_path);
131
                    /** @noinspection PhpIncludeInspection */
132 1
                    include $class_path;
133
134 1
                    $path_loaded = $class_path;
135
136 1
                    $this->logger->info("Completed loading {$path_loaded}");
137 1
                } else {
138 2
                    $this->logger->debug("File {$class_path} does not exist given {$package_name} package");
139
140 2
                    $path_loaded = false;
141
                }
142 2
            }
143 2
        }
144
        
145 2
        $this->logger->debug("loadPackage is returning {$path_loaded}");
146 2
        return $path_loaded;
147
    }
148
149
    /**
150
     * Allows overriding the default file logger created at construction
151
     *
152
     * @param Logger $logger
153
     */
154 1
    public function setLogger(Logger $logger)
155
    {
156 1
        $this->logger = $logger;
157 1
    }
158
159
    /**
160
     * Gets the Logger set for the Autoloader
161
     *
162
     * @return Logger
163
     */
164 1
    public function getLogger()
165
    {
166 1
        return $this->logger;
167
    }
168
}
169