Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Environment::getGlobals() private #4075

Merged
merged 1 commit into from
Jul 5, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 4.0.0 (2024-XX-XX)

* Make `Environment::getGlobals()` private
* Drop support for PHP < 8.2
4 changes: 1 addition & 3 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,9 @@ public function addGlobal(string $name, $value)
}

/**
* @internal
*
* @return array<string, mixed>
*/
public function getGlobals(): array
private function getGlobals(): array
{
if ($this->extensionSet->isInitialized()) {
if (null === $this->resolvedGlobals) {
Expand Down
38 changes: 21 additions & 17 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,84 +62,86 @@ public function testGlobals()
$loader = $this->createMock(LoaderInterface::class);
$loader->expects($this->any())->method('getSourceContext')->willReturn(new Source('', ''));

$getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals');

// globals can be added after calling getGlobals
$twig = new Environment($loader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->addGlobal('foo', 'bar');
$globals = $twig->getGlobals();
$globals = $getGlobals->invoke($twig);
$this->assertEquals('bar', $globals['foo']);

// globals can be modified after a template has been loaded
$twig = new Environment($loader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->load('index');
$twig->addGlobal('foo', 'bar');
$globals = $twig->getGlobals();
$globals = $getGlobals->invoke($twig);
$this->assertEquals('bar', $globals['foo']);

// globals can be modified after extensions init
$twig = new Environment($loader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->getFunctions();
$twig->addGlobal('foo', 'bar');
$globals = $twig->getGlobals();
$globals = $getGlobals->invoke($twig);
$this->assertEquals('bar', $globals['foo']);

// globals can be modified after extensions and a template has been loaded
$arrayLoader = new ArrayLoader(['index' => '{{foo}}']);
$twig = new Environment($arrayLoader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->getFunctions();
$twig->load('index');
$twig->addGlobal('foo', 'bar');
$globals = $twig->getGlobals();
$globals = $getGlobals->invoke($twig);
$this->assertEquals('bar', $globals['foo']);

$twig = new Environment($arrayLoader);
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->addGlobal('foo', 'bar');
$template = $twig->load('index');
$this->assertEquals('bar', $template->render([]));

// globals cannot be added after a template has been loaded
$twig = new Environment($loader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->load('index');
try {
$twig->addGlobal('bar', 'bar');
$this->fail();
} catch (\LogicException $e) {
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
}

// globals cannot be added after extensions init
$twig = new Environment($loader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->getFunctions();
try {
$twig->addGlobal('bar', 'bar');
$this->fail();
} catch (\LogicException $e) {
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
}

// globals cannot be added after extensions and a template has been loaded
$twig = new Environment($loader);
$twig->addGlobal('foo', 'foo');
$twig->getGlobals();
$getGlobals->invoke($twig);
$twig->getFunctions();
$twig->load('index');
try {
$twig->addGlobal('bar', 'bar');
$this->fail();
} catch (\LogicException $e) {
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
}

// test adding globals after a template has been loaded without call to getGlobals
Expand All @@ -149,7 +151,7 @@ public function testGlobals()
$twig->addGlobal('bar', 'bar');
$this->fail();
} catch (\LogicException $e) {
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
}
}

Expand Down Expand Up @@ -277,13 +279,15 @@ public function testAddExtension()
$twig = new Environment($this->createMock(LoaderInterface::class));
$twig->addExtension(new EnvironmentTest_Extension());

$getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals');

$this->assertArrayHasKey('test', $twig->getTokenParsers());
$this->assertArrayHasKey('foo_filter', $twig->getFilters());
$this->assertArrayHasKey('foo_function', $twig->getFunctions());
$this->assertArrayHasKey('foo_test', $twig->getTests());
$this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators());
$this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators());
$this->assertArrayHasKey('foo_global', $twig->getGlobals());
$this->assertArrayHasKey('foo_global', $getGlobals->invoke($twig));
$visitors = $twig->getNodeVisitors();
$found = false;
foreach ($visitors as $visitor) {
Expand Down
Loading