Skip to content

Commit 415f8dd

Browse files
authored
Fixed Discoverer file processing for PHAR (#94)
1 parent fcf605d commit 415f8dd

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

src/Capability/Discovery/Discoverer.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,9 @@ public function discover(string $basePath, array $directories, array $excludeDir
143143
*/
144144
private function processFile(SplFileInfo $file, array &$discoveredCount, array &$tools, array &$resources, array &$prompts, array &$resourceTemplates): void
145145
{
146-
$filePath = $file->getRealPath();
147-
if (false === $filePath) {
148-
$this->logger->warning('Could not get real path for file', ['path' => $file->getPathname()]);
149-
150-
return;
151-
}
152-
153-
$className = $this->getClassFromFile($filePath);
146+
$className = $this->getClassFromFile($file);
154147
if (!$className) {
155-
$this->logger->warning('No valid class found in file', ['file' => $filePath]);
148+
$this->logger->warning('No valid class found in file', ['file' => $file->getPathname()]);
156149

157150
return;
158151
}
@@ -199,10 +192,10 @@ private function processFile(SplFileInfo $file, array &$discoveredCount, array &
199192
}
200193
}
201194
} catch (\ReflectionException $e) {
202-
$this->logger->error('Reflection error processing file for MCP discovery', ['file' => $filePath, 'class' => $className, 'exception' => $e->getMessage()]);
195+
$this->logger->error('Reflection error processing file for MCP discovery', ['file' => $file->getPathname(), 'class' => $className, 'exception' => $e->getMessage()]);
203196
} catch (\Throwable $e) {
204197
$this->logger->error('Unexpected error processing file for MCP discovery', [
205-
'file' => $filePath,
198+
'file' => $file->getPathname(),
206199
'class' => $className,
207200
'exception' => $e->getMessage(),
208201
'trace' => $e->getTraceAsString(),
@@ -329,34 +322,34 @@ private function getCompletionProviders(\ReflectionMethod $reflectionMethod): ar
329322
* Attempt to determine the FQCN from a PHP file path.
330323
* Uses tokenization to extract namespace and class name.
331324
*
332-
* @param string $filePath absolute path to the PHP file
333-
*
334325
* @return class-string|null the FQCN or null if not found/determinable
335326
*/
336-
private function getClassFromFile(string $filePath): ?string
327+
private function getClassFromFile(SplFileInfo $file): ?string
337328
{
338-
if (!file_exists($filePath) || !is_readable($filePath)) {
339-
$this->logger->warning('File does not exist or is not readable.', ['file' => $filePath]);
329+
$this->logger->debug('Processing file', ['path' => $file->getPathname()]);
330+
331+
try {
332+
$content = $file->getContents();
333+
} catch (\Throwable $e) {
334+
$this->logger->warning("Failed to read file content during class discovery: {$file->getPathname()}", [
335+
'exception' => $e->getMessage(),
336+
]);
340337

341338
return null;
342339
}
343340

344-
try {
345-
$content = file_get_contents($filePath);
346-
if (false === $content) {
347-
$this->logger->warning('Failed to read file content.', ['file' => $filePath]);
348-
349-
return null;
350-
}
351-
if (\strlen($content) > 500 * 1024) {
352-
$this->logger->debug('Skipping large file during class discovery.', ['file' => $filePath]);
341+
if (\strlen($content) > 500 * 1024) {
342+
$this->logger->warning('Skipping large file during class discovery.', ['file' => $file->getPathname()]);
353343

354-
return null;
355-
}
344+
return null;
345+
}
356346

347+
try {
357348
$tokens = token_get_all($content);
358349
} catch (\Throwable $e) {
359-
$this->logger->warning("Failed to read or tokenize file during class discovery: {$filePath}", ['exception' => $e->getMessage()]);
350+
$this->logger->warning("Failed to tokenize file during class discovery: {$file->getPathname()}", [
351+
'exception' => $e->getMessage(),
352+
]);
360353

361354
return null;
362355
}
@@ -427,7 +420,7 @@ private function getClassFromFile(string $filePath): ?string
427420

428421
if (!empty($potentialClasses)) {
429422
if (!class_exists($potentialClasses[0], false)) {
430-
$this->logger->debug('getClassFromFile returning potential non-class type. Are you sure this class has been autoloaded?', ['file' => $filePath, 'type' => $potentialClasses[0]]);
423+
$this->logger->debug('getClassFromFile returning potential non-class type. Are you sure this class has been autoloaded?', ['file' => $file->getPathname(), 'type' => $potentialClasses[0]]);
431424
}
432425

433426
return $potentialClasses[0];

0 commit comments

Comments
 (0)