@@ -339,7 +339,7 @@ |
||
| 339 | 339 | */ |
| 340 | 340 | protected function getNestedResourceUri(array $segments) |
| 341 | 341 | { |
| 342 | - return implode('/', array_map(function ($segment) { |
|
| 342 | + return implode('/', array_map(function($segment) { |
|
| 343 | 343 | return $segment.'/{'.$this->getResourceWilcard($segment).'}'; |
| 344 | 344 | }, $segments)); |
| 345 | 345 | } |
@@ -31,97 +31,97 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class RouteGroup |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * Merge the given group attributes. |
|
| 36 | - * |
|
| 37 | - * @param array $new |
|
| 38 | - * @param array $old |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public static function mergeGroup($new, $old) |
|
| 43 | - { |
|
| 44 | - if (isset($new['domain'])) { |
|
| 45 | - unset($old['domain']); |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * Merge the given group attributes. |
|
| 36 | + * |
|
| 37 | + * @param array $new |
|
| 38 | + * @param array $old |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public static function mergeGroup($new, $old) |
|
| 43 | + { |
|
| 44 | + if (isset($new['domain'])) { |
|
| 45 | + unset($old['domain']); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - $new = array_merge(static::formatUseAs($new, $old), [ |
|
| 48 | + $new = array_merge(static::formatUseAs($new, $old), [ |
|
| 49 | 49 | 'namespace' => static::formatUseNamespace($new, $old), |
| 50 | 50 | 'prefix' => static::formatUsePrefix($new, $old), |
| 51 | 51 | 'where' => static::formatUseWhere($new, $old) |
| 52 | 52 | ]); |
| 53 | 53 | |
| 54 | - return array_merge_recursive( |
|
| 55 | - Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
| 56 | - ); |
|
| 57 | - } |
|
| 54 | + return array_merge_recursive( |
|
| 55 | + Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
| 56 | + ); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Format the uses namespace for the new group attributes. |
|
| 61 | - * |
|
| 62 | - * @param array $new |
|
| 63 | - * @param array $old |
|
| 64 | - * |
|
| 65 | - * @return string|null |
|
| 66 | - */ |
|
| 67 | - protected static function formatUseNamespace($new, $old) |
|
| 68 | - { |
|
| 69 | - if (isset($new['namespace'])) { |
|
| 70 | - return isset($old['namespace']) |
|
| 71 | - ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
| 72 | - : trim($new['namespace'], '\\'); |
|
| 73 | - } |
|
| 59 | + /** |
|
| 60 | + * Format the uses namespace for the new group attributes. |
|
| 61 | + * |
|
| 62 | + * @param array $new |
|
| 63 | + * @param array $old |
|
| 64 | + * |
|
| 65 | + * @return string|null |
|
| 66 | + */ |
|
| 67 | + protected static function formatUseNamespace($new, $old) |
|
| 68 | + { |
|
| 69 | + if (isset($new['namespace'])) { |
|
| 70 | + return isset($old['namespace']) |
|
| 71 | + ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
| 72 | + : trim($new['namespace'], '\\'); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - return $old['namespace'] ?? null; |
|
| 76 | - } |
|
| 75 | + return $old['namespace'] ?? null; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Format the prefix for the new group attributes. |
|
| 80 | - * |
|
| 81 | - * @param array $new |
|
| 82 | - * @param array $old |
|
| 83 | - * |
|
| 84 | - * @return string|null |
|
| 85 | - */ |
|
| 86 | - protected static function formatUsePrefix($new, $old) |
|
| 87 | - { |
|
| 88 | - $old = $old['prefix'] ?? null; |
|
| 78 | + /** |
|
| 79 | + * Format the prefix for the new group attributes. |
|
| 80 | + * |
|
| 81 | + * @param array $new |
|
| 82 | + * @param array $old |
|
| 83 | + * |
|
| 84 | + * @return string|null |
|
| 85 | + */ |
|
| 86 | + protected static function formatUsePrefix($new, $old) |
|
| 87 | + { |
|
| 88 | + $old = $old['prefix'] ?? null; |
|
| 89 | 89 | |
| 90 | - return isset($new['prefix']) |
|
| 91 | - ? trim($old, '/').'/'.trim($new['prefix'], '/') |
|
| 92 | - : $old; |
|
| 93 | - } |
|
| 90 | + return isset($new['prefix']) |
|
| 91 | + ? trim($old, '/').'/'.trim($new['prefix'], '/') |
|
| 92 | + : $old; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Format the "wheres" for the new group attributes. |
|
| 97 | - * |
|
| 98 | - * @param array $new |
|
| 99 | - * @param array $old |
|
| 100 | - * |
|
| 101 | - * @return array |
|
| 102 | - */ |
|
| 103 | - protected static function formatUseWhere($new, $old) |
|
| 104 | - { |
|
| 105 | - return array_merge( |
|
| 106 | - $old['where'] ?? [], |
|
| 107 | - $new['where'] ?? [] |
|
| 108 | - ); |
|
| 109 | - } |
|
| 95 | + /** |
|
| 96 | + * Format the "wheres" for the new group attributes. |
|
| 97 | + * |
|
| 98 | + * @param array $new |
|
| 99 | + * @param array $old |
|
| 100 | + * |
|
| 101 | + * @return array |
|
| 102 | + */ |
|
| 103 | + protected static function formatUseWhere($new, $old) |
|
| 104 | + { |
|
| 105 | + return array_merge( |
|
| 106 | + $old['where'] ?? [], |
|
| 107 | + $new['where'] ?? [] |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Format the "as" clause of the new group attributes. |
|
| 113 | - * |
|
| 114 | - * @param array $new |
|
| 115 | - * @param array $old |
|
| 116 | - * |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - protected static function formatUseAs($new, $old) |
|
| 120 | - { |
|
| 121 | - if (isset($old['as'])) { |
|
| 111 | + /** |
|
| 112 | + * Format the "as" clause of the new group attributes. |
|
| 113 | + * |
|
| 114 | + * @param array $new |
|
| 115 | + * @param array $old |
|
| 116 | + * |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + protected static function formatUseAs($new, $old) |
|
| 120 | + { |
|
| 121 | + if (isset($old['as'])) { |
|
| 122 | 122 | $new['as'] = $old['as'].($new['as'] ?? ''); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - return $new; |
|
| 126 | - } |
|
| 125 | + return $new; |
|
| 126 | + } |
|
| 127 | 127 | } |
| 128 | 128 | \ No newline at end of file |
@@ -95,7 +95,7 @@ |
||
| 95 | 95 | |
| 96 | 96 | $parameters = array_intersect_key($matches, array_values($parameterNames)); |
| 97 | 97 | |
| 98 | - return array_filter($parameters, function ($value) { |
|
| 98 | + return array_filter($parameters, function($value) { |
|
| 99 | 99 | return (is_string($value) && strlen($value) > 0); |
| 100 | 100 | }); |
| 101 | 101 | } |
@@ -33,721 +33,721 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Cli |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * Background color identifier. |
|
| 38 | - * |
|
| 39 | - * @var array $backgroundColors |
|
| 40 | - */ |
|
| 41 | - protected static $backgroundColors = [ |
|
| 42 | - 'black' => '40', |
|
| 43 | - 'red' => '41', |
|
| 44 | - 'green' => '42', |
|
| 45 | - 'yellow' => '43', |
|
| 46 | - 'blue' => '44', |
|
| 47 | - 'magenta' => '45', |
|
| 48 | - 'cyan' => '46', |
|
| 49 | - 'light_gray' => '47' |
|
| 50 | - ]; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Foreground color identifier. |
|
| 54 | - * |
|
| 55 | - * @var array $foregroundColors |
|
| 56 | - */ |
|
| 57 | - protected static $foregroundColors = [ |
|
| 58 | - 'black' => '0;30', |
|
| 59 | - 'dark_gray' => '1;30', |
|
| 60 | - 'blue' => '0;34', |
|
| 61 | - 'dark_blue' => '1;34', |
|
| 62 | - 'light_blue' => '1;34', |
|
| 63 | - 'green' => '0;32', |
|
| 64 | - 'light_green' => '1;32', |
|
| 65 | - 'cyan' => '0;36', |
|
| 66 | - 'light_cyan' => '1;36', |
|
| 67 | - 'red' => '0;31', |
|
| 68 | - 'light_red' => '1;31', |
|
| 69 | - 'purple' => '0;35', |
|
| 70 | - 'light_purple' => '1;35', |
|
| 71 | - 'light_yellow' => '0;33', |
|
| 72 | - 'yellow' => '1;33', |
|
| 73 | - 'light_gray' => '0;37', |
|
| 74 | - 'white' => '1;37' |
|
| 75 | - ]; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Indicates that you do not use any color for foreground or background. |
|
| 79 | - * |
|
| 80 | - * @var bool $noColor |
|
| 81 | - */ |
|
| 82 | - public static $noColor = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * String of arguments to be used in console. |
|
| 86 | - * |
|
| 87 | - * @var array $options |
|
| 88 | - */ |
|
| 89 | - protected static $options = []; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Readline Support for command line. |
|
| 93 | - * |
|
| 94 | - * @var bool $readlineSupport |
|
| 95 | - */ |
|
| 96 | - public static $readlineSupport = false; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * List of array segments. |
|
| 100 | - * |
|
| 101 | - * @var array $segments |
|
| 102 | - */ |
|
| 103 | - protected static $segments = []; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The standar STDERR is where the application writes its error messages. |
|
| 107 | - * |
|
| 108 | - * @var string $stderr |
|
| 109 | - */ |
|
| 110 | - protected static $stderr; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * The estandar STDOUT is where the application records its output messages. |
|
| 114 | - * |
|
| 115 | - * @var resource $stdout |
|
| 116 | - */ |
|
| 117 | - protected static $stdout; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Message that tells the user that he is waiting to receive an order. |
|
| 121 | - * |
|
| 122 | - * @var string $waitMsg |
|
| 123 | - */ |
|
| 124 | - public static $waitMsg = 'Press any key to continue...'; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Static constructor. Parses all the CLI params. |
|
| 128 | - * |
|
| 129 | - * @return \Syscodes\Contracts\Core\Lenevor $core |
|
| 130 | - * |
|
| 131 | - * @throws \Exception |
|
| 132 | - */ |
|
| 133 | - public static function initialize() |
|
| 134 | - { |
|
| 135 | - // Readline is an extension for PHP that makes interactive the command console |
|
| 136 | - static::$readlineSupport = extension_loaded('readline'); |
|
| 137 | - |
|
| 138 | - // clear segments & options to keep testing clean |
|
| 139 | - static::$options = []; |
|
| 140 | - static::$segments = []; |
|
| 141 | - |
|
| 142 | - static::parseCommandLine(); |
|
| 143 | - |
|
| 144 | - // Writes its error messages |
|
| 145 | - static::$stderr = STDERR; |
|
| 146 | - |
|
| 147 | - // Records its output messages |
|
| 148 | - static::$stdout = STDOUT; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Beeps a certain number of times. |
|
| 153 | - * |
|
| 154 | - * @param int $num The number of times to beep |
|
| 155 | - * |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public static function bell(int $num = 1) |
|
| 159 | - { |
|
| 160 | - echo str_repeat("\x07", $num); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Clears the screen of output. |
|
| 165 | - * |
|
| 166 | - * @return void |
|
| 167 | - */ |
|
| 168 | - public static function clearScreen() |
|
| 169 | - { |
|
| 170 | - static::isWindows() |
|
| 171 | - // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 172 | - ? static::newLine(40) |
|
| 173 | - // Anything with a flair of Unix will handle these magic characters |
|
| 174 | - : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Returns the given text with the correct color codes for a foreground and |
|
| 179 | - * optionally a background color. |
|
| 180 | - * |
|
| 181 | - * @param string $text The text to color |
|
| 182 | - * @param string $foreground The foreground color |
|
| 183 | - * @param string $background The background color |
|
| 184 | - * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 185 | - * |
|
| 186 | - * @return string The color coded string |
|
| 187 | - * |
|
| 188 | - * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 189 | - */ |
|
| 190 | - public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 191 | - { |
|
| 192 | - if (static::$noColor) |
|
| 193 | - { |
|
| 194 | - return $text; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
| 198 | - throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | - throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 206 | - |
|
| 207 | - if ($background !== null) { |
|
| 208 | - $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($format === 'underline') { |
|
| 212 | - $string .= "\033[4m"; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - $string .= $text."\033[0m"; |
|
| 216 | - |
|
| 217 | - return $string; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get the number of characters in a string. |
|
| 222 | - * |
|
| 223 | - * @param string $string |
|
| 224 | - * |
|
| 225 | - * @return int |
|
| 226 | - */ |
|
| 227 | - public static function strlen(?string $string) |
|
| 228 | - { |
|
| 229 | - if (is_null($string)) { |
|
| 230 | - return 0; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - foreach (static::$foregroundColors as $color) { |
|
| 234 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - foreach (static::$backgroundColors as $color) { |
|
| 238 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 242 | - |
|
| 243 | - return mb_strlen($string); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 248 | - * |
|
| 249 | - * @param string|array $text The text to output, or array of errors |
|
| 250 | - * @param string $foreground The foreground color |
|
| 251 | - * @param string|null $background the background color |
|
| 252 | - * |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 256 | - { |
|
| 257 | - if (is_array($text)) { |
|
| 258 | - $text = implode(PHP_EOL, $text); |
|
| 259 | - } |
|
| 36 | + /** |
|
| 37 | + * Background color identifier. |
|
| 38 | + * |
|
| 39 | + * @var array $backgroundColors |
|
| 40 | + */ |
|
| 41 | + protected static $backgroundColors = [ |
|
| 42 | + 'black' => '40', |
|
| 43 | + 'red' => '41', |
|
| 44 | + 'green' => '42', |
|
| 45 | + 'yellow' => '43', |
|
| 46 | + 'blue' => '44', |
|
| 47 | + 'magenta' => '45', |
|
| 48 | + 'cyan' => '46', |
|
| 49 | + 'light_gray' => '47' |
|
| 50 | + ]; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Foreground color identifier. |
|
| 54 | + * |
|
| 55 | + * @var array $foregroundColors |
|
| 56 | + */ |
|
| 57 | + protected static $foregroundColors = [ |
|
| 58 | + 'black' => '0;30', |
|
| 59 | + 'dark_gray' => '1;30', |
|
| 60 | + 'blue' => '0;34', |
|
| 61 | + 'dark_blue' => '1;34', |
|
| 62 | + 'light_blue' => '1;34', |
|
| 63 | + 'green' => '0;32', |
|
| 64 | + 'light_green' => '1;32', |
|
| 65 | + 'cyan' => '0;36', |
|
| 66 | + 'light_cyan' => '1;36', |
|
| 67 | + 'red' => '0;31', |
|
| 68 | + 'light_red' => '1;31', |
|
| 69 | + 'purple' => '0;35', |
|
| 70 | + 'light_purple' => '1;35', |
|
| 71 | + 'light_yellow' => '0;33', |
|
| 72 | + 'yellow' => '1;33', |
|
| 73 | + 'light_gray' => '0;37', |
|
| 74 | + 'white' => '1;37' |
|
| 75 | + ]; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Indicates that you do not use any color for foreground or background. |
|
| 79 | + * |
|
| 80 | + * @var bool $noColor |
|
| 81 | + */ |
|
| 82 | + public static $noColor = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * String of arguments to be used in console. |
|
| 86 | + * |
|
| 87 | + * @var array $options |
|
| 88 | + */ |
|
| 89 | + protected static $options = []; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Readline Support for command line. |
|
| 93 | + * |
|
| 94 | + * @var bool $readlineSupport |
|
| 95 | + */ |
|
| 96 | + public static $readlineSupport = false; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * List of array segments. |
|
| 100 | + * |
|
| 101 | + * @var array $segments |
|
| 102 | + */ |
|
| 103 | + protected static $segments = []; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The standar STDERR is where the application writes its error messages. |
|
| 107 | + * |
|
| 108 | + * @var string $stderr |
|
| 109 | + */ |
|
| 110 | + protected static $stderr; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * The estandar STDOUT is where the application records its output messages. |
|
| 114 | + * |
|
| 115 | + * @var resource $stdout |
|
| 116 | + */ |
|
| 117 | + protected static $stdout; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Message that tells the user that he is waiting to receive an order. |
|
| 121 | + * |
|
| 122 | + * @var string $waitMsg |
|
| 123 | + */ |
|
| 124 | + public static $waitMsg = 'Press any key to continue...'; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Static constructor. Parses all the CLI params. |
|
| 128 | + * |
|
| 129 | + * @return \Syscodes\Contracts\Core\Lenevor $core |
|
| 130 | + * |
|
| 131 | + * @throws \Exception |
|
| 132 | + */ |
|
| 133 | + public static function initialize() |
|
| 134 | + { |
|
| 135 | + // Readline is an extension for PHP that makes interactive the command console |
|
| 136 | + static::$readlineSupport = extension_loaded('readline'); |
|
| 137 | + |
|
| 138 | + // clear segments & options to keep testing clean |
|
| 139 | + static::$options = []; |
|
| 140 | + static::$segments = []; |
|
| 141 | + |
|
| 142 | + static::parseCommandLine(); |
|
| 143 | + |
|
| 144 | + // Writes its error messages |
|
| 145 | + static::$stderr = STDERR; |
|
| 146 | + |
|
| 147 | + // Records its output messages |
|
| 148 | + static::$stdout = STDOUT; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Beeps a certain number of times. |
|
| 153 | + * |
|
| 154 | + * @param int $num The number of times to beep |
|
| 155 | + * |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public static function bell(int $num = 1) |
|
| 159 | + { |
|
| 160 | + echo str_repeat("\x07", $num); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Clears the screen of output. |
|
| 165 | + * |
|
| 166 | + * @return void |
|
| 167 | + */ |
|
| 168 | + public static function clearScreen() |
|
| 169 | + { |
|
| 170 | + static::isWindows() |
|
| 171 | + // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 172 | + ? static::newLine(40) |
|
| 173 | + // Anything with a flair of Unix will handle these magic characters |
|
| 174 | + : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Returns the given text with the correct color codes for a foreground and |
|
| 179 | + * optionally a background color. |
|
| 180 | + * |
|
| 181 | + * @param string $text The text to color |
|
| 182 | + * @param string $foreground The foreground color |
|
| 183 | + * @param string $background The background color |
|
| 184 | + * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 185 | + * |
|
| 186 | + * @return string The color coded string |
|
| 187 | + * |
|
| 188 | + * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 189 | + */ |
|
| 190 | + public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 191 | + { |
|
| 192 | + if (static::$noColor) |
|
| 193 | + { |
|
| 194 | + return $text; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
| 198 | + throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | + throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 206 | + |
|
| 207 | + if ($background !== null) { |
|
| 208 | + $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($format === 'underline') { |
|
| 212 | + $string .= "\033[4m"; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + $string .= $text."\033[0m"; |
|
| 216 | + |
|
| 217 | + return $string; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get the number of characters in a string. |
|
| 222 | + * |
|
| 223 | + * @param string $string |
|
| 224 | + * |
|
| 225 | + * @return int |
|
| 226 | + */ |
|
| 227 | + public static function strlen(?string $string) |
|
| 228 | + { |
|
| 229 | + if (is_null($string)) { |
|
| 230 | + return 0; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + foreach (static::$foregroundColors as $color) { |
|
| 234 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + foreach (static::$backgroundColors as $color) { |
|
| 238 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 242 | + |
|
| 243 | + return mb_strlen($string); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 248 | + * |
|
| 249 | + * @param string|array $text The text to output, or array of errors |
|
| 250 | + * @param string $foreground The foreground color |
|
| 251 | + * @param string|null $background the background color |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 256 | + { |
|
| 257 | + if (is_array($text)) { |
|
| 258 | + $text = implode(PHP_EOL, $text); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - if ($foreground || $background) { |
|
| 262 | - $text = static::color($text, $foreground, $background); |
|
| 263 | - } |
|
| 261 | + if ($foreground || $background) { |
|
| 262 | + $text = static::color($text, $foreground, $background); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - static::fwrite(static::$stderr, $text.PHP_EOL); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Attempts to determine the width of the viewable CLI window. |
|
| 270 | - * |
|
| 271 | - * @param int $default |
|
| 272 | - * |
|
| 273 | - * @return int |
|
| 274 | - */ |
|
| 275 | - public static function getWidth(int $default = 80) |
|
| 276 | - { |
|
| 277 | - if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
| 278 | - return $default; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - return (int) shell_exec('tput cols'); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Attempts to determine the height of the viewable CLI window. |
|
| 286 | - * |
|
| 287 | - * @param int $default |
|
| 288 | - * |
|
| 289 | - * @return int |
|
| 290 | - */ |
|
| 291 | - public static function getHeight(int $default = 32) |
|
| 292 | - { |
|
| 293 | - if (static::isWindows()) { |
|
| 294 | - return $default; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return (int) shell_exec('tput lines'); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 302 | - * If no maximum width is specified, will wrap to the window's max. |
|
| 303 | - * |
|
| 304 | - * @param string $string |
|
| 305 | - * @param int $max |
|
| 306 | - * @param int $padLeft |
|
| 307 | - * |
|
| 308 | - * @return string |
|
| 309 | - */ |
|
| 310 | - public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 311 | - { |
|
| 312 | - if (empty($string)) { |
|
| 313 | - return ''; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if ($max === 0) { |
|
| 317 | - $max = static::getWidth(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if (static::getWidth() < $max) { |
|
| 321 | - $max = static::getWidth(); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $max = $max - $padLeft; |
|
| 325 | - |
|
| 326 | - $lines = wordwrap($string, $max); |
|
| 327 | - |
|
| 328 | - if ($pad_left > 0) { |
|
| 329 | - $lines = explode(PHP_EOL, $lines); |
|
| 330 | - |
|
| 331 | - $first = true; |
|
| 332 | - |
|
| 333 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 334 | - if ( ! $first) { |
|
| 335 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 336 | - } else { |
|
| 337 | - $first = false; |
|
| 338 | - } |
|
| 339 | - }); |
|
| 340 | - |
|
| 341 | - $lines = implode(PHP_EOL, $lines); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - return $lines; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Get input from the shell, using readline or the standard STDIN. |
|
| 349 | - * |
|
| 350 | - * @param string|int $prefix The name of the option (int if unnamed) |
|
| 351 | - * |
|
| 352 | - * @return string |
|
| 353 | - */ |
|
| 354 | - public static function input($prefix = '') |
|
| 355 | - { |
|
| 356 | - if (static::$readlineSupport) { |
|
| 357 | - return readline($prefix); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - echo $prefix; |
|
| 361 | - |
|
| 362 | - return fgets(STDIN); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * If operating system === windows. |
|
| 367 | - * |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public static function isWindows() |
|
| 371 | - { |
|
| 372 | - return stripos(PHP_OS, 'WIN') === 0; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Enter a number of empty lines. |
|
| 377 | - * |
|
| 378 | - * @param int $num Number of lines to output |
|
| 379 | - * |
|
| 380 | - * @return void |
|
| 381 | - */ |
|
| 382 | - public static function newLine(int $num = 1) |
|
| 383 | - { |
|
| 384 | - for ($i = 0; $i < $num; $i++) { |
|
| 385 | - static::write(); |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Returns the option with the given name. You can also give the option number. |
|
| 391 | - * |
|
| 392 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 393 | - * @param mixed $default The value to return if the option is not defined |
|
| 394 | - * |
|
| 395 | - * @return mixed |
|
| 396 | - * |
|
| 397 | - * @uses \Syscodes\Contract\Core\Lenevor |
|
| 398 | - */ |
|
| 399 | - public static function option($name, $default = null) |
|
| 400 | - { |
|
| 401 | - if ( ! isset(static::$options[$name])) { |
|
| 402 | - return Lenevor::value($default); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return static::$options[$name]; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Parses the command line it was called from and collects all |
|
| 410 | - * options and valid segments. |
|
| 411 | - * |
|
| 412 | - * @return bool |
|
| 413 | - */ |
|
| 414 | - protected static function parseCommandLine() |
|
| 415 | - { |
|
| 416 | - $options = false; |
|
| 417 | - |
|
| 418 | - for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
| 419 | - if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
| 420 | - static::$segments[] = $_SERVER['argv'][$i]; |
|
| 421 | - |
|
| 422 | - continue; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - $options = true; |
|
| 426 | - |
|
| 427 | - $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 428 | - $value = null; |
|
| 429 | - |
|
| 430 | - if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
| 431 | - $value = $_SERVER['argv'][$i + 1]; |
|
| 432 | - $i++; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - static::$options[$args] = $value; |
|
| 436 | - |
|
| 437 | - $options = false; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Returns the command line string portions of the arguments, minus |
|
| 443 | - * any options, as a string. |
|
| 444 | - * |
|
| 445 | - * @return string |
|
| 446 | - */ |
|
| 447 | - public static function getURI() |
|
| 448 | - { |
|
| 449 | - return implode('/', static::$segments); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Returns an individual segment. |
|
| 454 | - * |
|
| 455 | - * @param int $index |
|
| 456 | - * |
|
| 457 | - * @return mixed|null |
|
| 458 | - */ |
|
| 459 | - public static function getSegment(int $index) |
|
| 460 | - { |
|
| 461 | - if ( ! isset(static::$segments[$index - 1])) { |
|
| 462 | - return null; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - return static::$segments[$index - 1]; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Returns the raw array of segments found. |
|
| 470 | - * |
|
| 471 | - * @return array |
|
| 472 | - */ |
|
| 473 | - public static function getSegments() |
|
| 474 | - { |
|
| 475 | - return static::$segments; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 480 | - * |
|
| 481 | - * Usage: |
|
| 482 | - * |
|
| 483 | - * // Waits for any key press |
|
| 484 | - * Cli::prompt(); |
|
| 485 | - * |
|
| 486 | - * // Takes any input |
|
| 487 | - * $color = Cli::prompt('What is your favorite color?'); |
|
| 488 | - * |
|
| 489 | - * // Takes any input, but offers default |
|
| 490 | - * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 491 | - * |
|
| 492 | - * // Will only accept the options in the array |
|
| 493 | - * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 494 | - * |
|
| 495 | - * @return string The user input |
|
| 496 | - */ |
|
| 497 | - public static function prompt() |
|
| 498 | - { |
|
| 499 | - $args = func_get_args(); |
|
| 500 | - |
|
| 501 | - $options = []; |
|
| 502 | - $output = ''; |
|
| 503 | - $default = null; |
|
| 504 | - |
|
| 505 | - // How many we got |
|
| 506 | - $arg_count = count($args); |
|
| 507 | - |
|
| 508 | - // Is the last argument a boolean? True means required |
|
| 509 | - $required = end($args) === true; |
|
| 510 | - |
|
| 511 | - // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 512 | - $required === true and --$arg_count; |
|
| 513 | - |
|
| 514 | - // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 515 | - switch ($arg_count) { |
|
| 516 | - case 2: |
|
| 517 | - |
|
| 518 | - // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 519 | - if (is_array($args[1])) { |
|
| 520 | - list($output, $options) = $args; |
|
| 521 | - } |
|
| 522 | - // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 523 | - elseif (is_string($args[1])) { |
|
| 524 | - list($output, $default) = $args; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - break; |
|
| 528 | - |
|
| 529 | - case 1: |
|
| 530 | - |
|
| 531 | - // No question (probably been asked already) so just show options |
|
| 532 | - // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 533 | - if (is_array($args[0])) { |
|
| 534 | - $options = $args[0]; |
|
| 535 | - } |
|
| 536 | - // Question without options |
|
| 537 | - // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 538 | - elseif (is_string($args[0])) { |
|
| 539 | - $output = $args[0]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - break; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - // If a question has been asked with the read |
|
| 546 | - if ($output !== '') { |
|
| 547 | - $extra_output = ''; |
|
| 548 | - |
|
| 549 | - if ($default !== null) { |
|
| 550 | - $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 551 | - } elseif ($options !== []) { |
|
| 552 | - $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - static::fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Read the input from keyboard. |
|
| 559 | - $input = trim(static::input()) ?: $default; |
|
| 560 | - |
|
| 561 | - // No input provided and we require one (default will stop this being called) |
|
| 562 | - if (empty($input) and $required === true) { |
|
| 563 | - static::write('This is required.'); |
|
| 564 | - static::newLine(); |
|
| 565 | - |
|
| 566 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - // If options are provided and the choice is not in the array, tell them to try again |
|
| 570 | - if ( ! empty($options) and ! in_array($input, $options)) { |
|
| 571 | - static::write('This is not a valid option. Please try again.'); |
|
| 572 | - static::newLine(); |
|
| 573 | - |
|
| 574 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $input; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Allows you to set a commandline option from code. |
|
| 582 | - * |
|
| 583 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 584 | - * @param mixed|null $value The value to set, or null to delete the option |
|
| 585 | - * |
|
| 586 | - * @return mixed |
|
| 587 | - */ |
|
| 588 | - public static function setOption($name, $value = null) |
|
| 589 | - { |
|
| 590 | - if ($value == null) { |
|
| 591 | - if (isset(static::$options[$name])) { |
|
| 592 | - unset(static::$options[$name]); |
|
| 593 | - } |
|
| 594 | - } else { |
|
| 595 | - static::$options[$name] = $value; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Waits a certain number of seconds, optionally showing a wait message and |
|
| 601 | - * waiting for a key press. |
|
| 602 | - * |
|
| 603 | - * @param int $seconds Number of seconds |
|
| 604 | - * @param bool $countdown Show a countdown or not |
|
| 605 | - * |
|
| 606 | - * @return string |
|
| 607 | - */ |
|
| 608 | - public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 609 | - { |
|
| 610 | - if ($countdown === true) { |
|
| 611 | - $time = $seconds; |
|
| 612 | - |
|
| 613 | - while ($time > 0) { |
|
| 614 | - fwrite(static::$stdout, $time.'... '); |
|
| 615 | - sleep(1); |
|
| 616 | - $time--; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - static::write(); |
|
| 620 | - } else { |
|
| 621 | - if ($seconds = 0) { |
|
| 622 | - sleep($seconds); |
|
| 623 | - } else { |
|
| 624 | - static::write(static::$waitMsg); |
|
| 625 | - static::input(); |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Outputs a string to the cli. If you send an array it will implode them |
|
| 632 | - * with a line break. |
|
| 633 | - * |
|
| 634 | - * @param string|array $text The text to output, or array of lines |
|
| 635 | - * @param string|null $foreground The foreground color |
|
| 636 | - * @param string|null $background The background color |
|
| 637 | - * |
|
| 638 | - * @return string |
|
| 639 | - */ |
|
| 640 | - public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 641 | - { |
|
| 642 | - if (is_array($text)) { |
|
| 643 | - $text = implode(PHP_EOL, $text); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - if ($foreground OR $background) { |
|
| 647 | - $text = static::color($text, $foreground, $background); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - static::fwrite(static::$stdout, $text.PHP_EOL); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Returns a well formatted table. |
|
| 655 | - * |
|
| 656 | - * @param array $tbody List of rows |
|
| 657 | - * @param array $thead List of columns |
|
| 658 | - * |
|
| 659 | - * @return void |
|
| 660 | - */ |
|
| 661 | - public static function table(array $tbody, array $thead = []) |
|
| 662 | - { |
|
| 663 | - $rows = []; |
|
| 664 | - |
|
| 665 | - if ( ! empty($thead)) { |
|
| 666 | - $rows[] = array_values($thead); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - foreach ($tbody as $tr) { |
|
| 670 | - $rows[] = count($rows); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - $totalRows = count($rows); |
|
| 674 | - |
|
| 675 | - // Store all columns lengths |
|
| 676 | - $allColsLengths = []; |
|
| 677 | - |
|
| 678 | - // Store maximum lengths by column |
|
| 679 | - $maxColsLengths = []; |
|
| 680 | - |
|
| 681 | - for ($row = 0; $row < $totalRows; $row++) { |
|
| 682 | - $column = 0; |
|
| 683 | - |
|
| 684 | - foreach ($rows[$row] as $col) { |
|
| 685 | - $allColsLengths[$row][$column] = static::strlen($col); |
|
| 686 | - |
|
| 687 | - if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
| 688 | - $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - $column++; |
|
| 692 | - } |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - for ($row = 0; $row < $totalRows; $row++) { |
|
| 696 | - $column = 0; |
|
| 697 | - |
|
| 698 | - foreach ($rows[$row] as $col) |
|
| 699 | - { |
|
| 700 | - $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 265 | + static::fwrite(static::$stderr, $text.PHP_EOL); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Attempts to determine the width of the viewable CLI window. |
|
| 270 | + * |
|
| 271 | + * @param int $default |
|
| 272 | + * |
|
| 273 | + * @return int |
|
| 274 | + */ |
|
| 275 | + public static function getWidth(int $default = 80) |
|
| 276 | + { |
|
| 277 | + if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
| 278 | + return $default; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + return (int) shell_exec('tput cols'); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Attempts to determine the height of the viewable CLI window. |
|
| 286 | + * |
|
| 287 | + * @param int $default |
|
| 288 | + * |
|
| 289 | + * @return int |
|
| 290 | + */ |
|
| 291 | + public static function getHeight(int $default = 32) |
|
| 292 | + { |
|
| 293 | + if (static::isWindows()) { |
|
| 294 | + return $default; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return (int) shell_exec('tput lines'); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 302 | + * If no maximum width is specified, will wrap to the window's max. |
|
| 303 | + * |
|
| 304 | + * @param string $string |
|
| 305 | + * @param int $max |
|
| 306 | + * @param int $padLeft |
|
| 307 | + * |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 310 | + public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 311 | + { |
|
| 312 | + if (empty($string)) { |
|
| 313 | + return ''; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if ($max === 0) { |
|
| 317 | + $max = static::getWidth(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if (static::getWidth() < $max) { |
|
| 321 | + $max = static::getWidth(); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $max = $max - $padLeft; |
|
| 325 | + |
|
| 326 | + $lines = wordwrap($string, $max); |
|
| 327 | + |
|
| 328 | + if ($pad_left > 0) { |
|
| 329 | + $lines = explode(PHP_EOL, $lines); |
|
| 330 | + |
|
| 331 | + $first = true; |
|
| 332 | + |
|
| 333 | + array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 334 | + if ( ! $first) { |
|
| 335 | + $line = str_repeat(' ', $pad_left) . $line; |
|
| 336 | + } else { |
|
| 337 | + $first = false; |
|
| 338 | + } |
|
| 339 | + }); |
|
| 340 | + |
|
| 341 | + $lines = implode(PHP_EOL, $lines); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + return $lines; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Get input from the shell, using readline or the standard STDIN. |
|
| 349 | + * |
|
| 350 | + * @param string|int $prefix The name of the option (int if unnamed) |
|
| 351 | + * |
|
| 352 | + * @return string |
|
| 353 | + */ |
|
| 354 | + public static function input($prefix = '') |
|
| 355 | + { |
|
| 356 | + if (static::$readlineSupport) { |
|
| 357 | + return readline($prefix); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + echo $prefix; |
|
| 361 | + |
|
| 362 | + return fgets(STDIN); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * If operating system === windows. |
|
| 367 | + * |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public static function isWindows() |
|
| 371 | + { |
|
| 372 | + return stripos(PHP_OS, 'WIN') === 0; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Enter a number of empty lines. |
|
| 377 | + * |
|
| 378 | + * @param int $num Number of lines to output |
|
| 379 | + * |
|
| 380 | + * @return void |
|
| 381 | + */ |
|
| 382 | + public static function newLine(int $num = 1) |
|
| 383 | + { |
|
| 384 | + for ($i = 0; $i < $num; $i++) { |
|
| 385 | + static::write(); |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Returns the option with the given name. You can also give the option number. |
|
| 391 | + * |
|
| 392 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 393 | + * @param mixed $default The value to return if the option is not defined |
|
| 394 | + * |
|
| 395 | + * @return mixed |
|
| 396 | + * |
|
| 397 | + * @uses \Syscodes\Contract\Core\Lenevor |
|
| 398 | + */ |
|
| 399 | + public static function option($name, $default = null) |
|
| 400 | + { |
|
| 401 | + if ( ! isset(static::$options[$name])) { |
|
| 402 | + return Lenevor::value($default); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return static::$options[$name]; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Parses the command line it was called from and collects all |
|
| 410 | + * options and valid segments. |
|
| 411 | + * |
|
| 412 | + * @return bool |
|
| 413 | + */ |
|
| 414 | + protected static function parseCommandLine() |
|
| 415 | + { |
|
| 416 | + $options = false; |
|
| 417 | + |
|
| 418 | + for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
| 419 | + if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
| 420 | + static::$segments[] = $_SERVER['argv'][$i]; |
|
| 421 | + |
|
| 422 | + continue; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + $options = true; |
|
| 426 | + |
|
| 427 | + $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 428 | + $value = null; |
|
| 429 | + |
|
| 430 | + if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
| 431 | + $value = $_SERVER['argv'][$i + 1]; |
|
| 432 | + $i++; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + static::$options[$args] = $value; |
|
| 436 | + |
|
| 437 | + $options = false; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Returns the command line string portions of the arguments, minus |
|
| 443 | + * any options, as a string. |
|
| 444 | + * |
|
| 445 | + * @return string |
|
| 446 | + */ |
|
| 447 | + public static function getURI() |
|
| 448 | + { |
|
| 449 | + return implode('/', static::$segments); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Returns an individual segment. |
|
| 454 | + * |
|
| 455 | + * @param int $index |
|
| 456 | + * |
|
| 457 | + * @return mixed|null |
|
| 458 | + */ |
|
| 459 | + public static function getSegment(int $index) |
|
| 460 | + { |
|
| 461 | + if ( ! isset(static::$segments[$index - 1])) { |
|
| 462 | + return null; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + return static::$segments[$index - 1]; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Returns the raw array of segments found. |
|
| 470 | + * |
|
| 471 | + * @return array |
|
| 472 | + */ |
|
| 473 | + public static function getSegments() |
|
| 474 | + { |
|
| 475 | + return static::$segments; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 480 | + * |
|
| 481 | + * Usage: |
|
| 482 | + * |
|
| 483 | + * // Waits for any key press |
|
| 484 | + * Cli::prompt(); |
|
| 485 | + * |
|
| 486 | + * // Takes any input |
|
| 487 | + * $color = Cli::prompt('What is your favorite color?'); |
|
| 488 | + * |
|
| 489 | + * // Takes any input, but offers default |
|
| 490 | + * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 491 | + * |
|
| 492 | + * // Will only accept the options in the array |
|
| 493 | + * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 494 | + * |
|
| 495 | + * @return string The user input |
|
| 496 | + */ |
|
| 497 | + public static function prompt() |
|
| 498 | + { |
|
| 499 | + $args = func_get_args(); |
|
| 500 | + |
|
| 501 | + $options = []; |
|
| 502 | + $output = ''; |
|
| 503 | + $default = null; |
|
| 504 | + |
|
| 505 | + // How many we got |
|
| 506 | + $arg_count = count($args); |
|
| 507 | + |
|
| 508 | + // Is the last argument a boolean? True means required |
|
| 509 | + $required = end($args) === true; |
|
| 510 | + |
|
| 511 | + // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 512 | + $required === true and --$arg_count; |
|
| 513 | + |
|
| 514 | + // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 515 | + switch ($arg_count) { |
|
| 516 | + case 2: |
|
| 517 | + |
|
| 518 | + // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 519 | + if (is_array($args[1])) { |
|
| 520 | + list($output, $options) = $args; |
|
| 521 | + } |
|
| 522 | + // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 523 | + elseif (is_string($args[1])) { |
|
| 524 | + list($output, $default) = $args; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + break; |
|
| 528 | + |
|
| 529 | + case 1: |
|
| 530 | + |
|
| 531 | + // No question (probably been asked already) so just show options |
|
| 532 | + // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 533 | + if (is_array($args[0])) { |
|
| 534 | + $options = $args[0]; |
|
| 535 | + } |
|
| 536 | + // Question without options |
|
| 537 | + // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 538 | + elseif (is_string($args[0])) { |
|
| 539 | + $output = $args[0]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + break; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + // If a question has been asked with the read |
|
| 546 | + if ($output !== '') { |
|
| 547 | + $extra_output = ''; |
|
| 548 | + |
|
| 549 | + if ($default !== null) { |
|
| 550 | + $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 551 | + } elseif ($options !== []) { |
|
| 552 | + $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + static::fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Read the input from keyboard. |
|
| 559 | + $input = trim(static::input()) ?: $default; |
|
| 560 | + |
|
| 561 | + // No input provided and we require one (default will stop this being called) |
|
| 562 | + if (empty($input) and $required === true) { |
|
| 563 | + static::write('This is required.'); |
|
| 564 | + static::newLine(); |
|
| 565 | + |
|
| 566 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + // If options are provided and the choice is not in the array, tell them to try again |
|
| 570 | + if ( ! empty($options) and ! in_array($input, $options)) { |
|
| 571 | + static::write('This is not a valid option. Please try again.'); |
|
| 572 | + static::newLine(); |
|
| 573 | + |
|
| 574 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $input; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Allows you to set a commandline option from code. |
|
| 582 | + * |
|
| 583 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 584 | + * @param mixed|null $value The value to set, or null to delete the option |
|
| 585 | + * |
|
| 586 | + * @return mixed |
|
| 587 | + */ |
|
| 588 | + public static function setOption($name, $value = null) |
|
| 589 | + { |
|
| 590 | + if ($value == null) { |
|
| 591 | + if (isset(static::$options[$name])) { |
|
| 592 | + unset(static::$options[$name]); |
|
| 593 | + } |
|
| 594 | + } else { |
|
| 595 | + static::$options[$name] = $value; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Waits a certain number of seconds, optionally showing a wait message and |
|
| 601 | + * waiting for a key press. |
|
| 602 | + * |
|
| 603 | + * @param int $seconds Number of seconds |
|
| 604 | + * @param bool $countdown Show a countdown or not |
|
| 605 | + * |
|
| 606 | + * @return string |
|
| 607 | + */ |
|
| 608 | + public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 609 | + { |
|
| 610 | + if ($countdown === true) { |
|
| 611 | + $time = $seconds; |
|
| 612 | + |
|
| 613 | + while ($time > 0) { |
|
| 614 | + fwrite(static::$stdout, $time.'... '); |
|
| 615 | + sleep(1); |
|
| 616 | + $time--; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + static::write(); |
|
| 620 | + } else { |
|
| 621 | + if ($seconds = 0) { |
|
| 622 | + sleep($seconds); |
|
| 623 | + } else { |
|
| 624 | + static::write(static::$waitMsg); |
|
| 625 | + static::input(); |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Outputs a string to the cli. If you send an array it will implode them |
|
| 632 | + * with a line break. |
|
| 633 | + * |
|
| 634 | + * @param string|array $text The text to output, or array of lines |
|
| 635 | + * @param string|null $foreground The foreground color |
|
| 636 | + * @param string|null $background The background color |
|
| 637 | + * |
|
| 638 | + * @return string |
|
| 639 | + */ |
|
| 640 | + public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 641 | + { |
|
| 642 | + if (is_array($text)) { |
|
| 643 | + $text = implode(PHP_EOL, $text); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + if ($foreground OR $background) { |
|
| 647 | + $text = static::color($text, $foreground, $background); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + static::fwrite(static::$stdout, $text.PHP_EOL); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Returns a well formatted table. |
|
| 655 | + * |
|
| 656 | + * @param array $tbody List of rows |
|
| 657 | + * @param array $thead List of columns |
|
| 658 | + * |
|
| 659 | + * @return void |
|
| 660 | + */ |
|
| 661 | + public static function table(array $tbody, array $thead = []) |
|
| 662 | + { |
|
| 663 | + $rows = []; |
|
| 664 | + |
|
| 665 | + if ( ! empty($thead)) { |
|
| 666 | + $rows[] = array_values($thead); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + foreach ($tbody as $tr) { |
|
| 670 | + $rows[] = count($rows); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + $totalRows = count($rows); |
|
| 674 | + |
|
| 675 | + // Store all columns lengths |
|
| 676 | + $allColsLengths = []; |
|
| 677 | + |
|
| 678 | + // Store maximum lengths by column |
|
| 679 | + $maxColsLengths = []; |
|
| 680 | + |
|
| 681 | + for ($row = 0; $row < $totalRows; $row++) { |
|
| 682 | + $column = 0; |
|
| 683 | + |
|
| 684 | + foreach ($rows[$row] as $col) { |
|
| 685 | + $allColsLengths[$row][$column] = static::strlen($col); |
|
| 686 | + |
|
| 687 | + if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
| 688 | + $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + $column++; |
|
| 692 | + } |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + for ($row = 0; $row < $totalRows; $row++) { |
|
| 696 | + $column = 0; |
|
| 697 | + |
|
| 698 | + foreach ($rows[$row] as $col) |
|
| 699 | + { |
|
| 700 | + $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 701 | 701 | |
| 702 | - if ($diverse) { |
|
| 703 | - $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - $column++; |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - $table = ''; |
|
| 711 | - $cols = ''; |
|
| 712 | - |
|
| 713 | - for ($row = 0; $row < $rows; $row++) { |
|
| 714 | - if (0 === $row) { |
|
| 715 | - $cols = '+'; |
|
| 716 | - |
|
| 717 | - foreach ($rows[$row] as $col) { |
|
| 718 | - $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - $table .= $cols.PHP_EOL; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 725 | - |
|
| 726 | - if (0 === $row && ( ! empty($thead)) || ($row + 1) === $rows) { |
|
| 727 | - $table .= $cols.PHP_EOL; |
|
| 728 | - } |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - static::write($table); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * The library is intended for used on Cli commanda, |
|
| 736 | - * this commands can be called from controllers and |
|
| 737 | - * elsewhere of framework. |
|
| 738 | - * |
|
| 739 | - * @param resource $handle |
|
| 740 | - * @param string $text |
|
| 741 | - * |
|
| 742 | - * @return string |
|
| 743 | - */ |
|
| 744 | - protected static function fwrite($handle, string $text) |
|
| 745 | - { |
|
| 746 | - if (isCli()) { |
|
| 747 | - fwrite($handle, $text); |
|
| 748 | - return; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - echo $text; |
|
| 752 | - } |
|
| 702 | + if ($diverse) { |
|
| 703 | + $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + $column++; |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + $table = ''; |
|
| 711 | + $cols = ''; |
|
| 712 | + |
|
| 713 | + for ($row = 0; $row < $rows; $row++) { |
|
| 714 | + if (0 === $row) { |
|
| 715 | + $cols = '+'; |
|
| 716 | + |
|
| 717 | + foreach ($rows[$row] as $col) { |
|
| 718 | + $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + $table .= $cols.PHP_EOL; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 725 | + |
|
| 726 | + if (0 === $row && ( ! empty($thead)) || ($row + 1) === $rows) { |
|
| 727 | + $table .= $cols.PHP_EOL; |
|
| 728 | + } |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + static::write($table); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * The library is intended for used on Cli commanda, |
|
| 736 | + * this commands can be called from controllers and |
|
| 737 | + * elsewhere of framework. |
|
| 738 | + * |
|
| 739 | + * @param resource $handle |
|
| 740 | + * @param string $text |
|
| 741 | + * |
|
| 742 | + * @return string |
|
| 743 | + */ |
|
| 744 | + protected static function fwrite($handle, string $text) |
|
| 745 | + { |
|
| 746 | + if (isCli()) { |
|
| 747 | + fwrite($handle, $text); |
|
| 748 | + return; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + echo $text; |
|
| 752 | + } |
|
| 753 | 753 | } |
| 754 | 754 | \ No newline at end of file |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 201 | + if ($background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | 202 | throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
| 203 | 203 | } |
| 204 | 204 | |
@@ -330,9 +330,9 @@ discard block |
||
| 330 | 330 | |
| 331 | 331 | $first = true; |
| 332 | 332 | |
| 333 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 333 | + array_walk($lines, function(&$line, $index) use ($pad_left, &$first) { |
|
| 334 | 334 | if ( ! $first) { |
| 335 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 335 | + $line = str_repeat(' ', $pad_left).$line; |
|
| 336 | 336 | } else { |
| 337 | 337 | $first = false; |
| 338 | 338 | } |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | $table = ''; |
| 711 | - $cols = ''; |
|
| 711 | + $cols = ''; |
|
| 712 | 712 | |
| 713 | 713 | for ($row = 0; $row < $rows; $row++) { |
| 714 | 714 | if (0 === $row) { |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | */ |
| 54 | 54 | public function register() |
| 55 | 55 | { |
| 56 | - $this->booting(function () { |
|
| 56 | + $this->booting(function() { |
|
| 57 | 57 | $events = $this->listens(); |
| 58 | 58 | |
| 59 | 59 | foreach ((array) $events as $event => $listeners) { |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | return $this->createClassListener($listener, $wildcard); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - return function ($event, $payload) use ($listener, $wildcard) { |
|
| 136 | + return function($event, $payload) use ($listener, $wildcard) { |
|
| 137 | 137 | if ($wildcard) { |
| 138 | 138 | return $listener($event, $payload); |
| 139 | 139 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public function createClassListener($listener, $wildcard = false) |
| 154 | 154 | { |
| 155 | - return function ($event, $payload) use ($listener, $wildcard) { |
|
| 155 | + return function($event, $payload) use ($listener, $wildcard) { |
|
| 156 | 156 | if ($wildcard) { |
| 157 | 157 | return call_user_func($this->createClassClosure($listener), $event, $payload); |
| 158 | 158 | } |
@@ -34,156 +34,156 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Parameters implements IteratorAggregate, Countable |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * Array parameters from the Server global. |
|
| 39 | - * |
|
| 40 | - * @var array $parameters |
|
| 41 | - */ |
|
| 42 | - protected $parameters = []; |
|
| 37 | + /** |
|
| 38 | + * Array parameters from the Server global. |
|
| 39 | + * |
|
| 40 | + * @var array $parameters |
|
| 41 | + */ |
|
| 42 | + protected $parameters = []; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Parameter Object Constructor. |
|
| 46 | - * |
|
| 47 | - * @param array $parameters |
|
| 48 | - * |
|
| 49 | - * @return array |
|
| 50 | - */ |
|
| 51 | - public function __construct(array $parameters = []) |
|
| 52 | - { |
|
| 53 | - $this->parameters = $parameters; |
|
| 54 | - } |
|
| 44 | + /** |
|
| 45 | + * Parameter Object Constructor. |
|
| 46 | + * |
|
| 47 | + * @param array $parameters |
|
| 48 | + * |
|
| 49 | + * @return array |
|
| 50 | + */ |
|
| 51 | + public function __construct(array $parameters = []) |
|
| 52 | + { |
|
| 53 | + $this->parameters = $parameters; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Returns the parameters. |
|
| 58 | - * |
|
| 59 | - * @return array |
|
| 60 | - */ |
|
| 61 | - public function all() |
|
| 62 | - { |
|
| 63 | - return $this->parameters; |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * Returns the parameters. |
|
| 58 | + * |
|
| 59 | + * @return array |
|
| 60 | + */ |
|
| 61 | + public function all() |
|
| 62 | + { |
|
| 63 | + return $this->parameters; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Returns the parameter keys. |
|
| 68 | - * |
|
| 69 | - * @return array |
|
| 70 | - */ |
|
| 71 | - public function keys() |
|
| 72 | - { |
|
| 73 | - return array_keys($this->parameters); |
|
| 74 | - } |
|
| 66 | + /** |
|
| 67 | + * Returns the parameter keys. |
|
| 68 | + * |
|
| 69 | + * @return array |
|
| 70 | + */ |
|
| 71 | + public function keys() |
|
| 72 | + { |
|
| 73 | + return array_keys($this->parameters); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Replaces the current parameters. |
|
| 78 | - * |
|
| 79 | - * @param array $parameters |
|
| 80 | - * |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - public function replace(array $parameters = []) |
|
| 84 | - { |
|
| 85 | - $this->parameters = $parameters; |
|
| 86 | - } |
|
| 76 | + /** |
|
| 77 | + * Replaces the current parameters. |
|
| 78 | + * |
|
| 79 | + * @param array $parameters |
|
| 80 | + * |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + public function replace(array $parameters = []) |
|
| 84 | + { |
|
| 85 | + $this->parameters = $parameters; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Adds parameters. |
|
| 90 | - * |
|
| 91 | - * @param array $parameters |
|
| 92 | - * |
|
| 93 | - * @return array |
|
| 94 | - */ |
|
| 95 | - public function add(array $parameters = []) |
|
| 96 | - { |
|
| 97 | - $this->parameters = array_replace($this->parameters, $parameters); |
|
| 98 | - } |
|
| 88 | + /** |
|
| 89 | + * Adds parameters. |
|
| 90 | + * |
|
| 91 | + * @param array $parameters |
|
| 92 | + * |
|
| 93 | + * @return array |
|
| 94 | + */ |
|
| 95 | + public function add(array $parameters = []) |
|
| 96 | + { |
|
| 97 | + $this->parameters = array_replace($this->parameters, $parameters); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Get a parameter array item. |
|
| 102 | - * |
|
| 103 | - * @param string $key |
|
| 104 | - * @param string|null $default |
|
| 105 | - * |
|
| 106 | - * @return mixed |
|
| 107 | - */ |
|
| 108 | - public function get($key, $default = null) |
|
| 109 | - { |
|
| 110 | - if ($this->has($key)) { |
|
| 111 | - return $this->parameters[$key]; |
|
| 112 | - } |
|
| 100 | + /** |
|
| 101 | + * Get a parameter array item. |
|
| 102 | + * |
|
| 103 | + * @param string $key |
|
| 104 | + * @param string|null $default |
|
| 105 | + * |
|
| 106 | + * @return mixed |
|
| 107 | + */ |
|
| 108 | + public function get($key, $default = null) |
|
| 109 | + { |
|
| 110 | + if ($this->has($key)) { |
|
| 111 | + return $this->parameters[$key]; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - return $default; |
|
| 115 | - } |
|
| 114 | + return $default; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Check if a parameter array item exists. |
|
| 119 | - * |
|
| 120 | - * @param string $key |
|
| 121 | - * |
|
| 122 | - * @return mixed |
|
| 123 | - */ |
|
| 124 | - public function has($key) |
|
| 125 | - { |
|
| 126 | - return Arr::exists($this->parameters, $key); |
|
| 127 | - } |
|
| 117 | + /** |
|
| 118 | + * Check if a parameter array item exists. |
|
| 119 | + * |
|
| 120 | + * @param string $key |
|
| 121 | + * |
|
| 122 | + * @return mixed |
|
| 123 | + */ |
|
| 124 | + public function has($key) |
|
| 125 | + { |
|
| 126 | + return Arr::exists($this->parameters, $key); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Set a parameter array item. |
|
| 131 | - * |
|
| 132 | - * @param string $key |
|
| 133 | - * @param string $value |
|
| 134 | - * |
|
| 135 | - * @return mixed |
|
| 136 | - */ |
|
| 137 | - public function set($key, $value) |
|
| 138 | - { |
|
| 139 | - $this->parameters[$key] = $value; |
|
| 140 | - } |
|
| 129 | + /** |
|
| 130 | + * Set a parameter array item. |
|
| 131 | + * |
|
| 132 | + * @param string $key |
|
| 133 | + * @param string $value |
|
| 134 | + * |
|
| 135 | + * @return mixed |
|
| 136 | + */ |
|
| 137 | + public function set($key, $value) |
|
| 138 | + { |
|
| 139 | + $this->parameters[$key] = $value; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Remove a parameter array item. |
|
| 144 | - * |
|
| 145 | - * @param string $key |
|
| 146 | - * |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - public function remove($key) |
|
| 150 | - { |
|
| 151 | - if ($this->has($key)) { |
|
| 152 | - unset($this->parameters[$key]); |
|
| 153 | - } |
|
| 154 | - } |
|
| 142 | + /** |
|
| 143 | + * Remove a parameter array item. |
|
| 144 | + * |
|
| 145 | + * @param string $key |
|
| 146 | + * |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + public function remove($key) |
|
| 150 | + { |
|
| 151 | + if ($this->has($key)) { |
|
| 152 | + unset($this->parameters[$key]); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /* |
|
| 156 | + /* |
|
| 157 | 157 | |----------------------------------------------------------------- |
| 158 | 158 | | IteratorAggregate Method |
| 159 | 159 | |----------------------------------------------------------------- |
| 160 | 160 | */ |
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Retrieve an external iterator. |
|
| 164 | - * |
|
| 165 | - * @see \IteratorAggregate::getIterator |
|
| 166 | - * |
|
| 167 | - * @return new \ArrayIterator |
|
| 168 | - */ |
|
| 169 | - public function getIterator() |
|
| 170 | - { |
|
| 171 | - return new ArrayIterator($this->parameters); |
|
| 172 | - } |
|
| 162 | + /** |
|
| 163 | + * Retrieve an external iterator. |
|
| 164 | + * |
|
| 165 | + * @see \IteratorAggregate::getIterator |
|
| 166 | + * |
|
| 167 | + * @return new \ArrayIterator |
|
| 168 | + */ |
|
| 169 | + public function getIterator() |
|
| 170 | + { |
|
| 171 | + return new ArrayIterator($this->parameters); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /* |
|
| 174 | + /* |
|
| 175 | 175 | |----------------------------------------------------------------- |
| 176 | 176 | | Countable Method |
| 177 | 177 | |----------------------------------------------------------------- |
| 178 | 178 | */ |
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Returns the number of parameters. |
|
| 182 | - * |
|
| 183 | - * @return int The number of parameters |
|
| 184 | - */ |
|
| 185 | - public function count() |
|
| 186 | - { |
|
| 187 | - return count($this->parameters); |
|
| 188 | - } |
|
| 180 | + /** |
|
| 181 | + * Returns the number of parameters. |
|
| 182 | + * |
|
| 183 | + * @return int The number of parameters |
|
| 184 | + */ |
|
| 185 | + public function count() |
|
| 186 | + { |
|
| 187 | + return count($this->parameters); |
|
| 188 | + } |
|
| 189 | 189 | } |
| 190 | 190 | \ No newline at end of file |
@@ -33,46 +33,46 @@ discard block |
||
| 33 | 33 | final class Inputs extends Parameters |
| 34 | 34 | { |
| 35 | 35 | /** |
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - public function all(string $key = null) |
|
| 39 | - { |
|
| 40 | - return parent::all($key); |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + public function all(string $key = null) |
|
| 39 | + { |
|
| 40 | + return parent::all($key); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * {@inheritdoc} |
|
| 45 | - */ |
|
| 46 | - public function replace(array $inputs = []) |
|
| 47 | - { |
|
| 48 | - $this->parameters = []; |
|
| 44 | + * {@inheritdoc} |
|
| 45 | + */ |
|
| 46 | + public function replace(array $inputs = []) |
|
| 47 | + { |
|
| 48 | + $this->parameters = []; |
|
| 49 | 49 | $this->add($inputs); |
| 50 | - } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Adds input values. |
|
| 52 | + /** |
|
| 53 | + * Adds input values. |
|
| 54 | 54 | * |
| 55 | 55 | * @param array $inputs |
| 56 | 56 | * |
| 57 | 57 | * @return mixed |
| 58 | - */ |
|
| 59 | - public function add(array $inputs = []) |
|
| 60 | - { |
|
| 58 | + */ |
|
| 59 | + public function add(array $inputs = []) |
|
| 60 | + { |
|
| 61 | 61 | foreach ($inputs as $key => $file) { |
| 62 | 62 | $this->set($key, $file); |
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * Gets a string input value by name. |
|
| 68 | - * |
|
| 69 | - * @param string $key |
|
| 70 | - * @param string|null $default |
|
| 71 | - * |
|
| 72 | - * @return string|null |
|
| 73 | - */ |
|
| 74 | - public function get($key, $default = null) |
|
| 75 | - { |
|
| 67 | + * Gets a string input value by name. |
|
| 68 | + * |
|
| 69 | + * @param string $key |
|
| 70 | + * @param string|null $default |
|
| 71 | + * |
|
| 72 | + * @return string|null |
|
| 73 | + */ |
|
| 74 | + public function get($key, $default = null) |
|
| 75 | + { |
|
| 76 | 76 | if (null !== $default && ! is_scalar($default) && ! (is_object($default)) && ! method_exist($default, '__toString')) { |
| 77 | 77 | throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
| 78 | 78 | } |
@@ -87,19 +87,19 @@ discard block |
||
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | - * Sets an input by name. |
|
| 91 | - * |
|
| 92 | - * @param string $key |
|
| 93 | - * @param string|array|null $value |
|
| 94 | - * |
|
| 95 | - * @return mixed |
|
| 96 | - */ |
|
| 97 | - public function set($key, $value) |
|
| 98 | - { |
|
| 90 | + * Sets an input by name. |
|
| 91 | + * |
|
| 92 | + * @param string $key |
|
| 93 | + * @param string|array|null $value |
|
| 94 | + * |
|
| 95 | + * @return mixed |
|
| 96 | + */ |
|
| 97 | + public function set($key, $value) |
|
| 98 | + { |
|
| 99 | 99 | if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exist($value, '__toString')) { |
| 100 | 100 | throw new BadRequestHttpException(sprintf('Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead', get_debug_type($value), __METHOD__)); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - $this->parameters[$key] = $value; |
|
| 104 | - } |
|
| 103 | + $this->parameters[$key] = $value; |
|
| 104 | + } |
|
| 105 | 105 | } |
| 106 | 106 | \ No newline at end of file |
@@ -33,35 +33,35 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class PostTooLargeHttpException extends HttpException |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * Get the HTTP status code. |
|
| 38 | - * |
|
| 39 | - * @var int $code |
|
| 40 | - */ |
|
| 41 | - protected $code = 413; |
|
| 36 | + /** |
|
| 37 | + * Get the HTTP status code. |
|
| 38 | + * |
|
| 39 | + * @var int $code |
|
| 40 | + */ |
|
| 41 | + protected $code = 413; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Initialize constructor. |
|
| 45 | - * |
|
| 46 | - * @param string|null $message |
|
| 47 | - * @param \Throwable|null $previous |
|
| 48 | - * @param int $code |
|
| 49 | - * @param array $headers |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public function __construct( |
|
| 54 | - string $message = null, |
|
| 55 | - Throwable $previous = null, |
|
| 56 | - int $code = 0, |
|
| 57 | - array $headers = [] |
|
| 58 | - ) { |
|
| 43 | + /** |
|
| 44 | + * Initialize constructor. |
|
| 45 | + * |
|
| 46 | + * @param string|null $message |
|
| 47 | + * @param \Throwable|null $previous |
|
| 48 | + * @param int $code |
|
| 49 | + * @param array $headers |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public function __construct( |
|
| 54 | + string $message = null, |
|
| 55 | + Throwable $previous = null, |
|
| 56 | + int $code = 0, |
|
| 57 | + array $headers = [] |
|
| 58 | + ) { |
|
| 59 | 59 | parent::__construct( |
| 60 | - $this->code, |
|
| 61 | - $message, |
|
| 62 | - $previous, |
|
| 63 | - $headers, |
|
| 64 | - $code |
|
| 65 | - ); |
|
| 66 | - } |
|
| 60 | + $this->code, |
|
| 61 | + $message, |
|
| 62 | + $previous, |
|
| 63 | + $headers, |
|
| 64 | + $code |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | 67 | } |
| 68 | 68 | \ No newline at end of file |