You've already forked Traefik-Redis-Api
183 lines
4.7 KiB
PHP
183 lines
4.7 KiB
PHP
<?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 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 EntityEnum $entity
|
|
* @param ProtocolEnum $protocol
|
|
* @return bool
|
|
*/
|
|
public 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 function getMiddleware(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
|
{
|
|
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::MIDDLEWARE->getValue() . "/$name/*";
|
|
|
|
return $this->fetchValuesToArray($pattern);
|
|
}
|
|
|
|
/**
|
|
* @param ProtocolEnum $type
|
|
* @return array
|
|
*/
|
|
public function getAllMiddlewares(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
|
{
|
|
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::MIDDLEWARE->getValue() . '/*';
|
|
|
|
return $this->getUniqueKeys($pattern);
|
|
}
|
|
|
|
/**
|
|
* @param ProtocolEnum $type
|
|
* @return array
|
|
*/
|
|
public function getAllServices(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
|
{
|
|
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::SERVICE->getValue() . '/*';
|
|
|
|
return $this->getUniqueKeys($pattern);
|
|
}
|
|
|
|
/**
|
|
* @param string $serviceName
|
|
* @param ProtocolEnum $type
|
|
* @return array
|
|
*/
|
|
public function getService(string $serviceName, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
|
{
|
|
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::SERVICE->getValue() . "/$serviceName/*";
|
|
|
|
return $this->fetchValuesToArray($pattern);
|
|
}
|
|
|
|
/**
|
|
* @param ProtocolEnum $type
|
|
* @return array
|
|
*/
|
|
public function getAllRouters(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
|
{
|
|
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::ROUTER->getValue() . '/*';
|
|
|
|
return $this->getUniqueKeys($pattern);
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param ProtocolEnum $type
|
|
* @return array
|
|
*/
|
|
public function getRouter(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
|
{
|
|
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::ROUTER->getValue() . "/$name/*";
|
|
|
|
return $this->fetchValuesToArray($pattern);
|
|
}
|
|
|
|
/**
|
|
* @param string $pattern
|
|
* @return array
|
|
*/
|
|
private function getUniqueKeys(string $pattern): array
|
|
{
|
|
$values = new Collection();
|
|
$redis = Redis::getFacadeRoot();
|
|
|
|
foreach (new Keyspace($redis, $pattern) as $key) {
|
|
$parts = explode('/', $key);
|
|
if (isset($parts[3])) {
|
|
$values->push($parts[3]);
|
|
}
|
|
}
|
|
|
|
return $values->unique()->values()->sort()->values()->toArray();
|
|
}
|
|
|
|
/**
|
|
* @param string $pattern
|
|
* @return array
|
|
*/
|
|
private 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 function flattenToDotArray(array $data): array
|
|
{
|
|
$collection = new Collection($data);
|
|
|
|
return $collection->dot()->toArray();
|
|
}
|
|
}
|