From e211731cd25cd2b00741be79f74e419e37ed746e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 16:33:08 +0800 Subject: [PATCH 1/3] Split Arr and Str Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Consul/Response.php | 5 +- src/Functions.php | 154 ------------------------- src/PathGenerator/DotPathGenerator.php | 10 +- src/PathGenerator/PathGenerator.php | 5 +- src/Registry/ConsulRegistry.php | 14 +-- src/Support/Arr.php | 111 ++++++++++++++++++ src/Support/Str.php | 87 ++++++++++++++ 7 files changed, 213 insertions(+), 173 deletions(-) create mode 100644 src/Support/Arr.php create mode 100644 src/Support/Str.php diff --git a/src/Consul/Response.php b/src/Consul/Response.php index 62ccbdb..6b05760 100644 --- a/src/Consul/Response.php +++ b/src/Consul/Response.php @@ -12,10 +12,9 @@ namespace FriendsOfHyperf\Jet\Consul; use FriendsOfHyperf\Jet\Exception\ServerException; +use FriendsOfHyperf\Jet\Support\Arr; use Psr\Http\Message\ResponseInterface; -use function FriendsOfHyperf\Jet\array_get; - class Response { /** @@ -57,7 +56,7 @@ public function json(?string $key = null, $default = null) return $this->decoded; } - return array_get($this->decoded, $key, $default); + return Arr::get($this->decoded, $key, $default); } /** diff --git a/src/Functions.php b/src/Functions.php index b21025c..370c9eb 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -104,68 +104,6 @@ function with($value, ?callable $callback = null) // @phpstan-ignore-line return is_null($callback) ? $value : $callback($value); } -/** - * @param string $delimiter - * @return string - */ -function str_snake(string $value, $delimiter = '_') -{ - if (! ctype_lower($value)) { - $value = preg_replace('/\s+/u', '', ucwords($value)); - $value = str_lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); - } - - return $value; -} - -/** - * @return string - */ -function str_lower(string $value) -{ - return mb_strtolower($value, 'UTF-8'); -} - -/** - * @return string - */ -function str_studly(string $value, string $gap = '') -{ - $value = ucwords(str_replace(['-', '_'], ' ', $value)); - - return str_replace(' ', $gap, $value); -} - -/** - * @return string - */ -function str_replace_first(string $search, string $replace, string $subject) -{ - if ($search == '') { - return $subject; - } - - $position = strpos($subject, $search); - - if ($position !== false) { - return substr_replace($subject, $replace, $position, strlen($search)); - } - - return $subject; -} - -/** - * @return string - */ -function str_replace_array(string $search, array $replace, string $subject) -{ - foreach ($replace as $value) { - $subject = str_replace_first($search, (string) $value, $subject); - } - - return $subject; -} - /** * @param mixed $value * @return mixed @@ -174,95 +112,3 @@ function value($value) { return $value instanceof \Closure ? $value() : $value; } - -/** - * Get an item from an array using "dot" notation. - * - * @param array|\ArrayAccess $array - * @param null|int|string $key - * @param mixed $default - */ -function array_get($array, $key = null, $default = null) -{ - if (is_null($key)) { - return $array; - } - - if (isset($array[$key])) { - return $array[$key]; - } - - if (! is_string($key) || strpos($key, '.') === false) { - return $array[$key] ?? value($default); - } - - foreach (explode('.', $key) as $segment) { - if (array_accessible($array) && array_exists($array, $segment)) { - $array = $array[$segment]; - } else { - return value($default); - } - } - - return $array; -} - -/** - * Check if an item or items exist in an array using "dot" notation. - * - * @param array|\ArrayAccess $array - * @param null|array|string $keys - */ -function array_has($array, $keys) -{ - if (is_null($keys)) { - return false; - } - - $keys = (array) $keys; - - if (! $array || $keys === []) { - return false; - } - - foreach ($keys as $key) { - $subKeyArray = $array; - - if (array_exists($array, $key)) { - continue; - } - - foreach (explode('.', $key) as $segment) { - if (array_accessible($subKeyArray) && array_exists($subKeyArray, $segment)) { - $subKeyArray = $subKeyArray[$segment]; - } else { - return false; - } - } - } - - return true; -} - -/** - * @param mixed $array - * @param int|string $key - * @return bool - */ -function array_exists($array, $key) -{ - if ($array instanceof \ArrayAccess) { - return $array->offsetExists($key); - } - - return array_key_exists($key, $array); -} - -/** - * @param mixed $value - * @return bool - */ -function array_accessible($value) -{ - return is_array($value) || $value instanceof \ArrayAccess; -} diff --git a/src/PathGenerator/DotPathGenerator.php b/src/PathGenerator/DotPathGenerator.php index 9655799..78b6003 100644 --- a/src/PathGenerator/DotPathGenerator.php +++ b/src/PathGenerator/DotPathGenerator.php @@ -12,18 +12,16 @@ namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; - -use function FriendsOfHyperf\Jet\str_replace_array; -use function FriendsOfHyperf\Jet\str_studly; +use FriendsOfHyperf\Jet\Support\Str; class DotPathGenerator implements PathGeneratorInterface { public function generate(string $service, string $method): string { $handledNamespace = explode('\\', $service); - $handledNamespace = str_replace_array('\\', ['/'], end($handledNamespace)); - $path = str_studly($handledNamespace); + $handledNamespace = Str::replaceArray('\\', ['/'], end($handledNamespace)); + $path = Str::studly($handledNamespace); - return $path . '.' . str_studly($method); + return $path . '.' . Str::studly($method); } } diff --git a/src/PathGenerator/PathGenerator.php b/src/PathGenerator/PathGenerator.php index 080a9d3..4d3a8f1 100644 --- a/src/PathGenerator/PathGenerator.php +++ b/src/PathGenerator/PathGenerator.php @@ -12,8 +12,7 @@ namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; - -use function FriendsOfHyperf\Jet\str_snake; +use FriendsOfHyperf\Jet\Support\Str; class PathGenerator implements PathGeneratorInterface { @@ -22,7 +21,7 @@ public function generate(string $service, string $method): string $handledNamespace = explode('\\', $service); $handledNamespace = str_replace('\\', '/', end($handledNamespace)); $handledNamespace = str_replace('Service', '', $handledNamespace); - $path = str_snake($handledNamespace); + $path = Str::snake($handledNamespace); if ($path[0] !== '/') { $path = '/' . $path; diff --git a/src/Registry/ConsulRegistry.php b/src/Registry/ConsulRegistry.php index 67765a5..b4534b4 100644 --- a/src/Registry/ConsulRegistry.php +++ b/src/Registry/ConsulRegistry.php @@ -17,11 +17,11 @@ use FriendsOfHyperf\Jet\Contract\RegistryInterface; use FriendsOfHyperf\Jet\LoadBalancer\Node; use FriendsOfHyperf\Jet\LoadBalancer\RoundRobin; +use FriendsOfHyperf\Jet\Support\Arr; use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter; use GuzzleHttp\Client; -use function FriendsOfHyperf\Jet\array_get; use function FriendsOfHyperf\Jet\retry; use function FriendsOfHyperf\Jet\with; @@ -120,21 +120,21 @@ public function getServiceNodes(string $service, ?string $protocol = null) $nodes = []; foreach ($serviceNodes as $node) { - if (array_get($node, 'Checks.1.Status') != 'passing') { + if (Arr::get($node, 'Checks.1.Status') != 'passing') { continue; } - if (! is_null($protocol) && $protocol != array_get($node, 'Service.Meta.Protocol')) { + if (! is_null($protocol) && $protocol != Arr::get($node, 'Service.Meta.Protocol')) { continue; } $nodes[] = new Node( - array_get($node, 'Service.Address'), - (int) array_get($node, 'Service.Port'), + Arr::get($node, 'Service.Address'), + (int) Arr::get($node, 'Service.Port'), 1, [ - 'type' => array_get($node, 'Checks.1.Type'), - 'protocol' => array_get($node, 'Service.Meta.Protocol'), + 'type' => Arr::get($node, 'Checks.1.Type'), + 'protocol' => Arr::get($node, 'Service.Meta.Protocol'), ] ); } diff --git a/src/Support/Arr.php b/src/Support/Arr.php new file mode 100644 index 0000000..afc1d8e --- /dev/null +++ b/src/Support/Arr.php @@ -0,0 +1,111 @@ +offsetExists($key); + } + + return array_key_exists($key, $array); + } + + /** + * @param mixed $value + * @return bool + */ + public static function accessible($value) + { + return is_array($value) || $value instanceof \ArrayAccess; + } +} diff --git a/src/Support/Str.php b/src/Support/Str.php new file mode 100644 index 0000000..a515b71 --- /dev/null +++ b/src/Support/Str.php @@ -0,0 +1,87 @@ + Date: Sun, 11 Aug 2024 16:38:39 +0800 Subject: [PATCH 2/3] feat: Update path generation to handle namespaces with backslashes --- src/PathGenerator/DotPathGenerator.php | 2 +- src/Support/Str.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PathGenerator/DotPathGenerator.php b/src/PathGenerator/DotPathGenerator.php index 78b6003..6caeb87 100644 --- a/src/PathGenerator/DotPathGenerator.php +++ b/src/PathGenerator/DotPathGenerator.php @@ -19,7 +19,7 @@ class DotPathGenerator implements PathGeneratorInterface public function generate(string $service, string $method): string { $handledNamespace = explode('\\', $service); - $handledNamespace = Str::replaceArray('\\', ['/'], end($handledNamespace)); + $handledNamespace = Str::replaceArray(['\\'], ['/'], end($handledNamespace)); $path = Str::studly($handledNamespace); return $path . '.' . Str::studly($method); diff --git a/src/Support/Str.php b/src/Support/Str.php index a515b71..b2b5434 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -50,8 +50,8 @@ public static function studly($value, $gap = '') } /** - * @param string $search - * @param string $replace + * @param array|string $search + * @param array|string $replace * @param string $subject * @return string */ From 02382f7ecaee47f6897e5d36101fd20b7d002089 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 16:42:37 +0800 Subject: [PATCH 3/3] feat: Fix backslash handling in path generation --- src/PathGenerator/DotPathGenerator.php | 2 +- src/Support/Str.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PathGenerator/DotPathGenerator.php b/src/PathGenerator/DotPathGenerator.php index 6caeb87..78b6003 100644 --- a/src/PathGenerator/DotPathGenerator.php +++ b/src/PathGenerator/DotPathGenerator.php @@ -19,7 +19,7 @@ class DotPathGenerator implements PathGeneratorInterface public function generate(string $service, string $method): string { $handledNamespace = explode('\\', $service); - $handledNamespace = Str::replaceArray(['\\'], ['/'], end($handledNamespace)); + $handledNamespace = Str::replaceArray('\\', ['/'], end($handledNamespace)); $path = Str::studly($handledNamespace); return $path . '.' . Str::studly($method); diff --git a/src/Support/Str.php b/src/Support/Str.php index b2b5434..e17bb4b 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -50,7 +50,7 @@ public static function studly($value, $gap = '') } /** - * @param array|string $search + * @param string $search * @param array|string $replace * @param string $subject * @return string @@ -71,8 +71,8 @@ public static function replaceFirst($search, $replace, $subject) } /** - * @param array $search - * @param array $replace + * @param string $search + * @param iterable $replace * @param string $subject * @return string */