<?php require_once __DIR__ . '/StreamReader.php'; require_once __DIR__ . '/PackStreamManager.php'; class PackContext { private PackStreamManager $manager; private string $packFile; private int $offset; private int $depth; private Closure $sizeResolver; private Closure $streamResolver; public function __construct( PackStreamManager $manager, string $packFile, int $offset, int $depth, Closure $sizeResolver, Closure $streamResolver ) { $this->manager = $manager; $this->packFile = $packFile; $this->offset = $offset; $this->depth = $depth; $this->sizeResolver = $sizeResolver; $this->streamResolver = $streamResolver; } public function deriveOffsetContext( int $negativeOffset ): self { return new self( $this->manager, $this->packFile, $this->offset - $negativeOffset, $this->depth, $this->sizeResolver, $this->streamResolver ); } public function computeInt( callable $callback, int $default ): int { return $this->manager->computeInt( $this->packFile, function( StreamReader $stream ) use ( $callback ): int { return $callback( $stream, $this->offset ); }, $default ); } public function computeIntDedicated( callable $callback, int $default ): int { return $this->manager->computeIntDedicated( $this->packFile, function( StreamReader $stream ) use ( $callback ): int { return $callback( $stream, $this->offset ); }, $default ); } public function computeStringDedicated( callable $callback, string $default ): string { return $this->manager->computeStringDedicated( $this->packFile, function( StreamReader $stream ) use ( $callback ): string { return $callback( $stream, $this->offset ); }, $default ); } public function computeArray( callable $callback, array $default ): array { return $this->manager->computeArray( $this->packFile, function( StreamReader $stream ) use ( $callback ): array { return $callback( $stream, $this->offset ); }, $default ); } public function streamGenerator( callable $callback ): Generator { yield from $this->manager->streamGenerator( $this->packFile, function( StreamReader $stream ) use ( $callback ): Generator { yield from $callback( $stream, $this->offset ); } ); } public function streamGeneratorDedicated( callable $callback ): Generator { yield from $this->manager->streamGeneratorDedicated( $this->packFile, function( StreamReader $stream ) use ( $callback ): Generator { yield from $callback( $stream, $this->offset ); } ); } public function resolveBaseSize( string $sha ): int { return ($this->sizeResolver)( $sha ); } public function resolveBaseStream( string $sha ): Generator { yield from ($this->streamResolver)( $sha, $this->depth + 1 ); } public function isWithinDepth( int $maxDepth ): bool { return $this->depth < $maxDepth; } }