@@ -116,7 +116,7 @@ |
||
116 | 116 | /** |
117 | 117 | * Dispatch the router |
118 | 118 | * @param string[] $args The arguments array. |
119 | - * @return boolean True if route was correctly dispatched. |
|
119 | + * @return null|boolean True if route was correctly dispatched. |
|
120 | 120 | */ |
121 | 121 | public static function run($args=null){ |
122 | 122 | if($args) { |
@@ -102,15 +102,15 @@ discard block |
||
102 | 102 | * @return array The commands and their description. |
103 | 103 | */ |
104 | 104 | public static function commands(){ |
105 | - $results = []; |
|
106 | - foreach(static::$commands as $name => $cmd){ |
|
105 | + $results = []; |
|
106 | + foreach(static::$commands as $name => $cmd){ |
|
107 | 107 | $results[] = [ |
108 | 108 | 'name' => $name, |
109 | 109 | 'params' => preg_replace('/:(\w+)/','[$1]',implode(' ',$cmd[0])), |
110 | 110 | 'description' => $cmd[2], |
111 | 111 | ]; |
112 | - } |
|
113 | - return $results; |
|
112 | + } |
|
113 | + return $results; |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | /** |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | if($args) { |
123 | 123 | $_args = $args; |
124 | 124 | static::$file = basename(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:__FILE__); |
125 | - } else { |
|
125 | + } else { |
|
126 | 126 | $_args = $_SERVER['argv']; |
127 | 127 | static::$file = basename(array_shift($_args)); |
128 | 128 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | if ($segment[0]==':'){ |
144 | 144 | // Extract paramenter |
145 | 145 | if (isset(static::$arguments[$_idx])){ |
146 | - $pars_vector[] = static::$arguments[$_idx]; |
|
146 | + $pars_vector[] = static::$arguments[$_idx]; |
|
147 | 147 | } else return static::triggerError("Command [".$command."] needs more parameters"); |
148 | 148 | } else { |
149 | 149 | // Match command |
@@ -159,15 +159,15 @@ discard block |
||
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
162 | - /** |
|
163 | - * Prints a message to the console with color formatting. |
|
164 | - * @param string $message The html-like encoded message |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - public static function write($message){ |
|
162 | + /** |
|
163 | + * Prints a message to the console with color formatting. |
|
164 | + * @param string $message The html-like encoded message |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + public static function write($message){ |
|
168 | 168 | if( preg_match('~<[^>]+>~',$message)) { |
169 | - // Use preg_replace_callback for fast regex matches navigation |
|
170 | - preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
169 | + // Use preg_replace_callback for fast regex matches navigation |
|
170 | + preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
171 | 171 | static::write($m[1]); |
172 | 172 | $color = strtoupper(trim($m[2])); |
173 | 173 | if( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
@@ -177,37 +177,37 @@ discard block |
||
177 | 177 | $back_color = array_pop(static::$color_stack) ?: static::$color_stack[]='NORMAL'; |
178 | 178 | if( isset(static::$shell_colors[$back_color]) ) echo static::$shell_colors[$back_color]; |
179 | 179 | static::write($m[4]); |
180 | - },$message); |
|
180 | + },$message); |
|
181 | 181 | } else { |
182 | - echo $message; |
|
182 | + echo $message; |
|
183 | 183 | } |
184 | - } |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Like CLI::write, but appends a newline at the end. |
|
188 | - * @param string $message The html-like encoded message |
|
189 | - * @return void |
|
190 | - */ |
|
191 | - public static function writeln($message){ |
|
192 | - static::write($message . PHP_EOL); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Set output ANSI color |
|
197 | - * @param string $color The color name constant. |
|
198 | - * @return void |
|
199 | - */ |
|
200 | - public static function color($color){ |
|
201 | - if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Edit a temporary block of text with $EDITOR (or nano as fallback) |
|
206 | - * @param string $text The initial text of the document. |
|
207 | - * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
|
208 | - * @return string The edited contents |
|
209 | - */ |
|
210 | - public static function edit($text,$filename=''){ |
|
186 | + /** |
|
187 | + * Like CLI::write, but appends a newline at the end. |
|
188 | + * @param string $message The html-like encoded message |
|
189 | + * @return void |
|
190 | + */ |
|
191 | + public static function writeln($message){ |
|
192 | + static::write($message . PHP_EOL); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Set output ANSI color |
|
197 | + * @param string $color The color name constant. |
|
198 | + * @return void |
|
199 | + */ |
|
200 | + public static function color($color){ |
|
201 | + if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Edit a temporary block of text with $EDITOR (or nano as fallback) |
|
206 | + * @param string $text The initial text of the document. |
|
207 | + * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
|
208 | + * @return string The edited contents |
|
209 | + */ |
|
210 | + public static function edit($text,$filename=''){ |
|
211 | 211 | $EDITOR = getenv('EDITOR')?:'nano'; |
212 | 212 | $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename,'/','_'); |
213 | 213 | file_put_contents($tmp, $text); |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | // Standard Help Message |
225 | 225 | CLI::help(function(){ |
226 | 226 | echo 'Usage: ', CLI::name(),' [commands]', PHP_EOL, |
227 | - 'Commands:',PHP_EOL; |
|
227 | + 'Commands:',PHP_EOL; |
|
228 | 228 | foreach( CLI::commands() as $cmd ){ |
229 | 229 | echo "\t", $cmd['name'], ' ' ,$cmd['params'], PHP_EOL; |
230 | 230 | if($cmd['description']) |
@@ -21,18 +21,18 @@ discard block |
||
21 | 21 | protected static $error = null; |
22 | 22 | |
23 | 23 | protected static $shell_colors = [ |
24 | - 'BLACK' =>"\033[0;30m", 'DARKGRAY' =>"\033[1;30m", |
|
25 | - 'BLUE' =>"\033[0;34m", 'LIGHTBLUE' =>"\033[1;34m", |
|
26 | - 'GREEN' =>"\033[0;32m", 'LIGHTGREEN' =>"\033[1;32m", |
|
27 | - 'CYAN' =>"\033[0;36m", 'LIGHTCYAN' =>"\033[1;36m", |
|
28 | - 'RED' =>"\033[0;31m", 'LIGHTRED' =>"\033[1;31m", |
|
29 | - 'PURPLE' =>"\033[0;35m", 'LIGHTPURPLE' =>"\033[1;35m", |
|
30 | - 'BROWN' =>"\033[0;33m", 'YELLOW' =>"\033[1;33m", |
|
31 | - 'LIGHTGRAY' =>"\033[0;37m", 'WHITE' =>"\033[1;37m", |
|
32 | - 'NORMAL' =>"\033[0;37m", 'B' =>"\033[1m", |
|
33 | - 'ERROR' =>"\033[1;31m", 'INFO' =>"\033[0;36m", |
|
34 | - 'I' =>"\033[0;30;104m", 'IB' =>"\033[1;30;104m", |
|
35 | - 'U' =>"\033[4m", 'D' =>"\033[2m", |
|
24 | + 'BLACK' =>"\033[0;30m", 'DARKGRAY' =>"\033[1;30m", |
|
25 | + 'BLUE' =>"\033[0;34m", 'LIGHTBLUE' =>"\033[1;34m", |
|
26 | + 'GREEN' =>"\033[0;32m", 'LIGHTGREEN' =>"\033[1;32m", |
|
27 | + 'CYAN' =>"\033[0;36m", 'LIGHTCYAN' =>"\033[1;36m", |
|
28 | + 'RED' =>"\033[0;31m", 'LIGHTRED' =>"\033[1;31m", |
|
29 | + 'PURPLE' =>"\033[0;35m", 'LIGHTPURPLE' =>"\033[1;35m", |
|
30 | + 'BROWN' =>"\033[0;33m", 'YELLOW' =>"\033[1;33m", |
|
31 | + 'LIGHTGRAY' =>"\033[0;37m", 'WHITE' =>"\033[1;37m", |
|
32 | + 'NORMAL' =>"\033[0;37m", 'B' =>"\033[1m", |
|
33 | + 'ERROR' =>"\033[1;31m", 'INFO' =>"\033[0;36m", |
|
34 | + 'I' =>"\033[0;30;104m", 'IB' =>"\033[1;30;104m", |
|
35 | + 'U' =>"\033[4m", 'D' =>"\033[2m", |
|
36 | 36 | ]; |
37 | 37 | protected static $color_stack = ['NORMAL']; |
38 | 38 | |
@@ -42,16 +42,16 @@ discard block |
||
42 | 42 | * @param string $command The command route, use ":" before a parameter for extraction. |
43 | 43 | * @param callable $callback The callback to be binded to the route. |
44 | 44 | */ |
45 | - public static function on($command,callable $callback,$description=''){ |
|
46 | - $parts = preg_split('/\s+/',$command); |
|
47 | - static::$commands[array_shift($parts)] = [$parts,$callback,$description]; |
|
45 | + public static function on($command, callable $callback, $description = '') { |
|
46 | + $parts = preg_split('/\s+/', $command); |
|
47 | + static::$commands[array_shift($parts)] = [$parts, $callback, $description]; |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
51 | 51 | * Bind a callback to the "help" route. |
52 | 52 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
53 | 53 | */ |
54 | - public static function help(callable $callback = null){ |
|
54 | + public static function help(callable $callback = null) { |
|
55 | 55 | $callback |
56 | 56 | ? is_callable($callback) && static::$help = $callback |
57 | 57 | : static::$help && call_user_func(static::$help); |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * Bind a callback when an error occurs. |
62 | 62 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
63 | 63 | */ |
64 | - public static function error(callable $callback = null){ |
|
64 | + public static function error(callable $callback = null) { |
|
65 | 65 | $callback |
66 | 66 | ? is_callable($callback) && static::$error = $callback |
67 | 67 | : static::$error && call_user_func(static::$error); |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * Returns the script name. |
72 | 72 | * @return string |
73 | 73 | */ |
74 | - public static function name(){ |
|
74 | + public static function name() { |
|
75 | 75 | return static::$file; |
76 | 76 | } |
77 | 77 | |
@@ -79,8 +79,8 @@ discard block |
||
79 | 79 | * Triggers an error and exit |
80 | 80 | * @param string $message |
81 | 81 | */ |
82 | - protected static function triggerError($message){ |
|
83 | - is_callable(static::$error) && call_user_func(static::$error,$message); |
|
82 | + protected static function triggerError($message) { |
|
83 | + is_callable(static::$error) && call_user_func(static::$error, $message); |
|
84 | 84 | exit -1; |
85 | 85 | } |
86 | 86 | |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | * @param mixed $default The default value if parameter is omitted. (if a callable it will be evaluated) |
91 | 91 | * @return mixed |
92 | 92 | */ |
93 | - public static function input($key=null,$default=null){ |
|
94 | - return $key ? (isset(static::$options[$key]) ? static::$options[$key] : (is_callable($default)?call_user_func($default):$default)) : static::$options; |
|
93 | + public static function input($key = null, $default = null) { |
|
94 | + return $key ? (isset(static::$options[$key]) ? static::$options[$key] : (is_callable($default) ? call_user_func($default) : $default)) : static::$options; |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -101,12 +101,12 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @return array The commands and their description. |
103 | 103 | */ |
104 | - public static function commands(){ |
|
104 | + public static function commands() { |
|
105 | 105 | $results = []; |
106 | - foreach(static::$commands as $name => $cmd){ |
|
106 | + foreach (static::$commands as $name => $cmd) { |
|
107 | 107 | $results[] = [ |
108 | 108 | 'name' => $name, |
109 | - 'params' => preg_replace('/:(\w+)/','[$1]',implode(' ',$cmd[0])), |
|
109 | + 'params' => preg_replace('/:(\w+)/', '[$1]', implode(' ', $cmd[0])), |
|
110 | 110 | 'description' => $cmd[2], |
111 | 111 | ]; |
112 | 112 | } |
@@ -118,40 +118,40 @@ discard block |
||
118 | 118 | * @param string[] $args The arguments array. |
119 | 119 | * @return boolean True if route was correctly dispatched. |
120 | 120 | */ |
121 | - public static function run($args=null){ |
|
122 | - if($args) { |
|
121 | + public static function run($args = null) { |
|
122 | + if ($args) { |
|
123 | 123 | $_args = $args; |
124 | - static::$file = basename(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:__FILE__); |
|
124 | + static::$file = basename(isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : __FILE__); |
|
125 | 125 | } else { |
126 | 126 | $_args = $_SERVER['argv']; |
127 | 127 | static::$file = basename(array_shift($_args)); |
128 | 128 | } |
129 | - foreach($_args as $e) if(strpos($e,'-')===0) { |
|
130 | - $h = explode('=',$e); |
|
131 | - static::$options[ltrim(current($h),'-')] = isset($h[1])?$h[1]:true; |
|
129 | + foreach ($_args as $e) if (strpos($e, '-') === 0) { |
|
130 | + $h = explode('=', $e); |
|
131 | + static::$options[ltrim(current($h), '-')] = isset($h[1]) ? $h[1] : true; |
|
132 | 132 | } else { |
133 | 133 | static::$arguments[] = $e; |
134 | 134 | } |
135 | 135 | |
136 | - if(isset(static::$arguments[0])){ |
|
136 | + if (isset(static::$arguments[0])) { |
|
137 | 137 | $command = array_shift(static::$arguments); |
138 | 138 | if (empty(static::$commands[$command])) |
139 | 139 | return static::triggerError("Unknown command [".$command."]."); |
140 | 140 | $cmd = static::$commands[$command]; |
141 | 141 | $pars_vector = []; |
142 | 142 | foreach ($cmd[0] as $_idx => $segment) { |
143 | - if ($segment[0]==':'){ |
|
143 | + if ($segment[0] == ':') { |
|
144 | 144 | // Extract paramenter |
145 | - if (isset(static::$arguments[$_idx])){ |
|
145 | + if (isset(static::$arguments[$_idx])) { |
|
146 | 146 | $pars_vector[] = static::$arguments[$_idx]; |
147 | 147 | } else return static::triggerError("Command [".$command."] needs more parameters"); |
148 | 148 | } else { |
149 | 149 | // Match command |
150 | - if (empty(static::$arguments[$_idx]) || $segment!=static::$arguments[$_idx]) |
|
150 | + if (empty(static::$arguments[$_idx]) || $segment != static::$arguments[$_idx]) |
|
151 | 151 | return static::triggerError("Command [".$command."] is incomplete."); |
152 | 152 | } |
153 | 153 | } |
154 | - call_user_func_array($cmd[1],$pars_vector); |
|
154 | + call_user_func_array($cmd[1], $pars_vector); |
|
155 | 155 | return true; |
156 | 156 | } else { |
157 | 157 | static::help(); |
@@ -164,18 +164,18 @@ discard block |
||
164 | 164 | * @param string $message The html-like encoded message |
165 | 165 | * @return void |
166 | 166 | */ |
167 | - public static function write($message){ |
|
168 | - if( preg_match('~<[^>]+>~',$message)) { |
|
167 | + public static function write($message) { |
|
168 | + if (preg_match('~<[^>]+>~', $message)) { |
|
169 | 169 | // Use preg_replace_callback for fast regex matches navigation |
170 | - preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
170 | + preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm', function($m) { |
|
171 | 171 | static::write($m[1]); |
172 | 172 | $color = strtoupper(trim($m[2])); |
173 | - if( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
173 | + if (isset(static::$shell_colors[$color])) echo static::$shell_colors[$color]; |
|
174 | 174 | static::$color_stack[] = $color; |
175 | 175 | static::write($m[3]); |
176 | 176 | array_pop(static::$color_stack); |
177 | - $back_color = array_pop(static::$color_stack) ?: static::$color_stack[]='NORMAL'; |
|
178 | - if( isset(static::$shell_colors[$back_color]) ) echo static::$shell_colors[$back_color]; |
|
177 | + $back_color = array_pop(static::$color_stack) ?: static::$color_stack[] = 'NORMAL'; |
|
178 | + if (isset(static::$shell_colors[$back_color])) echo static::$shell_colors[$back_color]; |
|
179 | 179 | static::write($m[4]); |
180 | 180 | },$message); |
181 | 181 | } else { |
@@ -188,8 +188,8 @@ discard block |
||
188 | 188 | * @param string $message The html-like encoded message |
189 | 189 | * @return void |
190 | 190 | */ |
191 | - public static function writeln($message){ |
|
192 | - static::write($message . PHP_EOL); |
|
191 | + public static function writeln($message) { |
|
192 | + static::write($message.PHP_EOL); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | /** |
@@ -197,8 +197,8 @@ discard block |
||
197 | 197 | * @param string $color The color name constant. |
198 | 198 | * @return void |
199 | 199 | */ |
200 | - public static function color($color){ |
|
201 | - if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
200 | + public static function color($color) { |
|
201 | + if (isset(static::$shell_colors[$color])) echo static::$shell_colors[$color]; |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -207,9 +207,9 @@ discard block |
||
207 | 207 | * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
208 | 208 | * @return string The edited contents |
209 | 209 | */ |
210 | - public static function edit($text,$filename=''){ |
|
211 | - $EDITOR = getenv('EDITOR')?:'nano'; |
|
212 | - $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename,'/','_'); |
|
210 | + public static function edit($text, $filename = '') { |
|
211 | + $EDITOR = getenv('EDITOR') ?: 'nano'; |
|
212 | + $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename, '/', '_'); |
|
213 | 213 | file_put_contents($tmp, $text); |
214 | 214 | passthru("$EDITOR $tmp"); |
215 | 215 | $result = file_get_contents($tmp); |
@@ -222,17 +222,17 @@ discard block |
||
222 | 222 | |
223 | 223 | |
224 | 224 | // Standard Help Message |
225 | -CLI::help(function(){ |
|
226 | - echo 'Usage: ', CLI::name(),' [commands]', PHP_EOL, |
|
227 | - 'Commands:',PHP_EOL; |
|
228 | - foreach( CLI::commands() as $cmd ){ |
|
229 | - echo "\t", $cmd['name'], ' ' ,$cmd['params'], PHP_EOL; |
|
230 | - if($cmd['description']) |
|
231 | - echo "\t\t- ", str_replace("\n","\n\t\t ",$cmd['description']), PHP_EOL, PHP_EOL; |
|
225 | +CLI::help(function() { |
|
226 | + echo 'Usage: ', CLI::name(), ' [commands]', PHP_EOL, |
|
227 | + 'Commands:', PHP_EOL; |
|
228 | + foreach (CLI::commands() as $cmd) { |
|
229 | + echo "\t", $cmd['name'], ' ', $cmd['params'], PHP_EOL; |
|
230 | + if ($cmd['description']) |
|
231 | + echo "\t\t- ", str_replace("\n", "\n\t\t ", $cmd['description']), PHP_EOL, PHP_EOL; |
|
232 | 232 | } |
233 | 233 | }); |
234 | 234 | |
235 | 235 | // Standard Error Message |
236 | -CLI::error(function($message){ |
|
237 | - echo 'Error: ',$message,PHP_EOL; |
|
236 | +CLI::error(function($message) { |
|
237 | + echo 'Error: ', $message, PHP_EOL; |
|
238 | 238 | }); |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | * @param string $command The command route, use ":" before a parameter for extraction. |
43 | 43 | * @param callable $callback The callback to be binded to the route. |
44 | 44 | */ |
45 | - public static function on($command,callable $callback,$description=''){ |
|
45 | + public static function on($command,callable $callback,$description='') { |
|
46 | 46 | $parts = preg_split('/\s+/',$command); |
47 | 47 | static::$commands[array_shift($parts)] = [$parts,$callback,$description]; |
48 | 48 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * Bind a callback to the "help" route. |
52 | 52 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
53 | 53 | */ |
54 | - public static function help(callable $callback = null){ |
|
54 | + public static function help(callable $callback = null) { |
|
55 | 55 | $callback |
56 | 56 | ? is_callable($callback) && static::$help = $callback |
57 | 57 | : static::$help && call_user_func(static::$help); |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * Bind a callback when an error occurs. |
62 | 62 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
63 | 63 | */ |
64 | - public static function error(callable $callback = null){ |
|
64 | + public static function error(callable $callback = null) { |
|
65 | 65 | $callback |
66 | 66 | ? is_callable($callback) && static::$error = $callback |
67 | 67 | : static::$error && call_user_func(static::$error); |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * Returns the script name. |
72 | 72 | * @return string |
73 | 73 | */ |
74 | - public static function name(){ |
|
74 | + public static function name() { |
|
75 | 75 | return static::$file; |
76 | 76 | } |
77 | 77 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * Triggers an error and exit |
80 | 80 | * @param string $message |
81 | 81 | */ |
82 | - protected static function triggerError($message){ |
|
82 | + protected static function triggerError($message) { |
|
83 | 83 | is_callable(static::$error) && call_user_func(static::$error,$message); |
84 | 84 | exit -1; |
85 | 85 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @param mixed $default The default value if parameter is omitted. (if a callable it will be evaluated) |
91 | 91 | * @return mixed |
92 | 92 | */ |
93 | - public static function input($key=null,$default=null){ |
|
93 | + public static function input($key=null,$default=null) { |
|
94 | 94 | return $key ? (isset(static::$options[$key]) ? static::$options[$key] : (is_callable($default)?call_user_func($default):$default)) : static::$options; |
95 | 95 | } |
96 | 96 | |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @return array The commands and their description. |
103 | 103 | */ |
104 | - public static function commands(){ |
|
104 | + public static function commands() { |
|
105 | 105 | $results = []; |
106 | - foreach(static::$commands as $name => $cmd){ |
|
106 | + foreach(static::$commands as $name => $cmd) { |
|
107 | 107 | $results[] = [ |
108 | 108 | 'name' => $name, |
109 | 109 | 'params' => preg_replace('/:(\w+)/','[$1]',implode(' ',$cmd[0])), |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @param string[] $args The arguments array. |
119 | 119 | * @return boolean True if route was correctly dispatched. |
120 | 120 | */ |
121 | - public static function run($args=null){ |
|
121 | + public static function run($args=null) { |
|
122 | 122 | if($args) { |
123 | 123 | $_args = $args; |
124 | 124 | static::$file = basename(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:__FILE__); |
@@ -133,22 +133,26 @@ discard block |
||
133 | 133 | static::$arguments[] = $e; |
134 | 134 | } |
135 | 135 | |
136 | - if(isset(static::$arguments[0])){ |
|
136 | + if(isset(static::$arguments[0])) { |
|
137 | 137 | $command = array_shift(static::$arguments); |
138 | - if (empty(static::$commands[$command])) |
|
139 | - return static::triggerError("Unknown command [".$command."]."); |
|
138 | + if (empty(static::$commands[$command])) { |
|
139 | + return static::triggerError("Unknown command [".$command."]."); |
|
140 | + } |
|
140 | 141 | $cmd = static::$commands[$command]; |
141 | 142 | $pars_vector = []; |
142 | 143 | foreach ($cmd[0] as $_idx => $segment) { |
143 | - if ($segment[0]==':'){ |
|
144 | + if ($segment[0]==':') { |
|
144 | 145 | // Extract paramenter |
145 | - if (isset(static::$arguments[$_idx])){ |
|
146 | + if (isset(static::$arguments[$_idx])) { |
|
146 | 147 | $pars_vector[] = static::$arguments[$_idx]; |
147 | - } else return static::triggerError("Command [".$command."] needs more parameters"); |
|
148 | + } else { |
|
149 | + return static::triggerError("Command [".$command."] needs more parameters"); |
|
150 | + } |
|
148 | 151 | } else { |
149 | 152 | // Match command |
150 | - if (empty(static::$arguments[$_idx]) || $segment!=static::$arguments[$_idx]) |
|
151 | - return static::triggerError("Command [".$command."] is incomplete."); |
|
153 | + if (empty(static::$arguments[$_idx]) || $segment!=static::$arguments[$_idx]) { |
|
154 | + return static::triggerError("Command [".$command."] is incomplete."); |
|
155 | + } |
|
152 | 156 | } |
153 | 157 | } |
154 | 158 | call_user_func_array($cmd[1],$pars_vector); |
@@ -164,18 +168,22 @@ discard block |
||
164 | 168 | * @param string $message The html-like encoded message |
165 | 169 | * @return void |
166 | 170 | */ |
167 | - public static function write($message){ |
|
171 | + public static function write($message) { |
|
168 | 172 | if( preg_match('~<[^>]+>~',$message)) { |
169 | 173 | // Use preg_replace_callback for fast regex matches navigation |
170 | - preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
174 | + preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m) { |
|
171 | 175 | static::write($m[1]); |
172 | 176 | $color = strtoupper(trim($m[2])); |
173 | - if( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
177 | + if( isset(static::$shell_colors[$color]) ) { |
|
178 | + echo static::$shell_colors[$color]; |
|
179 | + } |
|
174 | 180 | static::$color_stack[] = $color; |
175 | 181 | static::write($m[3]); |
176 | 182 | array_pop(static::$color_stack); |
177 | 183 | $back_color = array_pop(static::$color_stack) ?: static::$color_stack[]='NORMAL'; |
178 | - if( isset(static::$shell_colors[$back_color]) ) echo static::$shell_colors[$back_color]; |
|
184 | + if( isset(static::$shell_colors[$back_color]) ) { |
|
185 | + echo static::$shell_colors[$back_color]; |
|
186 | + } |
|
179 | 187 | static::write($m[4]); |
180 | 188 | },$message); |
181 | 189 | } else { |
@@ -188,7 +196,7 @@ discard block |
||
188 | 196 | * @param string $message The html-like encoded message |
189 | 197 | * @return void |
190 | 198 | */ |
191 | - public static function writeln($message){ |
|
199 | + public static function writeln($message) { |
|
192 | 200 | static::write($message . PHP_EOL); |
193 | 201 | } |
194 | 202 | |
@@ -197,8 +205,10 @@ discard block |
||
197 | 205 | * @param string $color The color name constant. |
198 | 206 | * @return void |
199 | 207 | */ |
200 | - public static function color($color){ |
|
201 | - if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
208 | + public static function color($color) { |
|
209 | + if ( isset(static::$shell_colors[$color]) ) { |
|
210 | + echo static::$shell_colors[$color]; |
|
211 | + } |
|
202 | 212 | } |
203 | 213 | |
204 | 214 | /** |
@@ -207,7 +217,7 @@ discard block |
||
207 | 217 | * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
208 | 218 | * @return string The edited contents |
209 | 219 | */ |
210 | - public static function edit($text,$filename=''){ |
|
220 | + public static function edit($text,$filename='') { |
|
211 | 221 | $EDITOR = getenv('EDITOR')?:'nano'; |
212 | 222 | $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename,'/','_'); |
213 | 223 | file_put_contents($tmp, $text); |
@@ -222,17 +232,18 @@ discard block |
||
222 | 232 | |
223 | 233 | |
224 | 234 | // Standard Help Message |
225 | -CLI::help(function(){ |
|
235 | +CLI::help(function() { |
|
226 | 236 | echo 'Usage: ', CLI::name(),' [commands]', PHP_EOL, |
227 | 237 | 'Commands:',PHP_EOL; |
228 | - foreach( CLI::commands() as $cmd ){ |
|
238 | + foreach( CLI::commands() as $cmd ) { |
|
229 | 239 | echo "\t", $cmd['name'], ' ' ,$cmd['params'], PHP_EOL; |
230 | - if($cmd['description']) |
|
231 | - echo "\t\t- ", str_replace("\n","\n\t\t ",$cmd['description']), PHP_EOL, PHP_EOL; |
|
240 | + if($cmd['description']) { |
|
241 | + echo "\t\t- ", str_replace("\n","\n\t\t ",$cmd['description']), PHP_EOL, PHP_EOL; |
|
242 | + } |
|
232 | 243 | } |
233 | 244 | }); |
234 | 245 | |
235 | 246 | // Standard Error Message |
236 | -CLI::error(function($message){ |
|
247 | +CLI::error(function($message) { |
|
237 | 248 | echo 'Error: ',$message,PHP_EOL; |
238 | 249 | }); |
@@ -57,6 +57,9 @@ |
||
57 | 57 | return true; |
58 | 58 | } |
59 | 59 | |
60 | + /** |
|
61 | + * @param ErrorException $e |
|
62 | + */ |
|
60 | 63 | public static function traceException($e){ |
61 | 64 | switch(self::$mode){ |
62 | 65 | case self::HTML : |
@@ -20,20 +20,20 @@ discard block |
||
20 | 20 | |
21 | 21 | static $mode = self::SILENT; |
22 | 22 | |
23 | - public static function capture($tracing_level=null){ |
|
24 | - if($tracing_level!==null) error_reporting($tracing_level); |
|
23 | + public static function capture($tracing_level = null) { |
|
24 | + if ($tracing_level !== null) error_reporting($tracing_level); |
|
25 | 25 | set_error_handler(__CLASS__.'::traceError'); |
26 | 26 | set_exception_handler(__CLASS__.'::traceException'); |
27 | 27 | } |
28 | 28 | |
29 | - public static function mode($mode=null){ |
|
30 | - return $mode ? self::$mode=$mode : self::$mode; |
|
29 | + public static function mode($mode = null) { |
|
30 | + return $mode ? self::$mode = $mode : self::$mode; |
|
31 | 31 | } |
32 | 32 | |
33 | - public static function traceError($errno,$errstr,$errfile=null,$errline=null){ |
|
33 | + public static function traceError($errno, $errstr, $errfile = null, $errline = null) { |
|
34 | 34 | // This error code is not included in error_reporting |
35 | 35 | if (!(error_reporting() & $errno)) return; |
36 | - switch ( $errno ) { |
|
36 | + switch ($errno) { |
|
37 | 37 | case E_USER_ERROR: |
38 | 38 | $type = 'Fatal'; |
39 | 39 | break; |
@@ -51,16 +51,16 @@ discard block |
||
51 | 51 | break; |
52 | 52 | } |
53 | 53 | $e = new \ErrorException($type.': '.$errstr, 0, $errno, $errfile, $errline); |
54 | - $chk_specific = array_filter((array)Event::trigger('core.error.'.strtolower($type),$e)); |
|
55 | - $chk_general = array_filter((array)Event::trigger('core.error',$e)); |
|
56 | - if (! ($chk_specific || $chk_general) ) static::traceException($e); |
|
54 | + $chk_specific = array_filter((array) Event::trigger('core.error.'.strtolower($type), $e)); |
|
55 | + $chk_general = array_filter((array) Event::trigger('core.error', $e)); |
|
56 | + if (!($chk_specific || $chk_general)) static::traceException($e); |
|
57 | 57 | return true; |
58 | 58 | } |
59 | 59 | |
60 | - public static function traceException($e){ |
|
61 | - switch(self::$mode){ |
|
60 | + public static function traceException($e) { |
|
61 | + switch (self::$mode) { |
|
62 | 62 | case self::HTML : |
63 | - echo '<pre class="app error"><code>',$e->getMessage(),'</code></pre>',PHP_EOL; |
|
63 | + echo '<pre class="app error"><code>', $e->getMessage(), '</code></pre>', PHP_EOL; |
|
64 | 64 | break; |
65 | 65 | case self::JSON : |
66 | 66 | echo json_encode(['error' => $e->getMessage()]); |
@@ -70,26 +70,26 @@ discard block |
||
70 | 70 | break; |
71 | 71 | case self::SIMPLE : |
72 | 72 | default: |
73 | - echo $e->getMessage(),PHP_EOL; |
|
73 | + echo $e->getMessage(), PHP_EOL; |
|
74 | 74 | break; |
75 | 75 | } |
76 | 76 | return true; |
77 | 77 | } |
78 | 78 | |
79 | - public static function onFatal(callable $listener){ |
|
80 | - Event::on('core.error.fatal',$listener); |
|
79 | + public static function onFatal(callable $listener) { |
|
80 | + Event::on('core.error.fatal', $listener); |
|
81 | 81 | } |
82 | 82 | |
83 | - public static function onWarning(callable $listener){ |
|
84 | - Event::on('core.error.warning',$listener); |
|
83 | + public static function onWarning(callable $listener) { |
|
84 | + Event::on('core.error.warning', $listener); |
|
85 | 85 | } |
86 | 86 | |
87 | - public static function onNotice(callable $listener){ |
|
88 | - Event::on('core.error.notice',$listener); |
|
87 | + public static function onNotice(callable $listener) { |
|
88 | + Event::on('core.error.notice', $listener); |
|
89 | 89 | } |
90 | 90 | |
91 | - public static function onAny(callable $listener){ |
|
92 | - Event::on('core.error',$listener); |
|
91 | + public static function onAny(callable $listener) { |
|
92 | + Event::on('core.error', $listener); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | } |
@@ -20,19 +20,23 @@ discard block |
||
20 | 20 | |
21 | 21 | static $mode = self::SILENT; |
22 | 22 | |
23 | - public static function capture($tracing_level=null){ |
|
24 | - if($tracing_level!==null) error_reporting($tracing_level); |
|
23 | + public static function capture($tracing_level=null) { |
|
24 | + if($tracing_level!==null) { |
|
25 | + error_reporting($tracing_level); |
|
26 | + } |
|
25 | 27 | set_error_handler(__CLASS__.'::traceError'); |
26 | 28 | set_exception_handler(__CLASS__.'::traceException'); |
27 | 29 | } |
28 | 30 | |
29 | - public static function mode($mode=null){ |
|
31 | + public static function mode($mode=null) { |
|
30 | 32 | return $mode ? self::$mode=$mode : self::$mode; |
31 | 33 | } |
32 | 34 | |
33 | - public static function traceError($errno,$errstr,$errfile=null,$errline=null){ |
|
35 | + public static function traceError($errno,$errstr,$errfile=null,$errline=null) { |
|
34 | 36 | // This error code is not included in error_reporting |
35 | - if (!(error_reporting() & $errno)) return; |
|
37 | + if (!(error_reporting() & $errno)) { |
|
38 | + return; |
|
39 | + } |
|
36 | 40 | switch ( $errno ) { |
37 | 41 | case E_USER_ERROR: |
38 | 42 | $type = 'Fatal'; |
@@ -53,12 +57,14 @@ discard block |
||
53 | 57 | $e = new \ErrorException($type.': '.$errstr, 0, $errno, $errfile, $errline); |
54 | 58 | $chk_specific = array_filter((array)Event::trigger('core.error.'.strtolower($type),$e)); |
55 | 59 | $chk_general = array_filter((array)Event::trigger('core.error',$e)); |
56 | - if (! ($chk_specific || $chk_general) ) static::traceException($e); |
|
60 | + if (! ($chk_specific || $chk_general) ) { |
|
61 | + static::traceException($e); |
|
62 | + } |
|
57 | 63 | return true; |
58 | 64 | } |
59 | 65 | |
60 | - public static function traceException($e){ |
|
61 | - switch(self::$mode){ |
|
66 | + public static function traceException($e) { |
|
67 | + switch(self::$mode) { |
|
62 | 68 | case self::HTML : |
63 | 69 | echo '<pre class="app error"><code>',$e->getMessage(),'</code></pre>',PHP_EOL; |
64 | 70 | break; |
@@ -76,19 +82,19 @@ discard block |
||
76 | 82 | return true; |
77 | 83 | } |
78 | 84 | |
79 | - public static function onFatal(callable $listener){ |
|
85 | + public static function onFatal(callable $listener) { |
|
80 | 86 | Event::on('core.error.fatal',$listener); |
81 | 87 | } |
82 | 88 | |
83 | - public static function onWarning(callable $listener){ |
|
89 | + public static function onWarning(callable $listener) { |
|
84 | 90 | Event::on('core.error.warning',$listener); |
85 | 91 | } |
86 | 92 | |
87 | - public static function onNotice(callable $listener){ |
|
93 | + public static function onNotice(callable $listener) { |
|
88 | 94 | Event::on('core.error.notice',$listener); |
89 | 95 | } |
90 | 96 | |
91 | - public static function onAny(callable $listener){ |
|
97 | + public static function onAny(callable $listener) { |
|
92 | 98 | Event::on('core.error',$listener); |
93 | 99 | } |
94 | 100 |
@@ -27,6 +27,9 @@ |
||
27 | 27 | return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
28 | 28 | } |
29 | 29 | |
30 | + /** |
|
31 | + * @param string|false $data |
|
32 | + */ |
|
30 | 33 | public function write($path, $data){ |
31 | 34 | $r_path = $this->realPath($path); |
32 | 35 | if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
@@ -16,42 +16,42 @@ discard block |
||
16 | 16 | protected $root; |
17 | 17 | |
18 | 18 | public function __construct(array $options = []) { |
19 | - $this->root = empty($options['root'])?'/':(rtrim($options['root'],'/').'/'); |
|
19 | + $this->root = empty($options['root']) ? '/' : (rtrim($options['root'], '/').'/'); |
|
20 | 20 | } |
21 | 21 | |
22 | - public function exists($path){ |
|
22 | + public function exists($path) { |
|
23 | 23 | return file_exists($this->realPath($path)); |
24 | 24 | } |
25 | 25 | |
26 | - public function read($path){ |
|
26 | + public function read($path) { |
|
27 | 27 | return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
28 | 28 | } |
29 | 29 | |
30 | - public function write($path, $data){ |
|
30 | + public function write($path, $data) { |
|
31 | 31 | $r_path = $this->realPath($path); |
32 | - if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
|
32 | + if (!is_dir($r_dir = dirname($r_path))) @mkdir($r_dir, 0775, true); |
|
33 | 33 | return file_put_contents($r_path, $data); |
34 | 34 | } |
35 | 35 | |
36 | - public function append($path, $data){ |
|
36 | + public function append($path, $data) { |
|
37 | 37 | return file_put_contents($this->realPath($path), $data, FILE_APPEND); |
38 | 38 | } |
39 | 39 | |
40 | - public function move($old, $new){ |
|
40 | + public function move($old, $new) { |
|
41 | 41 | // Atomic |
42 | - if($this->exists($old)){ |
|
43 | - return $this->write($new,$this->read($old)) && $this->delete($old); |
|
42 | + if ($this->exists($old)) { |
|
43 | + return $this->write($new, $this->read($old)) && $this->delete($old); |
|
44 | 44 | } else return false; |
45 | 45 | } |
46 | 46 | |
47 | - public function delete($path){ |
|
47 | + public function delete($path) { |
|
48 | 48 | return $this->exists($path) ? unlink($this->realPath($path)) : false; |
49 | 49 | } |
50 | 50 | |
51 | - public function search($pattern, $recursive=true){ |
|
51 | + public function search($pattern, $recursive = true) { |
|
52 | 52 | $results = []; |
53 | 53 | $root_len = strlen($this->root); |
54 | - $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
|
54 | + $rx_pattern = '('.strtr($pattern, ['.'=>'\.', '*'=>'.*', '?'=>'.']).')Ai'; |
|
55 | 55 | |
56 | 56 | /* |
57 | 57 | $files = new \RegexIterator(new \RecursiveDirectoryIterator($this->root, |
@@ -67,12 +67,12 @@ discard block |
||
67 | 67 | new \RecursiveDirectoryIterator( |
68 | 68 | $this->root, |
69 | 69 | \RecursiveDirectoryIterator::SKIP_DOTS)), |
70 | - $rx_pattern,\RegexIterator::GET_MATCH); |
|
70 | + $rx_pattern, \RegexIterator::GET_MATCH); |
|
71 | 71 | |
72 | 72 | $fileList = []; |
73 | - foreach($tree as $group) { |
|
74 | - foreach($group as $path) { |
|
75 | - $results[] = trim(substr($path, $root_len),'/'); |
|
73 | + foreach ($tree as $group) { |
|
74 | + foreach ($group as $path) { |
|
75 | + $results[] = trim(substr($path, $root_len), '/'); |
|
76 | 76 | } |
77 | 77 | } |
78 | 78 | |
@@ -80,8 +80,8 @@ discard block |
||
80 | 80 | |
81 | 81 | } |
82 | 82 | |
83 | - protected function realPath($path){ |
|
84 | - return $this->root . $path; |
|
83 | + protected function realPath($path) { |
|
84 | + return $this->root.$path; |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | } |
@@ -19,36 +19,40 @@ discard block |
||
19 | 19 | $this->root = empty($options['root'])?'/':(rtrim($options['root'],'/').'/'); |
20 | 20 | } |
21 | 21 | |
22 | - public function exists($path){ |
|
22 | + public function exists($path) { |
|
23 | 23 | return file_exists($this->realPath($path)); |
24 | 24 | } |
25 | 25 | |
26 | - public function read($path){ |
|
26 | + public function read($path) { |
|
27 | 27 | return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
28 | 28 | } |
29 | 29 | |
30 | - public function write($path, $data){ |
|
30 | + public function write($path, $data) { |
|
31 | 31 | $r_path = $this->realPath($path); |
32 | - if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
|
32 | + if ( ! is_dir($r_dir = dirname($r_path)) ) { |
|
33 | + @mkdir($r_dir,0775,true); |
|
34 | + } |
|
33 | 35 | return file_put_contents($r_path, $data); |
34 | 36 | } |
35 | 37 | |
36 | - public function append($path, $data){ |
|
38 | + public function append($path, $data) { |
|
37 | 39 | return file_put_contents($this->realPath($path), $data, FILE_APPEND); |
38 | 40 | } |
39 | 41 | |
40 | - public function move($old, $new){ |
|
42 | + public function move($old, $new) { |
|
41 | 43 | // Atomic |
42 | - if($this->exists($old)){ |
|
44 | + if($this->exists($old)) { |
|
43 | 45 | return $this->write($new,$this->read($old)) && $this->delete($old); |
44 | - } else return false; |
|
46 | + } else { |
|
47 | + return false; |
|
48 | + } |
|
45 | 49 | } |
46 | 50 | |
47 | - public function delete($path){ |
|
51 | + public function delete($path) { |
|
48 | 52 | return $this->exists($path) ? unlink($this->realPath($path)) : false; |
49 | 53 | } |
50 | 54 | |
51 | - public function search($pattern, $recursive=true){ |
|
55 | + public function search($pattern, $recursive=true) { |
|
52 | 56 | $results = []; |
53 | 57 | $root_len = strlen($this->root); |
54 | 58 | $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
@@ -80,7 +84,7 @@ discard block |
||
80 | 84 | |
81 | 85 | } |
82 | 86 | |
83 | - protected function realPath($path){ |
|
87 | + protected function realPath($path) { |
|
84 | 88 | return $this->root . $path; |
85 | 89 | } |
86 | 90 |
@@ -198,7 +198,6 @@ discard block |
||
198 | 198 | |
199 | 199 | /** |
200 | 200 | * Bind a middleware callback to invoked before the route definition |
201 | - * @param callable $before The callback to be invoked ($this is binded to the route object). |
|
202 | 201 | * @return Route |
203 | 202 | */ |
204 | 203 | public function & before($callback){ |
@@ -318,7 +317,7 @@ discard block |
||
318 | 317 | * @param string $pattern The URL schema with the named parameters |
319 | 318 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
320 | 319 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
321 | - * @return array The extracted variables |
|
320 | + * @return callable The extracted variables |
|
322 | 321 | */ |
323 | 322 | protected static function extractVariablesFromURL($pattern,$URL=null,$cut=false){ |
324 | 323 | $URL = $URL ?: Request::URI(); |
@@ -14,9 +14,9 @@ discard block |
||
14 | 14 | use Module; |
15 | 15 | |
16 | 16 | protected static $routes, |
17 | - $base = '', |
|
18 | - $prefix = [], |
|
19 | - $group = []; |
|
17 | + $base = '', |
|
18 | + $prefix = [], |
|
19 | + $group = []; |
|
20 | 20 | |
21 | 21 | protected $URLPattern = '', |
22 | 22 | $pattern = '', |
@@ -85,20 +85,20 @@ discard block |
||
85 | 85 | $method = strtolower($method); |
86 | 86 | $this->response = ''; |
87 | 87 | $this->response_object = null; |
88 | - $this->response_is_object = false; |
|
88 | + $this->response_is_object = false; |
|
89 | 89 | |
90 | 90 | // Call direct befores |
91 | 91 | if ( $this->befores ) { |
92 | 92 | // Reverse befores order |
93 | 93 | foreach (array_reverse($this->befores) as $mw) { |
94 | - ob_start(); |
|
94 | + ob_start(); |
|
95 | 95 | $mw_result = call_user_func($mw->bindTo($this)); |
96 | - $this->response .= ob_get_clean(); |
|
96 | + $this->response .= ob_get_clean(); |
|
97 | 97 | if ( false === $mw_result ) { |
98 | - return ['']; |
|
98 | + return ['']; |
|
99 | 99 | } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
100 | 100 | $this->response .= (string)$mw_result; |
101 | - } |
|
101 | + } |
|
102 | 102 | } |
103 | 103 | } |
104 | 104 | |
@@ -109,51 +109,51 @@ discard block |
||
109 | 109 | |
110 | 110 | if (is_callable($callback)) { |
111 | 111 | // Capure callback output |
112 | - Response::type(Response::TYPE_HTML); |
|
113 | - ob_start(); |
|
114 | - // Silence "Cannot bind an instance to a static closure" warnings |
|
115 | - $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
116 | - $this->response .= ob_get_clean(); |
|
117 | - |
|
118 | - // Render View if returned, else echo string or encode json response |
|
119 | - if ( null !== $view_results ) { |
|
120 | - if (is_a($view_results,'View') || is_string($view_results)) { |
|
121 | - $this->response .= (string)$view_results; |
|
122 | - } else { |
|
123 | - $this->response_is_object = true; |
|
124 | - $this->response_object = $view_results; |
|
125 | - } |
|
126 | - } |
|
112 | + Response::type(Response::TYPE_HTML); |
|
113 | + ob_start(); |
|
114 | + // Silence "Cannot bind an instance to a static closure" warnings |
|
115 | + $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
116 | + $this->response .= ob_get_clean(); |
|
117 | + |
|
118 | + // Render View if returned, else echo string or encode json response |
|
119 | + if ( null !== $view_results ) { |
|
120 | + if (is_a($view_results,'View') || is_string($view_results)) { |
|
121 | + $this->response .= (string)$view_results; |
|
122 | + } else { |
|
123 | + $this->response_is_object = true; |
|
124 | + $this->response_object = $view_results; |
|
125 | + } |
|
126 | + } |
|
127 | 127 | |
128 | 128 | } else if (is_a($callback,'View') || is_string($callback)) { |
129 | 129 | // return rendered View or direct string |
130 | - $this->response .= (string)$callback; |
|
130 | + $this->response .= (string)$callback; |
|
131 | 131 | } else { |
132 | 132 | // JSON encode returned value |
133 | - $this->response_is_object = true; |
|
134 | - $this->response_object = $callback; |
|
133 | + $this->response_is_object = true; |
|
134 | + $this->response_object = $callback; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | // Apply afters |
138 | 138 | if ( $this->afters ) { |
139 | 139 | foreach ($this->afters as $mw) { |
140 | - ob_start(); |
|
140 | + ob_start(); |
|
141 | 141 | $mw_result = call_user_func($mw->bindTo($this)); |
142 | - $this->response .= ob_get_clean(); |
|
142 | + $this->response .= ob_get_clean(); |
|
143 | 143 | if ( false === $mw_result ) { |
144 | - return ['']; |
|
144 | + return ['']; |
|
145 | 145 | } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
146 | 146 | $this->response .= (string)$mw_result; |
147 | - } |
|
147 | + } |
|
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | 151 | Event::trigger('core.route.after', $this); |
152 | 152 | |
153 | 153 | if ( $this->response_is_object ){ |
154 | - $this->response = Response::json($this->response_object); |
|
154 | + $this->response = Response::json($this->response_object); |
|
155 | 155 | } else { |
156 | - Response::add($this->response); |
|
156 | + Response::add($this->response); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | return [$this->response]; |
@@ -281,10 +281,10 @@ discard block |
||
281 | 281 | $route = new static($URLPattern); |
282 | 282 | $route->callback = []; |
283 | 283 | foreach ($callbacks as $method => $callback) { |
284 | - $method = strtolower($method); |
|
285 | - if (Request::method() !== $method) continue; |
|
286 | - $route->callback[$method] = $callback; |
|
287 | - $route->methods[$method] = 1; |
|
284 | + $method = strtolower($method); |
|
285 | + if (Request::method() !== $method) continue; |
|
286 | + $route->callback[$method] = $callback; |
|
287 | + $route->methods[$method] = 1; |
|
288 | 288 | } |
289 | 289 | return $route; |
290 | 290 | } |
@@ -386,9 +386,9 @@ discard block |
||
386 | 386 | } |
387 | 387 | |
388 | 388 | public static function exitWithError($code,$message="Application Error"){ |
389 | - Response::error($code,$message); |
|
390 | - Response::send(); |
|
391 | - exit; |
|
389 | + Response::error($code,$message); |
|
390 | + Response::send(); |
|
391 | + exit; |
|
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
@@ -38,9 +38,9 @@ discard block |
||
38 | 38 | * @param string $method The HTTP method for which the route must respond. |
39 | 39 | * @return Route |
40 | 40 | */ |
41 | - public function __construct($URLPattern, $callback = null, $method='get'){ |
|
42 | - $this->URLPattern = rtrim(implode('',static::$prefix),'/') . '/' . trim($URLPattern,'/')?:'/'; |
|
43 | - $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern,'/') : $this->URLPattern; |
|
41 | + public function __construct($URLPattern, $callback = null, $method = 'get') { |
|
42 | + $this->URLPattern = rtrim(implode('', static::$prefix), '/').'/'.trim($URLPattern, '/') ?: '/'; |
|
43 | + $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern, '/') : $this->URLPattern; |
|
44 | 44 | $this->dynamic = $this->isDynamic($this->URLPattern); |
45 | 45 | $this->pattern = $this->dynamic ? $this->compilePatternAsRegex($this->URLPattern, $this->rules) : $this->URLPattern; |
46 | 46 | $this->callback = $callback; |
@@ -56,19 +56,19 @@ discard block |
||
56 | 56 | * @param string $method The HTTP Method to check against. |
57 | 57 | * @return boolean |
58 | 58 | */ |
59 | - public function match($URL,$method='get'){ |
|
59 | + public function match($URL, $method = 'get') { |
|
60 | 60 | $method = strtolower($method); |
61 | 61 | |
62 | 62 | // * is an http method wildcard |
63 | 63 | if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
64 | - $URL = rtrim($URL,'/'); |
|
64 | + $URL = rtrim($URL, '/'); |
|
65 | 65 | $args = []; |
66 | - if ( $this->dynamic |
|
67 | - ? preg_match($this->pattern,$URL,$args) |
|
68 | - : $URL == rtrim($this->pattern,'/') |
|
69 | - ){ |
|
66 | + if ($this->dynamic |
|
67 | + ? preg_match($this->pattern, $URL, $args) |
|
68 | + : $URL == rtrim($this->pattern, '/') |
|
69 | + ) { |
|
70 | 70 | foreach ($args as $key => $value) { |
71 | - if ( false === is_string($key) ) unset($args[$key]); |
|
71 | + if (false === is_string($key)) unset($args[$key]); |
|
72 | 72 | } |
73 | 73 | return $args; |
74 | 74 | } |
@@ -81,23 +81,23 @@ discard block |
||
81 | 81 | * @param string $method The HTTP Method requested. |
82 | 82 | * @return array The callback response. |
83 | 83 | */ |
84 | - public function run(array $args,$method='get'){ |
|
84 | + public function run(array $args, $method = 'get') { |
|
85 | 85 | $method = strtolower($method); |
86 | - $this->response = ''; |
|
87 | - $this->response_object = null; |
|
86 | + $this->response = ''; |
|
87 | + $this->response_object = null; |
|
88 | 88 | $this->response_is_object = false; |
89 | 89 | |
90 | 90 | // Call direct befores |
91 | - if ( $this->befores ) { |
|
91 | + if ($this->befores) { |
|
92 | 92 | // Reverse befores order |
93 | 93 | foreach (array_reverse($this->befores) as $mw) { |
94 | 94 | ob_start(); |
95 | 95 | $mw_result = call_user_func($mw->bindTo($this)); |
96 | 96 | $this->response .= ob_get_clean(); |
97 | - if ( false === $mw_result ) { |
|
97 | + if (false === $mw_result) { |
|
98 | 98 | return ['']; |
99 | - } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
|
100 | - $this->response .= (string)$mw_result; |
|
99 | + } else if (is_a($mw_result, 'View') || is_string($mw_result)) { |
|
100 | + $this->response .= (string) $mw_result; |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | } |
@@ -112,45 +112,45 @@ discard block |
||
112 | 112 | Response::type(Response::TYPE_HTML); |
113 | 113 | ob_start(); |
114 | 114 | // Silence "Cannot bind an instance to a static closure" warnings |
115 | - $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
115 | + $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
116 | 116 | $this->response .= ob_get_clean(); |
117 | 117 | |
118 | 118 | // Render View if returned, else echo string or encode json response |
119 | - if ( null !== $view_results ) { |
|
120 | - if (is_a($view_results,'View') || is_string($view_results)) { |
|
121 | - $this->response .= (string)$view_results; |
|
119 | + if (null !== $view_results) { |
|
120 | + if (is_a($view_results, 'View') || is_string($view_results)) { |
|
121 | + $this->response .= (string) $view_results; |
|
122 | 122 | } else { |
123 | 123 | $this->response_is_object = true; |
124 | - $this->response_object = $view_results; |
|
124 | + $this->response_object = $view_results; |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
128 | - } else if (is_a($callback,'View') || is_string($callback)) { |
|
128 | + } else if (is_a($callback, 'View') || is_string($callback)) { |
|
129 | 129 | // return rendered View or direct string |
130 | - $this->response .= (string)$callback; |
|
130 | + $this->response .= (string) $callback; |
|
131 | 131 | } else { |
132 | 132 | // JSON encode returned value |
133 | 133 | $this->response_is_object = true; |
134 | - $this->response_object = $callback; |
|
134 | + $this->response_object = $callback; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | // Apply afters |
138 | - if ( $this->afters ) { |
|
138 | + if ($this->afters) { |
|
139 | 139 | foreach ($this->afters as $mw) { |
140 | 140 | ob_start(); |
141 | 141 | $mw_result = call_user_func($mw->bindTo($this)); |
142 | 142 | $this->response .= ob_get_clean(); |
143 | - if ( false === $mw_result ) { |
|
143 | + if (false === $mw_result) { |
|
144 | 144 | return ['']; |
145 | - } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
|
146 | - $this->response .= (string)$mw_result; |
|
145 | + } else if (is_a($mw_result, 'View') || is_string($mw_result)) { |
|
146 | + $this->response .= (string) $mw_result; |
|
147 | 147 | } |
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | 151 | Event::trigger('core.route.after', $this); |
152 | 152 | |
153 | - if ( $this->response_is_object ){ |
|
153 | + if ($this->response_is_object) { |
|
154 | 154 | $this->response = Response::json($this->response_object); |
155 | 155 | } else { |
156 | 156 | Response::add($this->response); |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | * @param string $method The HTTP Method to check against. |
166 | 166 | * @return array The callback response. |
167 | 167 | */ |
168 | - public function runIfMatch($URL, $method='get'){ |
|
169 | - return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
|
168 | + public function runIfMatch($URL, $method = 'get') { |
|
169 | + return ($args = $this->match($URL, $method)) ? $this->run($args, $method) : null; |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
@@ -175,8 +175,8 @@ discard block |
||
175 | 175 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
176 | 176 | * @return Route |
177 | 177 | */ |
178 | - public static function on($URLPattern, $callback = null){ |
|
179 | - return new Route($URLPattern,$callback); |
|
178 | + public static function on($URLPattern, $callback = null) { |
|
179 | + return new Route($URLPattern, $callback); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
@@ -185,8 +185,8 @@ discard block |
||
185 | 185 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
186 | 186 | * @return Route |
187 | 187 | */ |
188 | - public static function any($URLPattern, $callback = null){ |
|
189 | - return (new Route($URLPattern,$callback))->via('*'); |
|
188 | + public static function any($URLPattern, $callback = null) { |
|
189 | + return (new Route($URLPattern, $callback))->via('*'); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | */ |
232 | 232 | public function & via(){ |
233 | 233 | $this->methods = []; |
234 | - foreach (func_get_args() as $method){ |
|
234 | + foreach (func_get_args() as $method) { |
|
235 | 235 | $this->methods[strtolower($method)] = true; |
236 | 236 | } |
237 | 237 | return $this; |
@@ -253,10 +253,10 @@ discard block |
||
253 | 253 | * @return Route |
254 | 254 | */ |
255 | 255 | public function & rules(array $rules){ |
256 | - foreach ((array)$rules as $varname => $rule){ |
|
256 | + foreach ((array) $rules as $varname => $rule) { |
|
257 | 257 | $this->rules[$varname] = $rule; |
258 | 258 | } |
259 | - $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
|
259 | + $this->pattern = $this->compilePatternAsRegex($this->URLPattern, $this->rules); |
|
260 | 260 | return $this; |
261 | 261 | } |
262 | 262 | |
@@ -294,10 +294,10 @@ discard block |
||
294 | 294 | * @param string $pattern The URL schema. |
295 | 295 | * @return string The compiled PREG RegEx. |
296 | 296 | */ |
297 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
298 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
299 | - return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
|
300 | - },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
|
297 | + protected static function compilePatternAsRegex($pattern, $rules = []) { |
|
298 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S', function($g) use (&$rules){ |
|
299 | + return '(?<'.$g[1].'>'.(isset($rules[$g[1]]) ? $rules[$g[1]] : '[^/]+').')'; |
|
300 | + },str_replace(['.', ')', '*'], ['\.', ')?', '.+'], $pattern)).'$#'; |
|
301 | 301 | } |
302 | 302 | |
303 | 303 | /** |
@@ -307,10 +307,10 @@ discard block |
||
307 | 307 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
308 | 308 | * @return array The extracted variables |
309 | 309 | */ |
310 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
310 | + protected static function extractVariablesFromURL($pattern, $URL = null, $cut = false) { |
|
311 | 311 | $URL = $URL ?: Request::URI(); |
312 | - $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
|
313 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
312 | + $pattern = $cut ? str_replace('$#', '', $pattern).'#' : $pattern; |
|
313 | + if (!preg_match($pattern, $URL, $args)) return false; |
|
314 | 314 | foreach ($args as $key => $value) { |
315 | 315 | if (false === is_string($key)) unset($args[$key]); |
316 | 316 | } |
@@ -322,8 +322,8 @@ discard block |
||
322 | 322 | * @param string $pattern The URL schema. |
323 | 323 | * @return boolean |
324 | 324 | */ |
325 | - protected static function isDynamic($pattern){ |
|
326 | - return strlen($pattern) != strcspn($pattern,':(?[*+'); |
|
325 | + protected static function isDynamic($pattern) { |
|
326 | + return strlen($pattern) != strcspn($pattern, ':(?[*+'); |
|
327 | 327 | } |
328 | 328 | |
329 | 329 | /** |
@@ -331,9 +331,9 @@ discard block |
||
331 | 331 | * @param Route $r |
332 | 332 | * @return Route |
333 | 333 | */ |
334 | - public static function add($r){ |
|
335 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
336 | - return static::$routes[implode('',static::$prefix)][] = $r; |
|
334 | + public static function add($r) { |
|
335 | + if (isset(static::$group[0])) static::$group[0]->add($r); |
|
336 | + return static::$routes[implode('', static::$prefix)][] = $r; |
|
337 | 337 | } |
338 | 338 | |
339 | 339 | /** |
@@ -341,16 +341,16 @@ discard block |
||
341 | 341 | * @param string $prefix The url prefix for the internal route definitions. |
342 | 342 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
343 | 343 | */ |
344 | - public static function group($prefix,$callback=null){ |
|
344 | + public static function group($prefix, $callback = null) { |
|
345 | 345 | |
346 | 346 | // Skip definition if current request doesn't match group. |
347 | 347 | |
348 | - $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
|
348 | + $prefix_complete = rtrim(implode('', static::$prefix), '/').$prefix; |
|
349 | 349 | |
350 | - if ( static::isDynamic($prefix) ){ |
|
350 | + if (static::isDynamic($prefix)) { |
|
351 | 351 | |
352 | 352 | // Dynamic group, capture vars |
353 | - $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
|
353 | + $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
|
354 | 354 | |
355 | 355 | // Errors in compile pattern or variable extraction, aborting. |
356 | 356 | if (false === $vars) return; |
@@ -358,14 +358,14 @@ discard block |
||
358 | 358 | static::$prefix[] = $prefix; |
359 | 359 | if (empty(static::$group)) static::$group = []; |
360 | 360 | array_unshift(static::$group, new RouteGroup()); |
361 | - if ($callback) call_user_func_array($callback,$vars); |
|
361 | + if ($callback) call_user_func_array($callback, $vars); |
|
362 | 362 | $group = static::$group[0]; |
363 | 363 | array_shift(static::$group); |
364 | 364 | array_pop(static::$prefix); |
365 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
365 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
366 | 366 | return $group; |
367 | 367 | |
368 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
368 | + } else if (0 === strpos(Request::URI(), $prefix_complete)) { |
|
369 | 369 | |
370 | 370 | // Static group |
371 | 371 | static::$prefix[] = $prefix; |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | $group = static::$group[0]; |
376 | 376 | array_shift(static::$group); |
377 | 377 | array_pop(static::$prefix); |
378 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
378 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
379 | 379 | return $group; |
380 | 380 | } else { |
381 | 381 | |
@@ -385,8 +385,8 @@ discard block |
||
385 | 385 | |
386 | 386 | } |
387 | 387 | |
388 | - public static function exitWithError($code,$message="Application Error"){ |
|
389 | - Response::error($code,$message); |
|
388 | + public static function exitWithError($code, $message = "Application Error") { |
|
389 | + Response::error($code, $message); |
|
390 | 390 | Response::send(); |
391 | 391 | exit; |
392 | 392 | } |
@@ -396,13 +396,13 @@ discard block |
||
396 | 396 | * @param string $URL The URL to match onto. |
397 | 397 | * @return boolean true if a route callback was executed. |
398 | 398 | */ |
399 | - public static function dispatch($URL=null,$method=null){ |
|
399 | + public static function dispatch($URL = null, $method = null) { |
|
400 | 400 | if (!$URL) $URL = Request::URI(); |
401 | 401 | if (!$method) $method = Request::method(); |
402 | - foreach ((array)static::$routes as $group => $routes){ |
|
402 | + foreach ((array) static::$routes as $group => $routes) { |
|
403 | 403 | foreach ($routes as $route) { |
404 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
405 | - $route->run($args,$method); |
|
404 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
405 | + $route->run($args, $method); |
|
406 | 406 | return true; |
407 | 407 | } |
408 | 408 | } |
@@ -416,34 +416,34 @@ discard block |
||
416 | 416 | class RouteGroup { |
417 | 417 | protected $routes; |
418 | 418 | |
419 | - public function __construct(){ |
|
419 | + public function __construct() { |
|
420 | 420 | $this->routes = new SplObjectStorage; |
421 | 421 | return Route::add($this); |
422 | 422 | } |
423 | 423 | |
424 | - public function has($r){ |
|
424 | + public function has($r) { |
|
425 | 425 | return $this->routes->contains($r); |
426 | 426 | } |
427 | 427 | |
428 | - public function add($r){ |
|
428 | + public function add($r) { |
|
429 | 429 | $this->routes->attach($r); |
430 | 430 | return $this; |
431 | 431 | } |
432 | 432 | |
433 | - public function remove($r){ |
|
433 | + public function remove($r) { |
|
434 | 434 | if ($this->routes->contains($r)) $this->routes->detach($r); |
435 | 435 | return $this; |
436 | 436 | } |
437 | 437 | |
438 | - public function before($callbacks){ |
|
439 | - foreach ($this->routes as $route){ |
|
438 | + public function before($callbacks) { |
|
439 | + foreach ($this->routes as $route) { |
|
440 | 440 | $route->before($callbacks); |
441 | 441 | } |
442 | 442 | return $this; |
443 | 443 | } |
444 | 444 | |
445 | - public function after($callbacks){ |
|
446 | - foreach ($this->routes as $route){ |
|
445 | + public function after($callbacks) { |
|
446 | + foreach ($this->routes as $route) { |
|
447 | 447 | $route->after($callbacks); |
448 | 448 | } |
449 | 449 | return $this; |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * @param string $method The HTTP method for which the route must respond. |
39 | 39 | * @return Route |
40 | 40 | */ |
41 | - public function __construct($URLPattern, $callback = null, $method='get'){ |
|
41 | + public function __construct($URLPattern, $callback = null, $method='get') { |
|
42 | 42 | $this->URLPattern = rtrim(implode('',static::$prefix),'/') . '/' . trim($URLPattern,'/')?:'/'; |
43 | 43 | $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern,'/') : $this->URLPattern; |
44 | 44 | $this->dynamic = $this->isDynamic($this->URLPattern); |
@@ -56,19 +56,23 @@ discard block |
||
56 | 56 | * @param string $method The HTTP Method to check against. |
57 | 57 | * @return boolean |
58 | 58 | */ |
59 | - public function match($URL,$method='get'){ |
|
59 | + public function match($URL,$method='get') { |
|
60 | 60 | $method = strtolower($method); |
61 | 61 | |
62 | 62 | // * is an http method wildcard |
63 | - if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
|
63 | + if (empty($this->methods[$method]) && empty($this->methods['*'])) { |
|
64 | + return false; |
|
65 | + } |
|
64 | 66 | $URL = rtrim($URL,'/'); |
65 | 67 | $args = []; |
66 | 68 | if ( $this->dynamic |
67 | 69 | ? preg_match($this->pattern,$URL,$args) |
68 | 70 | : $URL == rtrim($this->pattern,'/') |
69 | - ){ |
|
71 | + ) { |
|
70 | 72 | foreach ($args as $key => $value) { |
71 | - if ( false === is_string($key) ) unset($args[$key]); |
|
73 | + if ( false === is_string($key) ) { |
|
74 | + unset($args[$key]); |
|
75 | + } |
|
72 | 76 | } |
73 | 77 | return $args; |
74 | 78 | } |
@@ -81,7 +85,7 @@ discard block |
||
81 | 85 | * @param string $method The HTTP Method requested. |
82 | 86 | * @return array The callback response. |
83 | 87 | */ |
84 | - public function run(array $args,$method='get'){ |
|
88 | + public function run(array $args,$method='get') { |
|
85 | 89 | $method = strtolower($method); |
86 | 90 | $this->response = ''; |
87 | 91 | $this->response_object = null; |
@@ -150,7 +154,7 @@ discard block |
||
150 | 154 | |
151 | 155 | Event::trigger('core.route.after', $this); |
152 | 156 | |
153 | - if ( $this->response_is_object ){ |
|
157 | + if ( $this->response_is_object ) { |
|
154 | 158 | $this->response = Response::json($this->response_object); |
155 | 159 | } else { |
156 | 160 | Response::add($this->response); |
@@ -165,7 +169,7 @@ discard block |
||
165 | 169 | * @param string $method The HTTP Method to check against. |
166 | 170 | * @return array The callback response. |
167 | 171 | */ |
168 | - public function runIfMatch($URL, $method='get'){ |
|
172 | + public function runIfMatch($URL, $method='get') { |
|
169 | 173 | return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
170 | 174 | } |
171 | 175 | |
@@ -175,7 +179,7 @@ discard block |
||
175 | 179 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
176 | 180 | * @return Route |
177 | 181 | */ |
178 | - public static function on($URLPattern, $callback = null){ |
|
182 | + public static function on($URLPattern, $callback = null) { |
|
179 | 183 | return new Route($URLPattern,$callback); |
180 | 184 | } |
181 | 185 | |
@@ -185,7 +189,7 @@ discard block |
||
185 | 189 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
186 | 190 | * @return Route |
187 | 191 | */ |
188 | - public static function any($URLPattern, $callback = null){ |
|
192 | + public static function any($URLPattern, $callback = null) { |
|
189 | 193 | return (new Route($URLPattern,$callback))->via('*'); |
190 | 194 | } |
191 | 195 | |
@@ -231,7 +235,7 @@ discard block |
||
231 | 235 | */ |
232 | 236 | public function & via(){ |
233 | 237 | $this->methods = []; |
234 | - foreach (func_get_args() as $method){ |
|
238 | + foreach (func_get_args() as $method) { |
|
235 | 239 | $this->methods[strtolower($method)] = true; |
236 | 240 | } |
237 | 241 | return $this; |
@@ -253,7 +257,7 @@ discard block |
||
253 | 257 | * @return Route |
254 | 258 | */ |
255 | 259 | public function & rules(array $rules){ |
256 | - foreach ((array)$rules as $varname => $rule){ |
|
260 | + foreach ((array)$rules as $varname => $rule) { |
|
257 | 261 | $this->rules[$varname] = $rule; |
258 | 262 | } |
259 | 263 | $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
@@ -282,7 +286,9 @@ discard block |
||
282 | 286 | $route->callback = []; |
283 | 287 | foreach ($callbacks as $method => $callback) { |
284 | 288 | $method = strtolower($method); |
285 | - if (Request::method() !== $method) continue; |
|
289 | + if (Request::method() !== $method) { |
|
290 | + continue; |
|
291 | + } |
|
286 | 292 | $route->callback[$method] = $callback; |
287 | 293 | $route->methods[$method] = 1; |
288 | 294 | } |
@@ -294,8 +300,8 @@ discard block |
||
294 | 300 | * @param string $pattern The URL schema. |
295 | 301 | * @return string The compiled PREG RegEx. |
296 | 302 | */ |
297 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
298 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
303 | + protected static function compilePatternAsRegex($pattern, $rules=[]) { |
|
304 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules) { |
|
299 | 305 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
300 | 306 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
301 | 307 | } |
@@ -307,12 +313,16 @@ discard block |
||
307 | 313 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
308 | 314 | * @return array The extracted variables |
309 | 315 | */ |
310 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
316 | + protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false) { |
|
311 | 317 | $URL = $URL ?: Request::URI(); |
312 | 318 | $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
313 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
319 | + if ( !preg_match($pattern,$URL,$args) ) { |
|
320 | + return false; |
|
321 | + } |
|
314 | 322 | foreach ($args as $key => $value) { |
315 | - if (false === is_string($key)) unset($args[$key]); |
|
323 | + if (false === is_string($key)) { |
|
324 | + unset($args[$key]); |
|
325 | + } |
|
316 | 326 | } |
317 | 327 | return $args; |
318 | 328 | } |
@@ -322,7 +332,7 @@ discard block |
||
322 | 332 | * @param string $pattern The URL schema. |
323 | 333 | * @return boolean |
324 | 334 | */ |
325 | - protected static function isDynamic($pattern){ |
|
335 | + protected static function isDynamic($pattern) { |
|
326 | 336 | return strlen($pattern) != strcspn($pattern,':(?[*+'); |
327 | 337 | } |
328 | 338 | |
@@ -331,8 +341,10 @@ discard block |
||
331 | 341 | * @param Route $r |
332 | 342 | * @return Route |
333 | 343 | */ |
334 | - public static function add($r){ |
|
335 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
344 | + public static function add($r) { |
|
345 | + if ( isset(static::$group[0]) ) { |
|
346 | + static::$group[0]->add($r); |
|
347 | + } |
|
336 | 348 | return static::$routes[implode('',static::$prefix)][] = $r; |
337 | 349 | } |
338 | 350 | |
@@ -341,41 +353,55 @@ discard block |
||
341 | 353 | * @param string $prefix The url prefix for the internal route definitions. |
342 | 354 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
343 | 355 | */ |
344 | - public static function group($prefix,$callback=null){ |
|
356 | + public static function group($prefix,$callback=null) { |
|
345 | 357 | |
346 | 358 | // Skip definition if current request doesn't match group. |
347 | 359 | |
348 | 360 | $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
349 | 361 | |
350 | - if ( static::isDynamic($prefix) ){ |
|
362 | + if ( static::isDynamic($prefix) ) { |
|
351 | 363 | |
352 | 364 | // Dynamic group, capture vars |
353 | 365 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
354 | 366 | |
355 | 367 | // Errors in compile pattern or variable extraction, aborting. |
356 | - if (false === $vars) return; |
|
368 | + if (false === $vars) { |
|
369 | + return; |
|
370 | + } |
|
357 | 371 | |
358 | 372 | static::$prefix[] = $prefix; |
359 | - if (empty(static::$group)) static::$group = []; |
|
373 | + if (empty(static::$group)) { |
|
374 | + static::$group = []; |
|
375 | + } |
|
360 | 376 | array_unshift(static::$group, new RouteGroup()); |
361 | - if ($callback) call_user_func_array($callback,$vars); |
|
377 | + if ($callback) { |
|
378 | + call_user_func_array($callback,$vars); |
|
379 | + } |
|
362 | 380 | $group = static::$group[0]; |
363 | 381 | array_shift(static::$group); |
364 | 382 | array_pop(static::$prefix); |
365 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
383 | + if (empty(static::$prefix)) { |
|
384 | + static::$prefix=['']; |
|
385 | + } |
|
366 | 386 | return $group; |
367 | 387 | |
368 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
388 | + } else if ( 0 === strpos(Request::URI(), $prefix_complete) ) { |
|
369 | 389 | |
370 | 390 | // Static group |
371 | 391 | static::$prefix[] = $prefix; |
372 | - if (empty(static::$group)) static::$group = []; |
|
392 | + if (empty(static::$group)) { |
|
393 | + static::$group = []; |
|
394 | + } |
|
373 | 395 | array_unshift(static::$group, new RouteGroup()); |
374 | - if ($callback) call_user_func($callback); |
|
396 | + if ($callback) { |
|
397 | + call_user_func($callback); |
|
398 | + } |
|
375 | 399 | $group = static::$group[0]; |
376 | 400 | array_shift(static::$group); |
377 | 401 | array_pop(static::$prefix); |
378 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
402 | + if (empty(static::$prefix)) { |
|
403 | + static::$prefix=['']; |
|
404 | + } |
|
379 | 405 | return $group; |
380 | 406 | } else { |
381 | 407 | |
@@ -385,7 +411,7 @@ discard block |
||
385 | 411 | |
386 | 412 | } |
387 | 413 | |
388 | - public static function exitWithError($code,$message="Application Error"){ |
|
414 | + public static function exitWithError($code,$message="Application Error") { |
|
389 | 415 | Response::error($code,$message); |
390 | 416 | Response::send(); |
391 | 417 | exit; |
@@ -396,12 +422,16 @@ discard block |
||
396 | 422 | * @param string $URL The URL to match onto. |
397 | 423 | * @return boolean true if a route callback was executed. |
398 | 424 | */ |
399 | - public static function dispatch($URL=null,$method=null){ |
|
400 | - if (!$URL) $URL = Request::URI(); |
|
401 | - if (!$method) $method = Request::method(); |
|
402 | - foreach ((array)static::$routes as $group => $routes){ |
|
425 | + public static function dispatch($URL=null,$method=null) { |
|
426 | + if (!$URL) { |
|
427 | + $URL = Request::URI(); |
|
428 | + } |
|
429 | + if (!$method) { |
|
430 | + $method = Request::method(); |
|
431 | + } |
|
432 | + foreach ((array)static::$routes as $group => $routes) { |
|
403 | 433 | foreach ($routes as $route) { |
404 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
434 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
405 | 435 | $route->run($args,$method); |
406 | 436 | return true; |
407 | 437 | } |
@@ -416,34 +446,36 @@ discard block |
||
416 | 446 | class RouteGroup { |
417 | 447 | protected $routes; |
418 | 448 | |
419 | - public function __construct(){ |
|
449 | + public function __construct() { |
|
420 | 450 | $this->routes = new SplObjectStorage; |
421 | 451 | return Route::add($this); |
422 | 452 | } |
423 | 453 | |
424 | - public function has($r){ |
|
454 | + public function has($r) { |
|
425 | 455 | return $this->routes->contains($r); |
426 | 456 | } |
427 | 457 | |
428 | - public function add($r){ |
|
458 | + public function add($r) { |
|
429 | 459 | $this->routes->attach($r); |
430 | 460 | return $this; |
431 | 461 | } |
432 | 462 | |
433 | - public function remove($r){ |
|
434 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
463 | + public function remove($r) { |
|
464 | + if ($this->routes->contains($r)) { |
|
465 | + $this->routes->detach($r); |
|
466 | + } |
|
435 | 467 | return $this; |
436 | 468 | } |
437 | 469 | |
438 | - public function before($callbacks){ |
|
439 | - foreach ($this->routes as $route){ |
|
470 | + public function before($callbacks) { |
|
471 | + foreach ($this->routes as $route) { |
|
440 | 472 | $route->before($callbacks); |
441 | 473 | } |
442 | 474 | return $this; |
443 | 475 | } |
444 | 476 | |
445 | - public function after($callbacks){ |
|
446 | - foreach ($this->routes as $route){ |
|
477 | + public function after($callbacks) { |
|
478 | + foreach ($this->routes as $route) { |
|
447 | 479 | $route->after($callbacks); |
448 | 480 | } |
449 | 481 | return $this; |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | |
30 | 30 | /** |
31 | 31 | * Load a Template Handler |
32 | - * @param class $handler The template handler class instance |
|
32 | + * @param View\Adapter $handler The template handler class instance |
|
33 | 33 | */ |
34 | 34 | public static function using(View\Adapter &$handler){ |
35 | 35 | static::$handler = $handler; |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | /** |
71 | 71 | * Returns the handler instance |
72 | - * @return mixed |
|
72 | + * @return View\Adapter |
|
73 | 73 | */ |
74 | 74 | public static function & handler(){ |
75 | 75 | return static::$handler; |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * Construct a new view based on the passed template |
24 | 24 | * @param string $template The template path |
25 | 25 | */ |
26 | - public function __construct($template){ |
|
26 | + public function __construct($template) { |
|
27 | 27 | $this->options['template'] = $template; |
28 | 28 | } |
29 | 29 | |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * Load a Template Handler |
32 | 32 | * @param class $handler The template handler class instance |
33 | 33 | */ |
34 | - public static function using(View\Adapter &$handler){ |
|
34 | + public static function using(View\Adapter & $handler) { |
|
35 | 35 | static::$handler = $handler; |
36 | 36 | } |
37 | 37 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @param array $data The key-value map of data to pass to the view |
42 | 42 | * @return View |
43 | 43 | */ |
44 | - public static function from($template,$data=null){ |
|
44 | + public static function from($template, $data = null) { |
|
45 | 45 | $view = new self($template); |
46 | 46 | return $data ? $view->with($data) : $view; |
47 | 47 | } |
@@ -51,9 +51,9 @@ discard block |
||
51 | 51 | * @param array $data The key-value map of data to pass to the view |
52 | 52 | * @return View |
53 | 53 | */ |
54 | - public function with($data){ |
|
55 | - if ($data){ |
|
56 | - $tmp = array_merge($data, (isset($this->options['data'])?$this->options['data']:[])); |
|
54 | + public function with($data) { |
|
55 | + if ($data) { |
|
56 | + $tmp = array_merge($data, (isset($this->options['data']) ? $this->options['data'] : [])); |
|
57 | 57 | $this->options['data'] = $tmp; |
58 | 58 | } |
59 | 59 | return $this; |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | * Render view when casted to a string |
64 | 64 | * @return string The rendered view |
65 | 65 | */ |
66 | - public function __toString(){ |
|
67 | - return Filter::with('core.view',static::$handler->render($this->options['template'],$this->options['data'])); |
|
66 | + public function __toString() { |
|
67 | + return Filter::with('core.view', static::$handler->render($this->options['template'], $this->options['data'])); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * Check if a template exists |
80 | 80 | * @return bool |
81 | 81 | */ |
82 | - public static function exists($templatePath){ |
|
82 | + public static function exists($templatePath) { |
|
83 | 83 | return static::$handler->exists($templatePath); |
84 | 84 | } |
85 | 85 | |
@@ -87,15 +87,15 @@ discard block |
||
87 | 87 | /** |
88 | 88 | * Propagate the call to the handler |
89 | 89 | */ |
90 | - public function __call($n,$p){ |
|
91 | - return call_user_func_array([static::$handler,$n],$p); |
|
90 | + public function __call($n, $p) { |
|
91 | + return call_user_func_array([static::$handler, $n], $p); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
95 | 95 | * Propagate the static call to the handler |
96 | 96 | */ |
97 | - public static function __callStatic($n,$p){ |
|
98 | - return forward_static_call_array([static::$handler,$n],$p); |
|
97 | + public static function __callStatic($n, $p) { |
|
98 | + return forward_static_call_array([static::$handler, $n], $p); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * Construct a new view based on the passed template |
24 | 24 | * @param string $template The template path |
25 | 25 | */ |
26 | - public function __construct($template){ |
|
26 | + public function __construct($template) { |
|
27 | 27 | $this->options['template'] = $template; |
28 | 28 | } |
29 | 29 | |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * Load a Template Handler |
32 | 32 | * @param class $handler The template handler class instance |
33 | 33 | */ |
34 | - public static function using(View\Adapter &$handler){ |
|
34 | + public static function using(View\Adapter &$handler) { |
|
35 | 35 | static::$handler = $handler; |
36 | 36 | } |
37 | 37 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @param array $data The key-value map of data to pass to the view |
42 | 42 | * @return View |
43 | 43 | */ |
44 | - public static function from($template,$data=null){ |
|
44 | + public static function from($template,$data=null) { |
|
45 | 45 | $view = new self($template); |
46 | 46 | return $data ? $view->with($data) : $view; |
47 | 47 | } |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | * @param array $data The key-value map of data to pass to the view |
52 | 52 | * @return View |
53 | 53 | */ |
54 | - public function with($data){ |
|
55 | - if ($data){ |
|
54 | + public function with($data) { |
|
55 | + if ($data) { |
|
56 | 56 | $tmp = array_merge($data, (isset($this->options['data'])?$this->options['data']:[])); |
57 | 57 | $this->options['data'] = $tmp; |
58 | 58 | } |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | * Render view when casted to a string |
64 | 64 | * @return string The rendered view |
65 | 65 | */ |
66 | - public function __toString(){ |
|
66 | + public function __toString() { |
|
67 | 67 | return Filter::with('core.view',static::$handler->render($this->options['template'],$this->options['data'])); |
68 | 68 | } |
69 | 69 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * Check if a template exists |
80 | 80 | * @return bool |
81 | 81 | */ |
82 | - public static function exists($templatePath){ |
|
82 | + public static function exists($templatePath) { |
|
83 | 83 | return static::$handler->exists($templatePath); |
84 | 84 | } |
85 | 85 | |
@@ -87,14 +87,14 @@ discard block |
||
87 | 87 | /** |
88 | 88 | * Propagate the call to the handler |
89 | 89 | */ |
90 | - public function __call($n,$p){ |
|
90 | + public function __call($n,$p) { |
|
91 | 91 | return call_user_func_array([static::$handler,$n],$p); |
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
95 | 95 | * Propagate the static call to the handler |
96 | 96 | */ |
97 | - public static function __callStatic($n,$p){ |
|
97 | + public static function __callStatic($n,$p) { |
|
98 | 98 | return forward_static_call_array([static::$handler,$n],$p); |
99 | 99 | } |
100 | 100 |
@@ -57,6 +57,9 @@ |
||
57 | 57 | class PHPContext { |
58 | 58 | protected $data = []; |
59 | 59 | |
60 | + /** |
|
61 | + * @param string $path |
|
62 | + */ |
|
60 | 63 | public function __construct($data=[], $path=null){ |
61 | 64 | $this->data = $data; |
62 | 65 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | const EXTENSION = 'php'; |
18 | 18 | |
19 | 19 | protected static $templatePath, |
20 | - $globals = []; |
|
20 | + $globals = []; |
|
21 | 21 | |
22 | 22 | public function __construct($path=null,$options=[]){ |
23 | 23 | self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/'; |
@@ -14,32 +14,32 @@ discard block |
||
14 | 14 | |
15 | 15 | class PHP implements Adapter { |
16 | 16 | |
17 | - const EXTENSION = 'php'; |
|
17 | + const EXTENSION = 'php'; |
|
18 | 18 | |
19 | 19 | protected static $templatePath, |
20 | 20 | $globals = []; |
21 | 21 | |
22 | - public function __construct($path=null,$options=[]){ |
|
23 | - self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/'; |
|
22 | + public function __construct($path = null, $options = []) { |
|
23 | + self::$templatePath = ($path ? rtrim($path, '/') : __DIR__).'/'; |
|
24 | 24 | } |
25 | 25 | |
26 | - public static function exists($path){ |
|
26 | + public static function exists($path) { |
|
27 | 27 | return is_file(self::$templatePath.$path.'.php'); |
28 | 28 | } |
29 | 29 | |
30 | - public static function addGlobal($key,$val){ |
|
30 | + public static function addGlobal($key, $val) { |
|
31 | 31 | self::$globals[$key] = $val; |
32 | 32 | } |
33 | 33 | |
34 | - public static function addGlobals(array $defs){ |
|
35 | - foreach ((array)$defs as $key=>$val) { |
|
34 | + public static function addGlobals(array $defs) { |
|
35 | + foreach ((array) $defs as $key=>$val) { |
|
36 | 36 | self::$globals[$key] = $val; |
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | - public function render($template,$data=[]){ |
|
41 | - $template_path = self::$templatePath . trim($template,'/') . '.php'; |
|
42 | - $sandbox = function() use ($template_path){ |
|
40 | + public function render($template, $data = []) { |
|
41 | + $template_path = self::$templatePath.trim($template, '/').'.php'; |
|
42 | + $sandbox = function() use ($template_path){ |
|
43 | 43 | ob_start(); |
44 | 44 | include($template_path); |
45 | 45 | $__buffer__ = ob_get_contents(); |
@@ -55,21 +55,21 @@ discard block |
||
55 | 55 | } |
56 | 56 | |
57 | 57 | class PHPContext { |
58 | - protected $data = []; |
|
58 | + protected $data = []; |
|
59 | 59 | |
60 | - public function __construct($data=[], $path=null){ |
|
60 | + public function __construct($data = [], $path = null) { |
|
61 | 61 | $this->data = $data; |
62 | 62 | } |
63 | 63 | |
64 | - public function partial($template, $vars=[]){ |
|
65 | - return \View::from($template,array_merge($this->data,$vars)); |
|
64 | + public function partial($template, $vars = []) { |
|
65 | + return \View::from($template, array_merge($this->data, $vars)); |
|
66 | 66 | } |
67 | 67 | |
68 | - public function __isset($n){ return true; } |
|
68 | + public function __isset($n) { return true;} |
|
69 | 69 | |
70 | - public function __unset($n){} |
|
70 | + public function __unset($n) {} |
|
71 | 71 | |
72 | - public function __get($n){ |
|
72 | + public function __get($n) { |
|
73 | 73 | return empty($this->data[$n]) ? '' : $this->data[$n]; |
74 | 74 | } |
75 | 75 |
@@ -19,27 +19,27 @@ discard block |
||
19 | 19 | protected static $templatePath, |
20 | 20 | $globals = []; |
21 | 21 | |
22 | - public function __construct($path=null,$options=[]){ |
|
22 | + public function __construct($path=null,$options=[]) { |
|
23 | 23 | self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/'; |
24 | 24 | } |
25 | 25 | |
26 | - public static function exists($path){ |
|
26 | + public static function exists($path) { |
|
27 | 27 | return is_file(self::$templatePath.$path.'.php'); |
28 | 28 | } |
29 | 29 | |
30 | - public static function addGlobal($key,$val){ |
|
30 | + public static function addGlobal($key,$val) { |
|
31 | 31 | self::$globals[$key] = $val; |
32 | 32 | } |
33 | 33 | |
34 | - public static function addGlobals(array $defs){ |
|
34 | + public static function addGlobals(array $defs) { |
|
35 | 35 | foreach ((array)$defs as $key=>$val) { |
36 | 36 | self::$globals[$key] = $val; |
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | - public function render($template,$data=[]){ |
|
40 | + public function render($template,$data=[]) { |
|
41 | 41 | $template_path = self::$templatePath . trim($template,'/') . '.php'; |
42 | - $sandbox = function() use ($template_path){ |
|
42 | + $sandbox = function() use ($template_path) { |
|
43 | 43 | ob_start(); |
44 | 44 | include($template_path); |
45 | 45 | $__buffer__ = ob_get_contents(); |
@@ -57,11 +57,11 @@ discard block |
||
57 | 57 | class PHPContext { |
58 | 58 | protected $data = []; |
59 | 59 | |
60 | - public function __construct($data=[], $path=null){ |
|
60 | + public function __construct($data=[], $path=null) { |
|
61 | 61 | $this->data = $data; |
62 | 62 | } |
63 | 63 | |
64 | - public function partial($template, $vars=[]){ |
|
64 | + public function partial($template, $vars=[]) { |
|
65 | 65 | return \View::from($template,array_merge($this->data,$vars)); |
66 | 66 | } |
67 | 67 | |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | public function __unset($n){} |
71 | 71 | |
72 | - public function __get($n){ |
|
72 | + public function __get($n) { |
|
73 | 73 | return empty($this->data[$n]) ? '' : $this->data[$n]; |
74 | 74 | } |
75 | 75 |
@@ -101,6 +101,11 @@ |
||
101 | 101 | $queries = [], |
102 | 102 | $last_exec_success = true; |
103 | 103 | |
104 | + /** |
|
105 | + * @param string $dsn |
|
106 | + * @param string $username |
|
107 | + * @param string $password |
|
108 | + */ |
|
104 | 109 | public function __construct($dsn, $username=null, $password=null, $options=[]){ |
105 | 110 | $this->connection = [ |
106 | 111 | 'dsn' => $dsn, |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | class SQL { |
15 | 15 | use Module; |
16 | 16 | protected static $connections = [], |
17 | - $current = 'default'; |
|
17 | + $current = 'default'; |
|
18 | 18 | |
19 | 19 | /** |
20 | 20 | * Register a new datasource |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | } |
209 | 209 | } |
210 | 210 | |
211 | - public function run($script){ |
|
211 | + public function run($script){ |
|
212 | 212 | if(!$this->connection()) return false; |
213 | 213 | |
214 | 214 | $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
@@ -223,8 +223,8 @@ discard block |
||
223 | 223 | } |
224 | 224 | |
225 | 225 | public function all($query, $params=[], callable $looper = null){ |
226 | - if(!$this->connection()) return false; |
|
227 | - return $this->each($query,$params,$looper); |
|
226 | + if(!$this->connection()) return false; |
|
227 | + return $this->each($query,$params,$looper); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @param array $options Options to pass to the PDO constructor |
26 | 26 | * @return SQLConnection The datasource resource |
27 | 27 | */ |
28 | - public static function register($name, $dsn, $username=null, $password=null, $options=[]){ |
|
28 | + public static function register($name, $dsn, $username = null, $password = null, $options = []) { |
|
29 | 29 | return self::$connections[$name] = new SQLConnection($dsn, $username, $password, $options); |
30 | 30 | } |
31 | 31 | |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param array $options Options to pass to the PDO constructor |
38 | 38 | * @return SQLConnection The datasource resource |
39 | 39 | */ |
40 | - public static function connect($dsn, $username=null, $password=null, $options=[]){ |
|
40 | + public static function connect($dsn, $username = null, $password = null, $options = []) { |
|
41 | 41 | return self::register('default', $dsn, $username, $password, $options); |
42 | 42 | } |
43 | 43 | |
@@ -46,8 +46,8 @@ discard block |
||
46 | 46 | * @param string $name The datasource name |
47 | 47 | * @return bool `true` if correctly changed |
48 | 48 | */ |
49 | - public static function defaultTo($name){ |
|
50 | - if (isset(self::$connections[$name])){ |
|
49 | + public static function defaultTo($name) { |
|
50 | + if (isset(self::$connections[$name])) { |
|
51 | 51 | self::$current = $name; |
52 | 52 | return true; |
53 | 53 | } else return false; |
@@ -58,11 +58,11 @@ discard block |
||
58 | 58 | * @param string $name The datasource name, omit for close all of them |
59 | 59 | * @return bool `true` if one or more datasource where closed |
60 | 60 | */ |
61 | - public static function close($name=null){ |
|
61 | + public static function close($name = null) { |
|
62 | 62 | if ($name === null) { |
63 | 63 | foreach (self::$connections as $conn) $conn->close(); |
64 | 64 | return true; |
65 | - } else if (isset(self::$connections[$name])){ |
|
65 | + } else if (isset(self::$connections[$name])) { |
|
66 | 66 | self::$connections[$name]->close(); |
67 | 67 | return true; |
68 | 68 | } else return false; |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | * @param strinf $name The datasource name |
74 | 74 | * @return SQLConnect The datasource connection |
75 | 75 | */ |
76 | - public static function using($name){ |
|
76 | + public static function using($name) { |
|
77 | 77 | if (empty(self::$connections[$name])) throw new \Exception("[SQL] Unknown connection named '$name'."); |
78 | 78 | return self::$connections[$name]; |
79 | 79 | } |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | * @param array $args The method arguments |
85 | 85 | * @return mixed The method return value |
86 | 86 | */ |
87 | - public static function __callStatic($method, $args){ |
|
87 | + public static function __callStatic($method, $args) { |
|
88 | 88 | if (empty(self::$connections[self::$current])) throw new \Exception("[SQL] No default connection defined."); |
89 | - return call_user_func_array([self::$connections[self::$current],$method],$args); |
|
89 | + return call_user_func_array([self::$connections[self::$current], $method], $args); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $queries = [], |
102 | 102 | $last_exec_success = true; |
103 | 103 | |
104 | - public function __construct($dsn, $username=null, $password=null, $options=[]){ |
|
104 | + public function __construct($dsn, $username = null, $password = null, $options = []) { |
|
105 | 105 | $this->connection = [ |
106 | 106 | 'dsn' => $dsn, |
107 | 107 | 'pdo' => null, |
@@ -111,20 +111,20 @@ discard block |
||
111 | 111 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
112 | 112 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
113 | 113 | PDO::ATTR_EMULATE_PREPARES => true, |
114 | - ],$options), |
|
114 | + ], $options), |
|
115 | 115 | ]; |
116 | 116 | // "The auto-commit mode cannot be changed for this driver" SQLite workaround |
117 | - if (strpos($dsn,'sqlite:') === 0) { |
|
117 | + if (strpos($dsn, 'sqlite:') === 0) { |
|
118 | 118 | $this->connection['options'] = $options; |
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | - public function close(){ |
|
122 | + public function close() { |
|
123 | 123 | $this->connection['pdo'] = null; |
124 | 124 | } |
125 | 125 | |
126 | - public function connection(){ |
|
127 | - if(empty($this->connection['pdo'])) { |
|
126 | + public function connection() { |
|
127 | + if (empty($this->connection['pdo'])) { |
|
128 | 128 | try { |
129 | 129 | $this->connection['pdo'] = new PDO( |
130 | 130 | $this->connection['dsn'], |
@@ -133,26 +133,26 @@ discard block |
||
133 | 133 | $this->connection['options'] |
134 | 134 | |
135 | 135 | ); |
136 | - Event::trigger('core.sql.connect',$this); |
|
137 | - } catch(Exception $e) { |
|
136 | + Event::trigger('core.sql.connect', $this); |
|
137 | + } catch (Exception $e) { |
|
138 | 138 | $this->connection['pdo'] = null; |
139 | 139 | } |
140 | 140 | } |
141 | 141 | return $this->connection['pdo']; |
142 | 142 | } |
143 | 143 | |
144 | - public function prepare($query){ |
|
145 | - if(!$this->connection()) return false; |
|
144 | + public function prepare($query) { |
|
145 | + if (!$this->connection()) return false; |
|
146 | 146 | return isset($this->queries[$query]) ? $this->queries[$query] : ($this->queries[$query] = $this->connection()->prepare($query)); |
147 | 147 | } |
148 | 148 | |
149 | - public function exec($query, $params=[]){ |
|
150 | - if(!$this->connection()) return false; |
|
149 | + public function exec($query, $params = []) { |
|
150 | + if (!$this->connection()) return false; |
|
151 | 151 | |
152 | - if (false==is_array($params)) $params = (array)$params; |
|
153 | - $query = Filter::with('core.sql.query',$query); |
|
154 | - if($statement = $this->prepare($query)){ |
|
155 | - Event::trigger('core.sql.query',$query,$params,(bool)$statement); |
|
152 | + if (false == is_array($params)) $params = (array) $params; |
|
153 | + $query = Filter::with('core.sql.query', $query); |
|
154 | + if ($statement = $this->prepare($query)) { |
|
155 | + Event::trigger('core.sql.query', $query, $params, (bool) $statement); |
|
156 | 156 | |
157 | 157 | foreach ($params as $key => $val) { |
158 | 158 | $type = PDO::PARAM_STR; |
@@ -164,10 +164,10 @@ discard block |
||
164 | 164 | $type = PDO::PARAM_INT; |
165 | 165 | } |
166 | 166 | // bindValue need a 1-based numeric parameter |
167 | - $statement->bindValue(is_numeric($key)?$key+1:':'.$key, $val, $type); |
|
167 | + $statement->bindValue(is_numeric($key) ? $key + 1 : ':'.$key, $val, $type); |
|
168 | 168 | } |
169 | 169 | } else { |
170 | - Event::trigger('core.sql.error',$query,$params); |
|
170 | + Event::trigger('core.sql.error', $query, $params); |
|
171 | 171 | return false; |
172 | 172 | } |
173 | 173 | |
@@ -175,32 +175,32 @@ discard block |
||
175 | 175 | return $statement; |
176 | 176 | } |
177 | 177 | |
178 | - public function value($query, $params=[], $column=0){ |
|
179 | - if(!$this->connection()) return false; |
|
178 | + public function value($query, $params = [], $column = 0) { |
|
179 | + if (!$this->connection()) return false; |
|
180 | 180 | |
181 | - $res = $this->exec($query,$params); |
|
181 | + $res = $this->exec($query, $params); |
|
182 | 182 | return $res ? $res->fetchColumn($column) : null; |
183 | 183 | } |
184 | 184 | |
185 | - public function each($query, $params=[], callable $looper = null){ |
|
186 | - if(!$this->connection()) return false; |
|
185 | + public function each($query, $params = [], callable $looper = null) { |
|
186 | + if (!$this->connection()) return false; |
|
187 | 187 | |
188 | 188 | // ($query,$looper) shorthand |
189 | - if ($looper===null && is_callable($params)) {$looper = $params; $params = [];} |
|
190 | - if( $res = $this->exec($query,$params) ){ |
|
191 | - if(is_callable($looper)) |
|
189 | + if ($looper === null && is_callable($params)) {$looper = $params;$params = [];} |
|
190 | + if ($res = $this->exec($query, $params)) { |
|
191 | + if (is_callable($looper)) |
|
192 | 192 | while ($row = $res->fetchObject()) $looper($row); |
193 | 193 | else |
194 | 194 | return $res->fetchAll(PDO::FETCH_CLASS); |
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
198 | - public function single($query, $params=[], callable $handler = null){ |
|
199 | - if(!$this->connection()) return false; |
|
198 | + public function single($query, $params = [], callable $handler = null) { |
|
199 | + if (!$this->connection()) return false; |
|
200 | 200 | |
201 | 201 | // ($query,$handler) shorthand |
202 | - if ($handler===null && is_callable($params)) {$handler = $params; $params = [];} |
|
203 | - if( $res = $this->exec($query,$params) ){ |
|
202 | + if ($handler === null && is_callable($params)) {$handler = $params;$params = [];} |
|
203 | + if ($res = $this->exec($query, $params)) { |
|
204 | 204 | if (is_callable($handler)) |
205 | 205 | $handler($res->fetchObject()); |
206 | 206 | else |
@@ -208,76 +208,76 @@ discard block |
||
208 | 208 | } |
209 | 209 | } |
210 | 210 | |
211 | - public function run($script){ |
|
212 | - if(!$this->connection()) return false; |
|
211 | + public function run($script) { |
|
212 | + if (!$this->connection()) return false; |
|
213 | 213 | |
214 | - $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
|
215 | - $sql_sep = Options::get('database.sql.separator',';'); |
|
216 | - if (is_file($f = "$sql_path/$script.sql")){ |
|
214 | + $sql_path = Options::get('database.sql.path', APP_DIR.'/sql'); |
|
215 | + $sql_sep = Options::get('database.sql.separator', ';'); |
|
216 | + if (is_file($f = "$sql_path/$script.sql")) { |
|
217 | 217 | $result = true; |
218 | - foreach(explode($sql_sep,file_get_contents($f)) as $statement) { |
|
218 | + foreach (explode($sql_sep, file_get_contents($f)) as $statement) { |
|
219 | 219 | $result = $this->exec($statement); |
220 | 220 | } |
221 | 221 | return $result; |
222 | 222 | } else return false; |
223 | 223 | } |
224 | 224 | |
225 | - public function all($query, $params=[], callable $looper = null){ |
|
226 | - if(!$this->connection()) return false; |
|
227 | - return $this->each($query,$params,$looper); |
|
225 | + public function all($query, $params = [], callable $looper = null) { |
|
226 | + if (!$this->connection()) return false; |
|
227 | + return $this->each($query, $params, $looper); |
|
228 | 228 | } |
229 | 229 | |
230 | - public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
|
231 | - if(!$this->connection()) return false; |
|
230 | + public function delete($table, $pks = null, $pk = 'id', $inclusive = true) { |
|
231 | + if (!$this->connection()) return false; |
|
232 | 232 | |
233 | - if (null===$pks) { |
|
233 | + if (null === $pks) { |
|
234 | 234 | return $this->exec("DELETE FROM `$table`"); |
235 | 235 | } else { |
236 | - return $this->exec("DELETE FROM `$table` WHERE `$pk` ".($inclusive ? "" : "NOT " )."IN (" . implode( ',', array_fill_keys( (array)$pks, '?' ) ) . ")",(array)$pks); |
|
236 | + return $this->exec("DELETE FROM `$table` WHERE `$pk` ".($inclusive ? "" : "NOT ")."IN (".implode(',', array_fill_keys((array) $pks, '?')).")", (array) $pks); |
|
237 | 237 | } |
238 | 238 | } |
239 | 239 | |
240 | - public function insert($table, $data){ |
|
241 | - if(!$this->connection()) return false; |
|
240 | + public function insert($table, $data) { |
|
241 | + if (!$this->connection()) return false; |
|
242 | 242 | |
243 | - if (false==is_array($data)) $data = (array)$data; |
|
243 | + if (false == is_array($data)) $data = (array) $data; |
|
244 | 244 | $k = array_keys($data); |
245 | 245 | asort($k); |
246 | 246 | $pk = $k; |
247 | - array_walk($pk,function(&$e){ $e = ':'.$e;}); |
|
248 | - $q = "INSERT INTO `$table` (`".implode('`,`',$k)."`) VALUES (".implode(',',$pk).")"; |
|
249 | - $this->exec($q,$data); |
|
247 | + array_walk($pk, function(&$e) { $e = ':'.$e;}); |
|
248 | + $q = "INSERT INTO `$table` (`".implode('`,`', $k)."`) VALUES (".implode(',', $pk).")"; |
|
249 | + $this->exec($q, $data); |
|
250 | 250 | return $this->last_exec_success ? $this->connection()->lastInsertId() : false; |
251 | 251 | } |
252 | 252 | |
253 | - public function updateWhere($table, $data, $where, $pk='id'){ |
|
254 | - if(!$this->connection()) return false; |
|
253 | + public function updateWhere($table, $data, $where, $pk = 'id') { |
|
254 | + if (!$this->connection()) return false; |
|
255 | 255 | |
256 | - if (false==is_array($data)) $data = (array)$data; |
|
256 | + if (false == is_array($data)) $data = (array) $data; |
|
257 | 257 | if (empty($data)) return false; |
258 | 258 | $k = array_keys($data); |
259 | 259 | asort($k); |
260 | 260 | |
261 | 261 | // Remove primary key from SET |
262 | - array_walk($k,function(&$e) use ($pk) { |
|
263 | - $e = ($e==$pk) ? null : "`$e`=:$e"; |
|
262 | + array_walk($k, function(&$e) use ($pk) { |
|
263 | + $e = ($e == $pk) ? null : "`$e`=:$e"; |
|
264 | 264 | }); |
265 | 265 | |
266 | - $q = "UPDATE `$table` SET ".implode(', ',array_filter($k))." WHERE $where"; |
|
267 | - $this->exec($q,$data); |
|
266 | + $q = "UPDATE `$table` SET ".implode(', ', array_filter($k))." WHERE $where"; |
|
267 | + $this->exec($q, $data); |
|
268 | 268 | return $this->last_exec_success; |
269 | 269 | } |
270 | 270 | |
271 | - public function update($table, $data, $pk='id', $extra_where=''){ |
|
271 | + public function update($table, $data, $pk = 'id', $extra_where = '') { |
|
272 | 272 | return $this->updateWhere($table, $data, "`$pk`=:$pk $extra_where", $pk); |
273 | 273 | } |
274 | 274 | |
275 | - public function insertOrUpdate($table, $data=[], $pk='id', $extra_where=''){ |
|
276 | - if(!$this->connection()) return false; |
|
275 | + public function insertOrUpdate($table, $data = [], $pk = 'id', $extra_where = '') { |
|
276 | + if (!$this->connection()) return false; |
|
277 | 277 | |
278 | - if (false==is_array($data)) $data = (array)$data; |
|
278 | + if (false == is_array($data)) $data = (array) $data; |
|
279 | 279 | if (empty($data[$pk])) return $this->insert($table, $data); |
280 | - if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ){ |
|
280 | + if ((string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk]) { |
|
281 | 281 | return $this->update($table, $data, $pk, $extra_where); |
282 | 282 | } else { |
283 | 283 | return $this->insert($table, $data); |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @param array $options Options to pass to the PDO constructor |
26 | 26 | * @return SQLConnection The datasource resource |
27 | 27 | */ |
28 | - public static function register($name, $dsn, $username=null, $password=null, $options=[]){ |
|
28 | + public static function register($name, $dsn, $username=null, $password=null, $options=[]) { |
|
29 | 29 | return self::$connections[$name] = new SQLConnection($dsn, $username, $password, $options); |
30 | 30 | } |
31 | 31 | |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param array $options Options to pass to the PDO constructor |
38 | 38 | * @return SQLConnection The datasource resource |
39 | 39 | */ |
40 | - public static function connect($dsn, $username=null, $password=null, $options=[]){ |
|
40 | + public static function connect($dsn, $username=null, $password=null, $options=[]) { |
|
41 | 41 | return self::register('default', $dsn, $username, $password, $options); |
42 | 42 | } |
43 | 43 | |
@@ -46,11 +46,13 @@ discard block |
||
46 | 46 | * @param string $name The datasource name |
47 | 47 | * @return bool `true` if correctly changed |
48 | 48 | */ |
49 | - public static function defaultTo($name){ |
|
50 | - if (isset(self::$connections[$name])){ |
|
49 | + public static function defaultTo($name) { |
|
50 | + if (isset(self::$connections[$name])) { |
|
51 | 51 | self::$current = $name; |
52 | 52 | return true; |
53 | - } else return false; |
|
53 | + } else { |
|
54 | + return false; |
|
55 | + } |
|
54 | 56 | } |
55 | 57 | |
56 | 58 | /** |
@@ -58,14 +60,16 @@ discard block |
||
58 | 60 | * @param string $name The datasource name, omit for close all of them |
59 | 61 | * @return bool `true` if one or more datasource where closed |
60 | 62 | */ |
61 | - public static function close($name=null){ |
|
63 | + public static function close($name=null) { |
|
62 | 64 | if ($name === null) { |
63 | 65 | foreach (self::$connections as $conn) $conn->close(); |
64 | 66 | return true; |
65 | - } else if (isset(self::$connections[$name])){ |
|
67 | + } else if (isset(self::$connections[$name])) { |
|
66 | 68 | self::$connections[$name]->close(); |
67 | 69 | return true; |
68 | - } else return false; |
|
70 | + } else { |
|
71 | + return false; |
|
72 | + } |
|
69 | 73 | } |
70 | 74 | |
71 | 75 | /** |
@@ -73,8 +77,10 @@ discard block |
||
73 | 77 | * @param strinf $name The datasource name |
74 | 78 | * @return SQLConnect The datasource connection |
75 | 79 | */ |
76 | - public static function using($name){ |
|
77 | - if (empty(self::$connections[$name])) throw new \Exception("[SQL] Unknown connection named '$name'."); |
|
80 | + public static function using($name) { |
|
81 | + if (empty(self::$connections[$name])) { |
|
82 | + throw new \Exception("[SQL] Unknown connection named '$name'."); |
|
83 | + } |
|
78 | 84 | return self::$connections[$name]; |
79 | 85 | } |
80 | 86 | |
@@ -84,8 +90,10 @@ discard block |
||
84 | 90 | * @param array $args The method arguments |
85 | 91 | * @return mixed The method return value |
86 | 92 | */ |
87 | - public static function __callStatic($method, $args){ |
|
88 | - if (empty(self::$connections[self::$current])) throw new \Exception("[SQL] No default connection defined."); |
|
93 | + public static function __callStatic($method, $args) { |
|
94 | + if (empty(self::$connections[self::$current])) { |
|
95 | + throw new \Exception("[SQL] No default connection defined."); |
|
96 | + } |
|
89 | 97 | return call_user_func_array([self::$connections[self::$current],$method],$args); |
90 | 98 | } |
91 | 99 | |
@@ -101,7 +109,7 @@ discard block |
||
101 | 109 | $queries = [], |
102 | 110 | $last_exec_success = true; |
103 | 111 | |
104 | - public function __construct($dsn, $username=null, $password=null, $options=[]){ |
|
112 | + public function __construct($dsn, $username=null, $password=null, $options=[]) { |
|
105 | 113 | $this->connection = [ |
106 | 114 | 'dsn' => $dsn, |
107 | 115 | 'pdo' => null, |
@@ -119,11 +127,11 @@ discard block |
||
119 | 127 | } |
120 | 128 | } |
121 | 129 | |
122 | - public function close(){ |
|
130 | + public function close() { |
|
123 | 131 | $this->connection['pdo'] = null; |
124 | 132 | } |
125 | 133 | |
126 | - public function connection(){ |
|
134 | + public function connection() { |
|
127 | 135 | if(empty($this->connection['pdo'])) { |
128 | 136 | try { |
129 | 137 | $this->connection['pdo'] = new PDO( |
@@ -141,17 +149,23 @@ discard block |
||
141 | 149 | return $this->connection['pdo']; |
142 | 150 | } |
143 | 151 | |
144 | - public function prepare($query){ |
|
145 | - if(!$this->connection()) return false; |
|
152 | + public function prepare($query) { |
|
153 | + if(!$this->connection()) { |
|
154 | + return false; |
|
155 | + } |
|
146 | 156 | return isset($this->queries[$query]) ? $this->queries[$query] : ($this->queries[$query] = $this->connection()->prepare($query)); |
147 | 157 | } |
148 | 158 | |
149 | - public function exec($query, $params=[]){ |
|
150 | - if(!$this->connection()) return false; |
|
159 | + public function exec($query, $params=[]) { |
|
160 | + if(!$this->connection()) { |
|
161 | + return false; |
|
162 | + } |
|
151 | 163 | |
152 | - if (false==is_array($params)) $params = (array)$params; |
|
164 | + if (false==is_array($params)) { |
|
165 | + $params = (array)$params; |
|
166 | + } |
|
153 | 167 | $query = Filter::with('core.sql.query',$query); |
154 | - if($statement = $this->prepare($query)){ |
|
168 | + if($statement = $this->prepare($query)) { |
|
155 | 169 | Event::trigger('core.sql.query',$query,$params,(bool)$statement); |
156 | 170 | |
157 | 171 | foreach ($params as $key => $val) { |
@@ -175,60 +189,76 @@ discard block |
||
175 | 189 | return $statement; |
176 | 190 | } |
177 | 191 | |
178 | - public function value($query, $params=[], $column=0){ |
|
179 | - if(!$this->connection()) return false; |
|
192 | + public function value($query, $params=[], $column=0) { |
|
193 | + if(!$this->connection()) { |
|
194 | + return false; |
|
195 | + } |
|
180 | 196 | |
181 | 197 | $res = $this->exec($query,$params); |
182 | 198 | return $res ? $res->fetchColumn($column) : null; |
183 | 199 | } |
184 | 200 | |
185 | - public function each($query, $params=[], callable $looper = null){ |
|
186 | - if(!$this->connection()) return false; |
|
201 | + public function each($query, $params=[], callable $looper = null) { |
|
202 | + if(!$this->connection()) { |
|
203 | + return false; |
|
204 | + } |
|
187 | 205 | |
188 | 206 | // ($query,$looper) shorthand |
189 | 207 | if ($looper===null && is_callable($params)) {$looper = $params; $params = [];} |
190 | - if( $res = $this->exec($query,$params) ){ |
|
191 | - if(is_callable($looper)) |
|
192 | - while ($row = $res->fetchObject()) $looper($row); |
|
193 | - else |
|
194 | - return $res->fetchAll(PDO::FETCH_CLASS); |
|
208 | + if( $res = $this->exec($query,$params) ) { |
|
209 | + if(is_callable($looper)) { |
|
210 | + while ($row = $res->fetchObject()) $looper($row); |
|
211 | + } else { |
|
212 | + return $res->fetchAll(PDO::FETCH_CLASS); |
|
213 | + } |
|
195 | 214 | } |
196 | 215 | } |
197 | 216 | |
198 | - public function single($query, $params=[], callable $handler = null){ |
|
199 | - if(!$this->connection()) return false; |
|
217 | + public function single($query, $params=[], callable $handler = null) { |
|
218 | + if(!$this->connection()) { |
|
219 | + return false; |
|
220 | + } |
|
200 | 221 | |
201 | 222 | // ($query,$handler) shorthand |
202 | 223 | if ($handler===null && is_callable($params)) {$handler = $params; $params = [];} |
203 | - if( $res = $this->exec($query,$params) ){ |
|
204 | - if (is_callable($handler)) |
|
205 | - $handler($res->fetchObject()); |
|
206 | - else |
|
207 | - return $res->fetchObject(); |
|
224 | + if( $res = $this->exec($query,$params) ) { |
|
225 | + if (is_callable($handler)) { |
|
226 | + $handler($res->fetchObject()); |
|
227 | + } else { |
|
228 | + return $res->fetchObject(); |
|
229 | + } |
|
208 | 230 | } |
209 | 231 | } |
210 | 232 | |
211 | - public function run($script){ |
|
212 | - if(!$this->connection()) return false; |
|
233 | + public function run($script) { |
|
234 | + if(!$this->connection()) { |
|
235 | + return false; |
|
236 | + } |
|
213 | 237 | |
214 | 238 | $sql_path = Options::get('database.sql.path',APP_DIR.'/sql'); |
215 | 239 | $sql_sep = Options::get('database.sql.separator',';'); |
216 | - if (is_file($f = "$sql_path/$script.sql")){ |
|
240 | + if (is_file($f = "$sql_path/$script.sql")) { |
|
217 | 241 | $result = true; |
218 | 242 | foreach(explode($sql_sep,file_get_contents($f)) as $statement) { |
219 | 243 | $result = $this->exec($statement); |
220 | 244 | } |
221 | 245 | return $result; |
222 | - } else return false; |
|
246 | + } else { |
|
247 | + return false; |
|
248 | + } |
|
223 | 249 | } |
224 | 250 | |
225 | - public function all($query, $params=[], callable $looper = null){ |
|
226 | - if(!$this->connection()) return false; |
|
251 | + public function all($query, $params=[], callable $looper = null) { |
|
252 | + if(!$this->connection()) { |
|
253 | + return false; |
|
254 | + } |
|
227 | 255 | return $this->each($query,$params,$looper); |
228 | 256 | } |
229 | 257 | |
230 | - public function delete($table, $pks=null, $pk='id', $inclusive=true){ |
|
231 | - if(!$this->connection()) return false; |
|
258 | + public function delete($table, $pks=null, $pk='id', $inclusive=true) { |
|
259 | + if(!$this->connection()) { |
|
260 | + return false; |
|
261 | + } |
|
232 | 262 | |
233 | 263 | if (null===$pks) { |
234 | 264 | return $this->exec("DELETE FROM `$table`"); |
@@ -237,10 +267,14 @@ discard block |
||
237 | 267 | } |
238 | 268 | } |
239 | 269 | |
240 | - public function insert($table, $data){ |
|
241 | - if(!$this->connection()) return false; |
|
270 | + public function insert($table, $data) { |
|
271 | + if(!$this->connection()) { |
|
272 | + return false; |
|
273 | + } |
|
242 | 274 | |
243 | - if (false==is_array($data)) $data = (array)$data; |
|
275 | + if (false==is_array($data)) { |
|
276 | + $data = (array)$data; |
|
277 | + } |
|
244 | 278 | $k = array_keys($data); |
245 | 279 | asort($k); |
246 | 280 | $pk = $k; |
@@ -250,11 +284,17 @@ discard block |
||
250 | 284 | return $this->last_exec_success ? $this->connection()->lastInsertId() : false; |
251 | 285 | } |
252 | 286 | |
253 | - public function updateWhere($table, $data, $where, $pk='id'){ |
|
254 | - if(!$this->connection()) return false; |
|
287 | + public function updateWhere($table, $data, $where, $pk='id') { |
|
288 | + if(!$this->connection()) { |
|
289 | + return false; |
|
290 | + } |
|
255 | 291 | |
256 | - if (false==is_array($data)) $data = (array)$data; |
|
257 | - if (empty($data)) return false; |
|
292 | + if (false==is_array($data)) { |
|
293 | + $data = (array)$data; |
|
294 | + } |
|
295 | + if (empty($data)) { |
|
296 | + return false; |
|
297 | + } |
|
258 | 298 | $k = array_keys($data); |
259 | 299 | asort($k); |
260 | 300 | |
@@ -268,16 +308,22 @@ discard block |
||
268 | 308 | return $this->last_exec_success; |
269 | 309 | } |
270 | 310 | |
271 | - public function update($table, $data, $pk='id', $extra_where=''){ |
|
311 | + public function update($table, $data, $pk='id', $extra_where='') { |
|
272 | 312 | return $this->updateWhere($table, $data, "`$pk`=:$pk $extra_where", $pk); |
273 | 313 | } |
274 | 314 | |
275 | - public function insertOrUpdate($table, $data=[], $pk='id', $extra_where=''){ |
|
276 | - if(!$this->connection()) return false; |
|
315 | + public function insertOrUpdate($table, $data=[], $pk='id', $extra_where='') { |
|
316 | + if(!$this->connection()) { |
|
317 | + return false; |
|
318 | + } |
|
277 | 319 | |
278 | - if (false==is_array($data)) $data = (array)$data; |
|
279 | - if (empty($data[$pk])) return $this->insert($table, $data); |
|
280 | - if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ){ |
|
320 | + if (false==is_array($data)) { |
|
321 | + $data = (array)$data; |
|
322 | + } |
|
323 | + if (empty($data[$pk])) { |
|
324 | + return $this->insert($table, $data); |
|
325 | + } |
|
326 | + if( (string) $this->value("SELECT `$pk` FROM `$table` WHERE `$pk`=? LIMIT 1", [$data[$pk]]) === (string) $data[$pk] ) { |
|
281 | 327 | return $this->update($table, $data, $pk, $extra_where); |
282 | 328 | } else { |
283 | 329 | return $this->insert($table, $data); |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | |
90 | 90 | /** |
91 | 91 | * Check if an response output buffering is active. |
92 | - * @return boolean |
|
92 | + * @return boolean|null |
|
93 | 93 | */ |
94 | 94 | public static function isBuffering(){ |
95 | 95 | return static::$buffer; |
@@ -113,7 +113,6 @@ discard block |
||
113 | 113 | |
114 | 114 | /** |
115 | 115 | * Append a text to the buffer. |
116 | - * @param mixed $payload Text to append to the response buffer |
|
117 | 116 | */ |
118 | 117 | public static function text(){ |
119 | 118 | static::type(static::TYPE_TEXT); |
@@ -122,7 +121,6 @@ discard block |
||
122 | 121 | |
123 | 122 | /** |
124 | 123 | * Append an XML string to the buffer. |
125 | - * @param mixed $payload Data to append to the response buffer |
|
126 | 124 | */ |
127 | 125 | public static function xml(){ |
128 | 126 | static::type(static::TYPE_XML); |
@@ -131,7 +129,6 @@ discard block |
||
131 | 129 | |
132 | 130 | /** |
133 | 131 | * Append a SVG string to the buffer. |
134 | - * @param mixed $payload Data to append to the response buffer |
|
135 | 132 | */ |
136 | 133 | public static function svg(){ |
137 | 134 | static::type(static::TYPE_SVG); |
@@ -140,7 +137,6 @@ discard block |
||
140 | 137 | |
141 | 138 | /** |
142 | 139 | * Append an HTML string to the buffer. |
143 | - * @param mixed $payload Data to append to the response buffer |
|
144 | 140 | */ |
145 | 141 | public static function html(){ |
146 | 142 | static::type(static::TYPE_HTML); |
@@ -149,16 +145,21 @@ discard block |
||
149 | 145 | |
150 | 146 | /** |
151 | 147 | * Append a raw string to the buffer. |
152 | - * @param mixed $payload Data to append to the response buffer |
|
153 | 148 | */ |
154 | 149 | public static function add(){ |
155 | 150 | static::$payload[] = implode('',func_get_args()); |
156 | 151 | } |
157 | 152 | |
153 | + /** |
|
154 | + * @param integer $code |
|
155 | + */ |
|
158 | 156 | public static function status($code,$message=''){ |
159 | 157 | static::header('Status',$message?:$code,$code); |
160 | 158 | } |
161 | 159 | |
160 | + /** |
|
161 | + * @param string $name |
|
162 | + */ |
|
162 | 163 | public static function header($name,$value,$code=null){ |
163 | 164 | static::$headers[$name] = [$value,$code]; |
164 | 165 | } |
@@ -23,12 +23,12 @@ discard block |
||
23 | 23 | TYPE_BIN = 'application/octet-stream'; |
24 | 24 | |
25 | 25 | protected static $payload = [], |
26 | - $status = 200, |
|
27 | - $charset = "utf-8", |
|
28 | - $headers = ['Content-Type' => ['text/html; charset=utf-8']], |
|
29 | - $buffer = null, |
|
30 | - $force_dl = false, |
|
31 | - $link = null; |
|
26 | + $status = 200, |
|
27 | + $charset = "utf-8", |
|
28 | + $headers = ['Content-Type' => ['text/html; charset=utf-8']], |
|
29 | + $buffer = null, |
|
30 | + $force_dl = false, |
|
31 | + $link = null; |
|
32 | 32 | |
33 | 33 | |
34 | 34 | public static function charset($charset){ |
@@ -184,8 +184,8 @@ discard block |
||
184 | 184 | } |
185 | 185 | |
186 | 186 | public static function headers($setHeaders=null){ |
187 | - if ($setHeaders) static::$headers = $setHeaders; |
|
188 | - return static::$headers; |
|
187 | + if ($setHeaders) static::$headers = $setHeaders; |
|
188 | + return static::$headers; |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | $link = null; |
32 | 32 | |
33 | 33 | |
34 | - public static function charset($charset){ |
|
34 | + public static function charset($charset) { |
|
35 | 35 | static::$charset = $charset; |
36 | 36 | } |
37 | 37 | |
38 | - public static function type($mime){ |
|
39 | - static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
|
38 | + public static function type($mime) { |
|
39 | + static::header('Content-Type', $mime.(static::$charset ? '; charset='.static::$charset : '')); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
@@ -44,39 +44,39 @@ discard block |
||
44 | 44 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
45 | 45 | * @return [type] [description] |
46 | 46 | */ |
47 | - public static function download($filename){ |
|
47 | + public static function download($filename) { |
|
48 | 48 | static::$force_dl = $filename; |
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
52 | 52 | * Start capturing output |
53 | 53 | */ |
54 | - public static function start(){ |
|
54 | + public static function start() { |
|
55 | 55 | static::$buffer = ob_start(); |
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Enable CORS HTTP headers. |
60 | 60 | */ |
61 | - public static function enableCORS(){ |
|
61 | + public static function enableCORS() { |
|
62 | 62 | |
63 | 63 | // Allow from any origin |
64 | - if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
|
64 | + if ($origin = filter_input(INPUT_SERVER, 'HTTP_ORIGIN')) { |
|
65 | 65 | static::header('Access-Control-Allow-Origin', $origin); |
66 | 66 | static::header('Access-Control-Allow-Credentials', 'true'); |
67 | 67 | static::header('Access-Control-Max-Age', 86400); |
68 | 68 | } |
69 | 69 | |
70 | 70 | // Access-Control headers are received during OPTIONS requests |
71 | - if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') { |
|
71 | + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == 'OPTIONS') { |
|
72 | 72 | static::clean(); |
73 | 73 | |
74 | - if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
74 | + if (filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
75 | 75 | static::header('Access-Control-Allow-Methods', |
76 | 76 | 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE'); |
77 | 77 | } |
78 | - if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
79 | - static::header('Access-Control-Allow-Headers',$req_h); |
|
78 | + if ($req_h = filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
79 | + static::header('Access-Control-Allow-Headers', $req_h); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | static::send(); |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | * Finish the output buffer capturing. |
89 | 89 | * @return string The captured buffer |
90 | 90 | */ |
91 | - public static function end(){ |
|
92 | - if (static::$buffer){ |
|
91 | + public static function end() { |
|
92 | + if (static::$buffer) { |
|
93 | 93 | static::$payload[] = ob_get_contents(); |
94 | 94 | ob_end_clean(); |
95 | 95 | static::$buffer = null; |
@@ -101,14 +101,14 @@ discard block |
||
101 | 101 | * Check if an response output buffering is active. |
102 | 102 | * @return boolean |
103 | 103 | */ |
104 | - public static function isBuffering(){ |
|
104 | + public static function isBuffering() { |
|
105 | 105 | return static::$buffer; |
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
109 | 109 | * Clear the response body |
110 | 110 | */ |
111 | - public static function clean(){ |
|
111 | + public static function clean() { |
|
112 | 112 | static::$payload = []; |
113 | 113 | } |
114 | 114 | |
@@ -116,74 +116,74 @@ discard block |
||
116 | 116 | * Append a JSON object to the buffer. |
117 | 117 | * @param mixed $payload Data to append to the response buffer |
118 | 118 | */ |
119 | - public static function json($payload){ |
|
119 | + public static function json($payload) { |
|
120 | 120 | static::type(static::TYPE_JSON); |
121 | - static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
|
121 | + static::$payload[] = json_encode($payload, Options::get('core.response.json_flags', JSON_NUMERIC_CHECK)); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
125 | 125 | * Append a text to the buffer. |
126 | 126 | * @param mixed $payload Text to append to the response buffer |
127 | 127 | */ |
128 | - public static function text(){ |
|
128 | + public static function text() { |
|
129 | 129 | static::type(static::TYPE_TEXT); |
130 | - static::$payload[] = implode('',func_get_args()); |
|
130 | + static::$payload[] = implode('', func_get_args()); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | /** |
134 | 134 | * Append an XML string to the buffer. |
135 | 135 | * @param mixed $payload Data to append to the response buffer |
136 | 136 | */ |
137 | - public static function xml(){ |
|
137 | + public static function xml() { |
|
138 | 138 | static::type(static::TYPE_XML); |
139 | - static::$payload[] = implode('',func_get_args()); |
|
139 | + static::$payload[] = implode('', func_get_args()); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
143 | 143 | * Append a SVG string to the buffer. |
144 | 144 | * @param mixed $payload Data to append to the response buffer |
145 | 145 | */ |
146 | - public static function svg(){ |
|
146 | + public static function svg() { |
|
147 | 147 | static::type(static::TYPE_SVG); |
148 | - static::$payload[] = implode('',func_get_args()); |
|
148 | + static::$payload[] = implode('', func_get_args()); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | /** |
152 | 152 | * Append an HTML string to the buffer. |
153 | 153 | * @param mixed $payload Data to append to the response buffer |
154 | 154 | */ |
155 | - public static function html(){ |
|
155 | + public static function html() { |
|
156 | 156 | static::type(static::TYPE_HTML); |
157 | - static::$payload[] = implode('',func_get_args()); |
|
157 | + static::$payload[] = implode('', func_get_args()); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
161 | 161 | * Append a raw string to the buffer. |
162 | 162 | * @param mixed $payload Data to append to the response buffer |
163 | 163 | */ |
164 | - public static function add(){ |
|
165 | - static::$payload[] = implode('',func_get_args()); |
|
164 | + public static function add() { |
|
165 | + static::$payload[] = implode('', func_get_args()); |
|
166 | 166 | } |
167 | 167 | |
168 | - public static function status($code,$message=''){ |
|
169 | - static::header('Status',$message?:$code,$code); |
|
168 | + public static function status($code, $message = '') { |
|
169 | + static::header('Status', $message ?: $code, $code); |
|
170 | 170 | } |
171 | 171 | |
172 | - public static function header($name,$value,$code=null){ |
|
173 | - static::$headers[$name] = [$value,$code]; |
|
172 | + public static function header($name, $value, $code = null) { |
|
173 | + static::$headers[$name] = [$value, $code]; |
|
174 | 174 | } |
175 | 175 | |
176 | - public static function error($code=500,$message='Application Error'){ |
|
177 | - Event::trigger('core.response.error',$code,$message); |
|
178 | - static::status($code,$message); |
|
176 | + public static function error($code = 500, $message = 'Application Error') { |
|
177 | + Event::trigger('core.response.error', $code, $message); |
|
178 | + static::status($code, $message); |
|
179 | 179 | } |
180 | 180 | |
181 | - public static function body($setBody=null){ |
|
181 | + public static function body($setBody = null) { |
|
182 | 182 | if ($setBody) static::$payload = [$setBody]; |
183 | - return Filter::with('core.response.body',is_array(static::$payload)?implode('',static::$payload):static::$payload); |
|
183 | + return Filter::with('core.response.body', is_array(static::$payload) ? implode('', static::$payload) : static::$payload); |
|
184 | 184 | } |
185 | 185 | |
186 | - public static function headers($setHeaders=null){ |
|
186 | + public static function headers($setHeaders = null) { |
|
187 | 187 | if ($setHeaders) static::$headers = $setHeaders; |
188 | 188 | return static::$headers; |
189 | 189 | } |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | * |
196 | 196 | * @return array Headers and body of the response |
197 | 197 | */ |
198 | - public static function save(){ |
|
198 | + public static function save() { |
|
199 | 199 | return [ |
200 | 200 | 'head' => static::$headers, |
201 | 201 | 'body' => static::body(), |
@@ -209,13 +209,13 @@ discard block |
||
209 | 209 | * |
210 | 210 | * @param array $data head/body saved state |
211 | 211 | */ |
212 | - public static function load($data){ |
|
213 | - $data = (object)$data; |
|
212 | + public static function load($data) { |
|
213 | + $data = (object) $data; |
|
214 | 214 | if (isset($data->head)) static::headers($data->head); |
215 | 215 | if (isset($data->body)) static::body($data->body); |
216 | 216 | } |
217 | 217 | |
218 | - public static function send(){ |
|
218 | + public static function send() { |
|
219 | 219 | Event::trigger('core.response.send'); |
220 | 220 | if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
221 | 221 | if (is_array($value_code)) { |
@@ -225,8 +225,8 @@ discard block |
||
225 | 225 | $code = null; |
226 | 226 | } |
227 | 227 | |
228 | - if ($value == 'Status'){ |
|
229 | - if (function_exists('http_response_code')){ |
|
228 | + if ($value == 'Status') { |
|
229 | + if (function_exists('http_response_code')) { |
|
230 | 230 | http_response_code($code); |
231 | 231 | } else { |
232 | 232 | header("Status: $code", true, $code); |
@@ -31,11 +31,11 @@ discard block |
||
31 | 31 | $link = null; |
32 | 32 | |
33 | 33 | |
34 | - public static function charset($charset){ |
|
34 | + public static function charset($charset) { |
|
35 | 35 | static::$charset = $charset; |
36 | 36 | } |
37 | 37 | |
38 | - public static function type($mime){ |
|
38 | + public static function type($mime) { |
|
39 | 39 | static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
40 | 40 | } |
41 | 41 | |
@@ -44,21 +44,21 @@ discard block |
||
44 | 44 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
45 | 45 | * @return [type] [description] |
46 | 46 | */ |
47 | - public static function download($filename){ |
|
47 | + public static function download($filename) { |
|
48 | 48 | static::$force_dl = $filename; |
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
52 | 52 | * Start capturing output |
53 | 53 | */ |
54 | - public static function start(){ |
|
54 | + public static function start() { |
|
55 | 55 | static::$buffer = ob_start(); |
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Enable CORS HTTP headers. |
60 | 60 | */ |
61 | - public static function enableCORS(){ |
|
61 | + public static function enableCORS() { |
|
62 | 62 | |
63 | 63 | // Allow from any origin |
64 | 64 | if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | * Finish the output buffer capturing. |
89 | 89 | * @return string The captured buffer |
90 | 90 | */ |
91 | - public static function end(){ |
|
92 | - if (static::$buffer){ |
|
91 | + public static function end() { |
|
92 | + if (static::$buffer) { |
|
93 | 93 | static::$payload[] = ob_get_contents(); |
94 | 94 | ob_end_clean(); |
95 | 95 | static::$buffer = null; |
@@ -101,14 +101,14 @@ discard block |
||
101 | 101 | * Check if an response output buffering is active. |
102 | 102 | * @return boolean |
103 | 103 | */ |
104 | - public static function isBuffering(){ |
|
104 | + public static function isBuffering() { |
|
105 | 105 | return static::$buffer; |
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
109 | 109 | * Clear the response body |
110 | 110 | */ |
111 | - public static function clean(){ |
|
111 | + public static function clean() { |
|
112 | 112 | static::$payload = []; |
113 | 113 | } |
114 | 114 | |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * Append a JSON object to the buffer. |
117 | 117 | * @param mixed $payload Data to append to the response buffer |
118 | 118 | */ |
119 | - public static function json($payload){ |
|
119 | + public static function json($payload) { |
|
120 | 120 | static::type(static::TYPE_JSON); |
121 | 121 | static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
122 | 122 | } |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | * Append a text to the buffer. |
126 | 126 | * @param mixed $payload Text to append to the response buffer |
127 | 127 | */ |
128 | - public static function text(){ |
|
128 | + public static function text() { |
|
129 | 129 | static::type(static::TYPE_TEXT); |
130 | 130 | static::$payload[] = implode('',func_get_args()); |
131 | 131 | } |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | * Append an XML string to the buffer. |
135 | 135 | * @param mixed $payload Data to append to the response buffer |
136 | 136 | */ |
137 | - public static function xml(){ |
|
137 | + public static function xml() { |
|
138 | 138 | static::type(static::TYPE_XML); |
139 | 139 | static::$payload[] = implode('',func_get_args()); |
140 | 140 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * Append a SVG string to the buffer. |
144 | 144 | * @param mixed $payload Data to append to the response buffer |
145 | 145 | */ |
146 | - public static function svg(){ |
|
146 | + public static function svg() { |
|
147 | 147 | static::type(static::TYPE_SVG); |
148 | 148 | static::$payload[] = implode('',func_get_args()); |
149 | 149 | } |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * Append an HTML string to the buffer. |
153 | 153 | * @param mixed $payload Data to append to the response buffer |
154 | 154 | */ |
155 | - public static function html(){ |
|
155 | + public static function html() { |
|
156 | 156 | static::type(static::TYPE_HTML); |
157 | 157 | static::$payload[] = implode('',func_get_args()); |
158 | 158 | } |
@@ -161,30 +161,34 @@ discard block |
||
161 | 161 | * Append a raw string to the buffer. |
162 | 162 | * @param mixed $payload Data to append to the response buffer |
163 | 163 | */ |
164 | - public static function add(){ |
|
164 | + public static function add() { |
|
165 | 165 | static::$payload[] = implode('',func_get_args()); |
166 | 166 | } |
167 | 167 | |
168 | - public static function status($code,$message=''){ |
|
168 | + public static function status($code,$message='') { |
|
169 | 169 | static::header('Status',$message?:$code,$code); |
170 | 170 | } |
171 | 171 | |
172 | - public static function header($name,$value,$code=null){ |
|
172 | + public static function header($name,$value,$code=null) { |
|
173 | 173 | static::$headers[$name] = [$value,$code]; |
174 | 174 | } |
175 | 175 | |
176 | - public static function error($code=500,$message='Application Error'){ |
|
176 | + public static function error($code=500,$message='Application Error') { |
|
177 | 177 | Event::trigger('core.response.error',$code,$message); |
178 | 178 | static::status($code,$message); |
179 | 179 | } |
180 | 180 | |
181 | - public static function body($setBody=null){ |
|
182 | - if ($setBody) static::$payload = [$setBody]; |
|
181 | + public static function body($setBody=null) { |
|
182 | + if ($setBody) { |
|
183 | + static::$payload = [$setBody]; |
|
184 | + } |
|
183 | 185 | return Filter::with('core.response.body',is_array(static::$payload)?implode('',static::$payload):static::$payload); |
184 | 186 | } |
185 | 187 | |
186 | - public static function headers($setHeaders=null){ |
|
187 | - if ($setHeaders) static::$headers = $setHeaders; |
|
188 | + public static function headers($setHeaders=null) { |
|
189 | + if ($setHeaders) { |
|
190 | + static::$headers = $setHeaders; |
|
191 | + } |
|
188 | 192 | return static::$headers; |
189 | 193 | } |
190 | 194 | |
@@ -195,7 +199,7 @@ discard block |
||
195 | 199 | * |
196 | 200 | * @return array Headers and body of the response |
197 | 201 | */ |
198 | - public static function save(){ |
|
202 | + public static function save() { |
|
199 | 203 | return [ |
200 | 204 | 'head' => static::$headers, |
201 | 205 | 'body' => static::body(), |
@@ -209,24 +213,30 @@ discard block |
||
209 | 213 | * |
210 | 214 | * @param array $data head/body saved state |
211 | 215 | */ |
212 | - public static function load($data){ |
|
216 | + public static function load($data) { |
|
213 | 217 | $data = (object)$data; |
214 | - if (isset($data->head)) static::headers($data->head); |
|
215 | - if (isset($data->body)) static::body($data->body); |
|
218 | + if (isset($data->head)) { |
|
219 | + static::headers($data->head); |
|
220 | + } |
|
221 | + if (isset($data->body)) { |
|
222 | + static::body($data->body); |
|
223 | + } |
|
216 | 224 | } |
217 | 225 | |
218 | - public static function send(){ |
|
226 | + public static function send() { |
|
219 | 227 | Event::trigger('core.response.send'); |
220 | - if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
|
228 | + if (false === headers_sent()) { |
|
229 | + foreach (static::$headers as $name => $value_code) { |
|
221 | 230 | if (is_array($value_code)) { |
222 | 231 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
232 | + } |
|
223 | 233 | } else { |
224 | 234 | $value = $value_code; |
225 | 235 | $code = null; |
226 | 236 | } |
227 | 237 | |
228 | - if ($value == 'Status'){ |
|
229 | - if (function_exists('http_response_code')){ |
|
238 | + if ($value == 'Status') { |
|
239 | + if (function_exists('http_response_code')) { |
|
230 | 240 | http_response_code($code); |
231 | 241 | } else { |
232 | 242 | header("Status: $code", true, $code); |
@@ -238,7 +248,9 @@ discard block |
||
238 | 248 | : header("$name: $value", true); |
239 | 249 | } |
240 | 250 | } |
241 | - if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
251 | + if (static::$force_dl) { |
|
252 | + header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
253 | + } |
|
242 | 254 | echo static::body(); |
243 | 255 | } |
244 | 256 |
@@ -32,7 +32,6 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @access public |
34 | 34 | * @static |
35 | - * @param string $key The session name |
|
36 | 35 | * @return string The session value |
37 | 36 | */ |
38 | 37 | static public function name($name=null){ |
@@ -62,7 +61,7 @@ discard block |
||
62 | 61 | * |
63 | 62 | * @access public |
64 | 63 | * @static |
65 | - * @param mixed $key The variable name |
|
64 | + * @param string $key The variable name |
|
66 | 65 | * @param mixed $value The variable value |
67 | 66 | * @return void |
68 | 67 | */ |
@@ -80,7 +79,7 @@ discard block |
||
80 | 79 | * |
81 | 80 | * @access public |
82 | 81 | * @static |
83 | - * @param mixed $key The variable name |
|
82 | + * @param string $key The variable name |
|
84 | 83 | * @return void |
85 | 84 | */ |
86 | 85 | static public function delete($key){ |
@@ -107,7 +106,7 @@ discard block |
||
107 | 106 | * |
108 | 107 | * @access public |
109 | 108 | * @static |
110 | - * @return void |
|
109 | + * @return boolean |
|
111 | 110 | */ |
112 | 111 | static public function active(){ |
113 | 112 | return session_status() == PHP_SESSION_ACTIVE; |
@@ -13,126 +13,126 @@ discard block |
||
13 | 13 | class Session { |
14 | 14 | use Module; |
15 | 15 | |
16 | - /** |
|
17 | - * Start session handler |
|
18 | - * |
|
19 | - * @access public |
|
20 | - * @static |
|
21 | - * @return void |
|
22 | - */ |
|
23 | - static public function start($name=null){ |
|
24 | - if (isset($_SESSION)) return; |
|
25 | - static::name($name); |
|
26 | - session_cache_limiter('must-revalidate'); |
|
27 | - @session_start(); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Get/Set Session name |
|
32 | - * |
|
33 | - * @access public |
|
34 | - * @static |
|
35 | - * @param string $key The session name |
|
36 | - * @return string The session value |
|
37 | - */ |
|
38 | - static public function name($name=null){ |
|
39 | - return $name ? session_name($name) : session_name(); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Get a session variable reference |
|
44 | - * |
|
45 | - * @access public |
|
46 | - * @static |
|
47 | - * @param mixed $key The variable name |
|
48 | - * @return mixed The variable value |
|
49 | - */ |
|
50 | - static public function get($key,$default=null){ |
|
16 | + /** |
|
17 | + * Start session handler |
|
18 | + * |
|
19 | + * @access public |
|
20 | + * @static |
|
21 | + * @return void |
|
22 | + */ |
|
23 | + static public function start($name=null){ |
|
24 | + if (isset($_SESSION)) return; |
|
25 | + static::name($name); |
|
26 | + session_cache_limiter('must-revalidate'); |
|
27 | + @session_start(); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Get/Set Session name |
|
32 | + * |
|
33 | + * @access public |
|
34 | + * @static |
|
35 | + * @param string $key The session name |
|
36 | + * @return string The session value |
|
37 | + */ |
|
38 | + static public function name($name=null){ |
|
39 | + return $name ? session_name($name) : session_name(); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Get a session variable reference |
|
44 | + * |
|
45 | + * @access public |
|
46 | + * @static |
|
47 | + * @param mixed $key The variable name |
|
48 | + * @return mixed The variable value |
|
49 | + */ |
|
50 | + static public function get($key,$default=null){ |
|
51 | 51 | if (($active = static::active()) && isset($_SESSION[$key])) { |
52 | - return $_SESSION[$key]; |
|
52 | + return $_SESSION[$key]; |
|
53 | 53 | } else if ($active) { |
54 | - return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default); |
|
54 | + return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default); |
|
55 | 55 | } else { |
56 | - return (is_callable($default)?call_user_func($default):$default); |
|
56 | + return (is_callable($default)?call_user_func($default):$default); |
|
57 | 57 | } |
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Set a session variable |
|
62 | - * |
|
63 | - * @access public |
|
64 | - * @static |
|
65 | - * @param mixed $key The variable name |
|
66 | - * @param mixed $value The variable value |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - static public function set($key,$value=null){ |
|
70 | - static::start(); |
|
71 | - if($value==null && is_array($key)){ |
|
72 | - foreach($key as $k=>$v) $_SESSION[$k]=$v; |
|
73 | - } else { |
|
74 | - $_SESSION[$key] = $value; |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Delete a session variable |
|
80 | - * |
|
81 | - * @access public |
|
82 | - * @static |
|
83 | - * @param mixed $key The variable name |
|
84 | - * @return void |
|
85 | - */ |
|
86 | - static public function delete($key){ |
|
87 | - static::start(); |
|
88 | - unset($_SESSION[$key]); |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Delete all session variables |
|
94 | - * |
|
95 | - * @access public |
|
96 | - * @static |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - static public function clear(){ |
|
100 | - static::start(); |
|
101 | - session_unset(); |
|
102 | - session_destroy(); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Check if session is active |
|
107 | - * |
|
108 | - * @access public |
|
109 | - * @static |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - static public function active(){ |
|
113 | - return session_status() == PHP_SESSION_ACTIVE; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Check if a session variable exists |
|
118 | - * |
|
119 | - * @access public |
|
120 | - * @static |
|
121 | - * @param mixed $key The variable name |
|
122 | - * @return bool |
|
123 | - */ |
|
124 | - static public function exists($key){ |
|
125 | - static::start(); |
|
126 | - return isset($_SESSION[$key]); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Return a read-only accessor to session variables for in-view use. |
|
131 | - * @return SessionReadOnly |
|
132 | - */ |
|
133 | - static public function readOnly(){ |
|
134 | - return new SessionReadOnly; |
|
135 | - } |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Set a session variable |
|
62 | + * |
|
63 | + * @access public |
|
64 | + * @static |
|
65 | + * @param mixed $key The variable name |
|
66 | + * @param mixed $value The variable value |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + static public function set($key,$value=null){ |
|
70 | + static::start(); |
|
71 | + if($value==null && is_array($key)){ |
|
72 | + foreach($key as $k=>$v) $_SESSION[$k]=$v; |
|
73 | + } else { |
|
74 | + $_SESSION[$key] = $value; |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Delete a session variable |
|
80 | + * |
|
81 | + * @access public |
|
82 | + * @static |
|
83 | + * @param mixed $key The variable name |
|
84 | + * @return void |
|
85 | + */ |
|
86 | + static public function delete($key){ |
|
87 | + static::start(); |
|
88 | + unset($_SESSION[$key]); |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Delete all session variables |
|
94 | + * |
|
95 | + * @access public |
|
96 | + * @static |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + static public function clear(){ |
|
100 | + static::start(); |
|
101 | + session_unset(); |
|
102 | + session_destroy(); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Check if session is active |
|
107 | + * |
|
108 | + * @access public |
|
109 | + * @static |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + static public function active(){ |
|
113 | + return session_status() == PHP_SESSION_ACTIVE; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Check if a session variable exists |
|
118 | + * |
|
119 | + * @access public |
|
120 | + * @static |
|
121 | + * @param mixed $key The variable name |
|
122 | + * @return bool |
|
123 | + */ |
|
124 | + static public function exists($key){ |
|
125 | + static::start(); |
|
126 | + return isset($_SESSION[$key]); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Return a read-only accessor to session variables for in-view use. |
|
131 | + * @return SessionReadOnly |
|
132 | + */ |
|
133 | + static public function readOnly(){ |
|
134 | + return new SessionReadOnly; |
|
135 | + } |
|
136 | 136 | |
137 | 137 | } /* End of class */ |
138 | 138 | |
@@ -144,36 +144,36 @@ discard block |
||
144 | 144 | |
145 | 145 | class SessionReadOnly { |
146 | 146 | |
147 | - /** |
|
148 | - * Get a session variable reference |
|
149 | - * |
|
150 | - * @access public |
|
151 | - * @param mixed $key The variable name |
|
152 | - * @return mixed The variable value |
|
153 | - */ |
|
154 | - public function get($key){ |
|
155 | - return Session::get($key); |
|
156 | - } |
|
157 | - public function __get($key){ |
|
158 | - return Session::get($key); |
|
159 | - } |
|
160 | - |
|
161 | - public function name(){ |
|
162 | - return Session::name(); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Check if a session variable exists |
|
167 | - * |
|
168 | - * @access public |
|
169 | - * @param mixed $key The variable name |
|
170 | - * @return bool |
|
171 | - */ |
|
172 | - public function exists($key){ |
|
173 | - return Session::exists($key); |
|
174 | - } |
|
175 | - public function __isset($key){ |
|
176 | - return Session::exists($key); |
|
177 | - } |
|
147 | + /** |
|
148 | + * Get a session variable reference |
|
149 | + * |
|
150 | + * @access public |
|
151 | + * @param mixed $key The variable name |
|
152 | + * @return mixed The variable value |
|
153 | + */ |
|
154 | + public function get($key){ |
|
155 | + return Session::get($key); |
|
156 | + } |
|
157 | + public function __get($key){ |
|
158 | + return Session::get($key); |
|
159 | + } |
|
160 | + |
|
161 | + public function name(){ |
|
162 | + return Session::name(); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Check if a session variable exists |
|
167 | + * |
|
168 | + * @access public |
|
169 | + * @param mixed $key The variable name |
|
170 | + * @return bool |
|
171 | + */ |
|
172 | + public function exists($key){ |
|
173 | + return Session::exists($key); |
|
174 | + } |
|
175 | + public function __isset($key){ |
|
176 | + return Session::exists($key); |
|
177 | + } |
|
178 | 178 | |
179 | 179 | } /* End of class */ |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * @static |
21 | 21 | * @return void |
22 | 22 | */ |
23 | - static public function start($name=null){ |
|
23 | + static public function start($name = null) { |
|
24 | 24 | if (isset($_SESSION)) return; |
25 | 25 | static::name($name); |
26 | 26 | session_cache_limiter('must-revalidate'); |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * @param string $key The session name |
36 | 36 | * @return string The session value |
37 | 37 | */ |
38 | - static public function name($name=null){ |
|
38 | + static public function name($name = null) { |
|
39 | 39 | return $name ? session_name($name) : session_name(); |
40 | 40 | } |
41 | 41 | |
@@ -47,13 +47,13 @@ discard block |
||
47 | 47 | * @param mixed $key The variable name |
48 | 48 | * @return mixed The variable value |
49 | 49 | */ |
50 | - static public function get($key,$default=null){ |
|
50 | + static public function get($key, $default = null) { |
|
51 | 51 | if (($active = static::active()) && isset($_SESSION[$key])) { |
52 | 52 | return $_SESSION[$key]; |
53 | 53 | } else if ($active) { |
54 | - return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default); |
|
54 | + return $_SESSION[$key] = (is_callable($default) ? call_user_func($default) : $default); |
|
55 | 55 | } else { |
56 | - return (is_callable($default)?call_user_func($default):$default); |
|
56 | + return (is_callable($default) ? call_user_func($default) : $default); |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | * @param mixed $value The variable value |
67 | 67 | * @return void |
68 | 68 | */ |
69 | - static public function set($key,$value=null){ |
|
69 | + static public function set($key, $value = null) { |
|
70 | 70 | static::start(); |
71 | - if($value==null && is_array($key)){ |
|
72 | - foreach($key as $k=>$v) $_SESSION[$k]=$v; |
|
71 | + if ($value == null && is_array($key)) { |
|
72 | + foreach ($key as $k=>$v) $_SESSION[$k] = $v; |
|
73 | 73 | } else { |
74 | 74 | $_SESSION[$key] = $value; |
75 | 75 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @param mixed $key The variable name |
84 | 84 | * @return void |
85 | 85 | */ |
86 | - static public function delete($key){ |
|
86 | + static public function delete($key) { |
|
87 | 87 | static::start(); |
88 | 88 | unset($_SESSION[$key]); |
89 | 89 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * @static |
97 | 97 | * @return void |
98 | 98 | */ |
99 | - static public function clear(){ |
|
99 | + static public function clear() { |
|
100 | 100 | static::start(); |
101 | 101 | session_unset(); |
102 | 102 | session_destroy(); |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | * @static |
110 | 110 | * @return void |
111 | 111 | */ |
112 | - static public function active(){ |
|
112 | + static public function active() { |
|
113 | 113 | return session_status() == PHP_SESSION_ACTIVE; |
114 | 114 | } |
115 | 115 | |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * @param mixed $key The variable name |
122 | 122 | * @return bool |
123 | 123 | */ |
124 | - static public function exists($key){ |
|
124 | + static public function exists($key) { |
|
125 | 125 | static::start(); |
126 | 126 | return isset($_SESSION[$key]); |
127 | 127 | } |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * Return a read-only accessor to session variables for in-view use. |
131 | 131 | * @return SessionReadOnly |
132 | 132 | */ |
133 | - static public function readOnly(){ |
|
133 | + static public function readOnly() { |
|
134 | 134 | return new SessionReadOnly; |
135 | 135 | } |
136 | 136 | |
@@ -151,14 +151,14 @@ discard block |
||
151 | 151 | * @param mixed $key The variable name |
152 | 152 | * @return mixed The variable value |
153 | 153 | */ |
154 | - public function get($key){ |
|
154 | + public function get($key) { |
|
155 | 155 | return Session::get($key); |
156 | 156 | } |
157 | - public function __get($key){ |
|
157 | + public function __get($key) { |
|
158 | 158 | return Session::get($key); |
159 | 159 | } |
160 | 160 | |
161 | - public function name(){ |
|
161 | + public function name() { |
|
162 | 162 | return Session::name(); |
163 | 163 | } |
164 | 164 | |
@@ -169,10 +169,10 @@ discard block |
||
169 | 169 | * @param mixed $key The variable name |
170 | 170 | * @return bool |
171 | 171 | */ |
172 | - public function exists($key){ |
|
172 | + public function exists($key) { |
|
173 | 173 | return Session::exists($key); |
174 | 174 | } |
175 | - public function __isset($key){ |
|
175 | + public function __isset($key) { |
|
176 | 176 | return Session::exists($key); |
177 | 177 | } |
178 | 178 |
@@ -20,8 +20,10 @@ discard block |
||
20 | 20 | * @static |
21 | 21 | * @return void |
22 | 22 | */ |
23 | - static public function start($name=null){ |
|
24 | - if (isset($_SESSION)) return; |
|
23 | + static public function start($name=null) { |
|
24 | + if (isset($_SESSION)) { |
|
25 | + return; |
|
26 | + } |
|
25 | 27 | static::name($name); |
26 | 28 | session_cache_limiter('must-revalidate'); |
27 | 29 | @session_start(); |
@@ -35,7 +37,7 @@ discard block |
||
35 | 37 | * @param string $key The session name |
36 | 38 | * @return string The session value |
37 | 39 | */ |
38 | - static public function name($name=null){ |
|
40 | + static public function name($name=null) { |
|
39 | 41 | return $name ? session_name($name) : session_name(); |
40 | 42 | } |
41 | 43 | |
@@ -47,7 +49,7 @@ discard block |
||
47 | 49 | * @param mixed $key The variable name |
48 | 50 | * @return mixed The variable value |
49 | 51 | */ |
50 | - static public function get($key,$default=null){ |
|
52 | + static public function get($key,$default=null) { |
|
51 | 53 | if (($active = static::active()) && isset($_SESSION[$key])) { |
52 | 54 | return $_SESSION[$key]; |
53 | 55 | } else if ($active) { |
@@ -66,9 +68,9 @@ discard block |
||
66 | 68 | * @param mixed $value The variable value |
67 | 69 | * @return void |
68 | 70 | */ |
69 | - static public function set($key,$value=null){ |
|
71 | + static public function set($key,$value=null) { |
|
70 | 72 | static::start(); |
71 | - if($value==null && is_array($key)){ |
|
73 | + if($value==null && is_array($key)) { |
|
72 | 74 | foreach($key as $k=>$v) $_SESSION[$k]=$v; |
73 | 75 | } else { |
74 | 76 | $_SESSION[$key] = $value; |
@@ -83,7 +85,7 @@ discard block |
||
83 | 85 | * @param mixed $key The variable name |
84 | 86 | * @return void |
85 | 87 | */ |
86 | - static public function delete($key){ |
|
88 | + static public function delete($key) { |
|
87 | 89 | static::start(); |
88 | 90 | unset($_SESSION[$key]); |
89 | 91 | } |
@@ -96,7 +98,7 @@ discard block |
||
96 | 98 | * @static |
97 | 99 | * @return void |
98 | 100 | */ |
99 | - static public function clear(){ |
|
101 | + static public function clear() { |
|
100 | 102 | static::start(); |
101 | 103 | session_unset(); |
102 | 104 | session_destroy(); |
@@ -109,7 +111,7 @@ discard block |
||
109 | 111 | * @static |
110 | 112 | * @return void |
111 | 113 | */ |
112 | - static public function active(){ |
|
114 | + static public function active() { |
|
113 | 115 | return session_status() == PHP_SESSION_ACTIVE; |
114 | 116 | } |
115 | 117 | |
@@ -121,7 +123,7 @@ discard block |
||
121 | 123 | * @param mixed $key The variable name |
122 | 124 | * @return bool |
123 | 125 | */ |
124 | - static public function exists($key){ |
|
126 | + static public function exists($key) { |
|
125 | 127 | static::start(); |
126 | 128 | return isset($_SESSION[$key]); |
127 | 129 | } |
@@ -130,7 +132,7 @@ discard block |
||
130 | 132 | * Return a read-only accessor to session variables for in-view use. |
131 | 133 | * @return SessionReadOnly |
132 | 134 | */ |
133 | - static public function readOnly(){ |
|
135 | + static public function readOnly() { |
|
134 | 136 | return new SessionReadOnly; |
135 | 137 | } |
136 | 138 | |
@@ -151,14 +153,14 @@ discard block |
||
151 | 153 | * @param mixed $key The variable name |
152 | 154 | * @return mixed The variable value |
153 | 155 | */ |
154 | - public function get($key){ |
|
156 | + public function get($key) { |
|
155 | 157 | return Session::get($key); |
156 | 158 | } |
157 | - public function __get($key){ |
|
159 | + public function __get($key) { |
|
158 | 160 | return Session::get($key); |
159 | 161 | } |
160 | 162 | |
161 | - public function name(){ |
|
163 | + public function name() { |
|
162 | 164 | return Session::name(); |
163 | 165 | } |
164 | 166 | |
@@ -169,10 +171,10 @@ discard block |
||
169 | 171 | * @param mixed $key The variable name |
170 | 172 | * @return bool |
171 | 173 | */ |
172 | - public function exists($key){ |
|
174 | + public function exists($key) { |
|
173 | 175 | return Session::exists($key); |
174 | 176 | } |
175 | - public function __isset($key){ |
|
177 | + public function __isset($key) { |
|
176 | 178 | return Session::exists($key); |
177 | 179 | } |
178 | 180 |