-
Notifications
You must be signed in to change notification settings - Fork 0
/
screws.php
402 lines (346 loc) · 9.97 KB
/
screws.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
//if there is no hammerpath defined must called specific so return false
if (!defined("HAMMERPATH")) { throw new Spanner("Hammer Path not defined", 99); }
//if no sitepath included make it app for other frameworks to understand
if (!defined("SITEPATH")) {
$cPath = dirname(__FILE__);
$cPath .= "app/";
define("SITEPATH", $cPath);
}
/**
* Screws
*
* @package
* @author keloran
* @copyright Copyright (c) 2011
* @version $Id$
* @access public
*/
class Screws {
private $cClass;
private $cPath;
private $aData;
private $cOriginal;
/**
* Screws::__construct()
*
*/
public function __construct() {
spl_autoload_register(array($this, "loader"), true);
}
/**
* Screws::loader()
*
* @param mixed $cClass
* @return
*/
private function loader($cClass) {
$this->cClass = $cClass;
$this->cOriginal = $cClass;
$this->definePaths();
$bExists = $this->checkExists();
//should really check if the class is inside a file thats been created
if (!$bExists) { $bExists = class_exists($cClass, false); }
//does the file exist
if ($bExists) {
try {
include $this->cPath;
} catch (Exception $e) {
throw new Spanner($e->getMessage());
}
//now make sure not todo this in a loop
$bSkip = false;
if (substr($cClass, -7) == "install") { $bSkip = true; }
//now do the installer unless skipped
if (!$bSkip) {
if ($this->cClassPath) {
try {
$this->doInstall($cClass, $this->cClassPath);
} catch (Exception $e) {
throw new Spanner($e->getMessage());
}
}
}
} else {
//so that the class not exisitng can be handled by the user/file
throw new ErrorException($cClass . ": Class File not found", 101);
}
}
/**
* Screws::__set()
*
* @param string $cName
* @param mixed $mValue
* @return null
*/
public function __set($cName, $mValue) {
$this->aData[$cName] = $mValue;
}
/**
* Screws::__isset()
*
* @param string $cName
* @return bool
*/
public function __isset($cName) {
$bReturn = false;
if (isset($this->aData[$cName])) { $bReturn = true; }
if (isset($this->$cName)) { $bReturn = true; }
return $bReturn;
}
/**
* Screws::__get()
*
* @param string $cName
* @return mixed
*/
public function __get($cName) {
if (isset($this->aData[$cName])) { return $this->aData[$cName]; }
return false;
}
/**
* Screws::definePaths()
*
* @return null
*/
private function definePaths() {
$this->defineClassVars();
$this->defineHammerPaths();
//is there a usernails folder
$bNails = $this->defineNailsPath();
//if there is no usernails folder, might be legacy
if (!$bNails) { $this->defineLibsPath(); }
}
/**
* Screws::defineClassVars()
*
* @return null
*/
private function defineClassVars() {
//Set teh defaults so it doesnt complain
$cClassName = strtolower($this->cClass);
//added this so it doesnt die when one doesnt exist
$cClass_b = false;
$aClass = false;
//get the nails class
$cClass_a = str_replace("_", "/", $cClassName);
$aClass = explode("_", $cClassName);
//so it doesnt do it on every loop
$iClass = count($aClass);
$cClass_b = $aClass[0];
$cClass_c = $cClassName;
//se if its a namespace
$cClass_d = str_replace("\\", "/", $cClassName);
//its not the base class (e.g. Database)
if ($iClass >= 2) {
$cClass_b = $cClass_a;
$cClass_b .= "/" . end($aClass);
} else {
$cClass_a = $cClassName . "/" . $cClassName;
}
$this->cClass_a = $cClass_a;
$this->cClass_b = $cClass_b;
$this->cClass_c = $cClass_c;
$this->cClass_d = $cClass_d;
$this->cClassName = $cClassName;
}
/**
* Screws::defineHammerPaths()
*
* @return null
*/
private function defineHammerPaths() {
//Hammer Class
$this->fHammerClass_a = HAMMERPATH . "/nails/" . $this->cClass_a . ".inc.php";
$this->fHammerClass_b = HAMMERPATH . "/nails/" . $this->cClass_b . ".inc.php";
//without the inc bit
$this->fHammerClass_c = HAMMERPATH . "/nails/" . $this->cClass_a . ".php";
$this->fHammerClass_d = HAMMERPATH . "/nails/" . $this->cClass_b . ".php";
//see if there is anything in namespaces
$this->fHammerClass_e = HAMMERPATH . "/nails/" . $this->cClass_d . ".php";
//Traits
$this->fHammerClass_Trait = HAMMERPATH . "/traits/" . $this->cClass_a . ".php";
//Base class, this is only for things like spanner
$this->fBaseClass = HAMMERPATH . "/" . $this->cClassName . ".php";
//get the paths
$this->fHammerClass_a_Path = HAMMERPATH . "/nails/" . $this->cClass_a;
$this->fHammerClass_b_Path = HAMMERPATH . "/nails/" . $this->cClass_b;
$this->fHammerClass_c_Path = HAMMERPATH . "/nails/" . $this->cClass_a;
$this->fHammerClass_d_Path = HAMMERPATH . "/nails/" . $this->cClass_b;
$this->fHammerClass_e_Path = HAMMERPATH . "/nails/" . $this->cClass_d;
//if the dir exists
if (is_dir(SITEPATH . "/nails")) {
if (!defined("USERNAILS")) {
define("USERNAILS", SITEPATH . "/nails/");
}
}
}
/**
* Screws::defineNailsPath()
*
* @return bool
*/
private function defineNailsPath() {
$bReturn = false;
if (defined("USERNAILS")) {
$bReturn = true;
} else {
return false;
}
//site class
$this->fSiteClass_a = USERNAILS . $this->cClass_a . ".inc.php";
$this->fSiteClass_b = USERNAILS . $this->cClass_b . ".inc.php";
$this->fSiteClass_e = USERNAILS . $this->cClass_d . ".inc.php";
//without the inc bit
$this->fSiteClass_c = USERNAILS . $this->cClass_a . ".php";
$this->fSiteClass_d = USERNAILS . $this->cClass_b . ".php";
$this->fSiteClass_f = USERNAILS . $this->cClass_d . ".php";
//Traits
$this->fSiteClass_Trait_a = SITEPATH . "/traits/" . $this->cClass_a . ".php";
$this->fSiteClass_Trait_b = USERNAILS . "/traits/" . $this->cClass_a . ".php";
//get the paths
$this->fSiteClass_a_Path = USERNAILS . $this->cClass_a;
$this->fSiteClass_b_Path = USERNAILS . $this->cClass_b;
$this->fSiteClass_c_Path = USERNAILS . $this->cClass_d;
$this->fSiteClass_d_Path = USERNAILS . $this->cClass_a;
$this->fSiteClass_e_Path = USERNAILS . $this->cClass_b;
$this->fSiteClass_f_Path = USERNAILS . $this->cClass_d;
return $bReturn;
}
/**
* Screws::defineLibsPath()
*
* @return null
*/
private function defineLibsPath() {
$this->fSiteClass_a = SITEPATH . "/libs/" . $this->cClass_a . ".inc.php";
$this->fSiteClass_b = SITEPATH . "/libs/" . $this->cClass_b . ".inc.php";
//without the inc bit
$this->fSiteClass_c = SITEPATH . "/libs/" . $this->cClass_a . ".php";
$this->fSiteClass_d = SITEPATH . "/libs/" . $this->cClass_b . ".php";
//get the paths
$this->fSiteClass_a_Path = SITEPATH . "/libs/" . $this->cClass_a;
$this->fSiteClass_b_Path = SITEPATH . "/libs/" . $this->cClass_b;
$this->fSiteClass_c_Path = SITEPATH . "/libs/" . $this->cClass_a;
$this->fSiteClass_d_Path = SITEPATH . "/libs/" . $this->cClass_b;
}
/**
* Screws::checkExists()
*
* @return bool
*/
private function checkExists() {
$aRange = range("a", "f");
$iRange = count($aRange); //random
//base class
if (file_exists($this->fBaseClass)) {
$this->cPath = $this->fBaseClass;
return true;
}
//now do the site classes
for ($i = 0; $i < $iRange; $i++) {
$cClass = "fSiteClass_";
$cClass .= $aRange[$i];
$cPath = $cClass . "_Path";
if (file_exists($this->$cClass)) {
$this->cPath = $this->$cClass;
$this->cClassPath = $this->$cPath;
return true;
}
}
//now do the hammer classes
for ($i = 0; $i < $iRange; $i++) {
$cClass = "fHammerClass_";
$cClass .= $aRange[$i];
$cPath = $cClass . "_Path";
if (file_exists($this->$cClass)) {
$this->cPath = $this->$cClass;
$this->cClassPath = $this->$cPath;
return true;
}
}
//Check the traits
if (file_exists($this->fSiteClass_Trait_a)) {
$this->cPath = $this->fSiteClass_Trait_a;
return true;
} else if (file_exists($this->fSiteClass_Trait_b)) {
$this->cPath = $this->fSiteClass_Trait_b;
return true;
} else if (file_exists($this->fHammerClass_Trait)) {
$this->cPath = $this->fHammerClass_Trait;
return true;
}
return false;
}
/**
* Screws::doInstall()
*
* @return null
*/
private function doInstall($cClass = false, $cPath = false) {
$aName = explode("_", $cClass);
$cLast = end($aName);
$iLength = strlen($cLast);
$cPathed = $cPath;
$cPath = substr($cPath, 0, -$iLength);
if (!$cPath) { $cPath = substr($cPathed, 0, -$iLength); }
$cInstallFile = $cPath . "/install/install.php";
$cInstallClass = $cClass . "_install";
$cInstallPath = $cPath . "/install/install.php";
$cChecker = $cPath . "/" . $cClass . "/install/install.php";
$cChecker2 = $cPathed . "/install/install.php";
$aDebug = array(
"Path" => $cPath,
"Install" => $cInstallFile,
"Class" => $cClass,
"Installer" => $cClass . "_install",
"Pathed" => $cPathed,
"InstallPath" => $cInstallPath,
"Name" => $aName,
"Last" => $cLast,
"Length" => $iLength,
"nLength" => -$iLength,
"checker" => $cChecker,
"checker2" => $cChecker2,
);
$oInstall = false;
if (file_exists($cInstallPath) || file_exists($cChecker) || file_exists($cChecker2)) {
if (file_exists($cChecker)) { //Checker 1
try {
$oNails = new Nails();
$oInstall = new $cInstallClass($oNails);
} catch (Exception $e) {
if (!strstr($e->getMessage(), "not found")) {
printRead($e->getMessage());
printRead($aDebug);
die();
}
}
} else if (file_exists($cChecker2)) { //Checker 2
try {
$oNails = new Nails();
$oInstall = new $cInstallClass($oNails);
} catch (Exception $e) {
if (!strstr($e->getMessage(), "not found")) {
printRead($e->getMessage());
printRead($aDebug);
die();
}
}
} else if (file_exists($cInstallPath)) { //not sure why it would get here
try {
$oNails = new Nails();
$oInstall = new $cInstallClass($oNails);
} catch (Exception $e) {
if (!strstr($e->getMessage(), "not found")) {
printRead($e->getMessage());
printRead($aDebug);
die();
}
}
}
}
}
}
//load the autoloader
$oLoader = new Screws();