You've already forked Traefik-Redis-Api
done. going to bed now.
This commit is contained in:
217
src/Traefik/RedisClient.php
Normal file
217
src/Traefik/RedisClient.php
Normal file
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Traefik;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Predis\Collection\Iterator\Keyspace;
|
||||
use Siteworxpro\App\Facades\Redis;
|
||||
|
||||
/**
|
||||
* Class RedisClient
|
||||
*/
|
||||
class RedisClient
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param EntityEnum $entity
|
||||
* @param ProtocolEnum $type
|
||||
* @return void
|
||||
*/
|
||||
public static function createOrReplace(string $name, array $data, EntityEnum $entity, ProtocolEnum $type = ProtocolEnum::HTTP): void
|
||||
{
|
||||
self::deleteAllKeys($name, $entity, $type);
|
||||
|
||||
$collection = self::flattenToDotArray($data);
|
||||
|
||||
foreach ($collection as $key => $value) {
|
||||
$redisKey = 'traefik/' . $type->getValue() . '/' . $entity->getValue() . "/$name/" . str_replace('.', '/', $key);
|
||||
Redis::set($redisKey, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param EntityEnum $entity
|
||||
* @param ProtocolEnum $type
|
||||
* @return void
|
||||
*/
|
||||
public static function patchEntity(string $name, array $data, EntityEnum $entity, ProtocolEnum $type = ProtocolEnum::HTTP): void
|
||||
{
|
||||
$collection = self::flattenToDotArray($data);
|
||||
|
||||
$checkKey = 'traefik/' . $type->getValue() . '/' . $entity->getValue() . "/$name";
|
||||
|
||||
$keys = Redis::keys($checkKey . '/*');
|
||||
if (empty($keys)) {
|
||||
throw new \InvalidArgumentException("The key $checkKey does not exist.");
|
||||
}
|
||||
|
||||
$cleanedUpKeys = [];
|
||||
|
||||
foreach ($collection as $key => $value) {
|
||||
// regex if key matches [key].[digit]
|
||||
if (preg_match('/\.[0-9]+$/', $key)) {
|
||||
$arrayKey = preg_replace('/\.[0-9]+$/', '', $key);
|
||||
|
||||
if (in_array($arrayKey, $cleanedUpKeys)) {
|
||||
$redisKey = 'traefik/' . $type->getValue() . '/' . $entity->getValue() . "/$name/" . str_replace('.', '/', $key);
|
||||
Redis::set($redisKey, $value);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// key is an array, delete keys under array
|
||||
$arrayKeys = Redis::keys($checkKey . '/' . str_replace('.', '/', $arrayKey) . '/*');
|
||||
foreach ($arrayKeys as $k) {
|
||||
Redis::del($k);
|
||||
}
|
||||
|
||||
$cleanedUpKeys[] = $arrayKey;
|
||||
}
|
||||
|
||||
$redisKey = 'traefik/' . $type->getValue() . '/' . $entity->getValue() . "/$name/" . str_replace('.', '/', $key);
|
||||
Redis::set($redisKey, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param EntityEnum $entity
|
||||
* @param ProtocolEnum $protocol
|
||||
* @return bool
|
||||
*/
|
||||
public static function deleteAllKeys(string $name, EntityEnum $entity, ProtocolEnum $protocol = ProtocolEnum::HTTP): bool
|
||||
{
|
||||
$pattern = 'traefik/' . $protocol->getValue() . '/' . $entity->getValue() . "/$name/*";
|
||||
|
||||
foreach (new Keyspace(Redis::getFacadeRoot(), $pattern) as $key) {
|
||||
Redis::del($key);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getMiddleware(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::MIDDLEWARE->getValue() . "/$name/*";
|
||||
|
||||
return self::fetchValuesToArray($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllMiddlewares(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::MIDDLEWARE->getValue() . '/*';
|
||||
|
||||
return self::getUniqueKeys($pattern, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllServices(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::SERVICE->getValue() . '/*';
|
||||
|
||||
return self::getUniqueKeys($pattern, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $serviceName
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getService(string $serviceName, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::SERVICE->getValue() . "/$serviceName/*";
|
||||
|
||||
return self::fetchValuesToArray($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllRouters(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::ROUTER->getValue() . '/*';
|
||||
|
||||
return self::getUniqueKeys($pattern, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getRouter(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::ROUTER->getValue() . "/$name/*";
|
||||
|
||||
return self::fetchValuesToArray($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pattern
|
||||
* @param int $position
|
||||
* @return array
|
||||
*/
|
||||
private static function getUniqueKeys(string $pattern, int $position): array
|
||||
{
|
||||
$values = new Collection();
|
||||
$redis = Redis::getFacadeRoot();
|
||||
|
||||
foreach (new Keyspace($redis, $pattern) as $key) {
|
||||
$parts = explode('/', $key);
|
||||
if (isset($parts[$position])) {
|
||||
$values->push($parts[$position]);
|
||||
}
|
||||
}
|
||||
|
||||
return $values->unique()->values()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pattern
|
||||
* @return array
|
||||
*/
|
||||
private static function fetchValuesToArray(string $pattern): array
|
||||
{
|
||||
$redis = Redis::getFacadeRoot();
|
||||
|
||||
$values = new Collection();
|
||||
|
||||
foreach (new Keyspace($redis, $pattern) as $key) {
|
||||
$parts = explode('/', $key);
|
||||
$arrayPath = array_slice($parts, 4);
|
||||
$keyPath = implode('.', $arrayPath);
|
||||
$values->put($keyPath, $redis->get($key));
|
||||
}
|
||||
|
||||
return $values->undot()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public static function flattenToDotArray(array $data): array
|
||||
{
|
||||
$collection = new Collection($data);
|
||||
|
||||
return $collection->dot()->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user