Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Instrument/ClassLoading/CachePathManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public function __construct(protected readonly AspectKernel $kernel)
$this->cacheStateLoaded = true;
}
}

// Flush pending cache records while the runtime environment is still fully
// intact: object destruction order during shutdown is unspecified, so relying
// on __destruct() alone can run the write after collaborators are torn down
register_shutdown_function($this->flushSilently(...));
}

/**
Expand Down Expand Up @@ -302,11 +307,28 @@ public function setCacheState(string $resource, array $metadata): void
/**
* Automatic destructor saves all new changes into the cache
*
* Safety net for managers released before shutdown; the shutdown function
* registered in the constructor has usually flushed already, making this a no-op.
* This implementation is not thread-safe, so be care
*/
public function __destruct()
{
$this->flushCacheState();
$this->flushSilently();
}

/**
* Flushes without ever propagating an error out of shutdown/destruction
*
* Losing one cache write is recoverable (the next request simply re-weaves);
* an exception escaping a destructor or shutdown function is not.
*/
private function flushSilently(): void
{
try {
$this->flushCacheState();
} catch (\Throwable) {
// Deliberately swallowed, see above
}
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Instrument/Transformer/FilterInjectorTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ public static function rewrite(string $originalResource, string $originalDir = '
{
self::ensureConfigured();

static $appDir, $cacheDir, $debug;
if ($appDir === null) {
extract(self::$options, EXTR_IF_EXISTS);
}
$cacheDir = self::$options['cacheDir'];
$debug = self::$options['debug'];

$resource = $originalResource;
if ($resource[0] !== '/') {
Expand Down
20 changes: 15 additions & 5 deletions src/Instrument/Transformer/MagicConstantTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* Transformer that replaces magic __DIR__ and __FILE__ constants in the source code
*
* Additionally, ReflectionClass->getFileName() is also wrapped into normalizer method call
*
* @phpstan-import-type KernelOptions from AspectKernel
*/
class MagicConstantTransformer extends BaseSourceTransformer
{
Expand All @@ -46,8 +48,18 @@ class MagicConstantTransformer extends BaseSourceTransformer
public function __construct(AspectKernel $kernel)
{
parent::__construct($kernel);
self::$rootPath = $this->options['appDir'];
self::$rewriteToPath = $this->options['cacheDir'] ?? '';
self::configurePaths($this->options);
}

/**
* Remembers the path mapping used by resolveFileName()
*
* @phpstan-param KernelOptions $options
*/
private static function configurePaths(array $options): void
{
self::$rootPath = $options['appDir'];
self::$rewriteToPath = $options['cacheDir'] ?? '';
}

/**
Expand All @@ -71,9 +83,7 @@ public function transform(StreamMetaData $metadata): TransformerResultEnum
public static function resolveFileName(string $fileName): string
{
if (self::$rootPath === '') {
$options = AspectKernel::getInstance()->getOptions();
self::$rootPath = $options['appDir'];
self::$rewriteToPath = $options['cacheDir'] ?? '';
self::configurePaths(AspectKernel::getInstance()->getOptions());
}
if (self::$rewriteToPath !== '' && str_starts_with($fileName, self::$rewriteToPath)) {
$fileName = str_replace(self::$rewriteToPath, self::$rootPath, $fileName);
Expand Down