Completed
Branch refactor/142 (8a1d2c)
by Luke
02:46
created

Autoloader::addPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
5
 * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV
6
 * standardization efforts, CSVelte was written in an effort to take all the
7
 * suck out of working with CSV.
8
 *
9
 * @version   v0.2.1
10
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
11
 * @author    Luke Visinoni <[email protected]>
12
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
13
 */
14
namespace CSVelte;
15
/**
16
 * CSVelte Autoloader
17
 *
18
 * For those crazy silly people who aren't using Composer, simply include this
19
 * file to have CSVelte's files auto-loaded by PHP.
20
 *
21
 * @package CSVelte
22
 * @subpackage Autoloader
23
 * @since v0.1
24
 */
25
class Autoloader
26
{
27
    /**
28
     * @var string Constant for namespace separator
29
     */
30
    const NAMESPACE_SEPARATOR = '\\';
31
32
    /**
33
     * An array of paths that will be searched when attempting to load a class
34
     * @var array
35
     */
36
    protected $paths;
37
38
    /**
39
     * Autoloader Constructor
40
     */
41 9
    public function __construct($paths = array())
42
    {
43 9
        $this->paths = explode(PATH_SEPARATOR, get_include_path());
44 9
        foreach ($paths as $path) {
45 2
            $this->addPath($path);
46 9
        }
47 9
    }
48
49
    /**
50
     * Add path to list of search paths
51
     *
52
     * Attempts to deduce the absolute (real) path from the path specified by the
53
     * $path argument. If successful, the absolute path is added to the search
54
     * path list and the method returns true. If one can't be found, it adds $path
55
     * to the search path list, as-is and returns false
56
     *
57
     * @param string A path to add to the list of search paths
58
     * @return boolean
59
     */
60 5
    public function addPath($path)
61
    {
62 5
        $paths = $this->getPaths();
63 5
        if ($rp = realpath($path)) {
64 4
            if (in_array($rp, $paths)) return true;
65 4
            $this->paths []= $rp;
66 4
            return true;
67
        }
68 2
        $this->paths []= $path;
69 2
        return false;
70
    }
71
72
    /**
73
     * Retrieve search path list (array)
74
     *
75
     * Simply returns the array containing all the paths that will be searched
76
     * when attempting to load a class.
77
     *
78
     * @return array An array of paths to search for classes
79
     */
80 7
    public function getPaths()
81
    {
82 7
        return $this->paths;
83
    }
84
85
    /**
86
     * Register the autoloader
87
     *
88
     * Registers this library's autoload function with the SPL-provided autoload
89
     * queue. This allows for CSVelte's autoloader to work its magic without
90
     * having to worry about interfering with any other autoloader.
91
     *
92
     * Also adds all of this class's search paths to PHP's include path.
93
     *
94
     * @return boolean Whatever the return value of spl_autoload_register is
95
     * @see spl_autoload_register
96
     */
97 2
    public function register()
98
    {
99 2
        set_include_path(implode(PATH_SEPARATOR, $this->getPaths()));
100 2
        spl_autoload_register(array($this, 'load'));
101 2
    }
102
103
    /**
104
     * Load a class
105
     *
106
     * This is the function (or method in this case) used to autoload all of
107
     * CSVelte's classes. It need not be called directly, but rather regestered
108
     * with the SPL's autoload queue using this class's register method.
109
     *
110
     * @param  string The fully qualified class name to load
111
     * @return boolean
112
     * @see \CSVelte\Autoloader::register()
113
     */
114 4
    public function load($className)
115
    {
116 4
        if(class_exists($className, false)) {
117 2
            return;
118
        }
119 2
        $fqcp = str_replace(self::NAMESPACE_SEPARATOR, DIRECTORY_SEPARATOR, $className);
120 2
        $paths = $this->getPaths();
121 2
        foreach ($paths as $path) {
122 2
            $classPath = $path . DIRECTORY_SEPARATOR . $fqcp . '.php';
123 2
            if(file_exists($classPath) && is_readable($classPath)) {
124 1
                require_once($classPath);
125 1
                return;
126
            }
127 2
        }
128 1
    }
129
}
130