This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoloader.php
178 lines (150 loc) · 4.83 KB
/
Autoloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
Copyright 2017 Stephen Bungert (email : hello@stephenbungert.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Loads all classes for sb_portfolio, this currently loads them alphabetically, so make sure your base classes
* are named in away so that any extending them are included after them! Or create subfolders for extending classes.
*
* Interfaces and abstract classes are now loaded first, this done by looking for "Abstract" at the beginning of the class,
* or "Interface" at the end of the class.
*
* @author Stephen Bungert <hello@stephenbungert.com>
* @package WordPress
* @subpackage SB Portfolio
* @since 0.1.0
*/
class Sbp_Autoloader {
/**
* Where includable files are stored.
*
* @since 0.1.0
* @access protected
* @var array
*/
const BASE_FOLDERS = array('classes/shared', 'classes', 'addons');
/**
* The maximum number of folders deep to look in (to stop infinite searching)
*
* @since 0.1.0
* @access protected
* @var integer
*/
const MAX_DEPTH = 10;
/**
* The type of autoload.
*
* @since 0.1.0
* @access protected
* @var string
*/
protected $type = '';
/**
* Interfaces that need loading, found by looking for "Interface" at the end of the class name.
*
* @since 0.1.0
* @access protected
* @var array
*/
protected $interfaceClasses = array();
/**
* Abstract classes that need loading, found by looking for "Abstract" at the beginning of the class name.
*
* @since 0.1.0
* @access protected
* @var array
*/
protected $abstractClasses = array();
/**
* Normal classes that need loading, anything not added to $interfaceClasses and $abstractClasses.
*
* @since 0.1.0
* @access protected
* @var array
*/
protected $normalClasses = array();
/**
* Constructor.
*
* @param string $type The type of autolading.
* @return void
*/
public function __construct($type = '') {
$this->type = $type;
if (!empty($this->type)) {
foreach (self::BASE_FOLDERS as $folder) {
$this->findClasses(SBP_FOLDER . self::BASE_FOLDERS[$folder]);
}
$this->includeClasses();
}
}
/**
* Includes found classes.
*
* @since 0.1.0
* @access protected
* @param string $filePath The folder to be looked in.
* @return void.
*/
protected function includeClasses() {
if (!empty($this->interfaceClasses)) {
foreach ($this->interfaceClasses as $file) {
require_once($file);
}
}
if (!empty($this->abstractClasses)) {
foreach ($this->abstractClasses as $file) {
require_once($file);
}
}
if (!empty($this->normalClasses)) {
foreach ($this->normalClasses as $file) {
require_once($file);
}
}
}
/**
* A recursive function that looks for php files and sorts them into their correct storage array.
*
* @since 0.1.0
* @access protected
* @param string $filePath The folder to be looked in.
* @return void.
*/
protected function findClasses($filePath, $currentDepth = 0) {
if ($filePath == 'Classes') $filePath .= '/' . $this->type;
$files = scandir($filePath);
$files = array_values(array_diff($files, array('.', '..'))); // Remove the . and .. folders and re-index
foreach ($files as $file) {
$fullFilePath = $filePath . DIRECTORY_SEPARATOR . $file;
if (is_dir($fullFilePath)) { // If it is a folder, scan it
if ($currentDepth < self::MAX_DEPTH) {
$newDepth = $currentDepth + 1;
$this->findClasses($fullFilePath, $newDepth);
}
} else { // Else see if it is a php file
$pathInfo = pathinfo($fullFilePath);
if ($pathInfo['extension'] == 'php') {
if (substr($pathInfo['filename'], -9) === 'Interface') {
$this->interfaceClasses[] = $fullFilePath;
} else if (substr($pathInfo['filename'], 0, 8) === 'Abstract') {
$this->abstractClasses[] = $fullFilePath;
} else {
$this->normalClasses[] = $fullFilePath;
}
}
}
}
}
}
?>