Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
This commit is contained in:
131
src/Framework/Discovery/Processing/ProcessingContext.php
Normal file
131
src/Framework/Discovery/Processing/ProcessingContext.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Discovery\Processing;
|
||||
|
||||
use App\Framework\Core\ValueObjects\ClassName;
|
||||
use App\Framework\Discovery\ValueObjects\FileContext;
|
||||
use App\Framework\Filesystem\File;
|
||||
use App\Framework\Reflection\ReflectionProvider;
|
||||
use App\Framework\Reflection\WrappedReflectionClass;
|
||||
|
||||
/**
|
||||
* Shared context for processing files during discovery
|
||||
*
|
||||
* This context holds reflection instances and other shared data
|
||||
* to avoid duplicate work between visitors
|
||||
*/
|
||||
final class ProcessingContext
|
||||
{
|
||||
private ?WrappedReflectionClass $currentReflection = null;
|
||||
|
||||
private ?ClassName $currentClassName = null;
|
||||
|
||||
private ?FileContext $currentFileContext = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly ReflectionProvider $reflectionProvider
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current file being processed
|
||||
*/
|
||||
public function setCurrentFile(FileContext $fileContext): void
|
||||
{
|
||||
$this->currentFileContext = $fileContext;
|
||||
// Clear reflection cache when switching files
|
||||
$this->currentReflection = null;
|
||||
$this->currentClassName = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get reflection for a class (cached within the same file)
|
||||
*/
|
||||
public function getReflection(ClassName $className): ?WrappedReflectionClass
|
||||
{
|
||||
// Return cached reflection if it's for the same class
|
||||
if ($this->currentClassName !== null &&
|
||||
$this->currentClassName->equals($className) &&
|
||||
$this->currentReflection !== null) {
|
||||
return $this->currentReflection;
|
||||
}
|
||||
|
||||
// Clear previous reflection
|
||||
if ($this->currentClassName !== null) {
|
||||
$this->reflectionProvider->forget($this->currentClassName);
|
||||
}
|
||||
|
||||
// Get new reflection
|
||||
try {
|
||||
if ($className->exists()) {
|
||||
$this->currentReflection = $this->reflectionProvider->getClass($className);
|
||||
$this->currentClassName = $className;
|
||||
|
||||
return $this->currentReflection;
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
// Class can't be reflected
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current file context
|
||||
*/
|
||||
public function getCurrentFileContext(): ?FileContext
|
||||
{
|
||||
return $this->currentFileContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up resources for the current file
|
||||
*/
|
||||
public function cleanup(): void
|
||||
{
|
||||
if ($this->currentClassName !== null) {
|
||||
$this->reflectionProvider->forget($this->currentClassName);
|
||||
}
|
||||
|
||||
$this->currentReflection = null;
|
||||
$this->currentClassName = null;
|
||||
$this->currentFileContext = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force garbage collection if needed
|
||||
*/
|
||||
public function maybeCollectGarbage(int $processedFiles): void
|
||||
{
|
||||
if ($processedFiles % 50 === 0) {
|
||||
gc_collect_cycles();
|
||||
|
||||
// Clear reflection cache less frequently
|
||||
if ($processedFiles % 100 === 0) {
|
||||
$this->reflectionProvider->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all caches for memory management
|
||||
*/
|
||||
public function clearCaches(): void
|
||||
{
|
||||
if ($this->currentClassName !== null) {
|
||||
$this->reflectionProvider->forget($this->currentClassName);
|
||||
}
|
||||
|
||||
// Flush the entire reflection provider cache
|
||||
$this->reflectionProvider->flush();
|
||||
|
||||
$this->currentReflection = null;
|
||||
$this->currentClassName = null;
|
||||
$this->currentFileContext = null;
|
||||
|
||||
// Force garbage collection
|
||||
gc_collect_cycles();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user