/
base.php
executable file
·247 lines (228 loc) · 5.88 KB
/
base.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
<?php
/**
* Basic Prank functions.
*
* Shorthands and utilities for easying both internal workings of the framework
* and the userland code.
*
* @filesource
* @copyright Copyright (c) 2008-2014, Kamil "Brego" Dzieliński
* @license http://opensource.org/licenses/mit-license.php The MIT License
* @author Kamil "Brego" Dzieliński <brego@brego.dk>
* @link http://prank.brego.dk/ Prank's project page
* @link http://github.com/brego/prank/ Prank's Git repository
* @package Prank
* @subpackage Core
* @since Prank 0.10
* @version Prank 0.75
*/
/**
* Shortcut for dumping variables.
*
* Dumps variables on the screen, using var_dump, wrapped in <pre>. Accepts
* variable number of arguments, and treats them separatly.
*
* @return void
**/
function d() {
$args = func_get_args();
foreach ($args as $arg) {
echo "<pre>\n";
var_dump($arg);
echo "</pre>\n";
}
}
/**
* Deletes empty elements from an array.
*
* Alias for array_filter.
*
* @return array
* @param array $array Array to be cleaned
**/
function array_cleanup($array) {
return array_filter($array);
}
/**
* This function is deprecated.
*
* @param string $name
* @return mixed
* @deprecated
*/
function c($name = false) {
throw new DeprecatedException('c');
}
/**
* This function is deprecated.
*
* Use the square bracket notation instead.
*
* @return array
* @deprecated
*/
function a() {
throw new DeprecatedException('a');
}
/**
* Sorts an array and returns the result
*
* Parameter can be either an array, or multiple parameers that'll be converted
* to an array. Sorting will be performed using sort().
*
* @return array
*/
function s() {
$array = func_get_args();
if (count($array) == 1 && is_array($array[0])) {
$array = $array[0];
}
sort($array);
return $array;
}
/**
* Returns a given arguments separated by current directory separator
*
* This method accepts multiple rguments, and returns them glued together by
* current directory separator. If the first argument has the separator as it's
* last character, it'll be stripped (meaning it's ok to use Prank's leading
* '/' configuration variables, as long as they're used as the first argument).
*
* @return string
*/
function file_path() {
$files = func_get_args();
$ds = DIRECTORY_SEPARATOR;
if (substr($files[0], -1) === $ds) {
$files[0] = substr($files[0], 0, -1);
}
return implode($ds, $files);
}
/******************************************************************************
* YAML functions.
*
* Deprecated.
*****************************************************************************/
/**
* This function is deprecated.
*
* @param mixed $variable
* @return void
* @deprecated
*/
function to_yaml($variable) {
throw new DeprecatedException('to_yaml');
}
/**
* This function is deprecated.
*
* @param string $variable
* @param string $file
* @return void
* @deprecated
*/
function to_yaml_file($variable, $file) {
throw new DeprecatedException('to_yaml_file');
}
/**
* This function is deprecated.
*
* @param string $yaml
* @return void
* @deprecated
*/
function from_yaml($yaml) {
throw new DeprecatedException('from_yaml');
}
/**
* This function is deprecated.
*
* @param string $file
* @return void
* @deprecated
*/
function from_yaml_file($file) {
throw new DeprecatedException('from_yaml_file');
}
/******************************************************************************
* Textual helper functions.
*****************************************************************************/
/**
* Uppercases the string.
*
* @return string Uppercased string.
* @param string $string String to be uppercased.
**/
function up($string) {
return strtoupper($string);
}
/**
* Lowercases the string.
*
* @return string Lowercased string.
* @param string $string String to be lowercased.
**/
function down($string) {
return strtolower($string);
}
/******************************************************************************
* Basic action handling functions.
*****************************************************************************/
/**
* Checks if method exists in the controller class.
*
* @return boolean
* @param string $action Shortname of the action.
* @param string $controller Shortname of the controller.
**/
function is_action_of($action, $controller) {
return method_exists(to_controller($controller), $action);
}
/**
* Use this to require helpers
*
* This method takes multiple arguments, and they don't have to contain .php
* extension. A helper file can be included only once, so the method will
* simply ignore a second call with the same helper name. Helpers are searched
* for in app/helpers and core/helpers respectively.
*
* If the helper doesn't exist, will throw an Exception.
*
* @todo require_once needs to be changed to require! damn test suite!
* @return void
*/
function use_helper() {
$files = func_get_args();
$registry = Registry::instance();
if (isset($registry->helpers_loaded) === false) {
$registry->helpers_loaded = new stdClass;
}
foreach ($files as $file) {
if (substr($file, -4, 4) !== '.php') {
$file = $file.'.php';
}
if (isset($registry->helpers_loaded->$file) === false) {
$core_path = file_path($registry->config->core, 'helpers', $file);
$app_path = file_path($registry->config->app, 'helpers', $file);
if (is_file($app_path)) {
$registry->helpers_loaded->$file = true;
require_once $app_path;
} elseif (is_file($core_path)) {
$registry->helpers_loaded->$file = true;
require_once $core_path;
} else {
break;
throw new Exception('Helper '.$file.' not found in core or application helper directories.');
}
}
}
}
/**
* Deprecated Exception
*/
class DeprecatedException extends Exception {
public function __construct($method, $code = 0) {
parent::__construct("Method '$method' is deprecated.", $code);
}
}
?>