From ae5a2e1bb1478d5f4051ac71a69496953f5f87cd Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 7 Jan 2023 12:00:20 +0100 Subject: [PATCH] symfony 6 support --- .github/workflows/ci.yaml | 1 + .gitignore | 1 + Command/ExtractTranslationCommand.php | 2 +- Command/ResourcesListCommand.php | 2 +- Resources/config/console.xml | 4 +- Tests/Functional/AppKernel.php | 31 ++++++++----- Tests/Functional/BaseTestCase.php | 15 +++++-- .../Functional/Command/ExtractCommandTest.php | 4 +- .../Command/ResourcesListCommandTest.php | 4 +- Tests/Functional/config/default_sf5.yml | 1 - Tests/Functional/config/framework_sf6.yml | 16 +++++++ .../config/test_updating_translations.yml | 3 -- Translation/Extractor/File/FormExtractor.php | 6 +-- Translation/Loader/Symfony/XliffLoader.php | 31 ++++++++++--- composer.json | 44 +++++++++---------- 15 files changed, 109 insertions(+), 56 deletions(-) create mode 100644 Tests/Functional/config/framework_sf6.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d5c5bb64..7c9dfd7d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,6 +20,7 @@ jobs: symfony-version: - '^4.4' - '^5.4' + - '^6.0' php-version: - "7.4" - "8.0" diff --git a/.gitignore b/.gitignore index 23a0948e..3ca416f2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ phpunit.xml composer.lock vendor/ .phpunit.result.cache +/.idea .phpcs-cache phpcs.xml diff --git a/Command/ExtractTranslationCommand.php b/Command/ExtractTranslationCommand.php index c67fad75..a7649a06 100644 --- a/Command/ExtractTranslationCommand.php +++ b/Command/ExtractTranslationCommand.php @@ -67,7 +67,7 @@ public function __construct(ConfigFactory $configFactory, Updater $updater, arra protected function configure() { $this - ->setName('translation:extract') + ->setName('jms:translation:extract') ->setDescription('Extracts translation messages from your code.') ->addArgument('locales', InputArgument::IS_ARRAY, 'The locales for which to extract messages.') ->addOption('enable-extractor', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The alias of an extractor which should be enabled.') diff --git a/Command/ResourcesListCommand.php b/Command/ResourcesListCommand.php index 0feae44f..b7816bee 100644 --- a/Command/ResourcesListCommand.php +++ b/Command/ResourcesListCommand.php @@ -62,7 +62,7 @@ public function __construct(string $projectDir, array $bundles, ?string $rootDir protected function configure() { $this - ->setName('translation:list-resources') + ->setName('jms:translation:list-resources') ->setDescription('List translation resources available.') ->addOption('files', null, InputOption::VALUE_OPTIONAL, 'Display only files'); } diff --git a/Resources/config/console.xml b/Resources/config/console.xml index 9083bbac..bcbcab01 100644 --- a/Resources/config/console.xml +++ b/Resources/config/console.xml @@ -6,14 +6,14 @@ - + %jms_translation.locales% - + %kernel.project_dir% %kernel.bundles% container.hasParameter('kernel.root_dir') ? parameter('kernel.root_dir') : null diff --git a/Tests/Functional/AppKernel.php b/Tests/Functional/AppKernel.php index a201e0ed..23d51052 100644 --- a/Tests/Functional/AppKernel.php +++ b/Tests/Functional/AppKernel.php @@ -34,23 +34,31 @@ class AppKernel extends Kernel { private $config; - public function __construct($config) + private $fwConfig; + + public function __construct(string $fwConfig, ?string $config) { parent::__construct('test', true); $fs = new Filesystem(); - if (! $fs->isAbsolutePath($config)) { - $config = __DIR__ . '/config/' . $config; + 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 (! file_exists($config)) { - throw new RuntimeException(sprintf('The config file "%s" does not exist.', $config)); + if (!$fs->isAbsolutePath($fwConfig)) { + $fwConfig = __DIR__ . '/config/' . $fwConfig; } - - $this->config = $config; + $this->fwConfig = $fwConfig; } - public function registerBundles() + public function registerBundles(): iterable { return [ new TestBundle(), @@ -63,7 +71,10 @@ public function registerBundles() public function registerContainerConfiguration(LoaderInterface $loader) { - $loader->load($this->config); + $loader->load($this->fwConfig); + if ($this->config) { + $loader->load($this->config); + } } public function getCacheDir(): string @@ -76,7 +87,7 @@ public function getLogDir(): string return $this->getBaseDir() . '/logs'; } - public function getProjectDir() + public function getProjectDir(): string { return __DIR__; } diff --git a/Tests/Functional/BaseTestCase.php b/Tests/Functional/BaseTestCase.php index 43ad51a1..84b13800 100644 --- a/Tests/Functional/BaseTestCase.php +++ b/Tests/Functional/BaseTestCase.php @@ -22,17 +22,24 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\HttpKernel\KernelInterface; class BaseTestCase extends WebTestCase { - protected static function createKernel(array $options = []) + protected static function createKernel(array $options = []): KernelInterface { $isSf5 = version_compare(Kernel::VERSION, '5.0.0') >= 0; $default = $isSf5 ? 'default_sf5.yml' : 'default.yml'; - return new AppKernel( - $options['config'] ?? $default - ); + if (version_compare(Kernel::VERSION, '6.0.0') >= 0) { + $conf = 'framework_sf6.yml'; + } elseif (version_compare(Kernel::VERSION, '5.0.0') >= 0) { + $conf = 'framework.yml'; + } else { + $conf = 'framework.yml'; + } + + return new AppKernel($conf, $options['config'] ?? $default); } } diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php index ed9f667d..46f54687 100644 --- a/Tests/Functional/Command/ExtractCommandTest.php +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -29,7 +29,7 @@ public function testExtract() { $input = new ArgvInput([ 'app/console', - 'translation:extract', + 'jms:translation:extract', 'en', '--dir=' . $inputDir = __DIR__ . '/../../Translation/Extractor/Fixture/SimpleTest', '--output-dir=' . ($outputDir = sys_get_temp_dir() . '/' . uniqid('extract')), @@ -62,7 +62,7 @@ public function testExtractDryRun() { $input = new ArgvInput([ 'app/console', - 'translation:extract', + 'jms:translation:extract', 'en', '--dir=' . $inputDir = __DIR__ . '/../../Translation/Extractor/Fixture/SimpleTest', '--output-dir=' . ($outputDir = sys_get_temp_dir() . '/' . uniqid('extract')), diff --git a/Tests/Functional/Command/ResourcesListCommandTest.php b/Tests/Functional/Command/ResourcesListCommandTest.php index 7d697016..64104e15 100644 --- a/Tests/Functional/Command/ResourcesListCommandTest.php +++ b/Tests/Functional/Command/ResourcesListCommandTest.php @@ -28,7 +28,7 @@ public function testList(): void { $input = new ArgvInput([ 'app/console', - 'translation:list-resources', + 'jms:translation:list-resources', ]); $expectedOutput = @@ -44,7 +44,7 @@ public function testListFiles(): void { $input = new ArgvInput([ 'app/console', - 'translation:list-resources', + 'jms:translation:list-resources', '--files', ]); diff --git a/Tests/Functional/config/default_sf5.yml b/Tests/Functional/config/default_sf5.yml index f6f04d58..b0fbfefc 100644 --- a/Tests/Functional/config/default_sf5.yml +++ b/Tests/Functional/config/default_sf5.yml @@ -1,5 +1,4 @@ imports: - - { resource: framework.yml } - { resource: twig.yml } - { resource: bundle.yml } diff --git a/Tests/Functional/config/framework_sf6.yml b/Tests/Functional/config/framework_sf6.yml new file mode 100644 index 00000000..59349537 --- /dev/null +++ b/Tests/Functional/config/framework_sf6.yml @@ -0,0 +1,16 @@ +framework: + secret: test + test: ~ + assets: ~ + session: + storage_factory_id: session.storage.factory.mock_file + form: true + csrf_protection: true + annotations: true + property_access: true + validation: + enabled: true + translator: + enabled: true + router: + resource: "%kernel.project_dir%/config/routing.yml" diff --git a/Tests/Functional/config/test_updating_translations.yml b/Tests/Functional/config/test_updating_translations.yml index 1ee24b29..6fb7d25b 100644 --- a/Tests/Functional/config/test_updating_translations.yml +++ b/Tests/Functional/config/test_updating_translations.yml @@ -1,6 +1,3 @@ -imports: - - { resource: default_sf5.yml } - parameters: translation_output_dir: "%kernel.cache_dir%/translations" diff --git a/Translation/Extractor/File/FormExtractor.php b/Translation/Extractor/File/FormExtractor.php index 413a30e5..93bb2fed 100644 --- a/Translation/Extractor/File/FormExtractor.php +++ b/Translation/Extractor/File/FormExtractor.php @@ -120,7 +120,7 @@ public function enterNode(Node $node) // look for options containing a message foreach ($node->items as $item) { - if (!$item || !$item->key instanceof Node\Scalar\String_) { + if (!is_object($item) || !$item->key instanceof Node\Scalar\String_) { continue; } @@ -171,7 +171,7 @@ public function getDomain(Node $node) $domain = null; foreach ($node->items as $item) { - if (!$item || !$item->key instanceof Node\Scalar\String_) { + if (!is_object($item) || !$item->key instanceof Node\Scalar\String_) { continue; } @@ -375,7 +375,7 @@ private function parseDefaultsCall(Node $node) // check if a translation_domain is set as a default option $domain = null; foreach ($node->args[0]->value->items as $item) { - if (!$item->key instanceof Node\Scalar\String_) { + if (!is_object($item) || !$item->key instanceof Node\Scalar\String_) { continue; } diff --git a/Translation/Loader/Symfony/XliffLoader.php b/Translation/Loader/Symfony/XliffLoader.php index 7cd12205..d1f6aeb7 100644 --- a/Translation/Loader/Symfony/XliffLoader.php +++ b/Translation/Loader/Symfony/XliffLoader.php @@ -22,6 +22,7 @@ use JMS\TranslationBundle\Exception\RuntimeException; use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Translation\Loader\LoaderInterface; use Symfony\Component\Translation\MessageCatalogue; @@ -34,12 +35,10 @@ * * @author Johannes M. Schmitt */ -class XliffLoader implements LoaderInterface +// phpcs:ignore +class XliffLoaderInternal { - /** - * {@inheritdoc} - */ - public function load($resource, $locale, $domain = 'messages') + protected function loadInternal($resource, $locale, $domain = 'messages') { $previous = libxml_use_internal_errors(true); if (false === $xml = simplexml_load_file((string) $resource)) { @@ -66,3 +65,25 @@ public function load($resource, $locale, $domain = 'messages') return $catalogue; } } + +$isSf6 = version_compare(Kernel::VERSION, '6.0.0') >= 0; + +if ($isSf6) { + // phpcs:ignore + class XliffLoader extends XliffLoaderInternal implements LoaderInterface + { + public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue + { + return $this->loadInternal($resource, $locale, $domain); + } + } +} else { + // phpcs:ignore + class XliffLoader extends XliffLoaderInternal implements LoaderInterface + { + public function load($resource, $locale, $domain = 'messages') + { + return $this->loadInternal($resource, $locale, $domain); + } + } +} diff --git a/composer.json b/composer.json index 553c3adb..521e4cad 100644 --- a/composer.json +++ b/composer.json @@ -23,13 +23,15 @@ "require": { "php": "^7.4 || ^8.0", "nikic/php-parser": "^4.9", - "symfony/console": "^4.3 || ^5.4", - "symfony/expression-language": "^4.3 || ^5.4", - "symfony/framework-bundle": "^4.3 || ^5.4", - "symfony/translation": "^4.3 || ^5.4", - "symfony/translation-contracts": "^1.1 || ^2.0", - "symfony/validator": "^4.3 || ^5.4", - "twig/twig": "^1.42.4 || ^2.12.5 || ^3.0" + "symfony/console": "^4.3 || ^5.4 || ^6.0", + "symfony/expression-language": "^4.3 || ^5.4 || ^6.0", + "symfony/framework-bundle": "^4.3 || ^5.4 || ^6.0", + "symfony/config": "^4.3 || ^5.4 || ^6.2", + "symfony/translation": "^4.3 || ^5.4 || ^6.0", + "symfony/translation-contracts": "^1.1 || ^2.0 || ^3.0", + "symfony/validator": "^4.3 || ^5.4 || ^6.0", + "twig/twig": "^1.42.4 || ^2.12.5 || ^3.0", + "psr/log": "^1.0 || ^2.0" }, "require-dev": { "doctrine/annotations": "^1.11", @@ -37,18 +39,17 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.1", "nyholm/nsa": "^1.0.1", "symfony/phpunit-bridge": ">=5.4", - "psr/log": "^1.0", - "sensio/framework-extra-bundle": "^5.5.4", - "symfony/asset": "^4.3 || ^5.4", - "symfony/browser-kit": "^4.3 || ^5.4", - "symfony/css-selector": "^4.3 || ^5.4", - "symfony/filesystem": "^4.3 || ^5.4", - "symfony/form": "^4.3 || ^5.4", - "symfony/security-csrf": "^4.3 || ^5.4", - "symfony/templating": "^4.3 || ^5.4", - "symfony/property-access": "^4.3 || ^5.4", - "symfony/routing": "^4.4.15 || ^5.4", - "symfony/twig-bundle": "^4.3.11 || ^5.4", + "sensio/framework-extra-bundle": "^6.2.4", + "symfony/asset": "^4.3 || ^5.4 || ^6.0", + "symfony/browser-kit": "^4.3 || ^5.4 || ^6.0", + "symfony/css-selector": "^4.3 || ^5.4 || ^6.0", + "symfony/filesystem": "^4.3 || ^5.4 || ^6.0", + "symfony/form": "^4.3 || ^5.4 || ^6.0", + "symfony/security-csrf": "^4.3 || ^5.4 || ^6.0", + "symfony/templating": "^4.3 || ^5.4 || ^6.0", + "symfony/property-access": "^4.3 || ^5.4 || ^6.0", + "symfony/routing": "^4.4.15 || ^5.4 || ^6.0", + "symfony/twig-bundle": "^4.3.11 || ^5.4 || ^6.0", "symfony/flex": "^1.19 || ^2.0" }, "config": { @@ -60,11 +61,10 @@ }, "extra": { "branch-alias": { - "dev-master": "1.7-dev" + "dev-master": "2.x-dev" }, "symfony": { - "allow-contrib": true, - "require": "^5.4" + "allow-contrib": true } }, "autoload": {