/
test.php
103 lines (82 loc) · 2.37 KB
/
test.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
<?php namespace SciActive;
error_reporting(E_ALL);
// Test making the max depth huuuuge. (Bigger than PHP recursion limit.)
//define('REQUIREPHP_MAX_DEPTH', 600);
require("src/RequirePHP.php");
// Use test.
RequirePHP::_(array('test'), function($test){
$test->value = '<p>It works!!</p>';
});
// Define test.
RequirePHP::_('test', array('depend', 'depend3'), function($d, $d2){
if (!$d || !$d2) {
echo '<p>Depend isn\'t working!!</p>';
}
class test {
public $value;
public function talk() {
echo $this->value;
}
}
return new test();
});
// Define the dependencies of test.
RequirePHP::_('depend', array(), function(){
return true;
});
// Test aliases
RequirePHP::alias('depend3', 'depend2');
RequirePHP::_('depend2', array(), function(){
// This is used to check that dependencies are being loaded correctly.
//return false;
return true;
});
// Use test again.
RequirePHP::alias('toast', 'test');
RequirePHP::alias('taste', 'toast');
RequirePHP::_(array('taste'), function($test){
// Check the require('thing') syntax.
$d = RequirePHP::_('depend');
$d2 = RequirePHP::_('depend2');
if (!$d || !$d2) {
echo '<p>Depend isn\'t working!!</p>';
}
// This makes sure we're getting the same instance, and not a copy, of $test.
$test->talk();
});
// Use test outside of a closure.
RequirePHP::_('test')->talk();
// Let's test circular dependencies.
try {
RequirePHP::_('circ1', array('circ2'), function($circ2){
return;
});
RequirePHP::_('circ2', array('circ1'), function($circ1){
return;
});
RequirePHP::_(array('circ1'), function($circ1){
echo '<p>This shouldn\'t have run!!</p>';
});
} catch (RequireTooDeepException $e) {
RequirePHP::undef('circ1');
echo '<p>Circular dependencies don\'t crash the script!! Yay!! See the message: '.$e->getMessage().'</p>' ;
}
// Let's test alias and module removal.
RequirePHP::_('removemodule', array(), function(){
echo '<p>Uh oh. Module removal failed. :(</p>';
});
RequirePHP::alias('removealias', 'removemodule');
RequirePHP::undefAlias('removealias');
RequirePHP::_(array('removealias'), function(){
echo '<p>Uh oh. Alias removal failed. :(</p>';
});
$failed = false;
RequirePHP::undef('removemodule');
RequirePHP::_(array('removemodule'), function(){
global $failed;
$failed = true;
return;
});
if (!$failed) {
echo '<p>Looks like alias and module removal passed! :)</p>';
}