123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace AlibabaCloud\Client;
- use Closure;
- use AlibabaCloud\Client\Support\Stringy;
- use League\CLImate\CLImate;
- use AlibabaCloud\Client\Exception\ClientException;
- /*
- |--------------------------------------------------------------------------
- | Global Functions for Alibaba Cloud
- |--------------------------------------------------------------------------
- |
- | Some common global functions are defined here.
- | This file will be automatically loaded.
- |
- */
- /**
- * @param $filename
- * @param bool $throwException
- *
- * @return bool
- * @throws ClientException
- */
- function inOpenBasedir($filename, $throwException = false)
- {
- $open_basedir = ini_get('open_basedir');
- if (!$open_basedir) {
- return true;
- }
- $dirs = explode(PATH_SEPARATOR, $open_basedir);
- if (empty($dirs)) {
- return true;
- }
- if (inDir($filename, $dirs)) {
- return true;
- }
- if ($throwException === false) {
- return false;
- }
- throw new ClientException(
- 'open_basedir restriction in effect. '
- . "File($filename) is not within the allowed path(s): ($open_basedir)",
- 'SDK.InvalidPath'
- );
- }
- /**
- * @param string $filename
- * @param array $dirs
- *
- * @return bool
- */
- function inDir($filename, array $dirs)
- {
- foreach ($dirs as $dir) {
- if (!Stringy::endsWith($dir, DIRECTORY_SEPARATOR)) {
- $dir .= DIRECTORY_SEPARATOR;
- }
- if (0 === strpos($filename, $dir)) {
- return true;
- }
- }
- return false;
- }
- /**
- * @return bool
- */
- function isWindows()
- {
- return PATH_SEPARATOR === ';';
- }
- /**
- * @return CLImate
- */
- function cliMate()
- {
- return new CLImate();
- }
- /**
- * @param string $string
- * @param string|null $flank
- * @param string|null $char
- * @param int|null $length
- *
- * @return void
- */
- function backgroundRed($string, $flank = null, $char = null, $length = null)
- {
- cliMate()->br();
- if ($flank !== null) {
- cliMate()->backgroundRed()->flank($flank, $char, $length);
- cliMate()->br();
- }
- cliMate()->backgroundRed($string);
- cliMate()->br();
- }
- /**
- * @param string $string
- * @param string|null $flank
- * @param string|null $char
- * @param int|null $length
- *
- * @return void
- */
- function backgroundGreen($string, $flank = null, $char = null, $length = null)
- {
- cliMate()->br();
- if ($flank !== null) {
- cliMate()->backgroundGreen()->flank($flank, $char, $length);
- }
- cliMate()->backgroundGreen($string);
- cliMate()->br();
- }
- /**
- * @param string $string
- * @param string|null $flank
- * @param string|null $char
- * @param int|null $length
- *
- * @return void
- */
- function backgroundBlue($string, $flank = null, $char = null, $length = null)
- {
- cliMate()->br();
- if ($flank !== null) {
- cliMate()->backgroundBlue()->flank($flank, $char, $length);
- }
- cliMate()->backgroundBlue($string);
- cliMate()->br();
- }
- /**
- * @param string $string
- * @param string|null $flank
- * @param string|null $char
- * @param int|null $length
- *
- * @return void
- */
- function backgroundMagenta($string, $flank = null, $char = null, $length = null)
- {
- cliMate()->br();
- if ($flank !== null) {
- cliMate()->backgroundMagenta()->flank($flank, $char, $length);
- }
- cliMate()->backgroundMagenta($string);
- cliMate()->br();
- }
- /**
- * @param array $array
- */
- function json(array $array)
- {
- cliMate()->br();
- cliMate()->backgroundGreen()->json($array);
- cliMate()->br();
- }
- /**
- * @param array $array
- *
- * @return void
- */
- function redTable($array)
- {
- /**
- * @noinspection PhpUndefinedMethodInspection
- */
- cliMate()->redTable($array);
- }
- /**
- * @param mixed $result
- * @param string $title
- *
- * @return void
- */
- function block($result, $title)
- {
- cliMate()->backgroundGreen()->flank($title, '--', 20);
- dump($result);
- }
- /**
- * Gets the value of an environment variable.
- *
- * @param string $key
- * @param mixed $default
- *
- * @return mixed
- */
- function env($key, $default = null)
- {
- $value = getenv($key);
- if ($value === false) {
- return value($default);
- }
- if (envSubstr($value)) {
- return substr($value, 1, -1);
- }
- return envConversion($value);
- }
- /**
- * @param $value
- *
- * @return bool|string|null
- */
- function envConversion($value)
- {
- $key = strtolower($value);
- if ($key === 'null' || $key === '(null)') {
- return null;
- }
- $list = [
- 'true' => true,
- '(true)' => true,
- 'false' => false,
- '(false)' => false,
- 'empty' => '',
- '(empty)' => '',
- ];
- return isset($list[$key]) ? $list[$key] : $value;
- }
- /**
- * @param $key
- *
- * @return bool|mixed
- * @throws ClientException
- */
- function envNotEmpty($key)
- {
- $value = env($key, false);
- if ($value !== false && !$value) {
- throw new ClientException(
- "Environment variable '$key' cannot be empty",
- SDK::INVALID_ARGUMENT
- );
- }
- if ($value) {
- return $value;
- }
- return false;
- }
- /**
- * @param $value
- *
- * @return bool
- */
- function envSubstr($value)
- {
- return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"';
- }
- /**
- * Return the default value of the given value.
- *
- * @param mixed $value
- *
- * @return mixed
- */
- function value($value)
- {
- return $value instanceof Closure ? $value() : $value;
- }
|