From 1939077582e8b1f191dedb6df366362279fe189f Mon Sep 17 00:00:00 2001 From: clementtalleu Date: Wed, 5 Jun 2024 23:49:46 +0200 Subject: [PATCH 1/2] fix schema + client env var --- src/Client/RedisClient.php | 2 +- src/Command/GenerateSchema.php | 68 ++++++++++++---------------------- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/Client/RedisClient.php b/src/Client/RedisClient.php index 41f147c..5372e3c 100644 --- a/src/Client/RedisClient.php +++ b/src/Client/RedisClient.php @@ -12,7 +12,7 @@ class RedisClient implements RedisClientInterface { public function __construct(protected ?\Redis $redis = null) { - $this->redis = $redis ?? new \Redis($_SERVER['REDIS_HOST'] ? ['host' => $_SERVER['REDIS_HOST']] : null); + $this->redis = $redis ?? new \Redis(array_key_exists('REDIS_HOST', $_SERVER) ? ['host' => $_SERVER['REDIS_HOST']] : null); } public function hMSet(string $key, array $data): void diff --git a/src/Command/GenerateSchema.php b/src/Command/GenerateSchema.php index 87bfe5b..b7b1479 100644 --- a/src/Command/GenerateSchema.php +++ b/src/Command/GenerateSchema.php @@ -24,16 +24,16 @@ public static function generateSchema(string $dir): void } foreach ($phpFiles as $phpFile) { - $fqcn = static::getFQCNFromFile($phpFile); + + $namespace = static::getNamespace($phpFile); + $class = static::getClass($phpFile); + $fqcn = $namespace.'\\'.$class; + if (!$fqcn) { continue; } - try { - $reflectionClass = new \ReflectionClass($fqcn); - } catch (\Exception $e) { - continue; - } + $reflectionClass = new \ReflectionClass($fqcn); $attributes = $reflectionClass->getAttributes(Entity::class); if ($attributes === []) { @@ -49,53 +49,31 @@ public static function generateSchema(string $dir): void } } - protected static function getFQCNFromFile(string $filePath): ?string + protected static function getNamespace(string $filePath): ?string { - $tokens = token_get_all(file_get_contents($filePath)); - $count = count($tokens); - - $nextTokenNs = false; - $namespace = ''; - for ($i = 0; $i < $count; $i++) { - - if (!is_array($tokens[$i])) { - continue; - } - - if ($tokens[$i][1] === 'namespace') { - $nextTokenNs = true; - continue; - } - - if ($nextTokenNs && !ctype_space($tokens[$i][1])) { - $namespace = $tokens[$i][1]; - break; - } + if (!file_exists($filePath)) { + return null; } - $nextTokenClass = false; - $className = null; - for ($i = 0; $i < $count; $i++) { - - if (!is_array($tokens[$i])) { - continue; - } + $src = file_get_contents($filePath); + if (preg_match('#^namespace\s+(.+?);$#sm', $src, $m)) { + return $m[1]; + } - if ($tokens[$i][1] === 'class') { - $nextTokenClass = true; - continue; - } + return null; + } - if ($nextTokenClass && !ctype_space($tokens[$i][1])) { - $className = $tokens[$i][1]; - break; - } + protected static function getClass(string $filePath): ?string + { + if (!file_exists($filePath)) { + return null; } - if (!$className) { - return null; + $src = file_get_contents($filePath); + if (preg_match('/\bclass\s+(\w+)\s*[^{]/', $src, $matches)) { + return $matches[1]; } - return $namespace.'\\'.$className; + return null; } } From b3d8a83bc50e67a70866afd713be93d951a24ee8 Mon Sep 17 00:00:00 2001 From: clementtalleu Date: Wed, 5 Jun 2024 23:53:13 +0200 Subject: [PATCH 2/2] manage exception generate schema --- src/Command/GenerateSchema.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Command/GenerateSchema.php b/src/Command/GenerateSchema.php index b7b1479..df6844f 100644 --- a/src/Command/GenerateSchema.php +++ b/src/Command/GenerateSchema.php @@ -33,7 +33,11 @@ public static function generateSchema(string $dir): void continue; } - $reflectionClass = new \ReflectionClass($fqcn); + try { + $reflectionClass = new \ReflectionClass($fqcn); + } catch (\ReflectionException $e) { + continue; + } $attributes = $reflectionClass->getAttributes(Entity::class); if ($attributes === []) {