1b60f96ce148ad102a64f9ab04f6f9b9b866af21.svn-base 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace Maze\Http;
  2. class Input
  3. {
  4. /**
  5. * term
  6. *
  7. * @var array
  8. */
  9. static public $request = array();
  10. /**
  11. * term
  12. *
  13. * @var array
  14. */
  15. static protected $term;
  16. /**
  17. * init
  18. *
  19. * @return array
  20. */
  21. static public function init()
  22. {
  23. global $argc,$argv; #$argc 和 $argv 不是全局变量,要在这里面再赋值一次 :(
  24. #如果参数大于 ,就说明有参数
  25. if($argc > 1)
  26. {
  27. #分析并保存Term数据
  28. $term = array();
  29. $key = '';
  30. #第一位是程序名称,不用分析
  31. for($i=1; $i<$argc; $i++)
  32. {
  33. #以“-”开头,说明是变量,保存Key值
  34. if(substr($argv[$i],0,1) == '-' || substr($argv[$i],0,1) == '^')
  35. {
  36. #上一个Key还未失效,就赋值为空
  37. if($key != '')
  38. {
  39. $term[$key] = $key;
  40. }
  41. $key = substr($argv[$i],1);
  42. continue;
  43. }
  44. #有Key设置为key,否则设置此变量为Key
  45. if($key != '')
  46. {
  47. $term[$key] = $argv[$i];
  48. $key = '';
  49. }
  50. else
  51. {
  52. $term[$argv[$i]] = $argv[$i];
  53. }
  54. }
  55. self::$request = $term;
  56. self::$request['maze_shell'] = 1;
  57. }
  58. else
  59. {
  60. self::$request = array_merge($_GET, $_POST, $_FILES);
  61. }
  62. return true;
  63. }
  64. /**
  65. * get
  66. * @param string $name
  67. *
  68. * @return array
  69. */
  70. static public function get($name = false, $default = '')
  71. {
  72. if(!$name) return self::$request;
  73. if($default && $default == '-u')
  74. {
  75. $array = array('update_', 'set_', 'add_');
  76. foreach($array as $k => $v)
  77. {
  78. if(isset(self::$request[$v . $name]))
  79. {
  80. return self::$request[$v . $name];
  81. }
  82. }
  83. return false;
  84. }
  85. if(isset(self::$request[$name]) && self::$request[$name])
  86. {
  87. $default = self::$request[$name];
  88. }
  89. return $default;
  90. }
  91. /**
  92. * prefix 获取包含有该前缀的值
  93. * @param string $name
  94. *
  95. * @return array
  96. */
  97. static public function prefix($name)
  98. {
  99. $key = 'prefix_' . $name;
  100. if(isset(self::$request[$key]))
  101. {
  102. return self::$request[$key];
  103. }
  104. self::$request[$key] = array();
  105. foreach(self::$request as $k => $v)
  106. {
  107. if(strpos($k, $name) === 0)
  108. {
  109. self::$request[$key][$k] = $v;
  110. }
  111. }
  112. return self::$request[$key];
  113. }
  114. /**
  115. * set
  116. * @param string $name
  117. *
  118. * @return array
  119. */
  120. static public function set($name, $value = '', $prefix = false)
  121. {
  122. if($name == 'all')
  123. {
  124. self::$request = array_merge(self::$request, $value);
  125. }
  126. else
  127. {
  128. self::$request[$name] = $value;
  129. }
  130. if($prefix)
  131. {
  132. $prefix = 'prefix_' . $prefix;
  133. if(isset(self::$request[$prefix]))
  134. {
  135. self::$request[$prefix][$name] = $value;
  136. }
  137. }
  138. return $value;
  139. }
  140. }