Skip to content

Commit

Permalink
Merge pull request #15 from clementtalleu/fix/redis_client
Browse files Browse the repository at this point in the history
fix schema + client env var
  • Loading branch information
clementtalleu authored Jun 5, 2024
2 parents 810e51d + b3d8a83 commit 6efddbf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 23 additions & 41 deletions src/Command/GenerateSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}

0 comments on commit 6efddbf

Please sign in to comment.