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