@@ -43,22 +43,22 @@ |
||
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * {@inheritdoc} |
|
| 47 | - */ |
|
| 48 | - public function replace(array $files = []) |
|
| 49 | - { |
|
| 50 | - $this->parameters = []; |
|
| 46 | + * {@inheritdoc} |
|
| 47 | + */ |
|
| 48 | + public function replace(array $files = []) |
|
| 49 | + { |
|
| 50 | + $this->parameters = []; |
|
| 51 | 51 | $this->add($files); |
| 52 | - } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * {@inheritdoc} |
|
| 56 | - */ |
|
| 57 | - public function add(array $files = []) |
|
| 58 | - { |
|
| 54 | + /** |
|
| 55 | + * {@inheritdoc} |
|
| 56 | + */ |
|
| 57 | + public function add(array $files = []) |
|
| 58 | + { |
|
| 59 | 59 | foreach ($files as $key => $file) |
| 60 | 60 | { |
| 61 | 61 | $this->set($key, $file); |
| 62 | 62 | } |
| 63 | - } |
|
| 63 | + } |
|
| 64 | 64 | } |
| 65 | 65 | \ No newline at end of file |
@@ -30,96 +30,96 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class Status |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * The HTTP status code. |
|
| 35 | - * |
|
| 36 | - * @var int $status |
|
| 37 | - */ |
|
| 38 | - protected $status = 200; |
|
| 33 | + /** |
|
| 34 | + * The HTTP status code. |
|
| 35 | + * |
|
| 36 | + * @var int $status |
|
| 37 | + */ |
|
| 38 | + protected $status = 200; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * An array of status codes and messages. |
|
| 42 | - * |
|
| 43 | - * @var array $statusCode |
|
| 44 | - */ |
|
| 45 | - public $statusCodes = [ |
|
| 46 | - // 1xx: Informational |
|
| 47 | - 100 => 'Continue', |
|
| 48 | - 101 => 'Switching Protocols', |
|
| 49 | - 102 => 'Processing', |
|
| 40 | + /** |
|
| 41 | + * An array of status codes and messages. |
|
| 42 | + * |
|
| 43 | + * @var array $statusCode |
|
| 44 | + */ |
|
| 45 | + public $statusCodes = [ |
|
| 46 | + // 1xx: Informational |
|
| 47 | + 100 => 'Continue', |
|
| 48 | + 101 => 'Switching Protocols', |
|
| 49 | + 102 => 'Processing', |
|
| 50 | 50 | |
| 51 | - // 2xx: Success |
|
| 52 | - 200 => 'OK', |
|
| 53 | - 201 => 'Created', |
|
| 54 | - 202 => 'Accepted', |
|
| 55 | - 203 => 'Non-Authoritative Information', |
|
| 56 | - 204 => 'No Content', |
|
| 57 | - 205 => 'Reset Content', |
|
| 58 | - 206 => 'Partial Content', |
|
| 59 | - 207 => 'Multi-Status', |
|
| 60 | - 208 => 'Already Reported', |
|
| 61 | - 226 => 'IM Used', |
|
| 51 | + // 2xx: Success |
|
| 52 | + 200 => 'OK', |
|
| 53 | + 201 => 'Created', |
|
| 54 | + 202 => 'Accepted', |
|
| 55 | + 203 => 'Non-Authoritative Information', |
|
| 56 | + 204 => 'No Content', |
|
| 57 | + 205 => 'Reset Content', |
|
| 58 | + 206 => 'Partial Content', |
|
| 59 | + 207 => 'Multi-Status', |
|
| 60 | + 208 => 'Already Reported', |
|
| 61 | + 226 => 'IM Used', |
|
| 62 | 62 | |
| 63 | - // 3xx: Redirection |
|
| 64 | - 300 => 'Multiple Choices', |
|
| 65 | - 301 => 'Moved Permanently', |
|
| 66 | - 302 => 'Found', |
|
| 67 | - 303 => 'See Other', |
|
| 68 | - 304 => 'Not Modified', |
|
| 69 | - 305 => 'Use Proxy', |
|
| 70 | - 307 => 'Temporary Redirect', |
|
| 71 | - 308 => 'Permanent Redirect', |
|
| 63 | + // 3xx: Redirection |
|
| 64 | + 300 => 'Multiple Choices', |
|
| 65 | + 301 => 'Moved Permanently', |
|
| 66 | + 302 => 'Found', |
|
| 67 | + 303 => 'See Other', |
|
| 68 | + 304 => 'Not Modified', |
|
| 69 | + 305 => 'Use Proxy', |
|
| 70 | + 307 => 'Temporary Redirect', |
|
| 71 | + 308 => 'Permanent Redirect', |
|
| 72 | 72 | |
| 73 | - // 4xx: Client error |
|
| 74 | - 400 => 'Bad Request', |
|
| 75 | - 401 => 'Unauthorized', |
|
| 76 | - 402 => 'Payment Required', |
|
| 77 | - 403 => 'Forbidden', |
|
| 78 | - 404 => 'Not Found', |
|
| 79 | - 405 => 'Method Not Allowed', |
|
| 80 | - 406 => 'Not Acceptable', |
|
| 81 | - 407 => 'Proxy Authentication Required', |
|
| 82 | - 408 => 'Request Timeout', |
|
| 83 | - 409 => 'Conflict', |
|
| 84 | - 410 => 'Gone', |
|
| 85 | - 411 => 'Length Required', |
|
| 86 | - 412 => 'Precondition Failed', |
|
| 87 | - 413 => 'Request Entity Too Large', |
|
| 88 | - 414 => 'Request-URI Too Long', |
|
| 89 | - 415 => 'Unsupported Media Type', |
|
| 90 | - 416 => 'Requested Range Not Satisfiable', |
|
| 91 | - 417 => 'Expectation Failed', |
|
| 92 | - 418 => 'I\'m a Teapot', |
|
| 93 | - // 419 (Authentication Timeout) is a non-standard status code with unknown origin |
|
| 94 | - 421 => 'Misdirected Request', |
|
| 95 | - 422 => 'Unprocessable Entity', |
|
| 96 | - 423 => 'Locked', |
|
| 97 | - 424 => 'Failed Dependency', |
|
| 98 | - 426 => 'Upgrade Required', |
|
| 99 | - 428 => 'Precondition Required', |
|
| 100 | - 429 => 'Too Many Requests', |
|
| 101 | - 431 => 'Request Header Fields Too Large', |
|
| 102 | - 451 => 'Unavailable For Legal Reasons', |
|
| 73 | + // 4xx: Client error |
|
| 74 | + 400 => 'Bad Request', |
|
| 75 | + 401 => 'Unauthorized', |
|
| 76 | + 402 => 'Payment Required', |
|
| 77 | + 403 => 'Forbidden', |
|
| 78 | + 404 => 'Not Found', |
|
| 79 | + 405 => 'Method Not Allowed', |
|
| 80 | + 406 => 'Not Acceptable', |
|
| 81 | + 407 => 'Proxy Authentication Required', |
|
| 82 | + 408 => 'Request Timeout', |
|
| 83 | + 409 => 'Conflict', |
|
| 84 | + 410 => 'Gone', |
|
| 85 | + 411 => 'Length Required', |
|
| 86 | + 412 => 'Precondition Failed', |
|
| 87 | + 413 => 'Request Entity Too Large', |
|
| 88 | + 414 => 'Request-URI Too Long', |
|
| 89 | + 415 => 'Unsupported Media Type', |
|
| 90 | + 416 => 'Requested Range Not Satisfiable', |
|
| 91 | + 417 => 'Expectation Failed', |
|
| 92 | + 418 => 'I\'m a Teapot', |
|
| 93 | + // 419 (Authentication Timeout) is a non-standard status code with unknown origin |
|
| 94 | + 421 => 'Misdirected Request', |
|
| 95 | + 422 => 'Unprocessable Entity', |
|
| 96 | + 423 => 'Locked', |
|
| 97 | + 424 => 'Failed Dependency', |
|
| 98 | + 426 => 'Upgrade Required', |
|
| 99 | + 428 => 'Precondition Required', |
|
| 100 | + 429 => 'Too Many Requests', |
|
| 101 | + 431 => 'Request Header Fields Too Large', |
|
| 102 | + 451 => 'Unavailable For Legal Reasons', |
|
| 103 | 103 | |
| 104 | - // 5xx: Server error |
|
| 105 | - 500 => 'Internal Server Error', |
|
| 106 | - 501 => 'Not Implemented', |
|
| 107 | - 502 => 'Bad Gateway', |
|
| 108 | - 503 => 'Service Unavailable', |
|
| 109 | - 504 => 'Gateway Timeout', |
|
| 110 | - 505 => 'HTTP Version Not Supported', |
|
| 111 | - 506 => 'Variant Also Negotiates', |
|
| 112 | - 507 => 'Insufficient Storage', |
|
| 113 | - 508 => 'Loop Detected', |
|
| 114 | - 509 => 'Bandwidth Limit Exceeded', |
|
| 115 | - 510 => 'Not Extended', |
|
| 116 | - 511 => 'Network Authentication Required' |
|
| 117 | - ]; |
|
| 104 | + // 5xx: Server error |
|
| 105 | + 500 => 'Internal Server Error', |
|
| 106 | + 501 => 'Not Implemented', |
|
| 107 | + 502 => 'Bad Gateway', |
|
| 108 | + 503 => 'Service Unavailable', |
|
| 109 | + 504 => 'Gateway Timeout', |
|
| 110 | + 505 => 'HTTP Version Not Supported', |
|
| 111 | + 506 => 'Variant Also Negotiates', |
|
| 112 | + 507 => 'Insufficient Storage', |
|
| 113 | + 508 => 'Loop Detected', |
|
| 114 | + 509 => 'Bandwidth Limit Exceeded', |
|
| 115 | + 510 => 'Not Extended', |
|
| 116 | + 511 => 'Network Authentication Required' |
|
| 117 | + ]; |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Gets string of status code. |
|
| 121 | - * |
|
| 122 | - * @var string $statusText |
|
| 123 | - */ |
|
| 124 | - protected $statusText; |
|
| 119 | + /** |
|
| 120 | + * Gets string of status code. |
|
| 121 | + * |
|
| 122 | + * @var string $statusText |
|
| 123 | + */ |
|
| 124 | + protected $statusText; |
|
| 125 | 125 | } |
| 126 | 126 | \ No newline at end of file |
@@ -34,31 +34,31 @@ discard block |
||
| 34 | 34 | final class Inputs extends Parameters |
| 35 | 35 | { |
| 36 | 36 | /** |
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - public function all(string $key = null) |
|
| 40 | - { |
|
| 41 | - return parent::all($key); |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + public function all(string $key = null) |
|
| 40 | + { |
|
| 41 | + return parent::all($key); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | - * {@inheritdoc} |
|
| 46 | - */ |
|
| 47 | - public function replace(array $inputs = []) |
|
| 48 | - { |
|
| 49 | - $this->parameters = []; |
|
| 45 | + * {@inheritdoc} |
|
| 46 | + */ |
|
| 47 | + public function replace(array $inputs = []) |
|
| 48 | + { |
|
| 49 | + $this->parameters = []; |
|
| 50 | 50 | $this->add($inputs); |
| 51 | - } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Adds input values. |
|
| 53 | + /** |
|
| 54 | + * Adds input values. |
|
| 55 | 55 | * |
| 56 | 56 | * @param array $inputs |
| 57 | 57 | * |
| 58 | 58 | * @return mixed |
| 59 | - */ |
|
| 60 | - public function add(array $inputs = []) |
|
| 61 | - { |
|
| 59 | + */ |
|
| 60 | + public function add(array $inputs = []) |
|
| 61 | + { |
|
| 62 | 62 | foreach ($inputs as $key => $file) |
| 63 | 63 | { |
| 64 | 64 | $this->set($key, $file); |
@@ -66,15 +66,15 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | - * Gets a string input value by name. |
|
| 70 | - * |
|
| 71 | - * @param string $key |
|
| 72 | - * @param string|null $default (null by default) |
|
| 73 | - * |
|
| 74 | - * @return string|null |
|
| 75 | - */ |
|
| 76 | - public function get($key, $default = null) |
|
| 77 | - { |
|
| 69 | + * Gets a string input value by name. |
|
| 70 | + * |
|
| 71 | + * @param string $key |
|
| 72 | + * @param string|null $default (null by default) |
|
| 73 | + * |
|
| 74 | + * @return string|null |
|
| 75 | + */ |
|
| 76 | + public function get($key, $default = null) |
|
| 77 | + { |
|
| 78 | 78 | if (null !== $default && ! is_scalar($default) && ! (is_object($default)) && ! method_exist($default, '__toString')) |
| 79 | 79 | { |
| 80 | 80 | throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
@@ -91,20 +91,20 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | - * Sets an input by name. |
|
| 95 | - * |
|
| 96 | - * @param string $key |
|
| 97 | - * @param string|array|null $value |
|
| 98 | - * |
|
| 99 | - * @return mixed |
|
| 100 | - */ |
|
| 101 | - public function set($key, $value) |
|
| 102 | - { |
|
| 94 | + * Sets an input by name. |
|
| 95 | + * |
|
| 96 | + * @param string $key |
|
| 97 | + * @param string|array|null $value |
|
| 98 | + * |
|
| 99 | + * @return mixed |
|
| 100 | + */ |
|
| 101 | + public function set($key, $value) |
|
| 102 | + { |
|
| 103 | 103 | if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exist($value, '__toString')) |
| 104 | 104 | { |
| 105 | 105 | 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__)); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - $this->parameters[$key] = $value; |
|
| 109 | - } |
|
| 108 | + $this->parameters[$key] = $value; |
|
| 109 | + } |
|
| 110 | 110 | } |
| 111 | 111 | \ No newline at end of file |
@@ -34,255 +34,255 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Headers implements IteratorAggregate, Countable |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * An array of HTTP headers. |
|
| 39 | - * |
|
| 40 | - * @var array $herders |
|
| 41 | - */ |
|
| 42 | - protected $headers = []; |
|
| 37 | + /** |
|
| 38 | + * An array of HTTP headers. |
|
| 39 | + * |
|
| 40 | + * @var array $herders |
|
| 41 | + */ |
|
| 42 | + protected $headers = []; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Specifies the directives for the caching mechanisms in both |
|
| 46 | - * the requests and the responses. |
|
| 47 | - * |
|
| 48 | - * @var array $cacheControl |
|
| 49 | - */ |
|
| 50 | - protected $cacheControl = []; |
|
| 44 | + /** |
|
| 45 | + * Specifies the directives for the caching mechanisms in both |
|
| 46 | + * the requests and the responses. |
|
| 47 | + * |
|
| 48 | + * @var array $cacheControl |
|
| 49 | + */ |
|
| 50 | + protected $cacheControl = []; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Constructor. The Headers class instance. |
|
| 54 | - * |
|
| 55 | - * @param array $headers |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function __construct(array $headers = []) |
|
| 60 | - { |
|
| 61 | - foreach ($headers as $key => $values) |
|
| 62 | - { |
|
| 63 | - $this->set($key, $values); |
|
| 64 | - } |
|
| 65 | - } |
|
| 52 | + /** |
|
| 53 | + * Constructor. The Headers class instance. |
|
| 54 | + * |
|
| 55 | + * @param array $headers |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function __construct(array $headers = []) |
|
| 60 | + { |
|
| 61 | + foreach ($headers as $key => $values) |
|
| 62 | + { |
|
| 63 | + $this->set($key, $values); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Returns all the headers. |
|
| 69 | - * |
|
| 70 | - * @return array |
|
| 71 | - */ |
|
| 72 | - public function all() |
|
| 73 | - { |
|
| 74 | - return $this->headers; |
|
| 75 | - } |
|
| 67 | + /** |
|
| 68 | + * Returns all the headers. |
|
| 69 | + * |
|
| 70 | + * @return array |
|
| 71 | + */ |
|
| 72 | + public function all() |
|
| 73 | + { |
|
| 74 | + return $this->headers; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Returns the parameter keys. |
|
| 79 | - * |
|
| 80 | - * @return array An array of parameter keys |
|
| 81 | - */ |
|
| 82 | - public function keys() |
|
| 83 | - { |
|
| 84 | - return array_keys($this->all()); |
|
| 85 | - } |
|
| 77 | + /** |
|
| 78 | + * Returns the parameter keys. |
|
| 79 | + * |
|
| 80 | + * @return array An array of parameter keys |
|
| 81 | + */ |
|
| 82 | + public function keys() |
|
| 83 | + { |
|
| 84 | + return array_keys($this->all()); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Replaces the current HTTP headers by a new set. |
|
| 89 | - * |
|
| 90 | - * @param array $headers |
|
| 91 | - * |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function replace(array $headers = []) |
|
| 95 | - { |
|
| 96 | - $this->headers = []; |
|
| 97 | - $this->add($headers); |
|
| 98 | - } |
|
| 87 | + /** |
|
| 88 | + * Replaces the current HTTP headers by a new set. |
|
| 89 | + * |
|
| 90 | + * @param array $headers |
|
| 91 | + * |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function replace(array $headers = []) |
|
| 95 | + { |
|
| 96 | + $this->headers = []; |
|
| 97 | + $this->add($headers); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Adds multiple header to the queue. |
|
| 102 | - * |
|
| 103 | - * @param array $headers The header name |
|
| 104 | - * |
|
| 105 | - * @return mixed |
|
| 106 | - */ |
|
| 107 | - public function add(array $headers) |
|
| 108 | - { |
|
| 109 | - foreach ($headers as $key => $values) |
|
| 110 | - { |
|
| 111 | - $this->set($key, $values); |
|
| 112 | - } |
|
| 100 | + /** |
|
| 101 | + * Adds multiple header to the queue. |
|
| 102 | + * |
|
| 103 | + * @param array $headers The header name |
|
| 104 | + * |
|
| 105 | + * @return mixed |
|
| 106 | + */ |
|
| 107 | + public function add(array $headers) |
|
| 108 | + { |
|
| 109 | + foreach ($headers as $key => $values) |
|
| 110 | + { |
|
| 111 | + $this->set($key, $values); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - return $this; |
|
| 115 | - } |
|
| 114 | + return $this; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Returns the headers, with original capitalizations. |
|
| 119 | - * |
|
| 120 | - * @return array An array of headers |
|
| 121 | - */ |
|
| 122 | - public function allPreserveCase() |
|
| 123 | - { |
|
| 124 | - $headers = []; |
|
| 117 | + /** |
|
| 118 | + * Returns the headers, with original capitalizations. |
|
| 119 | + * |
|
| 120 | + * @return array An array of headers |
|
| 121 | + */ |
|
| 122 | + public function allPreserveCase() |
|
| 123 | + { |
|
| 124 | + $headers = []; |
|
| 125 | 125 | |
| 126 | - foreach ($this->all() as $name => $value) |
|
| 127 | - { |
|
| 128 | - $headers[$name] = $value; |
|
| 129 | - } |
|
| 126 | + foreach ($this->all() as $name => $value) |
|
| 127 | + { |
|
| 128 | + $headers[$name] = $value; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - return $headers; |
|
| 132 | - } |
|
| 131 | + return $headers; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Gets a header value by name. |
|
| 136 | - * |
|
| 137 | - * @param string $key The header name, or null for all headers |
|
| 138 | - * @param string|null $default The default value |
|
| 139 | - * @param bool $option Whether to return the option value or all header values (true by default) |
|
| 140 | - * |
|
| 141 | - * @return mixed |
|
| 142 | - */ |
|
| 143 | - public function get($key, $default = null, $option = true) |
|
| 144 | - { |
|
| 145 | - $key = str_replace('_', '-', strtolower($key)); |
|
| 134 | + /** |
|
| 135 | + * Gets a header value by name. |
|
| 136 | + * |
|
| 137 | + * @param string $key The header name, or null for all headers |
|
| 138 | + * @param string|null $default The default value |
|
| 139 | + * @param bool $option Whether to return the option value or all header values (true by default) |
|
| 140 | + * |
|
| 141 | + * @return mixed |
|
| 142 | + */ |
|
| 143 | + public function get($key, $default = null, $option = true) |
|
| 144 | + { |
|
| 145 | + $key = str_replace('_', '-', strtolower($key)); |
|
| 146 | 146 | |
| 147 | - $headers = $this->all(); |
|
| 147 | + $headers = $this->all(); |
|
| 148 | 148 | |
| 149 | - if ( ! array_key_exists($key, $headers)) |
|
| 150 | - { |
|
| 151 | - if (null === $default) |
|
| 152 | - { |
|
| 153 | - return $option ? null : []; |
|
| 154 | - } |
|
| 149 | + if ( ! array_key_exists($key, $headers)) |
|
| 150 | + { |
|
| 151 | + if (null === $default) |
|
| 152 | + { |
|
| 153 | + return $option ? null : []; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - return $option ? $default : [$default]; |
|
| 157 | - } |
|
| 156 | + return $option ? $default : [$default]; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - if ($option) |
|
| 160 | - { |
|
| 161 | - return count($headers[$key]) ? $headers[$key][0] : $default; |
|
| 162 | - } |
|
| 159 | + if ($option) |
|
| 160 | + { |
|
| 161 | + return count($headers[$key]) ? $headers[$key][0] : $default; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - return $headers[$key]; |
|
| 165 | - } |
|
| 164 | + return $headers[$key]; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Sets a header by name. |
|
| 169 | - * |
|
| 170 | - * @param string $key The header name |
|
| 171 | - * @param string $values The value or an array of values |
|
| 172 | - * @param bool $replace If you want to replace the value exists by the header, |
|
| 173 | - * it is not overwritten (true by default) / overwritten when it is false |
|
| 174 | - * |
|
| 175 | - * @return $this |
|
| 176 | - */ |
|
| 177 | - public function set($key, $values, $replace = true) |
|
| 178 | - { |
|
| 179 | - $key = str_replace('_', '-', strtolower($key)); |
|
| 167 | + /** |
|
| 168 | + * Sets a header by name. |
|
| 169 | + * |
|
| 170 | + * @param string $key The header name |
|
| 171 | + * @param string $values The value or an array of values |
|
| 172 | + * @param bool $replace If you want to replace the value exists by the header, |
|
| 173 | + * it is not overwritten (true by default) / overwritten when it is false |
|
| 174 | + * |
|
| 175 | + * @return $this |
|
| 176 | + */ |
|
| 177 | + public function set($key, $values, $replace = true) |
|
| 178 | + { |
|
| 179 | + $key = str_replace('_', '-', strtolower($key)); |
|
| 180 | 180 | |
| 181 | - if (is_array($values)) |
|
| 182 | - { |
|
| 183 | - $values = array_values($values); |
|
| 181 | + if (is_array($values)) |
|
| 182 | + { |
|
| 183 | + $values = array_values($values); |
|
| 184 | 184 | |
| 185 | - if (true === $replace || ! isset($this->headers[$key])) |
|
| 186 | - { |
|
| 187 | - $this->headers[$key] = $values; |
|
| 188 | - } |
|
| 189 | - else |
|
| 190 | - { |
|
| 191 | - $this->headers[$key] = array_merge($this->headers[$key], $values); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - else |
|
| 195 | - { |
|
| 196 | - if (true === $replace || ! isset($this->headers[$key])) |
|
| 197 | - { |
|
| 198 | - $this->headers[$key] = [$values]; |
|
| 199 | - } |
|
| 200 | - else |
|
| 201 | - { |
|
| 202 | - $this->headers[$key][] = $values; |
|
| 203 | - } |
|
| 204 | - } |
|
| 185 | + if (true === $replace || ! isset($this->headers[$key])) |
|
| 186 | + { |
|
| 187 | + $this->headers[$key] = $values; |
|
| 188 | + } |
|
| 189 | + else |
|
| 190 | + { |
|
| 191 | + $this->headers[$key] = array_merge($this->headers[$key], $values); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + else |
|
| 195 | + { |
|
| 196 | + if (true === $replace || ! isset($this->headers[$key])) |
|
| 197 | + { |
|
| 198 | + $this->headers[$key] = [$values]; |
|
| 199 | + } |
|
| 200 | + else |
|
| 201 | + { |
|
| 202 | + $this->headers[$key][] = $values; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - return $this; |
|
| 207 | - } |
|
| 206 | + return $this; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Returns true if the HTTP header is defined. |
|
| 211 | - * |
|
| 212 | - * @param string $key The HTTP header |
|
| 213 | - * |
|
| 214 | - * @return bool true if the parameter exists, false otherwise |
|
| 215 | - */ |
|
| 216 | - public function has($key) |
|
| 217 | - { |
|
| 218 | - return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all()); |
|
| 219 | - } |
|
| 209 | + /** |
|
| 210 | + * Returns true if the HTTP header is defined. |
|
| 211 | + * |
|
| 212 | + * @param string $key The HTTP header |
|
| 213 | + * |
|
| 214 | + * @return bool true if the parameter exists, false otherwise |
|
| 215 | + */ |
|
| 216 | + public function has($key) |
|
| 217 | + { |
|
| 218 | + return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all()); |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Removes a header. |
|
| 223 | - * |
|
| 224 | - * @param string $name The header name |
|
| 225 | - * |
|
| 226 | - * @return mixed |
|
| 227 | - */ |
|
| 228 | - public function remove($key) |
|
| 229 | - { |
|
| 230 | - $key = str_replace('_', '-', strtolower($key)); |
|
| 221 | + /** |
|
| 222 | + * Removes a header. |
|
| 223 | + * |
|
| 224 | + * @param string $name The header name |
|
| 225 | + * |
|
| 226 | + * @return mixed |
|
| 227 | + */ |
|
| 228 | + public function remove($key) |
|
| 229 | + { |
|
| 230 | + $key = str_replace('_', '-', strtolower($key)); |
|
| 231 | 231 | |
| 232 | - unset($this->headers[$key]); |
|
| 232 | + unset($this->headers[$key]); |
|
| 233 | 233 | |
| 234 | - if ('cache-control' === $key) |
|
| 235 | - { |
|
| 236 | - $this->cacheControl = []; |
|
| 237 | - } |
|
| 238 | - } |
|
| 234 | + if ('cache-control' === $key) |
|
| 235 | + { |
|
| 236 | + $this->cacheControl = []; |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Returns an iterator for headers. |
|
| 242 | - * |
|
| 243 | - * @return \ArrayIterator An \ArrayIterator instance |
|
| 244 | - */ |
|
| 245 | - public function getIterator() |
|
| 246 | - { |
|
| 247 | - return new ArrayIterator($this->headers); |
|
| 248 | - } |
|
| 240 | + /** |
|
| 241 | + * Returns an iterator for headers. |
|
| 242 | + * |
|
| 243 | + * @return \ArrayIterator An \ArrayIterator instance |
|
| 244 | + */ |
|
| 245 | + public function getIterator() |
|
| 246 | + { |
|
| 247 | + return new ArrayIterator($this->headers); |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Returns the number of headers. |
|
| 252 | - * |
|
| 253 | - * @return int The number of headers |
|
| 254 | - */ |
|
| 255 | - public function count() |
|
| 256 | - { |
|
| 257 | - return count($this->headers); |
|
| 258 | - } |
|
| 250 | + /** |
|
| 251 | + * Returns the number of headers. |
|
| 252 | + * |
|
| 253 | + * @return int The number of headers |
|
| 254 | + */ |
|
| 255 | + public function count() |
|
| 256 | + { |
|
| 257 | + return count($this->headers); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Returns the headers as a string. |
|
| 262 | - * |
|
| 263 | - * @return string The headers |
|
| 264 | - */ |
|
| 265 | - public function __toString() |
|
| 266 | - { |
|
| 267 | - if ( ! $headers = $this->all()) |
|
| 268 | - { |
|
| 269 | - return ''; |
|
| 270 | - } |
|
| 260 | + /** |
|
| 261 | + * Returns the headers as a string. |
|
| 262 | + * |
|
| 263 | + * @return string The headers |
|
| 264 | + */ |
|
| 265 | + public function __toString() |
|
| 266 | + { |
|
| 267 | + if ( ! $headers = $this->all()) |
|
| 268 | + { |
|
| 269 | + return ''; |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - ksort($headers); |
|
| 273 | - $max = max(array_map('strlen', array_keys($headers))) + 1; |
|
| 274 | - $content = ''; |
|
| 272 | + ksort($headers); |
|
| 273 | + $max = max(array_map('strlen', array_keys($headers))) + 1; |
|
| 274 | + $content = ''; |
|
| 275 | 275 | |
| 276 | - foreach ($headers as $name => $values) |
|
| 277 | - { |
|
| 278 | - $name = ucwords($name, '-'); |
|
| 276 | + foreach ($headers as $name => $values) |
|
| 277 | + { |
|
| 278 | + $name = ucwords($name, '-'); |
|
| 279 | 279 | |
| 280 | - foreach ($values as $value) |
|
| 281 | - { |
|
| 282 | - $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); |
|
| 283 | - } |
|
| 284 | - } |
|
| 280 | + foreach ($values as $value) |
|
| 281 | + { |
|
| 282 | + $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - return $content; |
|
| 287 | - } |
|
| 286 | + return $content; |
|
| 287 | + } |
|
| 288 | 288 | } |
| 289 | 289 | \ No newline at end of file |
@@ -140,7 +140,7 @@ |
||
| 140 | 140 | * |
| 141 | 141 | * @return mixed |
| 142 | 142 | */ |
| 143 | - public function get($key, $default = null, $option = true) |
|
| 143 | + public function get($key, $default = null, $option = true) |
|
| 144 | 144 | { |
| 145 | 145 | $key = str_replace('_', '-', strtolower($key)); |
| 146 | 146 | |
@@ -185,19 +185,16 @@ |
||
| 185 | 185 | if (true === $replace || ! isset($this->headers[$key])) |
| 186 | 186 | { |
| 187 | 187 | $this->headers[$key] = $values; |
| 188 | - } |
|
| 189 | - else |
|
| 188 | + } else |
|
| 190 | 189 | { |
| 191 | 190 | $this->headers[$key] = array_merge($this->headers[$key], $values); |
| 192 | 191 | } |
| 193 | - } |
|
| 194 | - else |
|
| 192 | + } else |
|
| 195 | 193 | { |
| 196 | 194 | if (true === $replace || ! isset($this->headers[$key])) |
| 197 | 195 | { |
| 198 | 196 | $this->headers[$key] = [$values]; |
| 199 | - } |
|
| 200 | - else |
|
| 197 | + } else |
|
| 201 | 198 | { |
| 202 | 199 | $this->headers[$key][] = $values; |
| 203 | 200 | } |
@@ -34,158 +34,158 @@ |
||
| 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 (null by default) |
|
| 105 | - * |
|
| 106 | - * @return mixed |
|
| 107 | - */ |
|
| 108 | - public function get($key, $default = null) |
|
| 109 | - { |
|
| 110 | - if (Arr::exists($this->parameters, $key)) |
|
| 111 | - { |
|
| 112 | - return $this->parameters[$key]; |
|
| 113 | - } |
|
| 100 | + /** |
|
| 101 | + * Get a parameter array item. |
|
| 102 | + * |
|
| 103 | + * @param string $key |
|
| 104 | + * @param string|null $default (null by default) |
|
| 105 | + * |
|
| 106 | + * @return mixed |
|
| 107 | + */ |
|
| 108 | + public function get($key, $default = null) |
|
| 109 | + { |
|
| 110 | + if (Arr::exists($this->parameters, $key)) |
|
| 111 | + { |
|
| 112 | + return $this->parameters[$key]; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - return $default; |
|
| 116 | - } |
|
| 115 | + return $default; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * Check if a parameter array item exists. |
|
| 120 | - * |
|
| 121 | - * @param string $key |
|
| 122 | - * |
|
| 123 | - * @return mixed |
|
| 124 | - */ |
|
| 125 | - public function has($key) |
|
| 126 | - { |
|
| 127 | - return Arr::exists($this->parameters, $key); |
|
| 128 | - } |
|
| 118 | + /** |
|
| 119 | + * Check if a parameter array item exists. |
|
| 120 | + * |
|
| 121 | + * @param string $key |
|
| 122 | + * |
|
| 123 | + * @return mixed |
|
| 124 | + */ |
|
| 125 | + public function has($key) |
|
| 126 | + { |
|
| 127 | + return Arr::exists($this->parameters, $key); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Set a parameter array item. |
|
| 132 | - * |
|
| 133 | - * @param string $key |
|
| 134 | - * @param string $value |
|
| 135 | - * |
|
| 136 | - * @return mixed |
|
| 137 | - */ |
|
| 138 | - public function set($key, $value) |
|
| 139 | - { |
|
| 140 | - $this->parameters[$key] = $value; |
|
| 141 | - } |
|
| 130 | + /** |
|
| 131 | + * Set a parameter array item. |
|
| 132 | + * |
|
| 133 | + * @param string $key |
|
| 134 | + * @param string $value |
|
| 135 | + * |
|
| 136 | + * @return mixed |
|
| 137 | + */ |
|
| 138 | + public function set($key, $value) |
|
| 139 | + { |
|
| 140 | + $this->parameters[$key] = $value; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Remove a parameter array item. |
|
| 145 | - * |
|
| 146 | - * @param string $key |
|
| 147 | - * |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - public function remove($key) |
|
| 151 | - { |
|
| 152 | - if ($this->has($key)) |
|
| 153 | - { |
|
| 154 | - unset($this->parameters[$key]); |
|
| 155 | - } |
|
| 156 | - } |
|
| 143 | + /** |
|
| 144 | + * Remove a parameter array item. |
|
| 145 | + * |
|
| 146 | + * @param string $key |
|
| 147 | + * |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + public function remove($key) |
|
| 151 | + { |
|
| 152 | + if ($this->has($key)) |
|
| 153 | + { |
|
| 154 | + unset($this->parameters[$key]); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - /* |
|
| 158 | + /* |
|
| 159 | 159 | |----------------------------------------------------------------- |
| 160 | 160 | | IteratorAggregate Method |
| 161 | 161 | |----------------------------------------------------------------- |
| 162 | 162 | */ |
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Retrieve an external iterator. |
|
| 166 | - * |
|
| 167 | - * @see \IteratorAggregate::getIterator |
|
| 168 | - * |
|
| 169 | - * @return new \ArrayIterator |
|
| 170 | - */ |
|
| 171 | - public function getIterator() |
|
| 172 | - { |
|
| 173 | - return new ArrayIterator($this->parameters); |
|
| 174 | - } |
|
| 164 | + /** |
|
| 165 | + * Retrieve an external iterator. |
|
| 166 | + * |
|
| 167 | + * @see \IteratorAggregate::getIterator |
|
| 168 | + * |
|
| 169 | + * @return new \ArrayIterator |
|
| 170 | + */ |
|
| 171 | + public function getIterator() |
|
| 172 | + { |
|
| 173 | + return new ArrayIterator($this->parameters); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /* |
|
| 176 | + /* |
|
| 177 | 177 | |----------------------------------------------------------------- |
| 178 | 178 | | Countable Method |
| 179 | 179 | |----------------------------------------------------------------- |
| 180 | 180 | */ |
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Returns the number of parameters. |
|
| 184 | - * |
|
| 185 | - * @return int The number of parameters |
|
| 186 | - */ |
|
| 187 | - public function count() |
|
| 188 | - { |
|
| 189 | - return count($this->parameters); |
|
| 190 | - } |
|
| 182 | + /** |
|
| 183 | + * Returns the number of parameters. |
|
| 184 | + * |
|
| 185 | + * @return int The number of parameters |
|
| 186 | + */ |
|
| 187 | + public function count() |
|
| 188 | + { |
|
| 189 | + return count($this->parameters); |
|
| 190 | + } |
|
| 191 | 191 | } |
| 192 | 192 | \ No newline at end of file |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | */ |
| 90 | 90 | protected function getFilePath(string $path, string $file) |
| 91 | 91 | { |
| 92 | - return rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; |
|
| 92 | + return rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // If there is an equal sign, then we know we |
| 141 | - // are assigning a variable. |
|
| 141 | + // are assigning a variable. |
|
| 142 | 142 | if ($this->checkedLikeSetter($line)) { |
| 143 | 143 | $this->setVariable($line); |
| 144 | 144 | } |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | $quote = $value[0]; |
| 245 | 245 | |
| 246 | 246 | $regexPattern = sprintf( |
| 247 | - '/^ |
|
| 247 | + '/^ |
|
| 248 | 248 | %1$s # match a quote at the start of the value |
| 249 | 249 | ( # capturing sub-pattern used |
| 250 | 250 | (?: # we do not need to capture this |
@@ -290,7 +290,7 @@ |
||
| 290 | 290 | { |
| 291 | 291 | if (strpos($value, '$') !== false) { |
| 292 | 292 | $loader = $this; |
| 293 | - $value = preg_replace_callback(self::ALFANUMERIC_REGEX, function ($matchedPatterns) use ($loader) { |
|
| 293 | + $value = preg_replace_callback(self::ALFANUMERIC_REGEX, function($matchedPatterns) use ($loader) { |
|
| 294 | 294 | |
| 295 | 295 | $nestedVariable = $loader->getVariable($matchedPatterns[1]); |
| 296 | 296 | |
@@ -62,11 +62,11 @@ |
||
| 62 | 62 | */ |
| 63 | 63 | protected $locale; |
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Boolean value whether the intl libraries exist on the system. |
|
| 67 | - * |
|
| 68 | - * @var bool $intlSupport |
|
| 69 | - */ |
|
| 65 | + /** |
|
| 66 | + * Boolean value whether the intl libraries exist on the system. |
|
| 67 | + * |
|
| 68 | + * @var bool $intlSupport |
|
| 69 | + */ |
|
| 70 | 70 | protected $intlSupport = false; |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -39,7 +39,7 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | public function register() |
| 41 | 41 | { |
| 42 | - $this->app->singleton('translator', function () { |
|
| 42 | + $this->app->singleton('translator', function() { |
|
| 43 | 43 | |
| 44 | 44 | $locale = $this->app['config']['app.locale']; |
| 45 | 45 | |
@@ -35,773 +35,773 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class Cli |
| 37 | 37 | { |
| 38 | - /** |
|
| 39 | - * Background color identifier. |
|
| 40 | - * |
|
| 41 | - * @var array $backgroundColors |
|
| 42 | - */ |
|
| 43 | - protected static $backgroundColors = [ |
|
| 44 | - 'black' => '40', |
|
| 45 | - 'red' => '41', |
|
| 46 | - 'green' => '42', |
|
| 47 | - 'yellow' => '43', |
|
| 48 | - 'blue' => '44', |
|
| 49 | - 'magenta' => '45', |
|
| 50 | - 'cyan' => '46', |
|
| 51 | - 'light_gray' => '47' |
|
| 52 | - ]; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Foreground color identifier. |
|
| 56 | - * |
|
| 57 | - * @var array $foregroundColors |
|
| 58 | - */ |
|
| 59 | - protected static $foregroundColors = [ |
|
| 60 | - 'black' => '0;30', |
|
| 61 | - 'dark_gray' => '1;30', |
|
| 62 | - 'blue' => '0;34', |
|
| 63 | - 'dark_blue' => '1;34', |
|
| 64 | - 'light_blue' => '1;34', |
|
| 65 | - 'green' => '0;32', |
|
| 66 | - 'light_green' => '1;32', |
|
| 67 | - 'cyan' => '0;36', |
|
| 68 | - 'light_cyan' => '1;36', |
|
| 69 | - 'red' => '0;31', |
|
| 70 | - 'light_red' => '1;31', |
|
| 71 | - 'purple' => '0;35', |
|
| 72 | - 'light_purple' => '1;35', |
|
| 73 | - 'light_yellow' => '0;33', |
|
| 74 | - 'yellow' => '1;33', |
|
| 75 | - 'light_gray' => '0;37', |
|
| 76 | - 'white' => '1;37' |
|
| 77 | - ]; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Indicates that you do not use any color for foreground or background. |
|
| 81 | - * |
|
| 82 | - * @var bool $noColor |
|
| 83 | - */ |
|
| 84 | - public static $noColor = false; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * String of arguments to be used in console. |
|
| 88 | - * |
|
| 89 | - * @var array $options |
|
| 90 | - */ |
|
| 91 | - protected static $options = []; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Readline Support for command line. |
|
| 95 | - * |
|
| 96 | - * @var bool $readlineSupport |
|
| 97 | - */ |
|
| 98 | - public static $readlineSupport = false; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * List of array segments. |
|
| 102 | - * |
|
| 103 | - * @var array $segments |
|
| 104 | - */ |
|
| 105 | - protected static $segments = []; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * The standar STDERR is where the application writes its error messages. |
|
| 109 | - * |
|
| 110 | - * @var string $stderr |
|
| 111 | - */ |
|
| 112 | - protected static $stderr; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * The estandar STDOUT is where the application records its output messages. |
|
| 116 | - * |
|
| 117 | - * @var string $stdout |
|
| 118 | - */ |
|
| 119 | - protected static $stdout; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Message that tells the user that he is waiting to receive an order. |
|
| 123 | - * |
|
| 124 | - * @var string $waitMsg |
|
| 125 | - */ |
|
| 126 | - public static $waitMsg = 'Press any key to continue...'; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Static constructor. Parses all the CLI params. |
|
| 130 | - * |
|
| 131 | - * @return string |
|
| 132 | - * |
|
| 133 | - * @uses \Syscodes\Contracts\Core\Lenevor |
|
| 134 | - * |
|
| 135 | - * @throws \Exception |
|
| 136 | - */ |
|
| 137 | - public static function initialize(Lenevor $core) |
|
| 138 | - { |
|
| 139 | - if ( ! $core->initCli()) |
|
| 140 | - { |
|
| 141 | - throw new Exception('Cli class cannot be used outside of the command line'); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Readline is an extension for PHP that makes interactive the command console |
|
| 145 | - static::$readlineSupport = extension_loaded('readline'); |
|
| 146 | - |
|
| 147 | - // clear segments & options to keep testing clean |
|
| 148 | - static::$options = []; |
|
| 149 | - static::$segments = []; |
|
| 150 | - |
|
| 151 | - static::parseCommandLine(); |
|
| 152 | - |
|
| 153 | - // Writes its error messages |
|
| 154 | - static::$stderr = STDERR; |
|
| 155 | - |
|
| 156 | - // Records its output messages |
|
| 157 | - static::$stdout = STDOUT; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Beeps a certain number of times. |
|
| 162 | - * |
|
| 163 | - * @param int $num The number of times to beep |
|
| 164 | - * |
|
| 165 | - * @return int |
|
| 166 | - */ |
|
| 167 | - public static function bell(int $num = 1) |
|
| 168 | - { |
|
| 169 | - echo str_repeat("\x07", $num); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Clears the screen of output. |
|
| 174 | - * |
|
| 175 | - * @return void |
|
| 176 | - */ |
|
| 177 | - public static function clearScreen() |
|
| 178 | - { |
|
| 179 | - static::isWindows() |
|
| 180 | - |
|
| 181 | - // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 182 | - ? static::newLine(40) |
|
| 183 | - |
|
| 184 | - // Anything with a flair of Unix will handle these magic characters |
|
| 185 | - : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Returns the given text with the correct color codes for a foreground and |
|
| 190 | - * optionally a background color. |
|
| 191 | - * |
|
| 192 | - * @param string $text The text to color |
|
| 193 | - * @param string $foreground The foreground color |
|
| 194 | - * @param string $background The background color |
|
| 195 | - * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 196 | - * |
|
| 197 | - * @return string The color coded string |
|
| 198 | - * |
|
| 199 | - * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 200 | - */ |
|
| 201 | - public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 202 | - { |
|
| 203 | - if (static::$noColor) |
|
| 204 | - { |
|
| 205 | - return $text; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if ( ! Arr::exists(static::$foregroundColors, $foreground)) |
|
| 209 | - { |
|
| 210 | - throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) |
|
| 214 | - { |
|
| 215 | - throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 219 | - |
|
| 220 | - if ($background !== null) |
|
| 221 | - { |
|
| 222 | - $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if ($format === 'underline') |
|
| 226 | - { |
|
| 227 | - $string .= "\033[4m"; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $string .= $text."\033[0m"; |
|
| 231 | - |
|
| 232 | - return $string; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Get the number of characters in a string. |
|
| 237 | - * |
|
| 238 | - * @param string $string |
|
| 239 | - * |
|
| 240 | - * @return int |
|
| 241 | - */ |
|
| 242 | - public static function strlen(?string $string) |
|
| 243 | - { |
|
| 244 | - if (is_null($string)) |
|
| 245 | - { |
|
| 246 | - return 0; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - foreach (static::$foregroundColors as $color) |
|
| 250 | - { |
|
| 251 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - foreach (static::$backgroundColors as $color) |
|
| 255 | - { |
|
| 256 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 260 | - |
|
| 261 | - return mb_strlen($string); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 266 | - * |
|
| 267 | - * @param string|array $text The text to output, or array of errors |
|
| 268 | - * @param string $foreground The foreground color |
|
| 269 | - * @param string|null $background the background color |
|
| 270 | - * |
|
| 271 | - * @return string |
|
| 272 | - */ |
|
| 273 | - public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 274 | - { |
|
| 275 | - if (is_array($text)) |
|
| 276 | - { |
|
| 277 | - $text = implode(PHP_EOL, $text); |
|
| 278 | - } |
|
| 38 | + /** |
|
| 39 | + * Background color identifier. |
|
| 40 | + * |
|
| 41 | + * @var array $backgroundColors |
|
| 42 | + */ |
|
| 43 | + protected static $backgroundColors = [ |
|
| 44 | + 'black' => '40', |
|
| 45 | + 'red' => '41', |
|
| 46 | + 'green' => '42', |
|
| 47 | + 'yellow' => '43', |
|
| 48 | + 'blue' => '44', |
|
| 49 | + 'magenta' => '45', |
|
| 50 | + 'cyan' => '46', |
|
| 51 | + 'light_gray' => '47' |
|
| 52 | + ]; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Foreground color identifier. |
|
| 56 | + * |
|
| 57 | + * @var array $foregroundColors |
|
| 58 | + */ |
|
| 59 | + protected static $foregroundColors = [ |
|
| 60 | + 'black' => '0;30', |
|
| 61 | + 'dark_gray' => '1;30', |
|
| 62 | + 'blue' => '0;34', |
|
| 63 | + 'dark_blue' => '1;34', |
|
| 64 | + 'light_blue' => '1;34', |
|
| 65 | + 'green' => '0;32', |
|
| 66 | + 'light_green' => '1;32', |
|
| 67 | + 'cyan' => '0;36', |
|
| 68 | + 'light_cyan' => '1;36', |
|
| 69 | + 'red' => '0;31', |
|
| 70 | + 'light_red' => '1;31', |
|
| 71 | + 'purple' => '0;35', |
|
| 72 | + 'light_purple' => '1;35', |
|
| 73 | + 'light_yellow' => '0;33', |
|
| 74 | + 'yellow' => '1;33', |
|
| 75 | + 'light_gray' => '0;37', |
|
| 76 | + 'white' => '1;37' |
|
| 77 | + ]; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Indicates that you do not use any color for foreground or background. |
|
| 81 | + * |
|
| 82 | + * @var bool $noColor |
|
| 83 | + */ |
|
| 84 | + public static $noColor = false; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * String of arguments to be used in console. |
|
| 88 | + * |
|
| 89 | + * @var array $options |
|
| 90 | + */ |
|
| 91 | + protected static $options = []; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Readline Support for command line. |
|
| 95 | + * |
|
| 96 | + * @var bool $readlineSupport |
|
| 97 | + */ |
|
| 98 | + public static $readlineSupport = false; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * List of array segments. |
|
| 102 | + * |
|
| 103 | + * @var array $segments |
|
| 104 | + */ |
|
| 105 | + protected static $segments = []; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * The standar STDERR is where the application writes its error messages. |
|
| 109 | + * |
|
| 110 | + * @var string $stderr |
|
| 111 | + */ |
|
| 112 | + protected static $stderr; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * The estandar STDOUT is where the application records its output messages. |
|
| 116 | + * |
|
| 117 | + * @var string $stdout |
|
| 118 | + */ |
|
| 119 | + protected static $stdout; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Message that tells the user that he is waiting to receive an order. |
|
| 123 | + * |
|
| 124 | + * @var string $waitMsg |
|
| 125 | + */ |
|
| 126 | + public static $waitMsg = 'Press any key to continue...'; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Static constructor. Parses all the CLI params. |
|
| 130 | + * |
|
| 131 | + * @return string |
|
| 132 | + * |
|
| 133 | + * @uses \Syscodes\Contracts\Core\Lenevor |
|
| 134 | + * |
|
| 135 | + * @throws \Exception |
|
| 136 | + */ |
|
| 137 | + public static function initialize(Lenevor $core) |
|
| 138 | + { |
|
| 139 | + if ( ! $core->initCli()) |
|
| 140 | + { |
|
| 141 | + throw new Exception('Cli class cannot be used outside of the command line'); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Readline is an extension for PHP that makes interactive the command console |
|
| 145 | + static::$readlineSupport = extension_loaded('readline'); |
|
| 146 | + |
|
| 147 | + // clear segments & options to keep testing clean |
|
| 148 | + static::$options = []; |
|
| 149 | + static::$segments = []; |
|
| 150 | + |
|
| 151 | + static::parseCommandLine(); |
|
| 152 | + |
|
| 153 | + // Writes its error messages |
|
| 154 | + static::$stderr = STDERR; |
|
| 155 | + |
|
| 156 | + // Records its output messages |
|
| 157 | + static::$stdout = STDOUT; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Beeps a certain number of times. |
|
| 162 | + * |
|
| 163 | + * @param int $num The number of times to beep |
|
| 164 | + * |
|
| 165 | + * @return int |
|
| 166 | + */ |
|
| 167 | + public static function bell(int $num = 1) |
|
| 168 | + { |
|
| 169 | + echo str_repeat("\x07", $num); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Clears the screen of output. |
|
| 174 | + * |
|
| 175 | + * @return void |
|
| 176 | + */ |
|
| 177 | + public static function clearScreen() |
|
| 178 | + { |
|
| 179 | + static::isWindows() |
|
| 180 | + |
|
| 181 | + // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 182 | + ? static::newLine(40) |
|
| 183 | + |
|
| 184 | + // Anything with a flair of Unix will handle these magic characters |
|
| 185 | + : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Returns the given text with the correct color codes for a foreground and |
|
| 190 | + * optionally a background color. |
|
| 191 | + * |
|
| 192 | + * @param string $text The text to color |
|
| 193 | + * @param string $foreground The foreground color |
|
| 194 | + * @param string $background The background color |
|
| 195 | + * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 196 | + * |
|
| 197 | + * @return string The color coded string |
|
| 198 | + * |
|
| 199 | + * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 200 | + */ |
|
| 201 | + public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 202 | + { |
|
| 203 | + if (static::$noColor) |
|
| 204 | + { |
|
| 205 | + return $text; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if ( ! Arr::exists(static::$foregroundColors, $foreground)) |
|
| 209 | + { |
|
| 210 | + throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) |
|
| 214 | + { |
|
| 215 | + throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 219 | + |
|
| 220 | + if ($background !== null) |
|
| 221 | + { |
|
| 222 | + $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if ($format === 'underline') |
|
| 226 | + { |
|
| 227 | + $string .= "\033[4m"; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $string .= $text."\033[0m"; |
|
| 231 | + |
|
| 232 | + return $string; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Get the number of characters in a string. |
|
| 237 | + * |
|
| 238 | + * @param string $string |
|
| 239 | + * |
|
| 240 | + * @return int |
|
| 241 | + */ |
|
| 242 | + public static function strlen(?string $string) |
|
| 243 | + { |
|
| 244 | + if (is_null($string)) |
|
| 245 | + { |
|
| 246 | + return 0; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + foreach (static::$foregroundColors as $color) |
|
| 250 | + { |
|
| 251 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + foreach (static::$backgroundColors as $color) |
|
| 255 | + { |
|
| 256 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 260 | + |
|
| 261 | + return mb_strlen($string); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 266 | + * |
|
| 267 | + * @param string|array $text The text to output, or array of errors |
|
| 268 | + * @param string $foreground The foreground color |
|
| 269 | + * @param string|null $background the background color |
|
| 270 | + * |
|
| 271 | + * @return string |
|
| 272 | + */ |
|
| 273 | + public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 274 | + { |
|
| 275 | + if (is_array($text)) |
|
| 276 | + { |
|
| 277 | + $text = implode(PHP_EOL, $text); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - if ($foreground || $background) |
|
| 281 | - { |
|
| 282 | - $text = static::color($text, $foreground, $background); |
|
| 283 | - } |
|
| 280 | + if ($foreground || $background) |
|
| 281 | + { |
|
| 282 | + $text = static::color($text, $foreground, $background); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - fwrite(static::$stderr, $text.PHP_EOL); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Attempts to determine the width of the viewable CLI window. |
|
| 290 | - * |
|
| 291 | - * @param int $default |
|
| 292 | - * |
|
| 293 | - * @return int |
|
| 294 | - */ |
|
| 295 | - public static function getWidth(int $default = 80) |
|
| 296 | - { |
|
| 297 | - if (static::isWindows() || (int) shell_exec('tput cols') === 0) |
|
| 298 | - { |
|
| 299 | - return $default; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - return (int) shell_exec('tput cols'); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Attempts to determine the height of the viewable CLI window. |
|
| 307 | - * |
|
| 308 | - * @param int $default |
|
| 309 | - * |
|
| 310 | - * @return int |
|
| 311 | - */ |
|
| 312 | - public static function getHeight(int $default = 32) |
|
| 313 | - { |
|
| 314 | - if (static::isWindows()) |
|
| 315 | - { |
|
| 316 | - return $default; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return (int) shell_exec('tput lines'); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 324 | - * If no maximum width is specified, will wrap to the window's max. |
|
| 325 | - * |
|
| 326 | - * @param string $string (null by default) |
|
| 327 | - * @param int $max (0 by default) |
|
| 328 | - * @param int $padLeft (0 by default) |
|
| 329 | - * |
|
| 330 | - * @return string |
|
| 331 | - */ |
|
| 332 | - public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 333 | - { |
|
| 334 | - if (empty($string)) |
|
| 335 | - { |
|
| 336 | - return ''; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - if ($max === 0) |
|
| 340 | - { |
|
| 341 | - $max = static::getWidth(); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - if (static::getWidth() < $max) |
|
| 345 | - { |
|
| 346 | - $max = static::getWidth(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - $max = $max - $padLeft; |
|
| 350 | - |
|
| 351 | - $lines = wordwrap($string, $max); |
|
| 352 | - |
|
| 353 | - if ($pad_left > 0) |
|
| 354 | - { |
|
| 355 | - $lines = explode(PHP_EOL, $lines); |
|
| 356 | - |
|
| 357 | - $first = true; |
|
| 358 | - |
|
| 359 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 360 | - |
|
| 361 | - if ( ! $first) |
|
| 362 | - { |
|
| 363 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 364 | - } |
|
| 365 | - else |
|
| 366 | - { |
|
| 367 | - $first = false; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - }); |
|
| 371 | - |
|
| 372 | - $lines = implode(PHP_EOL, $lines); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - return $lines; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Get input from the shell, using readline or the standard STDIN. |
|
| 380 | - * |
|
| 381 | - * @param string|int $prefix The name of the option (int if unnamed) |
|
| 382 | - * |
|
| 383 | - * @return string |
|
| 384 | - */ |
|
| 385 | - public static function input($prefix = '') |
|
| 386 | - { |
|
| 387 | - if (static::$readlineSupport) |
|
| 388 | - { |
|
| 389 | - return readline($prefix); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - echo $prefix; |
|
| 393 | - |
|
| 394 | - return fgets(STDIN); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * If operating system === windows. |
|
| 399 | - * |
|
| 400 | - * @return string |
|
| 401 | - */ |
|
| 402 | - public static function isWindows() |
|
| 403 | - { |
|
| 404 | - return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Enter a number of empty lines. |
|
| 409 | - * |
|
| 410 | - * @param int $num Number of lines to output |
|
| 411 | - * |
|
| 412 | - * @return void |
|
| 413 | - */ |
|
| 414 | - public static function newLine(int $num = 1) |
|
| 415 | - { |
|
| 416 | - for ($i = 0; $i < $num; $i++) |
|
| 417 | - { |
|
| 418 | - static::write(); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Returns the option with the given name. You can also give the option number. |
|
| 424 | - * |
|
| 425 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 426 | - * @param mixed $default The value to return if the option is not defined |
|
| 427 | - * |
|
| 428 | - * @return mixed |
|
| 429 | - * |
|
| 430 | - * @uses \Syscodes\Contract\Core\Lenevor |
|
| 431 | - */ |
|
| 432 | - public static function option($name, $default = null) |
|
| 433 | - { |
|
| 434 | - if ( ! isset(static::$options[$name])) |
|
| 435 | - { |
|
| 436 | - return Lenevor::value($default); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return static::$options[$name]; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Parses the command line it was called from and collects all |
|
| 444 | - * options and valid segments. |
|
| 445 | - * |
|
| 446 | - * @return bool |
|
| 447 | - */ |
|
| 448 | - protected static function parseCommandLine() |
|
| 449 | - { |
|
| 450 | - $options = false; |
|
| 451 | - |
|
| 452 | - for ($i = 1; $i < $_SERVER['argc']; $i++) |
|
| 453 | - { |
|
| 454 | - if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) |
|
| 455 | - { |
|
| 456 | - static::$segments[] = $_SERVER['argv'][$i]; |
|
| 457 | - |
|
| 458 | - continue; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - $options = true; |
|
| 462 | - |
|
| 463 | - $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 464 | - $value = null; |
|
| 465 | - |
|
| 466 | - if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) |
|
| 467 | - { |
|
| 468 | - $value = $_SERVER['argv'][$i + 1]; |
|
| 469 | - $i++; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - static::$options[$args] = $value; |
|
| 473 | - |
|
| 474 | - $options = false; |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Returns the command line string portions of the arguments, minus |
|
| 480 | - * any options, as a string. |
|
| 481 | - * |
|
| 482 | - * @return string |
|
| 483 | - */ |
|
| 484 | - public static function getURI() |
|
| 485 | - { |
|
| 486 | - return implode('/', static::$segments); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Returns an individual segment. |
|
| 491 | - * |
|
| 492 | - * @param int $index |
|
| 493 | - * |
|
| 494 | - * @return mixed|null |
|
| 495 | - */ |
|
| 496 | - public static function getSegment(int $index) |
|
| 497 | - { |
|
| 498 | - if ( ! isset(static::$segments[$index - 1])) |
|
| 499 | - { |
|
| 500 | - return null; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - return static::$segments[$index - 1]; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Returns the raw array of segments found. |
|
| 508 | - * |
|
| 509 | - * @return array |
|
| 510 | - */ |
|
| 511 | - public static function getSegments() |
|
| 512 | - { |
|
| 513 | - return static::$segments; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 518 | - * |
|
| 519 | - * Usage: |
|
| 520 | - * |
|
| 521 | - * // Waits for any key press |
|
| 522 | - * Cli::prompt(); |
|
| 523 | - * |
|
| 524 | - * // Takes any input |
|
| 525 | - * $color = Cli::prompt('What is your favorite color?'); |
|
| 526 | - * |
|
| 527 | - * // Takes any input, but offers default |
|
| 528 | - * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 529 | - * |
|
| 530 | - * // Will only accept the options in the array |
|
| 531 | - * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 532 | - * |
|
| 533 | - * @return string The user input |
|
| 534 | - */ |
|
| 535 | - public static function prompt() |
|
| 536 | - { |
|
| 537 | - $args = func_get_args(); |
|
| 538 | - |
|
| 539 | - $options = []; |
|
| 540 | - $output = ''; |
|
| 541 | - $default = null; |
|
| 542 | - |
|
| 543 | - // How many we got |
|
| 544 | - $arg_count = count($args); |
|
| 545 | - |
|
| 546 | - // Is the last argument a boolean? True means required |
|
| 547 | - $required = end($args) === true; |
|
| 548 | - |
|
| 549 | - // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 550 | - $required === true and --$arg_count; |
|
| 551 | - |
|
| 552 | - // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 553 | - switch ($arg_count) |
|
| 554 | - { |
|
| 555 | - case 2: |
|
| 556 | - |
|
| 557 | - // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 558 | - if (is_array($args[1])) |
|
| 559 | - { |
|
| 560 | - list($output, $options) = $args; |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 564 | - elseif (is_string($args[1])) |
|
| 565 | - { |
|
| 566 | - list($output, $default) = $args; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - break; |
|
| 570 | - |
|
| 571 | - case 1: |
|
| 572 | - |
|
| 573 | - // No question (probably been asked already) so just show options |
|
| 574 | - // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 575 | - if (is_array($args[0])) |
|
| 576 | - { |
|
| 577 | - $options = $args[0]; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - // Question without options |
|
| 581 | - // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 582 | - elseif (is_string($args[0])) |
|
| 583 | - { |
|
| 584 | - $output = $args[0]; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - break; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - // If a question has been asked with the read |
|
| 591 | - if ($output !== '') |
|
| 592 | - { |
|
| 593 | - $extra_output = ''; |
|
| 594 | - |
|
| 595 | - if ($default !== null) |
|
| 596 | - { |
|
| 597 | - $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 598 | - } |
|
| 599 | - elseif ($options !== []) |
|
| 600 | - { |
|
| 601 | - $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - // Read the input from keyboard. |
|
| 608 | - $input = trim(static::input()) ?: $default; |
|
| 609 | - |
|
| 610 | - // No input provided and we require one (default will stop this being called) |
|
| 611 | - if (empty($input) and $required === true) |
|
| 612 | - { |
|
| 613 | - static::write('This is required.'); |
|
| 614 | - static::newLine(); |
|
| 615 | - |
|
| 616 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - // If options are provided and the choice is not in the array, tell them to try again |
|
| 620 | - if ( ! empty($options) and ! in_array($input, $options)) |
|
| 621 | - { |
|
| 622 | - static::write('This is not a valid option. Please try again.'); |
|
| 623 | - static::newLine(); |
|
| 624 | - |
|
| 625 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - return $input; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Allows you to set a commandline option from code. |
|
| 633 | - * |
|
| 634 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 635 | - * @param mixed|null $value The value to set, or null to delete the option |
|
| 636 | - * |
|
| 637 | - * @return mixed |
|
| 638 | - */ |
|
| 639 | - public static function setOption($name, $value = null) |
|
| 640 | - { |
|
| 641 | - if ($value == null) |
|
| 642 | - { |
|
| 643 | - if (isset(static::$options[$name])) |
|
| 644 | - { |
|
| 645 | - unset(static::$options[$name]); |
|
| 646 | - } |
|
| 647 | - } |
|
| 648 | - else |
|
| 649 | - { |
|
| 650 | - static::$options[$name] = $value; |
|
| 651 | - } |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * Waits a certain number of seconds, optionally showing a wait message and |
|
| 656 | - * waiting for a key press. |
|
| 657 | - * |
|
| 658 | - * @param int $seconds Number of seconds |
|
| 659 | - * @param bool $countdown Show a countdown or not |
|
| 660 | - * |
|
| 661 | - * @return string |
|
| 662 | - */ |
|
| 663 | - public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 664 | - { |
|
| 665 | - if ($countdown === true) |
|
| 666 | - { |
|
| 667 | - $time = $seconds; |
|
| 668 | - |
|
| 669 | - while ($time > 0) |
|
| 670 | - { |
|
| 671 | - fwrite(static::$stdout, $time.'... '); |
|
| 672 | - sleep(1); |
|
| 673 | - $time--; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - static::write(); |
|
| 677 | - } |
|
| 678 | - else |
|
| 679 | - { |
|
| 680 | - if ($seconds = 0) |
|
| 681 | - { |
|
| 682 | - sleep($seconds); |
|
| 683 | - } |
|
| 684 | - else |
|
| 685 | - { |
|
| 686 | - static::write(static::$waitMsg); |
|
| 687 | - static::input(); |
|
| 688 | - } |
|
| 689 | - } |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Outputs a string to the cli. If you send an array it will implode them |
|
| 694 | - * with a line break. |
|
| 695 | - * |
|
| 696 | - * @param string|array $text The text to output, or array of lines |
|
| 697 | - * @param string|null $foreground The foreground color |
|
| 698 | - * @param string|null $background The background color |
|
| 699 | - * |
|
| 700 | - * @return string |
|
| 701 | - */ |
|
| 702 | - public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 703 | - { |
|
| 704 | - if (is_array($text)) |
|
| 705 | - { |
|
| 706 | - $text = implode(PHP_EOL, $text); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - if ($foreground OR $background) |
|
| 710 | - { |
|
| 711 | - $text = static::color($text, $foreground, $background); |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - fwrite(static::$stdout, $text.PHP_EOL); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - /** |
|
| 718 | - * Returns a well formatted table. |
|
| 719 | - * |
|
| 720 | - * @param array $tbody List of rows |
|
| 721 | - * @param array $thead List of columns |
|
| 722 | - * |
|
| 723 | - * @return void |
|
| 724 | - */ |
|
| 725 | - public static function table(array $tbody, array $thead = []) |
|
| 726 | - { |
|
| 727 | - $rows = []; |
|
| 728 | - |
|
| 729 | - if ( ! empty($thead)) |
|
| 730 | - { |
|
| 731 | - $rows[] = array_values($thead); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - foreach ($tbody as $tr) |
|
| 735 | - { |
|
| 736 | - $rows[] = count($rows); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - $totalRows = count($rows); |
|
| 740 | - |
|
| 741 | - // Store all columns lengths |
|
| 742 | - $allColsLengths = []; |
|
| 743 | - |
|
| 744 | - // Store maximum lengths by column |
|
| 745 | - $maxColsLengths = []; |
|
| 746 | - |
|
| 747 | - for ($row = 0; $row < $totalRows; $row++) |
|
| 748 | - { |
|
| 749 | - $column = 0; |
|
| 750 | - |
|
| 751 | - foreach ($rows[$row] as $col) |
|
| 752 | - { |
|
| 753 | - $allColsLengths[$row][$column] = static::strlen($col); |
|
| 754 | - |
|
| 755 | - if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) |
|
| 756 | - { |
|
| 757 | - $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - $column++; |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - for ($row = 0; $row < $totalRows; $row++) |
|
| 765 | - { |
|
| 766 | - $column = 0; |
|
| 767 | - |
|
| 768 | - foreach ($rows[$row] as $col) |
|
| 769 | - { |
|
| 770 | - $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 285 | + fwrite(static::$stderr, $text.PHP_EOL); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Attempts to determine the width of the viewable CLI window. |
|
| 290 | + * |
|
| 291 | + * @param int $default |
|
| 292 | + * |
|
| 293 | + * @return int |
|
| 294 | + */ |
|
| 295 | + public static function getWidth(int $default = 80) |
|
| 296 | + { |
|
| 297 | + if (static::isWindows() || (int) shell_exec('tput cols') === 0) |
|
| 298 | + { |
|
| 299 | + return $default; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + return (int) shell_exec('tput cols'); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Attempts to determine the height of the viewable CLI window. |
|
| 307 | + * |
|
| 308 | + * @param int $default |
|
| 309 | + * |
|
| 310 | + * @return int |
|
| 311 | + */ |
|
| 312 | + public static function getHeight(int $default = 32) |
|
| 313 | + { |
|
| 314 | + if (static::isWindows()) |
|
| 315 | + { |
|
| 316 | + return $default; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return (int) shell_exec('tput lines'); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 324 | + * If no maximum width is specified, will wrap to the window's max. |
|
| 325 | + * |
|
| 326 | + * @param string $string (null by default) |
|
| 327 | + * @param int $max (0 by default) |
|
| 328 | + * @param int $padLeft (0 by default) |
|
| 329 | + * |
|
| 330 | + * @return string |
|
| 331 | + */ |
|
| 332 | + public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 333 | + { |
|
| 334 | + if (empty($string)) |
|
| 335 | + { |
|
| 336 | + return ''; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + if ($max === 0) |
|
| 340 | + { |
|
| 341 | + $max = static::getWidth(); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + if (static::getWidth() < $max) |
|
| 345 | + { |
|
| 346 | + $max = static::getWidth(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + $max = $max - $padLeft; |
|
| 350 | + |
|
| 351 | + $lines = wordwrap($string, $max); |
|
| 352 | + |
|
| 353 | + if ($pad_left > 0) |
|
| 354 | + { |
|
| 355 | + $lines = explode(PHP_EOL, $lines); |
|
| 356 | + |
|
| 357 | + $first = true; |
|
| 358 | + |
|
| 359 | + array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 360 | + |
|
| 361 | + if ( ! $first) |
|
| 362 | + { |
|
| 363 | + $line = str_repeat(' ', $pad_left) . $line; |
|
| 364 | + } |
|
| 365 | + else |
|
| 366 | + { |
|
| 367 | + $first = false; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + }); |
|
| 371 | + |
|
| 372 | + $lines = implode(PHP_EOL, $lines); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + return $lines; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Get input from the shell, using readline or the standard STDIN. |
|
| 380 | + * |
|
| 381 | + * @param string|int $prefix The name of the option (int if unnamed) |
|
| 382 | + * |
|
| 383 | + * @return string |
|
| 384 | + */ |
|
| 385 | + public static function input($prefix = '') |
|
| 386 | + { |
|
| 387 | + if (static::$readlineSupport) |
|
| 388 | + { |
|
| 389 | + return readline($prefix); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + echo $prefix; |
|
| 393 | + |
|
| 394 | + return fgets(STDIN); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * If operating system === windows. |
|
| 399 | + * |
|
| 400 | + * @return string |
|
| 401 | + */ |
|
| 402 | + public static function isWindows() |
|
| 403 | + { |
|
| 404 | + return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Enter a number of empty lines. |
|
| 409 | + * |
|
| 410 | + * @param int $num Number of lines to output |
|
| 411 | + * |
|
| 412 | + * @return void |
|
| 413 | + */ |
|
| 414 | + public static function newLine(int $num = 1) |
|
| 415 | + { |
|
| 416 | + for ($i = 0; $i < $num; $i++) |
|
| 417 | + { |
|
| 418 | + static::write(); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Returns the option with the given name. You can also give the option number. |
|
| 424 | + * |
|
| 425 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 426 | + * @param mixed $default The value to return if the option is not defined |
|
| 427 | + * |
|
| 428 | + * @return mixed |
|
| 429 | + * |
|
| 430 | + * @uses \Syscodes\Contract\Core\Lenevor |
|
| 431 | + */ |
|
| 432 | + public static function option($name, $default = null) |
|
| 433 | + { |
|
| 434 | + if ( ! isset(static::$options[$name])) |
|
| 435 | + { |
|
| 436 | + return Lenevor::value($default); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return static::$options[$name]; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Parses the command line it was called from and collects all |
|
| 444 | + * options and valid segments. |
|
| 445 | + * |
|
| 446 | + * @return bool |
|
| 447 | + */ |
|
| 448 | + protected static function parseCommandLine() |
|
| 449 | + { |
|
| 450 | + $options = false; |
|
| 451 | + |
|
| 452 | + for ($i = 1; $i < $_SERVER['argc']; $i++) |
|
| 453 | + { |
|
| 454 | + if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) |
|
| 455 | + { |
|
| 456 | + static::$segments[] = $_SERVER['argv'][$i]; |
|
| 457 | + |
|
| 458 | + continue; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + $options = true; |
|
| 462 | + |
|
| 463 | + $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 464 | + $value = null; |
|
| 465 | + |
|
| 466 | + if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) |
|
| 467 | + { |
|
| 468 | + $value = $_SERVER['argv'][$i + 1]; |
|
| 469 | + $i++; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + static::$options[$args] = $value; |
|
| 473 | + |
|
| 474 | + $options = false; |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Returns the command line string portions of the arguments, minus |
|
| 480 | + * any options, as a string. |
|
| 481 | + * |
|
| 482 | + * @return string |
|
| 483 | + */ |
|
| 484 | + public static function getURI() |
|
| 485 | + { |
|
| 486 | + return implode('/', static::$segments); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Returns an individual segment. |
|
| 491 | + * |
|
| 492 | + * @param int $index |
|
| 493 | + * |
|
| 494 | + * @return mixed|null |
|
| 495 | + */ |
|
| 496 | + public static function getSegment(int $index) |
|
| 497 | + { |
|
| 498 | + if ( ! isset(static::$segments[$index - 1])) |
|
| 499 | + { |
|
| 500 | + return null; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + return static::$segments[$index - 1]; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Returns the raw array of segments found. |
|
| 508 | + * |
|
| 509 | + * @return array |
|
| 510 | + */ |
|
| 511 | + public static function getSegments() |
|
| 512 | + { |
|
| 513 | + return static::$segments; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 518 | + * |
|
| 519 | + * Usage: |
|
| 520 | + * |
|
| 521 | + * // Waits for any key press |
|
| 522 | + * Cli::prompt(); |
|
| 523 | + * |
|
| 524 | + * // Takes any input |
|
| 525 | + * $color = Cli::prompt('What is your favorite color?'); |
|
| 526 | + * |
|
| 527 | + * // Takes any input, but offers default |
|
| 528 | + * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 529 | + * |
|
| 530 | + * // Will only accept the options in the array |
|
| 531 | + * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 532 | + * |
|
| 533 | + * @return string The user input |
|
| 534 | + */ |
|
| 535 | + public static function prompt() |
|
| 536 | + { |
|
| 537 | + $args = func_get_args(); |
|
| 538 | + |
|
| 539 | + $options = []; |
|
| 540 | + $output = ''; |
|
| 541 | + $default = null; |
|
| 542 | + |
|
| 543 | + // How many we got |
|
| 544 | + $arg_count = count($args); |
|
| 545 | + |
|
| 546 | + // Is the last argument a boolean? True means required |
|
| 547 | + $required = end($args) === true; |
|
| 548 | + |
|
| 549 | + // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 550 | + $required === true and --$arg_count; |
|
| 551 | + |
|
| 552 | + // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 553 | + switch ($arg_count) |
|
| 554 | + { |
|
| 555 | + case 2: |
|
| 556 | + |
|
| 557 | + // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 558 | + if (is_array($args[1])) |
|
| 559 | + { |
|
| 560 | + list($output, $options) = $args; |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 564 | + elseif (is_string($args[1])) |
|
| 565 | + { |
|
| 566 | + list($output, $default) = $args; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + break; |
|
| 570 | + |
|
| 571 | + case 1: |
|
| 572 | + |
|
| 573 | + // No question (probably been asked already) so just show options |
|
| 574 | + // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 575 | + if (is_array($args[0])) |
|
| 576 | + { |
|
| 577 | + $options = $args[0]; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + // Question without options |
|
| 581 | + // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 582 | + elseif (is_string($args[0])) |
|
| 583 | + { |
|
| 584 | + $output = $args[0]; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + break; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + // If a question has been asked with the read |
|
| 591 | + if ($output !== '') |
|
| 592 | + { |
|
| 593 | + $extra_output = ''; |
|
| 594 | + |
|
| 595 | + if ($default !== null) |
|
| 596 | + { |
|
| 597 | + $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 598 | + } |
|
| 599 | + elseif ($options !== []) |
|
| 600 | + { |
|
| 601 | + $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + // Read the input from keyboard. |
|
| 608 | + $input = trim(static::input()) ?: $default; |
|
| 609 | + |
|
| 610 | + // No input provided and we require one (default will stop this being called) |
|
| 611 | + if (empty($input) and $required === true) |
|
| 612 | + { |
|
| 613 | + static::write('This is required.'); |
|
| 614 | + static::newLine(); |
|
| 615 | + |
|
| 616 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + // If options are provided and the choice is not in the array, tell them to try again |
|
| 620 | + if ( ! empty($options) and ! in_array($input, $options)) |
|
| 621 | + { |
|
| 622 | + static::write('This is not a valid option. Please try again.'); |
|
| 623 | + static::newLine(); |
|
| 624 | + |
|
| 625 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + return $input; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Allows you to set a commandline option from code. |
|
| 633 | + * |
|
| 634 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 635 | + * @param mixed|null $value The value to set, or null to delete the option |
|
| 636 | + * |
|
| 637 | + * @return mixed |
|
| 638 | + */ |
|
| 639 | + public static function setOption($name, $value = null) |
|
| 640 | + { |
|
| 641 | + if ($value == null) |
|
| 642 | + { |
|
| 643 | + if (isset(static::$options[$name])) |
|
| 644 | + { |
|
| 645 | + unset(static::$options[$name]); |
|
| 646 | + } |
|
| 647 | + } |
|
| 648 | + else |
|
| 649 | + { |
|
| 650 | + static::$options[$name] = $value; |
|
| 651 | + } |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * Waits a certain number of seconds, optionally showing a wait message and |
|
| 656 | + * waiting for a key press. |
|
| 657 | + * |
|
| 658 | + * @param int $seconds Number of seconds |
|
| 659 | + * @param bool $countdown Show a countdown or not |
|
| 660 | + * |
|
| 661 | + * @return string |
|
| 662 | + */ |
|
| 663 | + public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 664 | + { |
|
| 665 | + if ($countdown === true) |
|
| 666 | + { |
|
| 667 | + $time = $seconds; |
|
| 668 | + |
|
| 669 | + while ($time > 0) |
|
| 670 | + { |
|
| 671 | + fwrite(static::$stdout, $time.'... '); |
|
| 672 | + sleep(1); |
|
| 673 | + $time--; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + static::write(); |
|
| 677 | + } |
|
| 678 | + else |
|
| 679 | + { |
|
| 680 | + if ($seconds = 0) |
|
| 681 | + { |
|
| 682 | + sleep($seconds); |
|
| 683 | + } |
|
| 684 | + else |
|
| 685 | + { |
|
| 686 | + static::write(static::$waitMsg); |
|
| 687 | + static::input(); |
|
| 688 | + } |
|
| 689 | + } |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Outputs a string to the cli. If you send an array it will implode them |
|
| 694 | + * with a line break. |
|
| 695 | + * |
|
| 696 | + * @param string|array $text The text to output, or array of lines |
|
| 697 | + * @param string|null $foreground The foreground color |
|
| 698 | + * @param string|null $background The background color |
|
| 699 | + * |
|
| 700 | + * @return string |
|
| 701 | + */ |
|
| 702 | + public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 703 | + { |
|
| 704 | + if (is_array($text)) |
|
| 705 | + { |
|
| 706 | + $text = implode(PHP_EOL, $text); |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + if ($foreground OR $background) |
|
| 710 | + { |
|
| 711 | + $text = static::color($text, $foreground, $background); |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + fwrite(static::$stdout, $text.PHP_EOL); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + /** |
|
| 718 | + * Returns a well formatted table. |
|
| 719 | + * |
|
| 720 | + * @param array $tbody List of rows |
|
| 721 | + * @param array $thead List of columns |
|
| 722 | + * |
|
| 723 | + * @return void |
|
| 724 | + */ |
|
| 725 | + public static function table(array $tbody, array $thead = []) |
|
| 726 | + { |
|
| 727 | + $rows = []; |
|
| 728 | + |
|
| 729 | + if ( ! empty($thead)) |
|
| 730 | + { |
|
| 731 | + $rows[] = array_values($thead); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + foreach ($tbody as $tr) |
|
| 735 | + { |
|
| 736 | + $rows[] = count($rows); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + $totalRows = count($rows); |
|
| 740 | + |
|
| 741 | + // Store all columns lengths |
|
| 742 | + $allColsLengths = []; |
|
| 743 | + |
|
| 744 | + // Store maximum lengths by column |
|
| 745 | + $maxColsLengths = []; |
|
| 746 | + |
|
| 747 | + for ($row = 0; $row < $totalRows; $row++) |
|
| 748 | + { |
|
| 749 | + $column = 0; |
|
| 750 | + |
|
| 751 | + foreach ($rows[$row] as $col) |
|
| 752 | + { |
|
| 753 | + $allColsLengths[$row][$column] = static::strlen($col); |
|
| 754 | + |
|
| 755 | + if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) |
|
| 756 | + { |
|
| 757 | + $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + $column++; |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + for ($row = 0; $row < $totalRows; $row++) |
|
| 765 | + { |
|
| 766 | + $column = 0; |
|
| 767 | + |
|
| 768 | + foreach ($rows[$row] as $col) |
|
| 769 | + { |
|
| 770 | + $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 771 | 771 | |
| 772 | - if ($diverse) |
|
| 773 | - { |
|
| 774 | - $rows[$row][$colum] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 775 | - } |
|
| 772 | + if ($diverse) |
|
| 773 | + { |
|
| 774 | + $rows[$row][$colum] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 775 | + } |
|
| 776 | 776 | |
| 777 | - $column++; |
|
| 778 | - } |
|
| 779 | - } |
|
| 777 | + $column++; |
|
| 778 | + } |
|
| 779 | + } |
|
| 780 | 780 | |
| 781 | - $table = ''; |
|
| 781 | + $table = ''; |
|
| 782 | 782 | |
| 783 | - for ($row = 0; $row < $rows; $row++) |
|
| 784 | - { |
|
| 785 | - if (0 === $row) |
|
| 786 | - { |
|
| 787 | - $cols = '+'; |
|
| 783 | + for ($row = 0; $row < $rows; $row++) |
|
| 784 | + { |
|
| 785 | + if (0 === $row) |
|
| 786 | + { |
|
| 787 | + $cols = '+'; |
|
| 788 | 788 | |
| 789 | - foreach ($rows[$row] as $col) |
|
| 790 | - { |
|
| 791 | - $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 792 | - } |
|
| 789 | + foreach ($rows[$row] as $col) |
|
| 790 | + { |
|
| 791 | + $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 792 | + } |
|
| 793 | 793 | |
| 794 | - $table .= $cols.PHP_EOL; |
|
| 795 | - } |
|
| 794 | + $table .= $cols.PHP_EOL; |
|
| 795 | + } |
|
| 796 | 796 | |
| 797 | - $table .= '| '.imploode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 797 | + $table .= '| '.imploode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 798 | 798 | |
| 799 | - if (0 === $row && ! empty($thead) || $row + 1 === $rows) |
|
| 800 | - { |
|
| 801 | - $table .= $cols.PHP_EOL; |
|
| 802 | - } |
|
| 803 | - } |
|
| 799 | + if (0 === $row && ! empty($thead) || $row + 1 === $rows) |
|
| 800 | + { |
|
| 801 | + $table .= $cols.PHP_EOL; |
|
| 802 | + } |
|
| 803 | + } |
|
| 804 | 804 | |
| 805 | - fwrite(static::$stdout, $table); |
|
| 806 | - } |
|
| 805 | + fwrite(static::$stdout, $table); |
|
| 806 | + } |
|
| 807 | 807 | } |
| 808 | 808 | \ No newline at end of file |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) |
|
| 213 | + if ($background !== null && ! Arr::exists(static::$backgroundColors, $background)) |
|
| 214 | 214 | { |
| 215 | 215 | throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
| 216 | 216 | } |
@@ -356,11 +356,11 @@ discard block |
||
| 356 | 356 | |
| 357 | 357 | $first = true; |
| 358 | 358 | |
| 359 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 359 | + array_walk($lines, function(&$line, $index) use ($pad_left, &$first) { |
|
| 360 | 360 | |
| 361 | 361 | if ( ! $first) |
| 362 | 362 | { |
| 363 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 363 | + $line = str_repeat(' ', $pad_left).$line; |
|
| 364 | 364 | } |
| 365 | 365 | else |
| 366 | 366 | { |
@@ -361,8 +361,7 @@ discard block |
||
| 361 | 361 | if ( ! $first) |
| 362 | 362 | { |
| 363 | 363 | $line = str_repeat(' ', $pad_left) . $line; |
| 364 | - } |
|
| 365 | - else |
|
| 364 | + } else |
|
| 366 | 365 | { |
| 367 | 366 | $first = false; |
| 368 | 367 | } |
@@ -595,8 +594,7 @@ discard block |
||
| 595 | 594 | if ($default !== null) |
| 596 | 595 | { |
| 597 | 596 | $extra_output = ' [ Default: "'.$default.'" ]'; |
| 598 | - } |
|
| 599 | - elseif ($options !== []) |
|
| 597 | + } elseif ($options !== []) |
|
| 600 | 598 | { |
| 601 | 599 | $extra_output = ' [ '.implode(' | ', $options).' ]'; |
| 602 | 600 | } |
@@ -644,8 +642,7 @@ discard block |
||
| 644 | 642 | { |
| 645 | 643 | unset(static::$options[$name]); |
| 646 | 644 | } |
| 647 | - } |
|
| 648 | - else |
|
| 645 | + } else |
|
| 649 | 646 | { |
| 650 | 647 | static::$options[$name] = $value; |
| 651 | 648 | } |
@@ -674,14 +671,12 @@ discard block |
||
| 674 | 671 | } |
| 675 | 672 | |
| 676 | 673 | static::write(); |
| 677 | - } |
|
| 678 | - else |
|
| 674 | + } else |
|
| 679 | 675 | { |
| 680 | 676 | if ($seconds = 0) |
| 681 | 677 | { |
| 682 | 678 | sleep($seconds); |
| 683 | - } |
|
| 684 | - else |
|
| 679 | + } else |
|
| 685 | 680 | { |
| 686 | 681 | static::write(static::$waitMsg); |
| 687 | 682 | static::input(); |