1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyAdmin\Plugins; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Loader |
7
|
|
|
* |
8
|
|
|
* Here are some of the regexes ive used to change code to using this class: |
9
|
|
|
* ('[^']*') *=> *('[^']*'), |
10
|
|
|
* $loader->add_requirement(\1, \2); |
11
|
|
|
* |
12
|
|
|
* @package MyAdmin |
13
|
|
|
*/ |
14
|
|
|
class Loader |
15
|
|
|
{ |
16
|
|
|
protected $requirements; |
17
|
|
|
protected $routes; |
18
|
|
|
protected $admin_routes; |
19
|
|
|
protected $public_routes; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Loader constructor. |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->requirements = []; |
27
|
|
|
$this->routes = []; |
28
|
|
|
$this->admin_routes = []; |
29
|
|
|
$this->public_routes = []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* gets the page routes |
34
|
|
|
* |
35
|
|
|
* @param bool $include_admin |
36
|
|
|
* @return array of routes |
37
|
|
|
*/ |
38
|
|
|
public function get_routes($include_admin = false) |
39
|
|
|
{ |
40
|
|
|
//if ($include_admin === FALSE && $GLOBALS['tf']->ima === 'admin') |
41
|
|
|
//$include_admin = TRUE; |
42
|
|
|
$routes = array_merge($this->public_routes, $this->routes); |
43
|
|
|
if ($include_admin === true) { |
44
|
|
|
$routes = array_merge($this->admin_routes, $routes); |
45
|
|
|
} |
46
|
|
|
uksort($routes, function ($a, $b) { |
47
|
|
|
if (strlen($a) == strlen($b)) { |
48
|
|
|
if ($a == $b) { |
49
|
|
|
return 0; |
50
|
|
|
} |
51
|
|
|
return ($a > $b) ? -1 : 1; |
52
|
|
|
} else { |
53
|
|
|
return (strlen($a) > strlen($b)) ? -1 : 1; |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
//myadmin_log('route', 'debug', json_encode($routes), __LINE__, __FILE__); |
57
|
|
|
return $routes; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* gets the admin page routes |
62
|
|
|
* |
63
|
|
|
* @return array of routes |
64
|
|
|
*/ |
65
|
|
|
public function get_admin_routes() |
66
|
|
|
{ |
67
|
|
|
return $this->admin_routes; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* gets the public page routes |
72
|
|
|
* |
73
|
|
|
* @return array of routes |
74
|
|
|
*/ |
75
|
|
|
public function get_public_routes() |
76
|
|
|
{ |
77
|
|
|
return $this->public_routes; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* returns the value of a setting |
82
|
|
|
* |
83
|
|
|
* @param string $setting |
84
|
|
|
* @return mixed the value of the setting |
85
|
|
|
*/ |
86
|
|
|
public function get_setting($setting) |
87
|
|
|
{ |
88
|
|
|
return constant($setting); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* adds a requirement into the loader and registers it as a page with the router |
93
|
|
|
* |
94
|
|
|
* @param string $function php function name or class.class_name |
95
|
|
|
* @param string $source php source file |
96
|
|
|
* @param string $namespace optional php namespace |
97
|
|
|
*/ |
98
|
|
|
public function add_page_requirement($function, $source, $namespace = '') |
99
|
|
|
{ |
100
|
|
|
$this->routes['/'.$function] = $namespace.$function; |
101
|
|
|
$this->routes['/admin/'.$function] = $namespace.$function; |
102
|
|
|
$this->add_requirement($function, $source, $namespace); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* adds a requirement into the loader and registers it as a page with the router |
107
|
|
|
* |
108
|
|
|
* @param string $function php function name or class.class_name |
109
|
|
|
* @param string $source php source file |
110
|
|
|
* @param string $namespace optional php namespace |
111
|
|
|
*/ |
112
|
|
|
public function add_root_page_requirement($function, $source, $namespace = '') |
113
|
|
|
{ |
114
|
|
|
$this->routes['/'.$function] = $namespace.$function; |
115
|
|
|
$this->add_requirement($function, $source, $namespace); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* adds a requirement into the loader and registers it as a page with the router |
120
|
|
|
* |
121
|
|
|
* @param string $function php function name or class.class_name |
122
|
|
|
* @param string $path source file path |
123
|
|
|
*/ |
124
|
|
|
public function add_public_path($page, $source) |
125
|
|
|
{ |
126
|
|
|
$this->public_routes['/'.$page] = $source; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* adds a requirement into the loader and registers it as a page with the router |
131
|
|
|
* |
132
|
|
|
* @param string $function php function name or class.class_name |
133
|
|
|
* @param string $source php source file |
134
|
|
|
* @param string $namespace optional php namespace |
135
|
|
|
*/ |
136
|
|
|
public function add_ajax_page_requirement($function, $source, $namespace = '') |
137
|
|
|
{ |
138
|
|
|
$this->routes['/ajax/'.$function] = $namespace.$function; |
139
|
|
|
$this->add_requirement($function, $source, $namespace); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* adds a requirement into the loader and registers it as a page with the router |
144
|
|
|
* |
145
|
|
|
* @param string $function php function name or class.class_name |
146
|
|
|
* @param string $source php source file |
147
|
|
|
* @param string $namespace optional php namespace |
148
|
|
|
*/ |
149
|
|
|
public function add_admin_page_requirement($function, $source, $namespace = '') |
150
|
|
|
{ |
151
|
|
|
$this->admin_routes['/admin/'.$function] = $namespace.$function; |
152
|
|
|
$this->add_requirement($function, $source, $namespace); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* adds a requirement into the loader |
157
|
|
|
* |
158
|
|
|
* @param string $function php function name or class.class_name |
159
|
|
|
* @param string $source php source file |
160
|
|
|
* @param string $namespace optional php namespace |
161
|
|
|
*/ |
162
|
|
|
public function add_requirement($function, $source, $namespace = '') |
163
|
|
|
{ |
164
|
|
|
$this->requirements[$function] = $namespace.$source; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* gets an array of requirements for loading |
169
|
|
|
* |
170
|
|
|
* @return array the array of requirements |
171
|
|
|
*/ |
172
|
|
|
public function get_requirements() |
173
|
|
|
{ |
174
|
|
|
return $this->requirements; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|