diff --git a/UPGRADE.md b/UPGRADE.md index caee12522..cba340be3 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,3 +1,39 @@ +## UPGRADE FOR `1.9.x` + +### FROM `1.8.x` TO `1.9.x` + +- `translations`: + +New configuration options were introduced to set the default and the defined (enabled) locales used by the default translation locale provider (`sylius.translation_locale_provider.immutable` service): + +```yaml +sylius_resource: + translation: + enabled_locales: [] + default_locale: null +``` + +Before version `1.9.x` the provider was configured with `locale` parameter. +If those options are not configured, for backward compatibility, it will fallback to the `locale` parameter if it's defined. +This is deprecated and will be removed in version 2.0. +If you want to keep using the `locale` parameter starting with version 2.0, then add the following configuration: + +```yaml +sylius_resource: + translation: + enabled_locales: ['%locale%'] + default_locale: '%locale%' +``` + +Starting with version 2.0, the default values will be set from `kernel.default_locale` and `kernel.enabled_locales` parameters. + +```yaml +sylius_resource: + translation: + enabled_locales: ['%kernel.enabled_locales%'] + default_locale: '%kernel.default_locale%' +``` + ## UPGRADE FOR `1.7.x` ### FROM `1.6.x` TO `1.7.x` diff --git a/docs/reference.md b/docs/reference.md index c0ecbf43d..07d77da00 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -2,6 +2,23 @@ ```yaml sylius_resource: + mapping: + paths: ['%kernel.project_dir%/src/Entity'] # Used for Routes with PHP attributes + translation: + enabled: true + enabled_locales: '%kernel.enabled_locales%' + default_locale: '%kernel.default_locale%' + locale_provider: 'sylius.translation_locale_provider.immutable' + settings: + paginate: ~ + limit: ~ + allowed_paginate: [10, 20, 30] + default_page_size: 10 + sortable: false + sorting: ~ + filterable: false + criteria: ~ + state_machine_component: winzou # or symfony resources: app.book: driver: doctrine/orm diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index 1d27c7d34..02836fefb 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -134,6 +134,11 @@ private function addTranslationsSection(ArrayNodeDefinition $node): void ->arrayNode('translation') ->canBeDisabled() ->children() + ->arrayNode('enabled_locales') + ->defaultValue([]) + ->prototype('scalar')->end() + ->end() + ->scalarNode('default_locale')->defaultNull()->end() ->scalarNode('locale_provider')->defaultValue('sylius.translation_locale_provider.immutable')->cannotBeEmpty()->end() ->end() ->end() diff --git a/src/Bundle/DependencyInjection/SyliusResourceExtension.php b/src/Bundle/DependencyInjection/SyliusResourceExtension.php index fec4372ab..9e7570c01 100644 --- a/src/Bundle/DependencyInjection/SyliusResourceExtension.php +++ b/src/Bundle/DependencyInjection/SyliusResourceExtension.php @@ -46,6 +46,7 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('services/integrations/translation.xml'); $container->setAlias('sylius.translation_locale_provider', $config['translation']['locale_provider'])->setPublic(true); + $this->createTranslationParameters($config, $container); } $container->setParameter('sylius.resource.mapping', $config['mapping']); @@ -135,4 +136,72 @@ private function loadResources(array $loadedResources, ContainerBuilder $contain } } } + + private function createTranslationParameters(array $config, ContainerBuilder $container): void + { + $this->createEnabledLocalesParameter($config, $container); + $this->createDefaultLocaleParameter($config, $container); + } + + private function createEnabledLocalesParameter(array $config, ContainerBuilder $container): void + { + $enabledLocales = $config['translation']['enabled_locales']; + + if (count($enabledLocales) > 0) { + $container->setParameter('sylius.resource.translation.enabled_locales', $enabledLocales); + + return; + } + + if ($container->hasParameter('locale')) { + trigger_deprecation('sylius/resource-bundle', '1.9', 'Locale parameter usage to defined the enabled locales will no longer used in 2.0, you should use %kernel.enabled_locales% instead.'); + + $container->setParameter('sylius.resource.translation.enabled_locales', [$container->getParameter('locale')]); + + return; + } + + if ($container->hasParameter('kernel.enabled_locales')) { + $kernelEnabledLocales = (array) $container->getParameter('kernel.enabled_locales'); + + if (count($kernelEnabledLocales) > 0) { + $container->setParameter('sylius.resource.translation.enabled_locales', $container->getParameter('kernel.enabled_locales')); + + return; + } + } + + $container->setParameter('sylius.resource.translation.enabled_locales', ['en']); + } + + private function createDefaultLocaleParameter(array $config, ContainerBuilder $container): void + { + $defaultLocale = $config['translation']['default_locale']; + + if (is_string($defaultLocale)) { + $container->setParameter('sylius.resource.translation.default_locale', $defaultLocale); + + return; + } + + if ($container->hasParameter('locale')) { + trigger_deprecation('sylius/resource-bundle', '1.9', 'Locale parameter usage to define the translation default locale will no longer used in 2.0, you should use %kernel.default_locale% instead.'); + + $container->setParameter('sylius.resource.translation.default_locale', $container->getParameter('locale')); + + return; + } + + if ($container->hasParameter('kernel.default_locale')) { + $kernelDefaultLocale = $container->getParameter('kernel.default_locale'); + + if (is_string($kernelDefaultLocale)) { + $container->setParameter('sylius.resource.translation.default_locale', $kernelDefaultLocale); + + return; + } + } + + $container->setParameter('sylius.resource.translation.default_locale', 'en'); + } } diff --git a/src/Bundle/Resources/config/services/integrations/translation.xml b/src/Bundle/Resources/config/services/integrations/translation.xml index 04ba81cc7..dfeb6753b 100644 --- a/src/Bundle/Resources/config/services/integrations/translation.xml +++ b/src/Bundle/Resources/config/services/integrations/translation.xml @@ -16,10 +16,8 @@ - - %locale% - - %locale% + %sylius.resource.translation.enabled_locales% + %sylius.resource.translation.default_locale% diff --git a/src/Bundle/Tests/Configuration/ConfigurationTest.php b/src/Bundle/Tests/Configuration/ConfigurationTest.php index 4b32a5d85..163e1d911 100644 --- a/src/Bundle/Tests/Configuration/ConfigurationTest.php +++ b/src/Bundle/Tests/Configuration/ConfigurationTest.php @@ -16,6 +16,7 @@ use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; use PHPUnit\Framework\TestCase; use Sylius\Bundle\ResourceBundle\DependencyInjection\Configuration; +use Symfony\Component\Config\Definition\ConfigurationInterface; class ConfigurationTest extends TestCase { @@ -24,7 +25,7 @@ class ConfigurationTest extends TestCase /** * @test */ - public function it_does_not_break_if_not_customized() + public function it_does_not_break_if_not_customized(): void { $this->assertConfigurationIsValid( [ @@ -36,7 +37,7 @@ public function it_does_not_break_if_not_customized() /** * @test */ - public function it_has_default_mapping_paths() + public function it_has_default_mapping_paths(): void { $this->assertProcessedConfigurationEquals( [ @@ -56,7 +57,7 @@ public function it_has_default_mapping_paths() /** * @test */ - public function its_mapping_paths_can_be_customized() + public function its_mapping_paths_can_be_customized(): void { $this->assertProcessedConfigurationEquals( [ @@ -78,7 +79,54 @@ public function its_mapping_paths_can_be_customized() /** * @test */ - public function it_has_default_authorization_checker() + public function it_has_default_translation(): void + { + $this->assertProcessedConfigurationEquals( + [ + [], + ], + [ + 'translation' => [ + 'enabled' => true, + 'enabled_locales' => [], + 'default_locale' => null, + 'locale_provider' => 'sylius.translation_locale_provider.immutable', + ], + ], + 'translation' + ); + } + + /** + * @test + */ + public function its_translation_can_be_customized(): void + { + $this->assertProcessedConfigurationEquals( + [ + ['translation' => [ + 'enabled' => false, + 'enabled_locales' => ['fr', 'pl'], + 'default_locale' => 'fr', + 'locale_provider' => 'app.locale_provider.custom', + ]], + ], + [ + 'translation' => [ + 'enabled' => false, + 'enabled_locales' => ['fr', 'pl'], + 'default_locale' => 'fr', + 'locale_provider' => 'app.locale_provider.custom', + ], + ], + 'translation' + ); + } + + /** + * @test + */ + public function it_has_default_authorization_checker(): void { $this->assertProcessedConfigurationEquals( [ @@ -92,7 +140,7 @@ public function it_has_default_authorization_checker() /** * @test */ - public function its_authorization_checker_can_be_customized() + public function its_authorization_checker_can_be_customized(): void { $this->assertProcessedConfigurationEquals( [ @@ -106,7 +154,7 @@ public function its_authorization_checker_can_be_customized() /** * @test */ - public function its_authorization_checker_cannot_be_empty() + public function its_authorization_checker_cannot_be_empty(): void { $this->assertPartialConfigurationIsInvalid( [ @@ -116,7 +164,7 @@ public function its_authorization_checker_cannot_be_empty() ); } - protected function getConfiguration() + protected function getConfiguration(): ConfigurationInterface { return new Configuration(); } diff --git a/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php b/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php index 5569ce293..bd383727f 100644 --- a/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php +++ b/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php @@ -72,7 +72,139 @@ public function it_aliases_authorization_checker_with_the_one_given_in_configura /** * @test */ - public function it_registers_default_translation_parameters() + public function it_registers_translation_enabled_locales_parameter_from_locale_parameter(): void + { + $this->setParameter('kernel.bundles', []); + $this->setParameter('locale', 'pl'); + + $this->load([ + 'translation' => [ + 'enabled_locales' => [], + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.enabled_locales', ['pl']); + } + + /** + * @test + */ + public function it_registers_translation_enabled_locales_parameter_from_kernel_enabled_locales_parameter(): void + { + $this->setParameter('kernel.bundles', []); + $this->setParameter('kernel.enabled_locales', ['fr', 'pl']); + + $this->load([ + 'translation' => [ + 'enabled_locales' => [], + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.enabled_locales', ['fr', 'pl']); + } + + /** + * @test + */ + public function it_registers_translation_enabled_locales_parameter_with_given_locales(): void + { + $this->setParameter('kernel.bundles', []); + + $this->load([ + 'translation' => [ + 'enabled_locales' => ['fr', 'pl'], + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.enabled_locales', ['fr', 'pl']); + } + + /** + * @test + */ + public function it_registers_translation_enabled_locales_parameter(): void + { + $this->setParameter('kernel.bundles', []); + + $this->load([ + 'translation' => [ + 'enabled_locales' => [], + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.enabled_locales', ['en']); + } + + /** + * @test + */ + public function it_registers_default_locale_parameter_from_locale_parameter(): void + { + $this->setParameter('kernel.bundles', []); + $this->setParameter('locale', 'pl'); + + $this->load([ + 'translation' => [ + 'default_locale' => null, + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.default_locale', 'pl'); + } + + /** + * @test + */ + public function it_registers_default_locale_parameter_from_kernel_default_locale_parameter(): void + { + $this->setParameter('kernel.bundles', []); + $this->setParameter('kernel.default_locale', 'pl'); + + $this->load([ + 'translation' => [ + 'default_locale' => null, + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.default_locale', 'pl'); + } + + /** + * @test + */ + public function it_registers_translation_default_locale_parameter_with_given_locale(): void + { + $this->setParameter('kernel.bundles', []); + + $this->load([ + 'translation' => [ + 'default_locale' => 'fr', + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.default_locale', 'fr'); + } + + /** + * @test + */ + public function it_registers_translation_default_locale_parameter(): void + { + $this->setParameter('kernel.bundles', []); + + $this->load([ + 'translation' => [ + 'default_locale' => null, + ], + ]); + + $this->assertContainerBuilderHasParameter('sylius.resource.translation.default_locale', 'en'); + } + + /** + * @test + */ + public function it_registers_default_translation_parameters(): void { // TODO: Move ResourceGrid integration to a dedicated compiler pass $this->setParameter('kernel.bundles', []);