|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
|
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 {version} |
|
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
|
|
|
/** |
|
17
|
|
|
* CSVelte Autoloader. |
|
18
|
|
|
* |
|
19
|
|
|
* For those crazy silly people who aren't using Composer, simply include this |
|
20
|
|
|
* file to have CSVelte's files auto-loaded by PHP. |
|
21
|
|
|
* |
|
22
|
|
|
* @package CSVelte |
|
23
|
|
|
* @subpackage Autoloader |
|
24
|
|
|
* |
|
25
|
|
|
* @since v0.1 |
|
26
|
|
|
*/ |
|
27
|
|
|
class Autoloader |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Constant for namespace separator |
|
31
|
|
|
*/ |
|
32
|
|
|
const NAMESPACE_SEPARATOR = '\\'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* An array of paths that will be searched when attempting to load a class. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $paths; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Autoloader Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $paths Paths to search for classes |
|
45
|
|
|
*/ |
|
46
|
8 |
|
public function __construct($paths = []) |
|
47
|
|
|
{ |
|
48
|
8 |
|
$this->paths = explode(PATH_SEPARATOR, get_include_path()); |
|
49
|
8 |
|
foreach ($paths as $path) { |
|
50
|
2 |
|
$this->addPath($path); |
|
51
|
8 |
|
} |
|
52
|
8 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Add path to list of search paths. |
|
56
|
|
|
* |
|
57
|
|
|
* Attempts to deduce the absolute (real) path from the path specified by the |
|
58
|
|
|
* $path argument. If successful, the absolute path is added to the search |
|
59
|
|
|
* path list and the method returns true. If one can't be found, it adds $path |
|
60
|
|
|
* to the search path list, as-is and returns false |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $path A path to add to the list of search paths |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
5 |
|
public function addPath($path) |
|
67
|
|
|
{ |
|
68
|
5 |
|
$paths = $this->getPaths(); |
|
69
|
5 |
|
if ($rp = realpath($path)) { |
|
70
|
4 |
|
if (in_array($rp, $paths)) { |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
4 |
|
$this->paths []= $rp; |
|
74
|
|
|
|
|
75
|
4 |
|
return true; |
|
76
|
|
|
} |
|
77
|
2 |
|
$this->paths []= $path; |
|
78
|
|
|
|
|
79
|
2 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Retrieve search path list (array). |
|
84
|
|
|
* |
|
85
|
|
|
* Simply returns the array containing all the paths that will be searched |
|
86
|
|
|
* when attempting to load a class. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array An array of paths to search for classes |
|
89
|
|
|
*/ |
|
90
|
106 |
|
public function getPaths() |
|
91
|
|
|
{ |
|
92
|
106 |
|
return $this->paths; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Register the autoloader. |
|
97
|
|
|
* |
|
98
|
|
|
* Registers this library's autoload function with the SPL-provided autoload |
|
99
|
|
|
* queue. This allows for CSVelte's autoloader to work its magic without |
|
100
|
|
|
* having to worry about interfering with any other autoloader. |
|
101
|
|
|
* |
|
102
|
|
|
* Also adds all of this class's search paths to PHP's include path. |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool Whatever the return value of spl_autoload_register is |
|
105
|
|
|
* |
|
106
|
|
|
* @see spl_autoload_register |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function register() |
|
109
|
|
|
{ |
|
110
|
2 |
|
set_include_path(implode(PATH_SEPARATOR, $this->getPaths())); |
|
111
|
2 |
|
spl_autoload_register([$this, 'load']); |
|
112
|
2 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Load a class. |
|
116
|
|
|
* |
|
117
|
|
|
* This is the function (or method in this case) used to autoload all of |
|
118
|
|
|
* CSVelte's classes. It need not be called directly, but rather regestered |
|
119
|
|
|
* with the SPL's autoload queue using this class's register method. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $className The fully qualified class name to load |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
* |
|
125
|
|
|
* @see Autoloader::register() |
|
126
|
|
|
*/ |
|
127
|
103 |
|
public function load($className) |
|
128
|
|
|
{ |
|
129
|
103 |
|
if (class_exists($className)) { |
|
130
|
2 |
|
return; |
|
131
|
|
|
} |
|
132
|
101 |
|
$fqcp = str_replace(self::NAMESPACE_SEPARATOR, DIRECTORY_SEPARATOR, $className); |
|
133
|
101 |
|
$paths = $this->getPaths(); |
|
134
|
101 |
|
foreach ($paths as $path) { |
|
135
|
101 |
|
$classPath = $path . DIRECTORY_SEPARATOR . $fqcp . '.php'; |
|
136
|
101 |
|
if (file_exists($classPath) && is_readable($classPath)) { |
|
137
|
|
|
require_once($classPath); |
|
138
|
|
|
|
|
139
|
|
|
return; |
|
140
|
|
|
} |
|
141
|
101 |
|
} |
|
142
|
101 |
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|