mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 10:26:32 +00:00
Make Environment::getGlobals() private
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
# 4.0.0 (2024-XX-XX)
|
# 4.0.0 (2024-XX-XX)
|
||||||
|
|
||||||
|
* Make `Environment::getGlobals()` private
|
||||||
* Drop support for PHP < 8.2
|
* Drop support for PHP < 8.2
|
||||||
|
|||||||
+1
-3
@@ -792,11 +792,9 @@ class Environment
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function getGlobals(): array
|
private function getGlobals(): array
|
||||||
{
|
{
|
||||||
if ($this->extensionSet->isInitialized()) {
|
if ($this->extensionSet->isInitialized()) {
|
||||||
if (null === $this->resolvedGlobals) {
|
if (null === $this->resolvedGlobals) {
|
||||||
|
|||||||
+21
-17
@@ -62,45 +62,47 @@ class EnvironmentTest extends TestCase
|
|||||||
$loader = $this->createMock(LoaderInterface::class);
|
$loader = $this->createMock(LoaderInterface::class);
|
||||||
$loader->expects($this->any())->method('getSourceContext')->willReturn(new Source('', ''));
|
$loader->expects($this->any())->method('getSourceContext')->willReturn(new Source('', ''));
|
||||||
|
|
||||||
|
$getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals');
|
||||||
|
|
||||||
// globals can be added after calling getGlobals
|
// globals can be added after calling getGlobals
|
||||||
$twig = new Environment($loader);
|
$twig = new Environment($loader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->addGlobal('foo', 'bar');
|
$twig->addGlobal('foo', 'bar');
|
||||||
$globals = $twig->getGlobals();
|
$globals = $getGlobals->invoke($twig);
|
||||||
$this->assertEquals('bar', $globals['foo']);
|
$this->assertEquals('bar', $globals['foo']);
|
||||||
|
|
||||||
// globals can be modified after a template has been loaded
|
// globals can be modified after a template has been loaded
|
||||||
$twig = new Environment($loader);
|
$twig = new Environment($loader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->load('index');
|
$twig->load('index');
|
||||||
$twig->addGlobal('foo', 'bar');
|
$twig->addGlobal('foo', 'bar');
|
||||||
$globals = $twig->getGlobals();
|
$globals = $getGlobals->invoke($twig);
|
||||||
$this->assertEquals('bar', $globals['foo']);
|
$this->assertEquals('bar', $globals['foo']);
|
||||||
|
|
||||||
// globals can be modified after extensions init
|
// globals can be modified after extensions init
|
||||||
$twig = new Environment($loader);
|
$twig = new Environment($loader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->getFunctions();
|
$twig->getFunctions();
|
||||||
$twig->addGlobal('foo', 'bar');
|
$twig->addGlobal('foo', 'bar');
|
||||||
$globals = $twig->getGlobals();
|
$globals = $getGlobals->invoke($twig);
|
||||||
$this->assertEquals('bar', $globals['foo']);
|
$this->assertEquals('bar', $globals['foo']);
|
||||||
|
|
||||||
// globals can be modified after extensions and a template has been loaded
|
// globals can be modified after extensions and a template has been loaded
|
||||||
$arrayLoader = new ArrayLoader(['index' => '{{foo}}']);
|
$arrayLoader = new ArrayLoader(['index' => '{{foo}}']);
|
||||||
$twig = new Environment($arrayLoader);
|
$twig = new Environment($arrayLoader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->getFunctions();
|
$twig->getFunctions();
|
||||||
$twig->load('index');
|
$twig->load('index');
|
||||||
$twig->addGlobal('foo', 'bar');
|
$twig->addGlobal('foo', 'bar');
|
||||||
$globals = $twig->getGlobals();
|
$globals = $getGlobals->invoke($twig);
|
||||||
$this->assertEquals('bar', $globals['foo']);
|
$this->assertEquals('bar', $globals['foo']);
|
||||||
|
|
||||||
$twig = new Environment($arrayLoader);
|
$twig = new Environment($arrayLoader);
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->addGlobal('foo', 'bar');
|
$twig->addGlobal('foo', 'bar');
|
||||||
$template = $twig->load('index');
|
$template = $twig->load('index');
|
||||||
$this->assertEquals('bar', $template->render([]));
|
$this->assertEquals('bar', $template->render([]));
|
||||||
@@ -108,38 +110,38 @@ class EnvironmentTest extends TestCase
|
|||||||
// globals cannot be added after a template has been loaded
|
// globals cannot be added after a template has been loaded
|
||||||
$twig = new Environment($loader);
|
$twig = new Environment($loader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->load('index');
|
$twig->load('index');
|
||||||
try {
|
try {
|
||||||
$twig->addGlobal('bar', 'bar');
|
$twig->addGlobal('bar', 'bar');
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (\LogicException $e) {
|
} catch (\LogicException $e) {
|
||||||
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
|
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
|
||||||
}
|
}
|
||||||
|
|
||||||
// globals cannot be added after extensions init
|
// globals cannot be added after extensions init
|
||||||
$twig = new Environment($loader);
|
$twig = new Environment($loader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->getFunctions();
|
$twig->getFunctions();
|
||||||
try {
|
try {
|
||||||
$twig->addGlobal('bar', 'bar');
|
$twig->addGlobal('bar', 'bar');
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (\LogicException $e) {
|
} 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
|
// globals cannot be added after extensions and a template has been loaded
|
||||||
$twig = new Environment($loader);
|
$twig = new Environment($loader);
|
||||||
$twig->addGlobal('foo', 'foo');
|
$twig->addGlobal('foo', 'foo');
|
||||||
$twig->getGlobals();
|
$getGlobals->invoke($twig);
|
||||||
$twig->getFunctions();
|
$twig->getFunctions();
|
||||||
$twig->load('index');
|
$twig->load('index');
|
||||||
try {
|
try {
|
||||||
$twig->addGlobal('bar', 'bar');
|
$twig->addGlobal('bar', 'bar');
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (\LogicException $e) {
|
} 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
|
// test adding globals after a template has been loaded without call to getGlobals
|
||||||
@@ -149,7 +151,7 @@ class EnvironmentTest extends TestCase
|
|||||||
$twig->addGlobal('bar', 'bar');
|
$twig->addGlobal('bar', 'bar');
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (\LogicException $e) {
|
} catch (\LogicException $e) {
|
||||||
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
|
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,13 +279,15 @@ class EnvironmentTest extends TestCase
|
|||||||
$twig = new Environment($this->createMock(LoaderInterface::class));
|
$twig = new Environment($this->createMock(LoaderInterface::class));
|
||||||
$twig->addExtension(new EnvironmentTest_Extension());
|
$twig->addExtension(new EnvironmentTest_Extension());
|
||||||
|
|
||||||
|
$getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals');
|
||||||
|
|
||||||
$this->assertArrayHasKey('test', $twig->getTokenParsers());
|
$this->assertArrayHasKey('test', $twig->getTokenParsers());
|
||||||
$this->assertArrayHasKey('foo_filter', $twig->getFilters());
|
$this->assertArrayHasKey('foo_filter', $twig->getFilters());
|
||||||
$this->assertArrayHasKey('foo_function', $twig->getFunctions());
|
$this->assertArrayHasKey('foo_function', $twig->getFunctions());
|
||||||
$this->assertArrayHasKey('foo_test', $twig->getTests());
|
$this->assertArrayHasKey('foo_test', $twig->getTests());
|
||||||
$this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators());
|
$this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators());
|
||||||
$this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators());
|
$this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators());
|
||||||
$this->assertArrayHasKey('foo_global', $twig->getGlobals());
|
$this->assertArrayHasKey('foo_global', $getGlobals->invoke($twig));
|
||||||
$visitors = $twig->getNodeVisitors();
|
$visitors = $twig->getNodeVisitors();
|
||||||
$found = false;
|
$found = false;
|
||||||
foreach ($visitors as $visitor) {
|
foreach ($visitors as $visitor) {
|
||||||
|
|||||||
Reference in New Issue
Block a user