Skip to content

Commit

Permalink
Ensure protected/private lastCompiled can be accessed by ScoutViewEng…
Browse files Browse the repository at this point in the history
…ineDecorator
  • Loading branch information
asgrim committed Aug 24, 2023
1 parent b688956 commit 5a91348
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
11 changes: 9 additions & 2 deletions src/Laravel/View/Engine/ScoutViewEngineDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Scoutapm\Laravel\View\Engine;

use Closure;
use Illuminate\Contracts\View\Engine;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\View\Factory;
Expand Down Expand Up @@ -57,9 +58,15 @@ public function __construct(Engine $engine, ScoutApmAgent $agent, Factory $viewF

/**
* @psalm-suppress MixedAssignment
* @psalm-suppress NoInterfaceProperties
* @psalm-suppress PossiblyInvalidFunctionCall
*/
$this->lastCompiled = &$engine->lastCompiled;
$this->lastCompiled = & Closure::bind(
function & () {
return $this->lastCompiled;
},
$engine,
$engine
)->__invoke();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@

class EngineImplementationWithGetCompilerMethod implements Engine
{
/** @var list<non-empty-string> */
protected $lastCompiled = [];

/** @inheritDoc */
public function get($path, array $data = [])
{
return '';
}

/** @param list<non-empty-string> $newValue */
public function setLastCompiled(array $newValue): void
{
$this->lastCompiled = $newValue;
}

public function getCompiler(): CompilerInterface
{
return new class implements CompilerInterface {
Expand Down
17 changes: 5 additions & 12 deletions tests/Unit/Laravel/View/Engine/ScoutViewEngineDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,17 @@ public function testSpatieLaravelIgnitionCompatibility(): void
/** @throws ReflectionException */
public function testDecoratorLastCompiledPropertyReferencesCompilerEngineLastCompiledPropertyWhenUsingReflection(): void
{
/**
* @noinspection PhpPossiblePolymorphicInvocationInspection
* @psalm-suppress NoInterfaceProperties
*/
$this->realEngine->lastCompiled = ['a', 'b'];
$realEngine = new EngineImplementationWithGetCompilerMethod();
$realEngine->setLastCompiled(['a', 'b']);

$this->viewEngineDecorator = new ScoutViewEngineDecorator($this->realEngine, $this->agent, $this->viewFactory);
$this->viewEngineDecorator = new ScoutViewEngineDecorator($realEngine, $this->agent, $this->viewFactory);

$prop = new ReflectionProperty($this->viewEngineDecorator, 'lastCompiled');
$prop->setAccessible(true);
self::assertSame(['a', 'b'], $prop->getValue($this->viewEngineDecorator));

/**
* @noinspection PhpPossiblePolymorphicInvocationInspection
* @psalm-suppress NoInterfaceProperties
*/
$this->realEngine->lastCompiled = ['a', 'b', 'c'];

// Make sure the value can be changed at runtime, and the decorator's value is also changed
$realEngine->setLastCompiled(['a', 'b', 'c']);
self::assertSame(['a', 'b', 'c'], $prop->getValue($this->viewEngineDecorator));
}
}

0 comments on commit 5a91348

Please sign in to comment.