Functions.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace AlibabaCloud\Client;
  3. use Closure;
  4. use AlibabaCloud\Client\Support\Stringy;
  5. use League\CLImate\CLImate;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. /*
  8. |--------------------------------------------------------------------------
  9. | Global Functions for Alibaba Cloud
  10. |--------------------------------------------------------------------------
  11. |
  12. | Some common global functions are defined here.
  13. | This file will be automatically loaded.
  14. |
  15. */
  16. /**
  17. * @param $filename
  18. * @param bool $throwException
  19. *
  20. * @return bool
  21. * @throws ClientException
  22. */
  23. function inOpenBasedir($filename, $throwException = false)
  24. {
  25. $open_basedir = ini_get('open_basedir');
  26. if (!$open_basedir) {
  27. return true;
  28. }
  29. $dirs = explode(PATH_SEPARATOR, $open_basedir);
  30. if (empty($dirs)) {
  31. return true;
  32. }
  33. if (inDir($filename, $dirs)) {
  34. return true;
  35. }
  36. if ($throwException === false) {
  37. return false;
  38. }
  39. throw new ClientException(
  40. 'open_basedir restriction in effect. '
  41. . "File($filename) is not within the allowed path(s): ($open_basedir)",
  42. 'SDK.InvalidPath'
  43. );
  44. }
  45. /**
  46. * @param string $filename
  47. * @param array $dirs
  48. *
  49. * @return bool
  50. */
  51. function inDir($filename, array $dirs)
  52. {
  53. foreach ($dirs as $dir) {
  54. if (!Stringy::endsWith($dir, DIRECTORY_SEPARATOR)) {
  55. $dir .= DIRECTORY_SEPARATOR;
  56. }
  57. if (0 === strpos($filename, $dir)) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * @return bool
  65. */
  66. function isWindows()
  67. {
  68. return PATH_SEPARATOR === ';';
  69. }
  70. /**
  71. * @return CLImate
  72. */
  73. function cliMate()
  74. {
  75. return new CLImate();
  76. }
  77. /**
  78. * @param string $string
  79. * @param string|null $flank
  80. * @param string|null $char
  81. * @param int|null $length
  82. *
  83. * @return void
  84. */
  85. function backgroundRed($string, $flank = null, $char = null, $length = null)
  86. {
  87. cliMate()->br();
  88. if ($flank !== null) {
  89. cliMate()->backgroundRed()->flank($flank, $char, $length);
  90. cliMate()->br();
  91. }
  92. cliMate()->backgroundRed($string);
  93. cliMate()->br();
  94. }
  95. /**
  96. * @param string $string
  97. * @param string|null $flank
  98. * @param string|null $char
  99. * @param int|null $length
  100. *
  101. * @return void
  102. */
  103. function backgroundGreen($string, $flank = null, $char = null, $length = null)
  104. {
  105. cliMate()->br();
  106. if ($flank !== null) {
  107. cliMate()->backgroundGreen()->flank($flank, $char, $length);
  108. }
  109. cliMate()->backgroundGreen($string);
  110. cliMate()->br();
  111. }
  112. /**
  113. * @param string $string
  114. * @param string|null $flank
  115. * @param string|null $char
  116. * @param int|null $length
  117. *
  118. * @return void
  119. */
  120. function backgroundBlue($string, $flank = null, $char = null, $length = null)
  121. {
  122. cliMate()->br();
  123. if ($flank !== null) {
  124. cliMate()->backgroundBlue()->flank($flank, $char, $length);
  125. }
  126. cliMate()->backgroundBlue($string);
  127. cliMate()->br();
  128. }
  129. /**
  130. * @param string $string
  131. * @param string|null $flank
  132. * @param string|null $char
  133. * @param int|null $length
  134. *
  135. * @return void
  136. */
  137. function backgroundMagenta($string, $flank = null, $char = null, $length = null)
  138. {
  139. cliMate()->br();
  140. if ($flank !== null) {
  141. cliMate()->backgroundMagenta()->flank($flank, $char, $length);
  142. }
  143. cliMate()->backgroundMagenta($string);
  144. cliMate()->br();
  145. }
  146. /**
  147. * @param array $array
  148. */
  149. function json(array $array)
  150. {
  151. cliMate()->br();
  152. cliMate()->backgroundGreen()->json($array);
  153. cliMate()->br();
  154. }
  155. /**
  156. * @param array $array
  157. *
  158. * @return void
  159. */
  160. function redTable($array)
  161. {
  162. /**
  163. * @noinspection PhpUndefinedMethodInspection
  164. */
  165. cliMate()->redTable($array);
  166. }
  167. /**
  168. * @param mixed $result
  169. * @param string $title
  170. *
  171. * @return void
  172. */
  173. function block($result, $title)
  174. {
  175. cliMate()->backgroundGreen()->flank($title, '--', 20);
  176. dump($result);
  177. }
  178. /**
  179. * Gets the value of an environment variable.
  180. *
  181. * @param string $key
  182. * @param mixed $default
  183. *
  184. * @return mixed
  185. */
  186. function env($key, $default = null)
  187. {
  188. $value = getenv($key);
  189. if ($value === false) {
  190. return value($default);
  191. }
  192. if (envSubstr($value)) {
  193. return substr($value, 1, -1);
  194. }
  195. return envConversion($value);
  196. }
  197. /**
  198. * @param $value
  199. *
  200. * @return bool|string|null
  201. */
  202. function envConversion($value)
  203. {
  204. $key = strtolower($value);
  205. if ($key === 'null' || $key === '(null)') {
  206. return null;
  207. }
  208. $list = [
  209. 'true' => true,
  210. '(true)' => true,
  211. 'false' => false,
  212. '(false)' => false,
  213. 'empty' => '',
  214. '(empty)' => '',
  215. ];
  216. return isset($list[$key]) ? $list[$key] : $value;
  217. }
  218. /**
  219. * @param $key
  220. *
  221. * @return bool|mixed
  222. * @throws ClientException
  223. */
  224. function envNotEmpty($key)
  225. {
  226. $value = env($key, false);
  227. if ($value !== false && !$value) {
  228. throw new ClientException(
  229. "Environment variable '$key' cannot be empty",
  230. SDK::INVALID_ARGUMENT
  231. );
  232. }
  233. if ($value) {
  234. return $value;
  235. }
  236. return false;
  237. }
  238. /**
  239. * @param $value
  240. *
  241. * @return bool
  242. */
  243. function envSubstr($value)
  244. {
  245. return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"';
  246. }
  247. /**
  248. * Return the default value of the given value.
  249. *
  250. * @param mixed $value
  251. *
  252. * @return mixed
  253. */
  254. function value($value)
  255. {
  256. return $value instanceof Closure ? $value() : $value;
  257. }