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..df6844f 100644 --- a/src/Command/GenerateSchema.php +++ b/src/Command/GenerateSchema.php @@ -24,14 +24,18 @@ 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) { + } catch (\ReflectionException $e) { continue; } @@ -49,53 +53,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; } }