@@ -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 | - echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
169 | + // Use preg_replace_callback for fast regex matches navigation |
|
170 | + echo strtr(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 | - },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
|
180 | + },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
|
181 | 181 | } else { |
182 | - echo strtr($message,["&BR;"=>PHP_EOL]); |
|
182 | + echo strtr($message,["&BR;"=>PHP_EOL]); |
|
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,22 +164,22 @@ 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 | - echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
170 | + echo strtr(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 | - },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
|
180 | + },strtr($message, ["\n"=>"&BR;"])), ["&BR;"=>PHP_EOL]); |
|
181 | 181 | } else { |
182 | - echo strtr($message,["&BR;"=>PHP_EOL]); |
|
182 | + echo strtr($message, ["&BR;"=>PHP_EOL]); |
|
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
@@ -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 | - echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
174 | + echo strtr(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 | },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
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 |
@@ -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 | Event::trigger('core.route.end', $this); |
@@ -303,10 +303,10 @@ discard block |
||
303 | 303 | $route = new static($URLPattern); |
304 | 304 | $route->callback = []; |
305 | 305 | foreach ($callbacks as $method => $callback) { |
306 | - $method = strtolower($method); |
|
307 | - if (Request::method() !== $method) continue; |
|
308 | - $route->callback[$method] = $callback; |
|
309 | - $route->methods[$method] = 1; |
|
306 | + $method = strtolower($method); |
|
307 | + if (Request::method() !== $method) continue; |
|
308 | + $route->callback[$method] = $callback; |
|
309 | + $route->methods[$method] = 1; |
|
310 | 310 | } |
311 | 311 | return $route; |
312 | 312 | } |
@@ -408,9 +408,9 @@ discard block |
||
408 | 408 | } |
409 | 409 | |
410 | 410 | public static function exitWithError($code,$message="Application Error"){ |
411 | - Response::error($code,$message); |
|
412 | - Response::send(); |
|
413 | - exit; |
|
411 | + Response::error($code,$message); |
|
412 | + Response::send(); |
|
413 | + exit; |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | /** |
@@ -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); |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | * @param string $method The HTTP Method to check against. |
168 | 168 | * @return array The callback response. |
169 | 169 | */ |
170 | - public function runIfMatch($URL, $method='get'){ |
|
171 | - return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
|
170 | + public function runIfMatch($URL, $method = 'get') { |
|
171 | + return ($args = $this->match($URL, $method)) ? $this->run($args, $method) : null; |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
@@ -177,8 +177,8 @@ discard block |
||
177 | 177 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
178 | 178 | * @return Route |
179 | 179 | */ |
180 | - public static function on($URLPattern, $callback = null){ |
|
181 | - return new Route($URLPattern,$callback); |
|
180 | + public static function on($URLPattern, $callback = null) { |
|
181 | + return new Route($URLPattern, $callback); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
@@ -187,8 +187,8 @@ discard block |
||
187 | 187 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
188 | 188 | * @return Route |
189 | 189 | */ |
190 | - public static function get($URLPattern, $callback = null){ |
|
191 | - return (new Route($URLPattern,$callback))->via('get'); |
|
190 | + public static function get($URLPattern, $callback = null) { |
|
191 | + return (new Route($URLPattern, $callback))->via('get'); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -197,8 +197,8 @@ discard block |
||
197 | 197 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
198 | 198 | * @return Route |
199 | 199 | */ |
200 | - public static function post($URLPattern, $callback = null){ |
|
201 | - return (new Route($URLPattern,$callback))->via('post'); |
|
200 | + public static function post($URLPattern, $callback = null) { |
|
201 | + return (new Route($URLPattern, $callback))->via('post'); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -207,8 +207,8 @@ discard block |
||
207 | 207 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
208 | 208 | * @return Route |
209 | 209 | */ |
210 | - public static function any($URLPattern, $callback = null){ |
|
211 | - return (new Route($URLPattern,$callback))->via('*'); |
|
210 | + public static function any($URLPattern, $callback = null) { |
|
211 | + return (new Route($URLPattern, $callback))->via('*'); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | /** |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | */ |
254 | 254 | public function & via(){ |
255 | 255 | $this->methods = []; |
256 | - foreach (func_get_args() as $method){ |
|
256 | + foreach (func_get_args() as $method) { |
|
257 | 257 | $this->methods[strtolower($method)] = true; |
258 | 258 | } |
259 | 259 | return $this; |
@@ -275,10 +275,10 @@ discard block |
||
275 | 275 | * @return Route |
276 | 276 | */ |
277 | 277 | public function & rules(array $rules){ |
278 | - foreach ((array)$rules as $varname => $rule){ |
|
278 | + foreach ((array) $rules as $varname => $rule) { |
|
279 | 279 | $this->rules[$varname] = $rule; |
280 | 280 | } |
281 | - $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
|
281 | + $this->pattern = $this->compilePatternAsRegex($this->URLPattern, $this->rules); |
|
282 | 282 | return $this; |
283 | 283 | } |
284 | 284 | |
@@ -316,10 +316,10 @@ discard block |
||
316 | 316 | * @param string $pattern The URL schema. |
317 | 317 | * @return string The compiled PREG RegEx. |
318 | 318 | */ |
319 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
320 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
321 | - return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
|
322 | - },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
|
319 | + protected static function compilePatternAsRegex($pattern, $rules = []) { |
|
320 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S', function($g) use (&$rules){ |
|
321 | + return '(?<'.$g[1].'>'.(isset($rules[$g[1]]) ? $rules[$g[1]] : '[^/]+').')'; |
|
322 | + },str_replace(['.', ')', '*'], ['\.', ')?', '.+'], $pattern)).'$#'; |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | /** |
@@ -329,10 +329,10 @@ discard block |
||
329 | 329 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
330 | 330 | * @return array The extracted variables |
331 | 331 | */ |
332 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
332 | + protected static function extractVariablesFromURL($pattern, $URL = null, $cut = false) { |
|
333 | 333 | $URL = $URL ?: Request::URI(); |
334 | - $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
|
335 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
334 | + $pattern = $cut ? str_replace('$#', '', $pattern).'#' : $pattern; |
|
335 | + if (!preg_match($pattern, $URL, $args)) return false; |
|
336 | 336 | foreach ($args as $key => $value) { |
337 | 337 | if (false === is_string($key)) unset($args[$key]); |
338 | 338 | } |
@@ -344,8 +344,8 @@ discard block |
||
344 | 344 | * @param string $pattern The URL schema. |
345 | 345 | * @return boolean |
346 | 346 | */ |
347 | - protected static function isDynamic($pattern){ |
|
348 | - return strlen($pattern) != strcspn($pattern,':(?[*+'); |
|
347 | + protected static function isDynamic($pattern) { |
|
348 | + return strlen($pattern) != strcspn($pattern, ':(?[*+'); |
|
349 | 349 | } |
350 | 350 | |
351 | 351 | /** |
@@ -353,9 +353,9 @@ discard block |
||
353 | 353 | * @param Route $r |
354 | 354 | * @return Route |
355 | 355 | */ |
356 | - public static function add($r){ |
|
357 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
358 | - return static::$routes[implode('',static::$prefix)][] = $r; |
|
356 | + public static function add($r) { |
|
357 | + if (isset(static::$group[0])) static::$group[0]->add($r); |
|
358 | + return static::$routes[implode('', static::$prefix)][] = $r; |
|
359 | 359 | } |
360 | 360 | |
361 | 361 | /** |
@@ -363,16 +363,16 @@ discard block |
||
363 | 363 | * @param string $prefix The url prefix for the internal route definitions. |
364 | 364 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
365 | 365 | */ |
366 | - public static function group($prefix,$callback=null){ |
|
366 | + public static function group($prefix, $callback = null) { |
|
367 | 367 | |
368 | 368 | // Skip definition if current request doesn't match group. |
369 | 369 | |
370 | - $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
|
370 | + $prefix_complete = rtrim(implode('', static::$prefix), '/').$prefix; |
|
371 | 371 | |
372 | - if ( static::isDynamic($prefix) ){ |
|
372 | + if (static::isDynamic($prefix)) { |
|
373 | 373 | |
374 | 374 | // Dynamic group, capture vars |
375 | - $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
|
375 | + $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
|
376 | 376 | |
377 | 377 | // Errors in compile pattern or variable extraction, aborting. |
378 | 378 | if (false === $vars) return; |
@@ -380,14 +380,14 @@ discard block |
||
380 | 380 | static::$prefix[] = $prefix; |
381 | 381 | if (empty(static::$group)) static::$group = []; |
382 | 382 | array_unshift(static::$group, new RouteGroup()); |
383 | - if ($callback) call_user_func_array($callback,$vars); |
|
383 | + if ($callback) call_user_func_array($callback, $vars); |
|
384 | 384 | $group = static::$group[0]; |
385 | 385 | array_shift(static::$group); |
386 | 386 | array_pop(static::$prefix); |
387 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
387 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
388 | 388 | return $group; |
389 | 389 | |
390 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
390 | + } else if (0 === strpos(Request::URI(), $prefix_complete)) { |
|
391 | 391 | |
392 | 392 | // Static group |
393 | 393 | static::$prefix[] = $prefix; |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | $group = static::$group[0]; |
398 | 398 | array_shift(static::$group); |
399 | 399 | array_pop(static::$prefix); |
400 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
400 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
401 | 401 | return $group; |
402 | 402 | } else { |
403 | 403 | |
@@ -407,8 +407,8 @@ discard block |
||
407 | 407 | |
408 | 408 | } |
409 | 409 | |
410 | - public static function exitWithError($code,$message="Application Error"){ |
|
411 | - Response::error($code,$message); |
|
410 | + public static function exitWithError($code, $message = "Application Error") { |
|
411 | + Response::error($code, $message); |
|
412 | 412 | Response::send(); |
413 | 413 | exit; |
414 | 414 | } |
@@ -418,20 +418,20 @@ discard block |
||
418 | 418 | * @param string $URL The URL to match onto. |
419 | 419 | * @return boolean true if a route callback was executed. |
420 | 420 | */ |
421 | - public static function dispatch($URL=null,$method=null){ |
|
421 | + public static function dispatch($URL = null, $method = null) { |
|
422 | 422 | if (!$URL) $URL = Request::URI(); |
423 | 423 | if (!$method) $method = Request::method(); |
424 | 424 | |
425 | - $__deferred_send = new Deferred(function(){ |
|
426 | - if (Options::get('core.response.autosend',true)){ |
|
425 | + $__deferred_send = new Deferred(function() { |
|
426 | + if (Options::get('core.response.autosend', true)) { |
|
427 | 427 | Response::send(); |
428 | 428 | } |
429 | 429 | }); |
430 | 430 | |
431 | - foreach ((array)static::$routes as $group => $routes){ |
|
431 | + foreach ((array) static::$routes as $group => $routes) { |
|
432 | 432 | foreach ($routes as $route) { |
433 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
434 | - $route->run($args,$method); |
|
433 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
434 | + $route->run($args, $method); |
|
435 | 435 | return true; |
436 | 436 | } |
437 | 437 | } |
@@ -446,34 +446,34 @@ discard block |
||
446 | 446 | class RouteGroup { |
447 | 447 | protected $routes; |
448 | 448 | |
449 | - public function __construct(){ |
|
449 | + public function __construct() { |
|
450 | 450 | $this->routes = new SplObjectStorage; |
451 | 451 | return Route::add($this); |
452 | 452 | } |
453 | 453 | |
454 | - public function has($r){ |
|
454 | + public function has($r) { |
|
455 | 455 | return $this->routes->contains($r); |
456 | 456 | } |
457 | 457 | |
458 | - public function add($r){ |
|
458 | + public function add($r) { |
|
459 | 459 | $this->routes->attach($r); |
460 | 460 | return $this; |
461 | 461 | } |
462 | 462 | |
463 | - public function remove($r){ |
|
463 | + public function remove($r) { |
|
464 | 464 | if ($this->routes->contains($r)) $this->routes->detach($r); |
465 | 465 | return $this; |
466 | 466 | } |
467 | 467 | |
468 | - public function before($callbacks){ |
|
469 | - foreach ($this->routes as $route){ |
|
468 | + public function before($callbacks) { |
|
469 | + foreach ($this->routes as $route) { |
|
470 | 470 | $route->before($callbacks); |
471 | 471 | } |
472 | 472 | return $this; |
473 | 473 | } |
474 | 474 | |
475 | - public function after($callbacks){ |
|
476 | - foreach ($this->routes as $route){ |
|
475 | + public function after($callbacks) { |
|
476 | + foreach ($this->routes as $route) { |
|
477 | 477 | $route->after($callbacks); |
478 | 478 | } |
479 | 479 | 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); |
@@ -167,7 +171,7 @@ discard block |
||
167 | 171 | * @param string $method The HTTP Method to check against. |
168 | 172 | * @return array The callback response. |
169 | 173 | */ |
170 | - public function runIfMatch($URL, $method='get'){ |
|
174 | + public function runIfMatch($URL, $method='get') { |
|
171 | 175 | return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
172 | 176 | } |
173 | 177 | |
@@ -177,7 +181,7 @@ discard block |
||
177 | 181 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
178 | 182 | * @return Route |
179 | 183 | */ |
180 | - public static function on($URLPattern, $callback = null){ |
|
184 | + public static function on($URLPattern, $callback = null) { |
|
181 | 185 | return new Route($URLPattern,$callback); |
182 | 186 | } |
183 | 187 | |
@@ -187,7 +191,7 @@ discard block |
||
187 | 191 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
188 | 192 | * @return Route |
189 | 193 | */ |
190 | - public static function get($URLPattern, $callback = null){ |
|
194 | + public static function get($URLPattern, $callback = null) { |
|
191 | 195 | return (new Route($URLPattern,$callback))->via('get'); |
192 | 196 | } |
193 | 197 | |
@@ -197,7 +201,7 @@ discard block |
||
197 | 201 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
198 | 202 | * @return Route |
199 | 203 | */ |
200 | - public static function post($URLPattern, $callback = null){ |
|
204 | + public static function post($URLPattern, $callback = null) { |
|
201 | 205 | return (new Route($URLPattern,$callback))->via('post'); |
202 | 206 | } |
203 | 207 | |
@@ -207,7 +211,7 @@ discard block |
||
207 | 211 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
208 | 212 | * @return Route |
209 | 213 | */ |
210 | - public static function any($URLPattern, $callback = null){ |
|
214 | + public static function any($URLPattern, $callback = null) { |
|
211 | 215 | return (new Route($URLPattern,$callback))->via('*'); |
212 | 216 | } |
213 | 217 | |
@@ -253,7 +257,7 @@ discard block |
||
253 | 257 | */ |
254 | 258 | public function & via(){ |
255 | 259 | $this->methods = []; |
256 | - foreach (func_get_args() as $method){ |
|
260 | + foreach (func_get_args() as $method) { |
|
257 | 261 | $this->methods[strtolower($method)] = true; |
258 | 262 | } |
259 | 263 | return $this; |
@@ -275,7 +279,7 @@ discard block |
||
275 | 279 | * @return Route |
276 | 280 | */ |
277 | 281 | public function & rules(array $rules){ |
278 | - foreach ((array)$rules as $varname => $rule){ |
|
282 | + foreach ((array)$rules as $varname => $rule) { |
|
279 | 283 | $this->rules[$varname] = $rule; |
280 | 284 | } |
281 | 285 | $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
@@ -304,7 +308,9 @@ discard block |
||
304 | 308 | $route->callback = []; |
305 | 309 | foreach ($callbacks as $method => $callback) { |
306 | 310 | $method = strtolower($method); |
307 | - if (Request::method() !== $method) continue; |
|
311 | + if (Request::method() !== $method) { |
|
312 | + continue; |
|
313 | + } |
|
308 | 314 | $route->callback[$method] = $callback; |
309 | 315 | $route->methods[$method] = 1; |
310 | 316 | } |
@@ -316,8 +322,8 @@ discard block |
||
316 | 322 | * @param string $pattern The URL schema. |
317 | 323 | * @return string The compiled PREG RegEx. |
318 | 324 | */ |
319 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
320 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
325 | + protected static function compilePatternAsRegex($pattern, $rules=[]) { |
|
326 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules) { |
|
321 | 327 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
322 | 328 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
323 | 329 | } |
@@ -329,12 +335,16 @@ discard block |
||
329 | 335 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
330 | 336 | * @return array The extracted variables |
331 | 337 | */ |
332 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
338 | + protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false) { |
|
333 | 339 | $URL = $URL ?: Request::URI(); |
334 | 340 | $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
335 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
341 | + if ( !preg_match($pattern,$URL,$args) ) { |
|
342 | + return false; |
|
343 | + } |
|
336 | 344 | foreach ($args as $key => $value) { |
337 | - if (false === is_string($key)) unset($args[$key]); |
|
345 | + if (false === is_string($key)) { |
|
346 | + unset($args[$key]); |
|
347 | + } |
|
338 | 348 | } |
339 | 349 | return $args; |
340 | 350 | } |
@@ -344,7 +354,7 @@ discard block |
||
344 | 354 | * @param string $pattern The URL schema. |
345 | 355 | * @return boolean |
346 | 356 | */ |
347 | - protected static function isDynamic($pattern){ |
|
357 | + protected static function isDynamic($pattern) { |
|
348 | 358 | return strlen($pattern) != strcspn($pattern,':(?[*+'); |
349 | 359 | } |
350 | 360 | |
@@ -353,8 +363,10 @@ discard block |
||
353 | 363 | * @param Route $r |
354 | 364 | * @return Route |
355 | 365 | */ |
356 | - public static function add($r){ |
|
357 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
366 | + public static function add($r) { |
|
367 | + if ( isset(static::$group[0]) ) { |
|
368 | + static::$group[0]->add($r); |
|
369 | + } |
|
358 | 370 | return static::$routes[implode('',static::$prefix)][] = $r; |
359 | 371 | } |
360 | 372 | |
@@ -363,41 +375,55 @@ discard block |
||
363 | 375 | * @param string $prefix The url prefix for the internal route definitions. |
364 | 376 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
365 | 377 | */ |
366 | - public static function group($prefix,$callback=null){ |
|
378 | + public static function group($prefix,$callback=null) { |
|
367 | 379 | |
368 | 380 | // Skip definition if current request doesn't match group. |
369 | 381 | |
370 | 382 | $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
371 | 383 | |
372 | - if ( static::isDynamic($prefix) ){ |
|
384 | + if ( static::isDynamic($prefix) ) { |
|
373 | 385 | |
374 | 386 | // Dynamic group, capture vars |
375 | 387 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
376 | 388 | |
377 | 389 | // Errors in compile pattern or variable extraction, aborting. |
378 | - if (false === $vars) return; |
|
390 | + if (false === $vars) { |
|
391 | + return; |
|
392 | + } |
|
379 | 393 | |
380 | 394 | static::$prefix[] = $prefix; |
381 | - if (empty(static::$group)) static::$group = []; |
|
395 | + if (empty(static::$group)) { |
|
396 | + static::$group = []; |
|
397 | + } |
|
382 | 398 | array_unshift(static::$group, new RouteGroup()); |
383 | - if ($callback) call_user_func_array($callback,$vars); |
|
399 | + if ($callback) { |
|
400 | + call_user_func_array($callback,$vars); |
|
401 | + } |
|
384 | 402 | $group = static::$group[0]; |
385 | 403 | array_shift(static::$group); |
386 | 404 | array_pop(static::$prefix); |
387 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
405 | + if (empty(static::$prefix)) { |
|
406 | + static::$prefix=['']; |
|
407 | + } |
|
388 | 408 | return $group; |
389 | 409 | |
390 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
410 | + } else if ( 0 === strpos(Request::URI(), $prefix_complete) ) { |
|
391 | 411 | |
392 | 412 | // Static group |
393 | 413 | static::$prefix[] = $prefix; |
394 | - if (empty(static::$group)) static::$group = []; |
|
414 | + if (empty(static::$group)) { |
|
415 | + static::$group = []; |
|
416 | + } |
|
395 | 417 | array_unshift(static::$group, new RouteGroup()); |
396 | - if ($callback) call_user_func($callback); |
|
418 | + if ($callback) { |
|
419 | + call_user_func($callback); |
|
420 | + } |
|
397 | 421 | $group = static::$group[0]; |
398 | 422 | array_shift(static::$group); |
399 | 423 | array_pop(static::$prefix); |
400 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
424 | + if (empty(static::$prefix)) { |
|
425 | + static::$prefix=['']; |
|
426 | + } |
|
401 | 427 | return $group; |
402 | 428 | } else { |
403 | 429 | |
@@ -407,7 +433,7 @@ discard block |
||
407 | 433 | |
408 | 434 | } |
409 | 435 | |
410 | - public static function exitWithError($code,$message="Application Error"){ |
|
436 | + public static function exitWithError($code,$message="Application Error") { |
|
411 | 437 | Response::error($code,$message); |
412 | 438 | Response::send(); |
413 | 439 | exit; |
@@ -418,19 +444,23 @@ discard block |
||
418 | 444 | * @param string $URL The URL to match onto. |
419 | 445 | * @return boolean true if a route callback was executed. |
420 | 446 | */ |
421 | - public static function dispatch($URL=null,$method=null){ |
|
422 | - if (!$URL) $URL = Request::URI(); |
|
423 | - if (!$method) $method = Request::method(); |
|
447 | + public static function dispatch($URL=null,$method=null) { |
|
448 | + if (!$URL) { |
|
449 | + $URL = Request::URI(); |
|
450 | + } |
|
451 | + if (!$method) { |
|
452 | + $method = Request::method(); |
|
453 | + } |
|
424 | 454 | |
425 | - $__deferred_send = new Deferred(function(){ |
|
426 | - if (Options::get('core.response.autosend',true)){ |
|
455 | + $__deferred_send = new Deferred(function() { |
|
456 | + if (Options::get('core.response.autosend',true)) { |
|
427 | 457 | Response::send(); |
428 | 458 | } |
429 | 459 | }); |
430 | 460 | |
431 | - foreach ((array)static::$routes as $group => $routes){ |
|
461 | + foreach ((array)static::$routes as $group => $routes) { |
|
432 | 462 | foreach ($routes as $route) { |
433 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
463 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
434 | 464 | $route->run($args,$method); |
435 | 465 | return true; |
436 | 466 | } |
@@ -446,34 +476,36 @@ discard block |
||
446 | 476 | class RouteGroup { |
447 | 477 | protected $routes; |
448 | 478 | |
449 | - public function __construct(){ |
|
479 | + public function __construct() { |
|
450 | 480 | $this->routes = new SplObjectStorage; |
451 | 481 | return Route::add($this); |
452 | 482 | } |
453 | 483 | |
454 | - public function has($r){ |
|
484 | + public function has($r) { |
|
455 | 485 | return $this->routes->contains($r); |
456 | 486 | } |
457 | 487 | |
458 | - public function add($r){ |
|
488 | + public function add($r) { |
|
459 | 489 | $this->routes->attach($r); |
460 | 490 | return $this; |
461 | 491 | } |
462 | 492 | |
463 | - public function remove($r){ |
|
464 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
493 | + public function remove($r) { |
|
494 | + if ($this->routes->contains($r)) { |
|
495 | + $this->routes->detach($r); |
|
496 | + } |
|
465 | 497 | return $this; |
466 | 498 | } |
467 | 499 | |
468 | - public function before($callbacks){ |
|
469 | - foreach ($this->routes as $route){ |
|
500 | + public function before($callbacks) { |
|
501 | + foreach ($this->routes as $route) { |
|
470 | 502 | $route->before($callbacks); |
471 | 503 | } |
472 | 504 | return $this; |
473 | 505 | } |
474 | 506 | |
475 | - public function after($callbacks){ |
|
476 | - foreach ($this->routes as $route){ |
|
507 | + public function after($callbacks) { |
|
508 | + foreach ($this->routes as $route) { |
|
477 | 509 | $route->after($callbacks); |
478 | 510 | } |
479 | 511 | 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 |
@@ -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 | } |
@@ -32,11 +32,11 @@ discard block |
||
32 | 32 | $sent = false; |
33 | 33 | |
34 | 34 | |
35 | - public static function charset($charset){ |
|
35 | + public static function charset($charset) { |
|
36 | 36 | static::$charset = $charset; |
37 | 37 | } |
38 | 38 | |
39 | - public static function type($mime){ |
|
39 | + public static function type($mime) { |
|
40 | 40 | static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
41 | 41 | } |
42 | 42 | |
@@ -45,21 +45,21 @@ discard block |
||
45 | 45 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
46 | 46 | * @return [type] [description] |
47 | 47 | */ |
48 | - public static function download($filename){ |
|
48 | + public static function download($filename) { |
|
49 | 49 | static::$force_dl = $filename; |
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Start capturing output |
54 | 54 | */ |
55 | - public static function start(){ |
|
55 | + public static function start() { |
|
56 | 56 | static::$buffer = ob_start(); |
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
60 | 60 | * Enable CORS HTTP headers. |
61 | 61 | */ |
62 | - public static function enableCORS(){ |
|
62 | + public static function enableCORS() { |
|
63 | 63 | |
64 | 64 | // Allow from any origin |
65 | 65 | if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
@@ -93,8 +93,8 @@ discard block |
||
93 | 93 | * Finish the output buffer capturing. |
94 | 94 | * @return string The captured buffer |
95 | 95 | */ |
96 | - public static function end(){ |
|
97 | - if (static::$buffer){ |
|
96 | + public static function end() { |
|
97 | + if (static::$buffer) { |
|
98 | 98 | static::$payload[] = ob_get_contents(); |
99 | 99 | ob_end_clean(); |
100 | 100 | static::$buffer = null; |
@@ -106,14 +106,14 @@ discard block |
||
106 | 106 | * Check if an response output buffering is active. |
107 | 107 | * @return boolean |
108 | 108 | */ |
109 | - public static function isBuffering(){ |
|
109 | + public static function isBuffering() { |
|
110 | 110 | return static::$buffer; |
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
114 | 114 | * Clear the response body |
115 | 115 | */ |
116 | - public static function clean(){ |
|
116 | + public static function clean() { |
|
117 | 117 | static::$payload = []; |
118 | 118 | } |
119 | 119 | |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * Append a JSON object to the buffer. |
122 | 122 | * @param mixed $payload Data to append to the response buffer |
123 | 123 | */ |
124 | - public static function json($payload){ |
|
124 | + public static function json($payload) { |
|
125 | 125 | static::type(static::TYPE_JSON); |
126 | 126 | static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
127 | 127 | } |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * Append a text to the buffer. |
131 | 131 | * @param mixed $payload Text to append to the response buffer |
132 | 132 | */ |
133 | - public static function text(){ |
|
133 | + public static function text() { |
|
134 | 134 | static::type(static::TYPE_TEXT); |
135 | 135 | static::$payload[] = implode('',func_get_args()); |
136 | 136 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * Append an XML string to the buffer. |
140 | 140 | * @param mixed $payload Data to append to the response buffer |
141 | 141 | */ |
142 | - public static function xml(){ |
|
142 | + public static function xml() { |
|
143 | 143 | static::type(static::TYPE_XML); |
144 | 144 | static::$payload[] = implode('',func_get_args()); |
145 | 145 | } |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | * Append a SVG string to the buffer. |
149 | 149 | * @param mixed $payload Data to append to the response buffer |
150 | 150 | */ |
151 | - public static function svg(){ |
|
151 | + public static function svg() { |
|
152 | 152 | static::type(static::TYPE_SVG); |
153 | 153 | static::$payload[] = implode('',func_get_args()); |
154 | 154 | } |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | * Append an HTML string to the buffer. |
158 | 158 | * @param mixed $payload Data to append to the response buffer |
159 | 159 | */ |
160 | - public static function html(){ |
|
160 | + public static function html() { |
|
161 | 161 | static::type(static::TYPE_HTML); |
162 | 162 | static::$payload[] = implode('',func_get_args()); |
163 | 163 | } |
@@ -166,30 +166,34 @@ discard block |
||
166 | 166 | * Append a raw string to the buffer. |
167 | 167 | * @param mixed $payload Data to append to the response buffer |
168 | 168 | */ |
169 | - public static function add(){ |
|
169 | + public static function add() { |
|
170 | 170 | static::$payload[] = implode('',func_get_args()); |
171 | 171 | } |
172 | 172 | |
173 | - public static function status($code,$message=''){ |
|
173 | + public static function status($code,$message='') { |
|
174 | 174 | static::header('Status',$message?:$code,$code); |
175 | 175 | } |
176 | 176 | |
177 | - public static function header($name,$value,$code=null){ |
|
177 | + public static function header($name,$value,$code=null) { |
|
178 | 178 | static::$headers[$name] = [$value,$code]; |
179 | 179 | } |
180 | 180 | |
181 | - public static function error($code=500,$message='Application Error'){ |
|
181 | + public static function error($code=500,$message='Application Error') { |
|
182 | 182 | Event::trigger('core.response.error',$code,$message); |
183 | 183 | static::status($code,$message); |
184 | 184 | } |
185 | 185 | |
186 | - public static function body($setBody=null){ |
|
187 | - if ($setBody) static::$payload = [$setBody]; |
|
186 | + public static function body($setBody=null) { |
|
187 | + if ($setBody) { |
|
188 | + static::$payload = [$setBody]; |
|
189 | + } |
|
188 | 190 | return Filter::with('core.response.body',is_array(static::$payload)?implode('',static::$payload):static::$payload); |
189 | 191 | } |
190 | 192 | |
191 | - public static function headers($setHeaders=null){ |
|
192 | - if ($setHeaders) static::$headers = $setHeaders; |
|
193 | + public static function headers($setHeaders=null) { |
|
194 | + if ($setHeaders) { |
|
195 | + static::$headers = $setHeaders; |
|
196 | + } |
|
193 | 197 | return static::$headers; |
194 | 198 | } |
195 | 199 | |
@@ -200,7 +204,7 @@ discard block |
||
200 | 204 | * |
201 | 205 | * @return array Headers and body of the response |
202 | 206 | */ |
203 | - public static function save(){ |
|
207 | + public static function save() { |
|
204 | 208 | return [ |
205 | 209 | 'head' => static::$headers, |
206 | 210 | 'body' => static::body(), |
@@ -214,27 +218,33 @@ discard block |
||
214 | 218 | * |
215 | 219 | * @param array $data head/body saved state |
216 | 220 | */ |
217 | - public static function load($data){ |
|
221 | + public static function load($data) { |
|
218 | 222 | $data = (object)$data; |
219 | - if (isset($data->head)) static::headers($data->head); |
|
220 | - if (isset($data->body)) static::body($data->body); |
|
223 | + if (isset($data->head)) { |
|
224 | + static::headers($data->head); |
|
225 | + } |
|
226 | + if (isset($data->body)) { |
|
227 | + static::body($data->body); |
|
228 | + } |
|
221 | 229 | } |
222 | 230 | |
223 | - public static function send($force = false){ |
|
231 | + public static function send($force = false) { |
|
224 | 232 | if (!static::$sent || $force) { |
225 | 233 | static::$sent = true; |
226 | 234 | Event::trigger('core.response.send'); |
227 | - if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
|
235 | + if (false === headers_sent()) { |
|
236 | + foreach (static::$headers as $name => $value_code) { |
|
228 | 237 | |
229 | 238 | if (is_array($value_code)) { |
230 | 239 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
240 | + } |
|
231 | 241 | } else { |
232 | 242 | $value = $value_code; |
233 | 243 | $code = null; |
234 | 244 | } |
235 | 245 | |
236 | - if ($value == 'Status'){ |
|
237 | - if (function_exists('http_response_code')){ |
|
246 | + if ($value == 'Status') { |
|
247 | + if (function_exists('http_response_code')) { |
|
238 | 248 | http_response_code($code); |
239 | 249 | } else { |
240 | 250 | header("Status: $code", true, $code); |
@@ -246,7 +256,9 @@ discard block |
||
246 | 256 | : header("$name: $value", true); |
247 | 257 | } |
248 | 258 | } |
249 | - if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
259 | + if (static::$force_dl) { |
|
260 | + header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
261 | + } |
|
250 | 262 | echo static::body(); |
251 | 263 | } |
252 | 264 | } |
@@ -23,13 +23,13 @@ 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, |
|
32 | - $sent = false; |
|
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 | + $sent = false; |
|
33 | 33 | |
34 | 34 | |
35 | 35 | public static function charset($charset){ |
@@ -187,12 +187,12 @@ discard block |
||
187 | 187 | if ($setBody) static::$payload = [$setBody]; |
188 | 188 | return Filter::with('core.response.body', |
189 | 189 | is_array(static::$payload) ? implode('',static::$payload) : static::$payload |
190 | - ); |
|
190 | + ); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | public static function headers($setHeaders=null){ |
194 | - if ($setHeaders) static::$headers = $setHeaders; |
|
195 | - return static::$headers; |
|
194 | + if ($setHeaders) static::$headers = $setHeaders; |
|
195 | + return static::$headers; |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
@@ -32,12 +32,12 @@ discard block |
||
32 | 32 | $sent = false; |
33 | 33 | |
34 | 34 | |
35 | - public static function charset($charset){ |
|
35 | + public static function charset($charset) { |
|
36 | 36 | static::$charset = $charset; |
37 | 37 | } |
38 | 38 | |
39 | - public static function type($mime){ |
|
40 | - static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
|
39 | + public static function type($mime) { |
|
40 | + static::header('Content-Type', $mime.(static::$charset ? '; charset='.static::$charset : '')); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -45,39 +45,39 @@ discard block |
||
45 | 45 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
46 | 46 | * @return [type] [description] |
47 | 47 | */ |
48 | - public static function download($filename){ |
|
48 | + public static function download($filename) { |
|
49 | 49 | static::$force_dl = $filename; |
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Start capturing output |
54 | 54 | */ |
55 | - public static function start(){ |
|
55 | + public static function start() { |
|
56 | 56 | static::$buffer = ob_start(); |
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
60 | 60 | * Enable CORS HTTP headers. |
61 | 61 | */ |
62 | - public static function enableCORS(){ |
|
62 | + public static function enableCORS() { |
|
63 | 63 | |
64 | 64 | // Allow from any origin |
65 | - if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
|
65 | + if ($origin = filter_input(INPUT_SERVER, 'HTTP_ORIGIN')) { |
|
66 | 66 | static::header('Access-Control-Allow-Origin', $origin); |
67 | 67 | static::header('Access-Control-Allow-Credentials', 'true'); |
68 | 68 | static::header('Access-Control-Max-Age', 86400); |
69 | 69 | } |
70 | 70 | |
71 | 71 | // Access-Control headers are received during OPTIONS requests |
72 | - if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') { |
|
72 | + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == 'OPTIONS') { |
|
73 | 73 | static::clean(); |
74 | 74 | |
75 | - if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
75 | + if (filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
76 | 76 | static::header('Access-Control-Allow-Methods', |
77 | 77 | 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE'); |
78 | 78 | } |
79 | - if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
80 | - static::header('Access-Control-Allow-Headers',$req_h); |
|
79 | + if ($req_h = filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
80 | + static::header('Access-Control-Allow-Headers', $req_h); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | static::send(); |
@@ -93,8 +93,8 @@ discard block |
||
93 | 93 | * Finish the output buffer capturing. |
94 | 94 | * @return string The captured buffer |
95 | 95 | */ |
96 | - public static function end(){ |
|
97 | - if (static::$buffer){ |
|
96 | + public static function end() { |
|
97 | + if (static::$buffer) { |
|
98 | 98 | static::$payload[] = ob_get_contents(); |
99 | 99 | ob_end_clean(); |
100 | 100 | static::$buffer = null; |
@@ -106,14 +106,14 @@ discard block |
||
106 | 106 | * Check if an response output buffering is active. |
107 | 107 | * @return boolean |
108 | 108 | */ |
109 | - public static function isBuffering(){ |
|
109 | + public static function isBuffering() { |
|
110 | 110 | return static::$buffer; |
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
114 | 114 | * Clear the response body |
115 | 115 | */ |
116 | - public static function clean(){ |
|
116 | + public static function clean() { |
|
117 | 117 | static::$payload = []; |
118 | 118 | } |
119 | 119 | |
@@ -121,76 +121,76 @@ discard block |
||
121 | 121 | * Append a JSON object to the buffer. |
122 | 122 | * @param mixed $payload Data to append to the response buffer |
123 | 123 | */ |
124 | - public static function json($payload){ |
|
124 | + public static function json($payload) { |
|
125 | 125 | static::type(static::TYPE_JSON); |
126 | - static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
|
126 | + static::$payload[] = json_encode($payload, Options::get('core.response.json_flags', JSON_NUMERIC_CHECK)); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
130 | 130 | * Append a text to the buffer. |
131 | 131 | * @param mixed $payload Text to append to the response buffer |
132 | 132 | */ |
133 | - public static function text(){ |
|
133 | + public static function text() { |
|
134 | 134 | static::type(static::TYPE_TEXT); |
135 | - static::$payload[] = implode('',func_get_args()); |
|
135 | + static::$payload[] = implode('', func_get_args()); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | 139 | * Append an XML string to the buffer. |
140 | 140 | * @param mixed $payload Data to append to the response buffer |
141 | 141 | */ |
142 | - public static function xml(){ |
|
142 | + public static function xml() { |
|
143 | 143 | static::type(static::TYPE_XML); |
144 | - static::$payload[] = implode('',func_get_args()); |
|
144 | + static::$payload[] = implode('', func_get_args()); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
148 | 148 | * Append a SVG string to the buffer. |
149 | 149 | * @param mixed $payload Data to append to the response buffer |
150 | 150 | */ |
151 | - public static function svg(){ |
|
151 | + public static function svg() { |
|
152 | 152 | static::type(static::TYPE_SVG); |
153 | - static::$payload[] = implode('',func_get_args()); |
|
153 | + static::$payload[] = implode('', func_get_args()); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
157 | 157 | * Append an HTML string to the buffer. |
158 | 158 | * @param mixed $payload Data to append to the response buffer |
159 | 159 | */ |
160 | - public static function html(){ |
|
160 | + public static function html() { |
|
161 | 161 | static::type(static::TYPE_HTML); |
162 | - static::$payload[] = implode('',func_get_args()); |
|
162 | + static::$payload[] = implode('', func_get_args()); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | /** |
166 | 166 | * Append a raw string to the buffer. |
167 | 167 | * @param mixed $payload Data to append to the response buffer |
168 | 168 | */ |
169 | - public static function add(){ |
|
170 | - static::$payload[] = implode('',func_get_args()); |
|
169 | + public static function add() { |
|
170 | + static::$payload[] = implode('', func_get_args()); |
|
171 | 171 | } |
172 | 172 | |
173 | - public static function status($code,$message=''){ |
|
174 | - static::header('Status',$message?:$code,$code); |
|
173 | + public static function status($code, $message = '') { |
|
174 | + static::header('Status', $message ?: $code, $code); |
|
175 | 175 | } |
176 | 176 | |
177 | - public static function header($name,$value,$code=null){ |
|
178 | - static::$headers[$name] = [$value,$code]; |
|
177 | + public static function header($name, $value, $code = null) { |
|
178 | + static::$headers[$name] = [$value, $code]; |
|
179 | 179 | } |
180 | 180 | |
181 | - public static function error($code=500,$message='Application Error'){ |
|
182 | - Event::trigger('core.response.error',$code,$message); |
|
183 | - static::status($code,$message); |
|
181 | + public static function error($code = 500, $message = 'Application Error') { |
|
182 | + Event::trigger('core.response.error', $code, $message); |
|
183 | + static::status($code, $message); |
|
184 | 184 | } |
185 | 185 | |
186 | - public static function body($setBody=null){ |
|
186 | + public static function body($setBody = null) { |
|
187 | 187 | if ($setBody) static::$payload = [$setBody]; |
188 | 188 | return Filter::with('core.response.body', |
189 | - is_array(static::$payload) ? implode('',static::$payload) : static::$payload |
|
189 | + is_array(static::$payload) ? implode('', static::$payload) : static::$payload |
|
190 | 190 | ); |
191 | 191 | } |
192 | 192 | |
193 | - public static function headers($setHeaders=null){ |
|
193 | + public static function headers($setHeaders = null) { |
|
194 | 194 | if ($setHeaders) static::$headers = $setHeaders; |
195 | 195 | return static::$headers; |
196 | 196 | } |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | * |
203 | 203 | * @return array Headers and body of the response |
204 | 204 | */ |
205 | - public static function save(){ |
|
205 | + public static function save() { |
|
206 | 206 | return [ |
207 | 207 | 'head' => static::$headers, |
208 | 208 | 'body' => static::body(), |
@@ -216,13 +216,13 @@ discard block |
||
216 | 216 | * |
217 | 217 | * @param array $data head/body saved state |
218 | 218 | */ |
219 | - public static function load($data){ |
|
220 | - $data = (object)$data; |
|
219 | + public static function load($data) { |
|
220 | + $data = (object) $data; |
|
221 | 221 | if (isset($data->head)) static::headers($data->head); |
222 | 222 | if (isset($data->body)) static::body($data->body); |
223 | 223 | } |
224 | 224 | |
225 | - public static function send($force = false){ |
|
225 | + public static function send($force = false) { |
|
226 | 226 | if (!static::$sent || $force) { |
227 | 227 | static::$sent = true; |
228 | 228 | Event::trigger('core.response.send'); |
@@ -235,8 +235,8 @@ discard block |
||
235 | 235 | $code = null; |
236 | 236 | } |
237 | 237 | |
238 | - if ($value == 'Status'){ |
|
239 | - if (function_exists('http_response_code')){ |
|
238 | + if ($value == 'Status') { |
|
239 | + if (function_exists('http_response_code')) { |
|
240 | 240 | http_response_code($code); |
241 | 241 | } else { |
242 | 242 | header("Status: $code", true, $code); |
@@ -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; |
@@ -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 |
@@ -13,128 +13,128 @@ 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); |
|
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 | 26 | // Obfuscate IDs |
27 | 27 | ini_set('session.hash_function', 'whirlpool'); |
28 | - session_cache_limiter('must-revalidate'); |
|
29 | - @session_start(); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Get/Set Session name |
|
34 | - * |
|
35 | - * @access public |
|
36 | - * @static |
|
37 | - * @param string $key The session name |
|
38 | - * @return string The session value |
|
39 | - */ |
|
40 | - static public function name($name=null){ |
|
41 | - return $name ? session_name($name) : session_name(); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Get a session variable reference |
|
46 | - * |
|
47 | - * @access public |
|
48 | - * @static |
|
49 | - * @param mixed $key The variable name |
|
50 | - * @return mixed The variable value |
|
51 | - */ |
|
52 | - static public function get($key,$default=null){ |
|
28 | + session_cache_limiter('must-revalidate'); |
|
29 | + @session_start(); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Get/Set Session name |
|
34 | + * |
|
35 | + * @access public |
|
36 | + * @static |
|
37 | + * @param string $key The session name |
|
38 | + * @return string The session value |
|
39 | + */ |
|
40 | + static public function name($name=null){ |
|
41 | + return $name ? session_name($name) : session_name(); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Get a session variable reference |
|
46 | + * |
|
47 | + * @access public |
|
48 | + * @static |
|
49 | + * @param mixed $key The variable name |
|
50 | + * @return mixed The variable value |
|
51 | + */ |
|
52 | + static public function get($key,$default=null){ |
|
53 | 53 | if (($active = static::active()) && isset($_SESSION[$key])) { |
54 | - return $_SESSION[$key]; |
|
54 | + return $_SESSION[$key]; |
|
55 | 55 | } else if ($active) { |
56 | - return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default); |
|
56 | + return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default); |
|
57 | 57 | } else { |
58 | - return (is_callable($default)?call_user_func($default):$default); |
|
58 | + return (is_callable($default)?call_user_func($default):$default); |
|
59 | 59 | } |
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Set a session variable |
|
64 | - * |
|
65 | - * @access public |
|
66 | - * @static |
|
67 | - * @param mixed $key The variable name |
|
68 | - * @param mixed $value The variable value |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - static public function set($key,$value=null){ |
|
72 | - static::start(); |
|
73 | - if($value==null && is_array($key)){ |
|
74 | - foreach($key as $k=>$v) $_SESSION[$k]=$v; |
|
75 | - } else { |
|
76 | - $_SESSION[$key] = $value; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Delete a session variable |
|
82 | - * |
|
83 | - * @access public |
|
84 | - * @static |
|
85 | - * @param mixed $key The variable name |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - static public function delete($key){ |
|
89 | - static::start(); |
|
90 | - unset($_SESSION[$key]); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * Delete all session variables |
|
96 | - * |
|
97 | - * @access public |
|
98 | - * @static |
|
99 | - * @return void |
|
100 | - */ |
|
101 | - static public function clear(){ |
|
102 | - static::start(); |
|
103 | - session_unset(); |
|
104 | - session_destroy(); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Check if session is active |
|
109 | - * |
|
110 | - * @access public |
|
111 | - * @static |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - static public function active(){ |
|
115 | - return session_status() == PHP_SESSION_ACTIVE; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Check if a session variable exists |
|
120 | - * |
|
121 | - * @access public |
|
122 | - * @static |
|
123 | - * @param mixed $key The variable name |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - static public function exists($key){ |
|
127 | - static::start(); |
|
128 | - return isset($_SESSION[$key]); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Return a read-only accessor to session variables for in-view use. |
|
133 | - * @return SessionReadOnly |
|
134 | - */ |
|
135 | - static public function readOnly(){ |
|
136 | - return new SessionReadOnly; |
|
137 | - } |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Set a session variable |
|
64 | + * |
|
65 | + * @access public |
|
66 | + * @static |
|
67 | + * @param mixed $key The variable name |
|
68 | + * @param mixed $value The variable value |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + static public function set($key,$value=null){ |
|
72 | + static::start(); |
|
73 | + if($value==null && is_array($key)){ |
|
74 | + foreach($key as $k=>$v) $_SESSION[$k]=$v; |
|
75 | + } else { |
|
76 | + $_SESSION[$key] = $value; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Delete a session variable |
|
82 | + * |
|
83 | + * @access public |
|
84 | + * @static |
|
85 | + * @param mixed $key The variable name |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + static public function delete($key){ |
|
89 | + static::start(); |
|
90 | + unset($_SESSION[$key]); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * Delete all session variables |
|
96 | + * |
|
97 | + * @access public |
|
98 | + * @static |
|
99 | + * @return void |
|
100 | + */ |
|
101 | + static public function clear(){ |
|
102 | + static::start(); |
|
103 | + session_unset(); |
|
104 | + session_destroy(); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Check if session is active |
|
109 | + * |
|
110 | + * @access public |
|
111 | + * @static |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + static public function active(){ |
|
115 | + return session_status() == PHP_SESSION_ACTIVE; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Check if a session variable exists |
|
120 | + * |
|
121 | + * @access public |
|
122 | + * @static |
|
123 | + * @param mixed $key The variable name |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + static public function exists($key){ |
|
127 | + static::start(); |
|
128 | + return isset($_SESSION[$key]); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Return a read-only accessor to session variables for in-view use. |
|
133 | + * @return SessionReadOnly |
|
134 | + */ |
|
135 | + static public function readOnly(){ |
|
136 | + return new SessionReadOnly; |
|
137 | + } |
|
138 | 138 | |
139 | 139 | } /* End of class */ |
140 | 140 | |
@@ -146,36 +146,36 @@ discard block |
||
146 | 146 | |
147 | 147 | class SessionReadOnly { |
148 | 148 | |
149 | - /** |
|
150 | - * Get a session variable reference |
|
151 | - * |
|
152 | - * @access public |
|
153 | - * @param mixed $key The variable name |
|
154 | - * @return mixed The variable value |
|
155 | - */ |
|
156 | - public function get($key){ |
|
157 | - return Session::get($key); |
|
158 | - } |
|
159 | - public function __get($key){ |
|
160 | - return Session::get($key); |
|
161 | - } |
|
162 | - |
|
163 | - public function name(){ |
|
164 | - return Session::name(); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Check if a session variable exists |
|
169 | - * |
|
170 | - * @access public |
|
171 | - * @param mixed $key The variable name |
|
172 | - * @return bool |
|
173 | - */ |
|
174 | - public function exists($key){ |
|
175 | - return Session::exists($key); |
|
176 | - } |
|
177 | - public function __isset($key){ |
|
178 | - return Session::exists($key); |
|
179 | - } |
|
149 | + /** |
|
150 | + * Get a session variable reference |
|
151 | + * |
|
152 | + * @access public |
|
153 | + * @param mixed $key The variable name |
|
154 | + * @return mixed The variable value |
|
155 | + */ |
|
156 | + public function get($key){ |
|
157 | + return Session::get($key); |
|
158 | + } |
|
159 | + public function __get($key){ |
|
160 | + return Session::get($key); |
|
161 | + } |
|
162 | + |
|
163 | + public function name(){ |
|
164 | + return Session::name(); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Check if a session variable exists |
|
169 | + * |
|
170 | + * @access public |
|
171 | + * @param mixed $key The variable name |
|
172 | + * @return bool |
|
173 | + */ |
|
174 | + public function exists($key){ |
|
175 | + return Session::exists($key); |
|
176 | + } |
|
177 | + public function __isset($key){ |
|
178 | + return Session::exists($key); |
|
179 | + } |
|
180 | 180 | |
181 | 181 | } /* End of class */ |
@@ -96,6 +96,9 @@ |
||
96 | 96 | return $this->replyTo; |
97 | 97 | } |
98 | 98 | |
99 | + /** |
|
100 | + * @return string |
|
101 | + */ |
|
99 | 102 | public function subject($value=null){ |
100 | 103 | if ($value!==null && $value) { |
101 | 104 | $this->compiled_head = null; |
@@ -15,17 +15,17 @@ |
||
15 | 15 | class Envelope { |
16 | 16 | |
17 | 17 | protected $uid, |
18 | - $to, |
|
19 | - $from, |
|
20 | - $cc, |
|
21 | - $bcc, |
|
22 | - $replyTo, |
|
23 | - $subject, |
|
24 | - $message, |
|
25 | - $contentType = 'text/html; charset="utf-8"', |
|
26 | - $attachments, |
|
27 | - $compiled_head, |
|
28 | - $compiled_body; |
|
18 | + $to, |
|
19 | + $from, |
|
20 | + $cc, |
|
21 | + $bcc, |
|
22 | + $replyTo, |
|
23 | + $subject, |
|
24 | + $message, |
|
25 | + $contentType = 'text/html; charset="utf-8"', |
|
26 | + $attachments, |
|
27 | + $compiled_head, |
|
28 | + $compiled_body; |
|
29 | 29 | |
30 | 30 | public function __construct($email=null){ |
31 | 31 | if ($email) { |
@@ -27,126 +27,126 @@ discard block |
||
27 | 27 | $compiled_head, |
28 | 28 | $compiled_body; |
29 | 29 | |
30 | - public function __construct($email=null){ |
|
30 | + public function __construct($email = null) { |
|
31 | 31 | if ($email) { |
32 | - $email = (object)$email; |
|
33 | - if(isset($email->to)) $this->to($email->to); |
|
34 | - if(isset($email->from)) $this->from($email->from); |
|
35 | - if(isset($email->cc)) $this->cc($email->cc); |
|
36 | - if(isset($email->bcc)) $this->bcc($email->bcc); |
|
37 | - if(isset($email->replyTo)) $this->replyTo($email->replyTo); |
|
38 | - if(isset($email->subject)) $this->subject($email->subject); |
|
39 | - if(isset($email->message)) $this->message($email->message); |
|
40 | - if(isset($email->attachments)) $this->attach($email->attachments); |
|
32 | + $email = (object) $email; |
|
33 | + if (isset($email->to)) $this->to($email->to); |
|
34 | + if (isset($email->from)) $this->from($email->from); |
|
35 | + if (isset($email->cc)) $this->cc($email->cc); |
|
36 | + if (isset($email->bcc)) $this->bcc($email->bcc); |
|
37 | + if (isset($email->replyTo)) $this->replyTo($email->replyTo); |
|
38 | + if (isset($email->subject)) $this->subject($email->subject); |
|
39 | + if (isset($email->message)) $this->message($email->message); |
|
40 | + if (isset($email->attachments)) $this->attach($email->attachments); |
|
41 | 41 | } |
42 | - $this->uid = '_CORE_'.md5(uniqid(time())); |
|
42 | + $this->uid = '_CORE_'.md5(uniqid(time())); |
|
43 | 43 | |
44 | 44 | } |
45 | 45 | |
46 | - protected function add_emails(&$pool, $emails, $append=true){ |
|
46 | + protected function add_emails(&$pool, $emails, $append = true) { |
|
47 | 47 | $this->compiled_head = null; |
48 | - foreach ((array)$emails as $values) { |
|
49 | - foreach(preg_split('/\s*,\s*/',$values) as $value) { |
|
50 | - if(strpos($value,'<')!==false){ |
|
51 | - $value = str_replace('>','',$value); |
|
52 | - $parts = explode('<',$value,2); |
|
48 | + foreach ((array) $emails as $values) { |
|
49 | + foreach (preg_split('/\s*,\s*/', $values) as $value) { |
|
50 | + if (strpos($value, '<') !== false) { |
|
51 | + $value = str_replace('>', '', $value); |
|
52 | + $parts = explode('<', $value, 2); |
|
53 | 53 | $name = trim(current($parts)); |
54 | 54 | $email = trim(end($parts)); |
55 | 55 | $address = "$name <{$email}>"; |
56 | 56 | } else { |
57 | 57 | $address = $value; |
58 | 58 | } |
59 | - if ($append) $pool[] = $address; else $pool = $address; |
|
59 | + if ($append) $pool[] = $address;else $pool = $address; |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | - public function from($value=null){ |
|
65 | - if ($value!==null && $value) { |
|
64 | + public function from($value = null) { |
|
65 | + if ($value !== null && $value) { |
|
66 | 66 | $this->add_emails($this->from, $value, false); |
67 | - } else if ($value===false) $this->from = ''; |
|
67 | + } else if ($value === false) $this->from = ''; |
|
68 | 68 | return $this->from; |
69 | 69 | } |
70 | 70 | |
71 | - public function to($value=null){ |
|
72 | - if ($value!==null && $value) { |
|
71 | + public function to($value = null) { |
|
72 | + if ($value !== null && $value) { |
|
73 | 73 | $this->add_emails($this->to, $value); |
74 | - } else if ($value===false) $this->to = []; |
|
74 | + } else if ($value === false) $this->to = []; |
|
75 | 75 | return $this->to; |
76 | 76 | } |
77 | 77 | |
78 | - public function cc($value=null){ |
|
79 | - if ($value!==null && $value) { |
|
78 | + public function cc($value = null) { |
|
79 | + if ($value !== null && $value) { |
|
80 | 80 | $this->add_emails($this->cc, $value); |
81 | - } else if ($value===false) $this->cc = []; |
|
81 | + } else if ($value === false) $this->cc = []; |
|
82 | 82 | return $this->cc; |
83 | 83 | } |
84 | 84 | |
85 | - public function bcc($value=null){ |
|
86 | - if ($value!==null && $value) { |
|
85 | + public function bcc($value = null) { |
|
86 | + if ($value !== null && $value) { |
|
87 | 87 | $this->add_emails($this->bcc, $value); |
88 | - } else if ($value===false) $this->bcc = []; |
|
88 | + } else if ($value === false) $this->bcc = []; |
|
89 | 89 | return $this->bcc; |
90 | 90 | } |
91 | 91 | |
92 | - public function replyTo($value=null){ |
|
93 | - if ($value!==null && $value) { |
|
92 | + public function replyTo($value = null) { |
|
93 | + if ($value !== null && $value) { |
|
94 | 94 | $this->add_emails($this->replyTo, $value, false); |
95 | - } else if ($value===false) $this->replyTo = ''; |
|
95 | + } else if ($value === false) $this->replyTo = ''; |
|
96 | 96 | return $this->replyTo; |
97 | 97 | } |
98 | 98 | |
99 | - public function subject($value=null){ |
|
100 | - if ($value!==null && $value) { |
|
99 | + public function subject($value = null) { |
|
100 | + if ($value !== null && $value) { |
|
101 | 101 | $this->compiled_head = null; |
102 | 102 | $this->subject = $value; |
103 | - } else if ($value===false) $this->subject = ''; |
|
103 | + } else if ($value === false) $this->subject = ''; |
|
104 | 104 | return $this->subject; |
105 | 105 | } |
106 | 106 | |
107 | - public function contentType($value=null){ |
|
108 | - if ($value!==null && $value) { |
|
107 | + public function contentType($value = null) { |
|
108 | + if ($value !== null && $value) { |
|
109 | 109 | $this->compiled_body = null; |
110 | 110 | $this->contentType = $value; |
111 | - } else if ($value===false) $this->contentType = ''; |
|
111 | + } else if ($value === false) $this->contentType = ''; |
|
112 | 112 | return $this->contentType; |
113 | 113 | } |
114 | 114 | |
115 | - public function message($value=null){ |
|
116 | - if ($value!==null && $value) { |
|
115 | + public function message($value = null) { |
|
116 | + if ($value !== null && $value) { |
|
117 | 117 | $this->compiled_body = null; |
118 | 118 | $this->message = $value; |
119 | - } else if ($value===false) $this->message = ''; |
|
119 | + } else if ($value === false) $this->message = ''; |
|
120 | 120 | return $this->message; |
121 | 121 | } |
122 | 122 | |
123 | - public function attach($file){ |
|
123 | + public function attach($file) { |
|
124 | 124 | $this->compiled_body = null; |
125 | 125 | if (isset($file->content) || isset($file['content'])) { |
126 | 126 | $this->attachments[] = $file; |
127 | - } else foreach ((array)$file as $curfile) { |
|
127 | + } else foreach ((array) $file as $curfile) { |
|
128 | 128 | $this->attachments[] = $curfile; |
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | - public function head($recompile = false){ |
|
133 | - if ($recompile || (null === $this->compiled_head)){ |
|
132 | + public function head($recompile = false) { |
|
133 | + if ($recompile || (null === $this->compiled_head)) { |
|
134 | 134 | $head = []; |
135 | 135 | $head[] = "Subject: {$this->subject}"; |
136 | - if($this->from) $head[] = "From: {$this->from}"; |
|
137 | - if(is_array($this->to) && !empty($this->to)) $head[] = "To: " . implode(', ',$this->to); |
|
138 | - if(is_array($this->cc) && !empty($this->cc)) $head[] = "Cc: " . implode(', ',$this->cc); |
|
139 | - if(is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
140 | - if($this->replyTo) $head[] = "Reply-To: {$this->replyTo}"; |
|
136 | + if ($this->from) $head[] = "From: {$this->from}"; |
|
137 | + if (is_array($this->to) && !empty($this->to)) $head[] = "To: ".implode(', ', $this->to); |
|
138 | + if (is_array($this->cc) && !empty($this->cc)) $head[] = "Cc: ".implode(', ', $this->cc); |
|
139 | + if (is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: ".implode(', ', $this->bcc); |
|
140 | + if ($this->replyTo) $head[] = "Reply-To: {$this->replyTo}"; |
|
141 | 141 | $head[] = 'MIME-Version: 1.0'; |
142 | 142 | $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->uid}\""; |
143 | 143 | $this->compiled_head = implode("\r\n", $head); |
144 | 144 | } |
145 | - return \Filter::with( 'core.email.source.head', $this->compiled_head); |
|
145 | + return \Filter::with('core.email.source.head', $this->compiled_head); |
|
146 | 146 | } |
147 | 147 | |
148 | - public function body($recompile = false){ |
|
149 | - if ($recompile || (null === $this->compiled_body)){ |
|
148 | + public function body($recompile = false) { |
|
149 | + if ($recompile || (null === $this->compiled_body)) { |
|
150 | 150 | $body[] = "--{$this->uid}"; |
151 | 151 | $body[] = "Content-Type: {$this->contentType}"; |
152 | 152 | $body[] = "Content-Transfer-Encoding: quoted-printable"; |
@@ -154,13 +154,13 @@ discard block |
||
154 | 154 | $body[] = quoted_printable_encode($this->message); |
155 | 155 | $body[] = ''; |
156 | 156 | |
157 | - if (!empty($this->attachments)) foreach ((array)$this->attachments as $file) { |
|
157 | + if (!empty($this->attachments)) foreach ((array) $this->attachments as $file) { |
|
158 | 158 | |
159 | 159 | if (is_string($file)) { |
160 | 160 | $name = basename($file); |
161 | 161 | $data = file_get_contents($file); |
162 | 162 | } else { |
163 | - $name = isset($file['name']) ? $file['name'] : 'untitled'; |
|
163 | + $name = isset($file['name']) ? $file['name'] : 'untitled'; |
|
164 | 164 | $data = isset($file['content']) ? $file['content'] : ''; |
165 | 165 | } |
166 | 166 | |
@@ -177,11 +177,11 @@ discard block |
||
177 | 177 | |
178 | 178 | $this->compiled_body = implode("\r\n", $body); |
179 | 179 | } |
180 | - return \Filter::with( 'core.email.source.body', $this->compiled_body); |
|
180 | + return \Filter::with('core.email.source.body', $this->compiled_body); |
|
181 | 181 | } |
182 | 182 | |
183 | - public function build(){ |
|
184 | - return \Filter::with( 'core.email.source', $this->head() . "\r\n" . $this->body() ); |
|
183 | + public function build() { |
|
184 | + return \Filter::with('core.email.source', $this->head()."\r\n".$this->body()); |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | } |
@@ -27,27 +27,43 @@ discard block |
||
27 | 27 | $compiled_head, |
28 | 28 | $compiled_body; |
29 | 29 | |
30 | - public function __construct($email=null){ |
|
30 | + public function __construct($email=null) { |
|
31 | 31 | if ($email) { |
32 | 32 | $email = (object)$email; |
33 | - if(isset($email->to)) $this->to($email->to); |
|
34 | - if(isset($email->from)) $this->from($email->from); |
|
35 | - if(isset($email->cc)) $this->cc($email->cc); |
|
36 | - if(isset($email->bcc)) $this->bcc($email->bcc); |
|
37 | - if(isset($email->replyTo)) $this->replyTo($email->replyTo); |
|
38 | - if(isset($email->subject)) $this->subject($email->subject); |
|
39 | - if(isset($email->message)) $this->message($email->message); |
|
40 | - if(isset($email->attachments)) $this->attach($email->attachments); |
|
33 | + if(isset($email->to)) { |
|
34 | + $this->to($email->to); |
|
35 | + } |
|
36 | + if(isset($email->from)) { |
|
37 | + $this->from($email->from); |
|
38 | + } |
|
39 | + if(isset($email->cc)) { |
|
40 | + $this->cc($email->cc); |
|
41 | + } |
|
42 | + if(isset($email->bcc)) { |
|
43 | + $this->bcc($email->bcc); |
|
44 | + } |
|
45 | + if(isset($email->replyTo)) { |
|
46 | + $this->replyTo($email->replyTo); |
|
47 | + } |
|
48 | + if(isset($email->subject)) { |
|
49 | + $this->subject($email->subject); |
|
50 | + } |
|
51 | + if(isset($email->message)) { |
|
52 | + $this->message($email->message); |
|
53 | + } |
|
54 | + if(isset($email->attachments)) { |
|
55 | + $this->attach($email->attachments); |
|
56 | + } |
|
41 | 57 | } |
42 | 58 | $this->uid = '_CORE_'.md5(uniqid(time())); |
43 | 59 | |
44 | 60 | } |
45 | 61 | |
46 | - protected function add_emails(&$pool, $emails, $append=true){ |
|
62 | + protected function add_emails(&$pool, $emails, $append=true) { |
|
47 | 63 | $this->compiled_head = null; |
48 | 64 | foreach ((array)$emails as $values) { |
49 | 65 | foreach(preg_split('/\s*,\s*/',$values) as $value) { |
50 | - if(strpos($value,'<')!==false){ |
|
66 | + if(strpos($value,'<')!==false) { |
|
51 | 67 | $value = str_replace('>','',$value); |
52 | 68 | $parts = explode('<',$value,2); |
53 | 69 | $name = trim(current($parts)); |
@@ -56,89 +72,123 @@ discard block |
||
56 | 72 | } else { |
57 | 73 | $address = $value; |
58 | 74 | } |
59 | - if ($append) $pool[] = $address; else $pool = $address; |
|
75 | + if ($append) { |
|
76 | + $pool[] = $address; |
|
77 | + } else { |
|
78 | + $pool = $address; |
|
79 | + } |
|
60 | 80 | } |
61 | 81 | } |
62 | 82 | } |
63 | 83 | |
64 | - public function from($value=null){ |
|
84 | + public function from($value=null) { |
|
65 | 85 | if ($value!==null && $value) { |
66 | 86 | $this->add_emails($this->from, $value, false); |
67 | - } else if ($value===false) $this->from = ''; |
|
87 | + } else if ($value===false) { |
|
88 | + $this->from = ''; |
|
89 | + } |
|
68 | 90 | return $this->from; |
69 | 91 | } |
70 | 92 | |
71 | - public function to($value=null){ |
|
93 | + public function to($value=null) { |
|
72 | 94 | if ($value!==null && $value) { |
73 | 95 | $this->add_emails($this->to, $value); |
74 | - } else if ($value===false) $this->to = []; |
|
96 | + } else if ($value===false) { |
|
97 | + $this->to = []; |
|
98 | + } |
|
75 | 99 | return $this->to; |
76 | 100 | } |
77 | 101 | |
78 | - public function cc($value=null){ |
|
102 | + public function cc($value=null) { |
|
79 | 103 | if ($value!==null && $value) { |
80 | 104 | $this->add_emails($this->cc, $value); |
81 | - } else if ($value===false) $this->cc = []; |
|
105 | + } else if ($value===false) { |
|
106 | + $this->cc = []; |
|
107 | + } |
|
82 | 108 | return $this->cc; |
83 | 109 | } |
84 | 110 | |
85 | - public function bcc($value=null){ |
|
111 | + public function bcc($value=null) { |
|
86 | 112 | if ($value!==null && $value) { |
87 | 113 | $this->add_emails($this->bcc, $value); |
88 | - } else if ($value===false) $this->bcc = []; |
|
114 | + } else if ($value===false) { |
|
115 | + $this->bcc = []; |
|
116 | + } |
|
89 | 117 | return $this->bcc; |
90 | 118 | } |
91 | 119 | |
92 | - public function replyTo($value=null){ |
|
120 | + public function replyTo($value=null) { |
|
93 | 121 | if ($value!==null && $value) { |
94 | 122 | $this->add_emails($this->replyTo, $value, false); |
95 | - } else if ($value===false) $this->replyTo = ''; |
|
123 | + } else if ($value===false) { |
|
124 | + $this->replyTo = ''; |
|
125 | + } |
|
96 | 126 | return $this->replyTo; |
97 | 127 | } |
98 | 128 | |
99 | - public function subject($value=null){ |
|
129 | + public function subject($value=null) { |
|
100 | 130 | if ($value!==null && $value) { |
101 | 131 | $this->compiled_head = null; |
102 | 132 | $this->subject = $value; |
103 | - } else if ($value===false) $this->subject = ''; |
|
133 | + } else if ($value===false) { |
|
134 | + $this->subject = ''; |
|
135 | + } |
|
104 | 136 | return $this->subject; |
105 | 137 | } |
106 | 138 | |
107 | - public function contentType($value=null){ |
|
139 | + public function contentType($value=null) { |
|
108 | 140 | if ($value!==null && $value) { |
109 | - if (empty($this->attachments)) $this->compiled_head = null; |
|
141 | + if (empty($this->attachments)) { |
|
142 | + $this->compiled_head = null; |
|
143 | + } |
|
110 | 144 | $this->compiled_body = null; |
111 | 145 | $this->contentType = $value; |
112 | - } else if ($value===false) $this->contentType = ''; |
|
146 | + } else if ($value===false) { |
|
147 | + $this->contentType = ''; |
|
148 | + } |
|
113 | 149 | return $this->contentType; |
114 | 150 | } |
115 | 151 | |
116 | - public function message($value=null){ |
|
152 | + public function message($value=null) { |
|
117 | 153 | if ($value!==null && $value) { |
118 | 154 | $this->compiled_body = null; |
119 | 155 | $this->message = $value; |
120 | - } else if ($value===false) $this->message = ''; |
|
156 | + } else if ($value===false) { |
|
157 | + $this->message = ''; |
|
158 | + } |
|
121 | 159 | return $this->message; |
122 | 160 | } |
123 | 161 | |
124 | - public function attach($file){ |
|
162 | + public function attach($file) { |
|
125 | 163 | $this->compiled_body = null; |
126 | 164 | if (isset($file->content) || isset($file['content'])) { |
127 | 165 | $this->attachments[] = $file; |
128 | - } else foreach ((array)$file as $curfile) { |
|
166 | + } else { |
|
167 | + foreach ((array)$file as $curfile) { |
|
129 | 168 | $this->attachments[] = $curfile; |
130 | 169 | } |
170 | + } |
|
131 | 171 | } |
132 | 172 | |
133 | - public function head($recompile = false){ |
|
134 | - if ($recompile || (null === $this->compiled_head)){ |
|
173 | + public function head($recompile = false) { |
|
174 | + if ($recompile || (null === $this->compiled_head)) { |
|
135 | 175 | $head = []; |
136 | 176 | $head[] = "Subject: {$this->subject}"; |
137 | - if($this->from) $head[] = "From: {$this->from}"; |
|
138 | - if(is_array($this->to) && !empty($this->to)) $head[] = "To: " . implode(', ',$this->to); |
|
139 | - if(is_array($this->cc) && !empty($this->cc)) $head[] = "Cc: " . implode(', ',$this->cc); |
|
140 | - if(is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
141 | - if($this->replyTo) $head[] = "Reply-To: {$this->replyTo}"; |
|
177 | + if($this->from) { |
|
178 | + $head[] = "From: {$this->from}"; |
|
179 | + } |
|
180 | + if(is_array($this->to) && !empty($this->to)) { |
|
181 | + $head[] = "To: " . implode(', ',$this->to); |
|
182 | + } |
|
183 | + if(is_array($this->cc) && !empty($this->cc)) { |
|
184 | + $head[] = "Cc: " . implode(', ',$this->cc); |
|
185 | + } |
|
186 | + if(is_array($this->bcc) && !empty($this->bcc)) { |
|
187 | + $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
188 | + } |
|
189 | + if($this->replyTo) { |
|
190 | + $head[] = "Reply-To: {$this->replyTo}"; |
|
191 | + } |
|
142 | 192 | $head[] = 'MIME-Version: 1.0'; |
143 | 193 | if (!empty($this->attachments)) { |
144 | 194 | $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->uid}\""; |
@@ -151,8 +201,8 @@ discard block |
||
151 | 201 | return \Filter::with( 'core.email.source.head', $this->compiled_head); |
152 | 202 | } |
153 | 203 | |
154 | - public function body($recompile = false){ |
|
155 | - if ($recompile || (null === $this->compiled_body)){ |
|
204 | + public function body($recompile = false) { |
|
205 | + if ($recompile || (null === $this->compiled_body)) { |
|
156 | 206 | if (!empty($this->attachments)) { |
157 | 207 | $body[] = "--{$this->uid}"; |
158 | 208 | $body[] = "Content-Type: {$this->contentType}"; |
@@ -162,10 +212,12 @@ discard block |
||
162 | 212 | $body[] = quoted_printable_encode($this->message); |
163 | 213 | $body[] = ''; |
164 | 214 | |
165 | - if (!empty($this->attachments)) foreach ((array)$this->attachments as $file) { |
|
215 | + if (!empty($this->attachments)) { |
|
216 | + foreach ((array)$this->attachments as $file) { |
|
166 | 217 | |
167 | 218 | if (is_string($file)) { |
168 | 219 | $name = basename($file); |
220 | + } |
|
169 | 221 | $data = file_get_contents($file); |
170 | 222 | } else { |
171 | 223 | $name = isset($file['name']) ? $file['name'] : 'untitled'; |
@@ -187,7 +239,7 @@ discard block |
||
187 | 239 | return \Filter::with( 'core.email.source.body', $this->compiled_body); |
188 | 240 | } |
189 | 241 | |
190 | - public function build(){ |
|
242 | + public function build() { |
|
191 | 243 | return \Filter::with( 'core.email.source', $this->head() . "\r\n" . $this->body() ); |
192 | 244 | } |
193 | 245 |
@@ -18,6 +18,9 @@ |
||
18 | 18 | protected static $headers = []; |
19 | 19 | protected static $last_info = null; |
20 | 20 | |
21 | + /** |
|
22 | + * @param string $method |
|
23 | + */ |
|
21 | 24 | protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
22 | 25 | $http_method = strtoupper($method); |
23 | 26 | $ch = curl_init($url); |
@@ -14,9 +14,9 @@ |
||
14 | 14 | use Module; |
15 | 15 | |
16 | 16 | protected static $UA = "Mozilla/4.0 (compatible; Core::HTTP; Windows NT 6.1)", |
17 | - $json_data = false, |
|
18 | - $headers = [], |
|
19 | - $last_info = null; |
|
17 | + $json_data = false, |
|
18 | + $headers = [], |
|
19 | + $last_info = null; |
|
20 | 20 | |
21 | 21 | protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
22 | 22 | $http_method = strtoupper($method); |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | $headers = [], |
19 | 19 | $last_info = null; |
20 | 20 | |
21 | - protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
|
21 | + protected static function request($method, $url, $data = [], array $headers = [], $data_as_json = false, $username = null, $password = null) { |
|
22 | 22 | $http_method = strtoupper($method); |
23 | 23 | $ch = curl_init($url); |
24 | 24 | $opt = [ |
@@ -33,26 +33,26 @@ discard block |
||
33 | 33 | CURLOPT_ENCODING => '', |
34 | 34 | ]; |
35 | 35 | |
36 | - if($username && $password) { |
|
36 | + if ($username && $password) { |
|
37 | 37 | $opt[CURLOPT_USERPWD] = "$username:$password"; |
38 | 38 | } |
39 | 39 | |
40 | - $headers = array_merge($headers,static::$headers); |
|
40 | + $headers = array_merge($headers, static::$headers); |
|
41 | 41 | |
42 | - if($http_method == 'GET'){ |
|
43 | - if($data && is_array($data)){ |
|
42 | + if ($http_method == 'GET') { |
|
43 | + if ($data && is_array($data)) { |
|
44 | 44 | $tmp = []; |
45 | 45 | $queried_url = $url; |
46 | - foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
|
47 | - $queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
|
48 | - $queried_url .= implode('&',$tmp); |
|
46 | + foreach ($data as $key=>$val) $tmp[] = $key.'='.$val; |
|
47 | + $queried_url .= (strpos($queried_url, '?') === false) ? '?' : '&'; |
|
48 | + $queried_url .= implode('&', $tmp); |
|
49 | 49 | $opt[CURLOPT_URL] = $queried_url; |
50 | 50 | $opt[CURLOPT_HTTPGET] = true; |
51 | 51 | unset($opt[CURLOPT_CUSTOMREQUEST]); |
52 | 52 | } |
53 | 53 | } else { |
54 | 54 | $opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
55 | - if($data_as_json or is_object($data)){ |
|
55 | + if ($data_as_json or is_object($data)) { |
|
56 | 56 | $headers['Content-Type'] = 'application/json'; |
57 | 57 | $opt[CURLOPT_POSTFIELDS] = json_encode($data); |
58 | 58 | } else { |
@@ -60,59 +60,59 @@ discard block |
||
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | - curl_setopt_array($ch,$opt); |
|
63 | + curl_setopt_array($ch, $opt); |
|
64 | 64 | $_harr = []; |
65 | - foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
|
65 | + foreach ($headers as $key=>$val) $_harr[] = $key.': '.$val; |
|
66 | 66 | curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
67 | 67 | $result = curl_exec($ch); |
68 | 68 | $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
69 | 69 | static::$last_info = curl_getinfo($ch); |
70 | - if(false !== strpos($contentType,'json')) $result = json_decode($result); |
|
70 | + if (false !== strpos($contentType, 'json')) $result = json_decode($result); |
|
71 | 71 | curl_close($ch); |
72 | 72 | return $result; |
73 | 73 | } |
74 | 74 | |
75 | - public static function useJSON($value=null){ |
|
76 | - return $value===null ? static::$json_data : static::$json_data = $value; |
|
75 | + public static function useJSON($value = null) { |
|
76 | + return $value === null ? static::$json_data : static::$json_data = $value; |
|
77 | 77 | } |
78 | 78 | |
79 | - public static function addHeader($name,$value){ |
|
79 | + public static function addHeader($name, $value) { |
|
80 | 80 | static::$headers[$name] = $value; |
81 | 81 | } |
82 | 82 | |
83 | - public static function removeHeader($name){ |
|
83 | + public static function removeHeader($name) { |
|
84 | 84 | unset(static::$headers[$name]); |
85 | 85 | } |
86 | 86 | |
87 | - public static function headers($name=null){ |
|
87 | + public static function headers($name = null) { |
|
88 | 88 | // null === $name ?? static::$headers ?? static::$headers[$name] |
89 | 89 | return null === $name |
90 | 90 | ? static::$headers |
91 | - : ( isset(static::$headers[$name]) ? static::$headers[$name] : '' ); |
|
91 | + : (isset(static::$headers[$name]) ? static::$headers[$name] : ''); |
|
92 | 92 | } |
93 | 93 | |
94 | - public static function userAgent($value=null){ |
|
95 | - return $value===null ? static::$UA : static::$UA = $value; |
|
94 | + public static function userAgent($value = null) { |
|
95 | + return $value === null ? static::$UA : static::$UA = $value; |
|
96 | 96 | } |
97 | 97 | |
98 | - public static function get($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
99 | - return static::request('get',$url,$data,$headers,false,$username,$password); |
|
98 | + public static function get($url, $data = null, array $headers = [], $username = null, $password = null) { |
|
99 | + return static::request('get', $url, $data, $headers, false, $username, $password); |
|
100 | 100 | } |
101 | 101 | |
102 | - public static function post($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
103 | - return static::request('post',$url,$data,$headers,static::$json_data,$username,$password); |
|
102 | + public static function post($url, $data = null, array $headers = [], $username = null, $password = null) { |
|
103 | + return static::request('post', $url, $data, $headers, static::$json_data, $username, $password); |
|
104 | 104 | } |
105 | 105 | |
106 | - public static function put($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
107 | - return static::request('put',$url,$data,$headers,static::$json_data,$username,$password); |
|
106 | + public static function put($url, $data = null, array $headers = [], $username = null, $password = null) { |
|
107 | + return static::request('put', $url, $data, $headers, static::$json_data, $username, $password); |
|
108 | 108 | } |
109 | 109 | |
110 | - public static function delete($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
111 | - return static::request('delete',$url,$data,$headers,static::$json_data,$username,$password); |
|
110 | + public static function delete($url, $data = null, array $headers = [], $username = null, $password = null) { |
|
111 | + return static::request('delete', $url, $data, $headers, static::$json_data, $username, $password); |
|
112 | 112 | } |
113 | 113 | |
114 | - public static function info($url = null){ |
|
115 | - if ($url){ |
|
114 | + public static function info($url = null) { |
|
115 | + if ($url) { |
|
116 | 116 | curl_setopt_array($ch = curl_init($url), [ |
117 | 117 | CURLOPT_SSL_VERIFYHOST => false, |
118 | 118 | CURLOPT_CONNECTTIMEOUT => 10, |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | $headers = [], |
19 | 19 | $last_info = null; |
20 | 20 | |
21 | - protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
|
21 | + protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null) { |
|
22 | 22 | $http_method = strtoupper($method); |
23 | 23 | $ch = curl_init($url); |
24 | 24 | $opt = [ |
@@ -39,8 +39,8 @@ discard block |
||
39 | 39 | |
40 | 40 | $headers = array_merge($headers,static::$headers); |
41 | 41 | |
42 | - if($http_method == 'GET'){ |
|
43 | - if($data && is_array($data)){ |
|
42 | + if($http_method == 'GET') { |
|
43 | + if($data && is_array($data)) { |
|
44 | 44 | $tmp = []; |
45 | 45 | $queried_url = $url; |
46 | 46 | foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | } |
53 | 53 | } else { |
54 | 54 | $opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
55 | - if($data_as_json or is_object($data)){ |
|
55 | + if($data_as_json or is_object($data)) { |
|
56 | 56 | $headers['Content-Type'] = 'application/json'; |
57 | 57 | $opt[CURLOPT_POSTFIELDS] = json_encode($data); |
58 | 58 | } else { |
@@ -67,52 +67,54 @@ discard block |
||
67 | 67 | $result = curl_exec($ch); |
68 | 68 | $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
69 | 69 | static::$last_info = curl_getinfo($ch); |
70 | - if(false !== strpos($contentType,'json')) $result = json_decode($result); |
|
70 | + if(false !== strpos($contentType,'json')) { |
|
71 | + $result = json_decode($result); |
|
72 | + } |
|
71 | 73 | curl_close($ch); |
72 | 74 | return $result; |
73 | 75 | } |
74 | 76 | |
75 | - public static function useJSON($value=null){ |
|
77 | + public static function useJSON($value=null) { |
|
76 | 78 | return $value===null ? static::$json_data : static::$json_data = $value; |
77 | 79 | } |
78 | 80 | |
79 | - public static function addHeader($name,$value){ |
|
81 | + public static function addHeader($name,$value) { |
|
80 | 82 | static::$headers[$name] = $value; |
81 | 83 | } |
82 | 84 | |
83 | - public static function removeHeader($name){ |
|
85 | + public static function removeHeader($name) { |
|
84 | 86 | unset(static::$headers[$name]); |
85 | 87 | } |
86 | 88 | |
87 | - public static function headers($name=null){ |
|
89 | + public static function headers($name=null) { |
|
88 | 90 | // null === $name ?? static::$headers ?? static::$headers[$name] |
89 | 91 | return null === $name |
90 | 92 | ? static::$headers |
91 | 93 | : ( isset(static::$headers[$name]) ? static::$headers[$name] : '' ); |
92 | 94 | } |
93 | 95 | |
94 | - public static function userAgent($value=null){ |
|
96 | + public static function userAgent($value=null) { |
|
95 | 97 | return $value===null ? static::$UA : static::$UA = $value; |
96 | 98 | } |
97 | 99 | |
98 | - public static function get($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
100 | + public static function get($url, $data=null, array $headers=[], $username = null, $password = null) { |
|
99 | 101 | return static::request('get',$url,$data,$headers,false,$username,$password); |
100 | 102 | } |
101 | 103 | |
102 | - public static function post($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
104 | + public static function post($url, $data=null, array $headers=[], $username = null, $password = null) { |
|
103 | 105 | return static::request('post',$url,$data,$headers,static::$json_data,$username,$password); |
104 | 106 | } |
105 | 107 | |
106 | - public static function put($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
108 | + public static function put($url, $data=null, array $headers=[], $username = null, $password = null) { |
|
107 | 109 | return static::request('put',$url,$data,$headers,static::$json_data,$username,$password); |
108 | 110 | } |
109 | 111 | |
110 | - public static function delete($url, $data=null, array $headers=[], $username = null, $password = null){ |
|
112 | + public static function delete($url, $data=null, array $headers=[], $username = null, $password = null) { |
|
111 | 113 | return static::request('delete',$url,$data,$headers,static::$json_data,$username,$password); |
112 | 114 | } |
113 | 115 | |
114 | - public static function info($url = null){ |
|
115 | - if ($url){ |
|
116 | + public static function info($url = null) { |
|
117 | + if ($url) { |
|
116 | 118 | curl_setopt_array($ch = curl_init($url), [ |
117 | 119 | CURLOPT_SSL_VERIFYHOST => false, |
118 | 120 | CURLOPT_CONNECTTIMEOUT => 10, |