Skip to content

Commit

Permalink
[TwigBridge] Add AppVariable::getEnabledLocales() to retrieve the e…
Browse files Browse the repository at this point in the history
…nabled locales
  • Loading branch information
jmsche committed May 15, 2023
1 parent eb3e308 commit f4f39d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AppVariable
private string $environment;
private bool $debug;
private LocaleSwitcher $localeSwitcher;
private array $enabledLocales;

/**
* @return void
Expand Down Expand Up @@ -69,6 +70,11 @@ public function setLocaleSwitcher(LocaleSwitcher $localeSwitcher): void
$this->localeSwitcher = $localeSwitcher;
}

public function setEnabledLocales(array $enabledLocales): void
{
$this->enabledLocales = $enabledLocales;
}

/**
* Returns the current token.
*
Expand Down Expand Up @@ -155,6 +161,15 @@ public function getLocale(): string
return $this->localeSwitcher->getLocale();
}

public function getEnabled_locales(): array
{
if (!isset($this->enabledLocales)) {
throw new \RuntimeException('The "app.enabled_locales" variable is not available.');
}

return $this->enabledLocales;
}

/**
* Returns some or all the existing flash messages:
* * getFlashes() returns all the flash messages
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales

6.3
---

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public function testGetLocale()
self::assertEquals('fr', $this->appVariable->getLocale());
}

public function testGetEnabledLocales()
{
$this->appVariable->setEnabledLocales(['en', 'fr']);

self::assertSame(['en', 'fr'], $this->appVariable->getEnabled_locales());
}

public function testGetTokenWithNoToken()
{
$tokenStorage = $this->createMock(TokenStorageInterface::class);
Expand Down Expand Up @@ -174,6 +181,13 @@ public function testGetLocaleWithLocaleSwitcherNotSet()
$this->appVariable->getLocale();
}

public function testGetEnabledLocalesWithEnabledLocalesNotSet()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The "app.enabled_locales" variable is not available.');
$this->appVariable->getEnabled_locales();
}

public function testGetFlashesWithNoRequest()
{
$this->setRequestStack(null);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()])
->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])
->call('setLocaleSwitcher', [service('translation.locale_switcher')->ignoreOnInvalid()])
->call('setEnabledLocales', [param('kernel.enabled_locales')])

->set('twig.template_iterator', TemplateIterator::class)
->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path'), abstract_arg('File name pattern')])
Expand Down

0 comments on commit f4f39d2

Please sign in to comment.