Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

123 rader
2.5 KiB

  1. <?php
  2. namespace Kuxin;
  3. /**
  4. * 命令行
  5. * Class Console
  6. *
  7. * @package Kuxin
  8. * @author Pakey <pakey@qq.com>
  9. */
  10. class Console
  11. {
  12. /**
  13. * 命令行参数
  14. * @var array
  15. */
  16. protected $params = [];
  17. /**
  18. * Console constructor.
  19. */
  20. public function __construct()
  21. {
  22. if((int)ini_get('memory_limit')<1024){
  23. ini_set('memory_limit','1024M');
  24. }
  25. set_time_limit(0);
  26. $this->params = Registry::get('cli_params', []);
  27. }
  28. /**
  29. * 初始化
  30. */
  31. public function init()
  32. {
  33. }
  34. /**
  35. * 终端输出
  36. *
  37. * @param $text
  38. * @param $status
  39. * @param $line
  40. * @return mixed
  41. */
  42. public function show(string $text, string $status = 'text', bool $line = true): void
  43. {
  44. printf(Response::terminal($text, $status, $line));
  45. }
  46. /**
  47. * 终端输出
  48. *
  49. * @param string $text
  50. * @param bool $line
  51. * @return mixed
  52. */
  53. public function info(string $text, bool $line = true): void
  54. {
  55. printf(Response::terminal($text, 'info', $line));
  56. }
  57. /**
  58. * @param string $text
  59. * @param bool $line
  60. * @return mixed
  61. */
  62. public function success(string $text, bool $line = true): void
  63. {
  64. printf(Response::terminal($text, 'success', $line));
  65. }
  66. /**
  67. * @param string $text
  68. * @param bool $line
  69. * @return mixed
  70. */
  71. public function warning(string $text, bool $line = true): void
  72. {
  73. printf(Response::terminal($text, 'success', $line));
  74. }
  75. /**
  76. * @param string $text
  77. * @param bool $line
  78. * @return mixed
  79. */
  80. public function error(string $text, bool $line = true): void
  81. {
  82. printf(Response::terminal($text, 'error', $line));
  83. }
  84. /**
  85. * 获取参数
  86. * @param string $key
  87. * @param string $type
  88. * @param null $default
  89. * @return array|float|int|mixed|null|string
  90. */
  91. public function param(string $key, string $type = 'str', $default = null)
  92. {
  93. return Input::param($key, $type, $default, $this->params);
  94. }
  95. /**
  96. * 终端给提示获取用户数据
  97. * @param string $text
  98. * @param string $status
  99. * @return string
  100. */
  101. public function prompt(string $text = '请输入', string $status = 'text')
  102. {
  103. //提示输入
  104. fwrite(STDOUT, Response::terminal($text . ":", $status, false));
  105. //获取用户输入数据
  106. $result = trim(fgets(STDIN));
  107. return $result;
  108. }
  109. }