|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Yolk - Gamer Network's PHP Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2013 Gamer Network Ltd. |
|
6
|
|
|
* |
|
7
|
|
|
* Distributed under the MIT License, a copy of which is available in the |
|
8
|
|
|
* LICENSE file that was bundled with this package, or online at: |
|
9
|
|
|
* https://github.com/gamernetwork/yolk-core |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace yolk; |
|
13
|
|
|
|
|
14
|
|
|
// global helper functions (e.g. d() and dd()) |
|
15
|
|
|
require __DIR__.'/bootstrap.php'; |
|
16
|
|
|
|
|
17
|
|
|
Yolk::registerHelpers([ |
|
18
|
|
|
'yolk\\helpers\\ArrayHelper', |
|
19
|
|
|
'yolk\\helpers\\StringHelper', |
|
20
|
|
|
'yolk\\helpers\\DateTimeHelper', |
|
21
|
|
|
'yolk\\helpers\\Inflector', |
|
22
|
|
|
]); |
|
23
|
|
|
|
|
24
|
|
|
class Yolk { |
|
25
|
|
|
|
|
26
|
|
|
const DUMP_TEXT = 'text'; |
|
27
|
|
|
const DUMP_HTML = 'html'; |
|
28
|
|
|
const DUMP_TERMINAL = 'terminal'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The debug mode flag. |
|
32
|
|
|
* @var boolean |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $debug; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Current error handler. |
|
38
|
|
|
* @var callable |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static $error_handler = ['\\yolk\\exceptions\\Handler', 'error']; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Current exception handler. |
|
44
|
|
|
* @var callable |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static $exception_handler = ['\\yolk\\exceptions\\Handler', 'exception']; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The error page to display for production web apps. |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected static $error_page; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Array of helper methods accessable as static method on this class. |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected static $helpers = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Cannot be instantiated. |
|
62
|
|
|
*/ |
|
63
|
|
|
private function __construct() {} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Determines if this is a command-line environment. |
|
67
|
|
|
* @return boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function isCLI() { |
|
70
|
|
|
return defined('STDIN') && is_resource(STDIN) && (get_resource_type(STDIN) == 'stream'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Determines if debug mode is enabled. |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function isDebug() { |
|
78
|
|
|
return static::$debug; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Enables or disabled debug mode. |
|
83
|
|
|
* @param boolean $debug |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function setDebug( $debug = false ) { |
|
87
|
|
|
static::$debug = (bool) $debug; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Pretty-print a variable - if running in debug mode. |
|
92
|
|
|
* @param mixed $var |
|
93
|
|
|
* @param string $format one of the Yolk::DUMP_* constants |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function dump( $var, $format = null ) { |
|
97
|
|
|
|
|
98
|
|
|
if( !static::isDebug() ) |
|
99
|
|
|
return; |
|
100
|
|
|
|
|
101
|
|
|
$dumpers = [ |
|
102
|
|
|
static::DUMP_HTML => '\\yolk\\debug\\HTMLDumper', |
|
103
|
|
|
static::DUMP_TERMINAL => '\\yolk\\debug\\TerminalDumper', |
|
104
|
|
|
static::DUMP_TEXT => '\\yolk\\debug\\TextDumper', |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
// auto-detect format if unknown or none specified |
|
108
|
|
|
if( !isset($dumpers[$format]) ) |
|
109
|
|
|
$format = static::isCLI() ? static::DUMP_TERMINAL : static::DUMP_HTML; |
|
110
|
|
|
|
|
111
|
|
|
$dumpers[$format]::dump($var); |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Register one or multiple static helper classes. |
|
117
|
|
|
* Static methods defined within each class will become statically callable |
|
118
|
|
|
* via the Yolk object. |
|
119
|
|
|
* .e.g yolk\helpers\ArrayHelper::sum() -> yolk\Yolk::sum() |
|
120
|
|
|
* @param string|array $classes |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function registerHelpers( $classes ) { |
|
123
|
|
|
|
|
124
|
|
|
if( !is_array($classes) ) |
|
125
|
|
|
$classes = [$classes]; |
|
126
|
|
|
|
|
127
|
|
|
foreach( $classes as $class ) { |
|
128
|
|
|
|
|
129
|
|
|
$class = new \ReflectionClass($class); |
|
130
|
|
|
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_STATIC); |
|
131
|
|
|
|
|
132
|
|
|
foreach( $methods as $m ) { |
|
133
|
|
|
static::addHelperMethod($class->name, $m->name); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Register a single static method as a helper. |
|
142
|
|
|
* @param string $class |
|
143
|
|
|
* @param string $method |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function addHelperMethod( $class, $method ) { |
|
146
|
|
|
|
|
147
|
|
|
$k = strtolower($method); |
|
148
|
|
|
|
|
149
|
|
|
if( method_exists(__CLASS__, $method) ) |
|
150
|
|
|
throw new \Exception(sprintf("Helper methods cannot override pre-defined Yolk methods - '%s' is reserved", $method)); |
|
151
|
|
|
elseif( isset(static::$helpers[$k]) && static::$helpers[$k][0] != $class ) |
|
152
|
|
|
throw new \Exception(sprintf("Helper method '%s' already defined in class '%s', duplicate in '%s'", $method, static::$helpers[$k][0], $class)); |
|
153
|
|
|
|
|
154
|
|
|
static::$helpers[$k] = [$class, $method]; |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Executes the specified closure, wrapping it in Yolk's error and exception handling. |
|
160
|
|
|
* @param \Closure $callable |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function run( Callable $callable ) { |
|
164
|
|
|
|
|
165
|
|
|
try { |
|
166
|
|
|
|
|
167
|
|
|
// catch fatal errors |
|
168
|
|
|
register_shutdown_function(['\\yolk\\exceptions\\Handler', 'checkFatal']); |
|
169
|
|
|
|
|
170
|
|
|
// use our error handler |
|
171
|
|
|
$error_handler = set_error_handler(static::$error_handler); |
|
172
|
|
|
|
|
173
|
|
|
$args = func_get_args(); |
|
174
|
|
|
array_shift($args); |
|
175
|
|
|
|
|
176
|
|
|
$result = call_user_func_array($callable, $args); |
|
177
|
|
|
|
|
178
|
|
|
// restore the original error handler |
|
179
|
|
|
// if $error_handler is null then passing it to set_error_handler() will fail on PHP < v5.5 |
|
180
|
|
|
// so we create an empty error handler thereby causing PHP to run it's own handler. |
|
181
|
|
|
set_error_handler($error_handler ?: function() { return false; }); |
|
182
|
|
|
|
|
183
|
|
|
return $result; |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
catch( \Exception $e ) { |
|
187
|
|
|
static::exception($e); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Specifies the function to execute in the event of an error being triggered during a call to Yolk::run(). |
|
194
|
|
|
* @param callable $handler |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function setErrorHandler( callable $handler = null ) { |
|
198
|
|
|
static::$error_handler = $handler ?: ['\\yolk\\exceptions\\Handler', 'error']; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Specifies the function to execute in the event of an exception being thrown during a call to Yolk::run(). |
|
203
|
|
|
* @param callable $handler |
|
204
|
|
|
* @return void |
|
205
|
|
|
*/ |
|
206
|
|
|
public static function setExceptionHandler( callable $handler = null ) { |
|
207
|
|
|
static::$exception_handler = $handler ?: ['\\yolk\\exceptions\\Handler', 'exception']; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Call the registered exception handler. |
|
212
|
|
|
* @param \Exception $error |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
|
|
public static function exception( \Exception $error ) { |
|
216
|
|
|
return call_user_func( |
|
217
|
|
|
static::$exception_handler, |
|
218
|
|
|
$error, |
|
219
|
|
|
static::$error_page ?: __DIR__. '/exceptions/error.php' |
|
220
|
|
|
); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Specifies the error page to display in the event of an error/exception. |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $file |
|
227
|
|
|
* @return void |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function setErrorPage( $file ) { |
|
230
|
|
|
static::$error_page = (string) $file; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public static function __callStatic( $method, array $args = [] ) { |
|
234
|
|
|
|
|
235
|
|
|
$k = strtolower($method); |
|
236
|
|
|
|
|
237
|
|
|
if( !isset(static::$helpers[$k]) ) |
|
238
|
|
|
throw new \BadMethodCallException("Unknown helper method '$method'"); |
|
239
|
|
|
|
|
240
|
|
|
return call_user_func_array(static::$helpers[$k], $args); |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
// EOF |