Skip to content
Open
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
6 changes: 0 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
fail-fast: false
matrix:
symfony-version:
- '^5.4'
- '^6.4'
- '^7.2'
php-version:
Expand All @@ -45,11 +44,6 @@ jobs:
tools: 'flex'
ini-values: "zend.assertions=1"

- name: 'Remove sensio/framework-extra-bundle'
if: matrix.symfony-version == '^7.2'
run: |
composer remove --dev --no-update sensio/framework-extra-bundle

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.4"
- '8.4'

steps:
- name: "Checkout"
Expand Down
32 changes: 6 additions & 26 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,22 @@
use JMS\TranslationBundle\Util\FileUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Attribute\Route;

/**
* @author Johannes M. Schmitt <[email protected]>
*
* @Route("/api")
*/
#[Route('/api')]
class ApiController
{
private ConfigFactory $configFactory;

private Updater $updater;

public function __construct(ConfigFactory $configFactory, Updater $updater)
{
$this->configFactory = $configFactory;
$this->updater = $updater;
public function __construct(
private ConfigFactory $configFactory,
private Updater $updater,
) {
}

/**
* @param Request $request
* @param string $config
* @param string $domain
* @param string $locale
*
* @return Response
*
* @Route("/configs/{config}/domains/{domain}/locales/{locale}/messages",
* methods={"PUT"},
* name="jms_translation_update_message",
* defaults = {"id" = null},
* options = {"i18n" = false})
*/
#[Route('/configs/{config}/domains/{domain}/locales/{locale}/messages', name: 'jms_translation_update_message', methods: [Request::METHOD_PUT], defaults: ['id' => null], options: ['i18n' => false])]
public function updateMessageAction(Request $request, $config, $domain, $locale)
public function updateMessageAction(Request $request, string $config, string $domain, string $locale): Response
{
$id = $request->query->get('id');

Expand Down
46 changes: 12 additions & 34 deletions Controller/TranslateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
use JMS\TranslationBundle\Translation\ConfigFactory;
use JMS\TranslationBundle\Translation\LoaderManager;
use JMS\TranslationBundle\Util\FileUtils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Attribute\Route;
use Twig\Environment;

/**
Expand All @@ -37,39 +36,24 @@
*/
class TranslateController
{
private ConfigFactory $configFactory;

private LoaderManager $loader;

private Environment|null $twig;

private string|null $sourceLanguage = null;

public function __construct(ConfigFactory $configFactory, LoaderManager $loader, ?Environment $twig = null)
{
$this->configFactory = $configFactory;
$this->loader = $loader;
$this->twig = $twig;
public function __construct(
private ConfigFactory $configFactory,
private LoaderManager $loader,
private Environment $twig,
) {
}

/**
* @param string $lang
*/
public function setSourceLanguage($lang)
public function setSourceLanguage(string $lang): static
{
$this->sourceLanguage = $lang;

return $this;
}

/**
* @param Request $request
*
* @return Response|array
*
* @Route("/", name="jms_translation_index", options = {"i18n" = false})
* @Template("@JMSTranslation/Translate/index.html.twig")
*/
#[Route('/', name: 'jms_translation_index', options: ['i18n' => false])]
public function indexAction(Request $request)
public function indexAction(Request $request): Response
{
$configs = $this->configFactory->getNames();
$config = $request->query->get('config') ?: reset($configs);
Expand Down Expand Up @@ -133,7 +117,7 @@ public function indexAction(Request $request)
$existingMessages[$id] = $message;
}

$variables = [
return new Response($this->twig->render('@JMSTranslation/Translate/index.html.twig', [
'selectedConfig' => $config,
'configs' => $configs,
'selectedDomain' => $domain,
Expand All @@ -147,12 +131,6 @@ public function indexAction(Request $request)
'isWriteable' => is_writable((string) $files[$domain][$locale][1]),
'file' => (string) $files[$domain][$locale][1],
'sourceLanguage' => $this->sourceLanguage,
];

if (null !== $this->twig) {
return new Response($this->twig->render('@JMSTranslation/Translate/index.html.twig', $variables));
}

return $variables;
]));
}
}
69 changes: 30 additions & 39 deletions Tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,68 +20,56 @@

namespace JMS\TranslationBundle\Tests\Functional;

use JMS\TranslationBundle\Exception\RuntimeException;
use JMS\TranslationBundle\JMSTranslationBundle;
use JMS\TranslationBundle\Tests\Functional\Fixture\TestBundle\TestBundle;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class AppKernel extends Kernel
{
private string|null $config;
use MicroKernelTrait;

private string $fwConfig;

public function __construct(string $fwConfig, ?string $config)
{
public function __construct(
private string $frameworkConfig,
private string|null $config = null
) {
parent::__construct('test', true);

$fs = new Filesystem();
if ($config) {
if (!$fs->isAbsolutePath($config)) {
$config = __DIR__ . '/config/' . $config;
}

if (!file_exists($config)) {
throw new RuntimeException(sprintf('The config file "%s" does not exist.', $config));
}
}
$this->config = $config;

if (!$fs->isAbsolutePath($fwConfig)) {
$fwConfig = __DIR__ . '/config/' . $fwConfig;
}
$this->fwConfig = $fwConfig;
}

public function registerBundles(): iterable
{
$bundles = [
return [
new TestBundle(),
new FrameworkBundle(),
new TwigBundle(),
new JMSTranslationBundle(),
];
}

private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
{
$configDir = $this->getConfigDir();

if (class_exists(SensioFrameworkExtraBundle::class)) {
$bundles[] = new SensioFrameworkExtraBundle();
$container->import($configDir . '/' . $this->frameworkConfig);

if (null !== $this->config) {
$loader->load($configDir . '/' . $this->config);
}

return $bundles;
$container->import($configDir . '/services.yaml');
}

public function registerContainerConfiguration(LoaderInterface $loader): void
private function configureRoutes(RoutingConfigurator $routes): void
{
$loader->load($this->fwConfig);
if ($this->config) {
$loader->load($this->config);
}
$configDir = $this->getConfigDir();

$loader->load($this->getProjectDir() . '/config/services.yaml');
$routes->import($configDir . '/routes.yaml');
}

public function getCacheDir(): string
Expand All @@ -104,13 +92,16 @@ private function getBaseDir(): string
return sys_get_temp_dir() . '/JMSTranslationBundle';
}

public function serialize()
public function __serialize(): array
{
return $this->config;
return [
'framework_config' => $this->frameworkConfig,
'config' => $this->config,
];
}

public function unserialize($config)
public function __unserialize(array $data): void
{
$this->__construct($config);
$this->__construct($data['framework_config'], $data['config']);
}
}
12 changes: 5 additions & 7 deletions Tests/Functional/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ class BaseTestCase extends WebTestCase
{
protected static function createKernel(array $options = []): KernelInterface
{
if (version_compare(Kernel::VERSION, '7.0.0') >= 0) {
$conf = 'framework_sf7.yaml';
} elseif (version_compare(Kernel::VERSION, '6.0.0') >= 0) {
$conf = 'framework_sf6.yml';
} else {
$conf = 'framework.yml';
$conf = 'framework.yaml';

if (version_compare(Kernel::VERSION, '7.0.0', '<')) {
$conf = 'framework_sf6.yaml';
}

return new AppKernel($conf, $options['config'] ?? 'default.yml');
return new AppKernel($conf, $options['config'] ?? 'default.yaml');
}
}
37 changes: 34 additions & 3 deletions Tests/Functional/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ class ApiControllerTest extends BaseTestCase
public function testUpdateAction(): void
{
// Start application
$client = static::createClient();
$outputDir = $client->getContainer()->getParameter('translation_output_dir');
$client = static::createClient();
$container = $client->getContainer();
$outputDir = $container->getParameter('translation_output_dir');

// Add a file
$file = $outputDir . '/navigation.en.yaml';
$written = file_put_contents($file, 'main.home: Home');
$this->assertTrue($written !== false && $written > 0);

$client->request('POST', '/_trans/api/configs/app/domains/navigation/locales/en/messages?id=main.home', ['_method' => 'PUT', 'message' => 'Away']);
$client->request('PUT', '/_trans/api/configs/app/domains/navigation/locales/en/messages?id=main.home', ['message' => 'Away']);

$fileContent = is_file($file) ? file_get_contents($file) : '';
unlink($file);
Expand All @@ -35,4 +36,34 @@ public function testUpdateAction(): void
$this->assertTrue(isset($array['main.home']), print_r($array, true));
$this->assertEquals('Away', $array['main.home']);
}

public function testUpdateWithHttpOverrideAction(): void
{
// Start application
$client = static::createClient();
$container = $client->getContainer();

if (!$container->getParameter('kernel.http_method_override')) {
$this->markTestSkipped('This test does not work with http method override disabled.');
}

$outputDir = $container->getParameter('translation_output_dir');

// Add a file
$file = $outputDir . '/navigation.en.yaml';
$written = file_put_contents($file, 'main.home: Home');
$this->assertTrue($written !== false && $written > 0);

$client->request('POST', '/_trans/api/configs/app/domains/navigation/locales/en/messages?id=main.home', ['_method' => 'PUT', 'message' => 'Back']);

$fileContent = is_file($file) ? file_get_contents($file) : '';
unlink($file);
$this->assertEquals(200, $client->getResponse()->getStatusCode());

// Verify that the file has new content
$array = Yaml::parse($fileContent);

$this->assertTrue(isset($array['main.home']), print_r($array, true));
$this->assertEquals('Back', $array['main.home']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Attribute\Route;

/**
* @author Johannes
Expand Down
10 changes: 10 additions & 0 deletions Tests/Functional/config/bundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jms_translation:
configs:
app:
dirs:
- '%kernel.project_dir%'
- '%kernel.project_dir%/Fixture/TestBundle'
output_dir: '%translation_output_dir%'
ignored_domains: ['routes']
excluded_names: ['*TestCase.php', '*Test.php']
excluded_dirs: ['cache', 'data', 'logs']
10 changes: 0 additions & 10 deletions Tests/Functional/config/bundle.yml

This file was deleted.

3 changes: 3 additions & 0 deletions Tests/Functional/config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
imports:
- { resource: 'twig.yaml' }
- { resource: 'bundle.yaml' }
3 changes: 0 additions & 3 deletions Tests/Functional/config/default.yml

This file was deleted.

3 changes: 0 additions & 3 deletions Tests/Functional/config/default_sf5.yml

This file was deleted.

Loading
Loading