Skip to content

Commit

Permalink
Merge pull request #573 from schmittjoh/sf6
Browse files Browse the repository at this point in the history
symfony 6 support
  • Loading branch information
goetas authored Jan 7, 2023
2 parents 619e880 + ae5a2e1 commit 1d73855
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 56 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
symfony-version:
- '^4.4'
- '^5.4'
- '^6.0'
php-version:
- "7.4"
- "8.0"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ phpunit.xml
composer.lock
vendor/
.phpunit.result.cache
/.idea
.phpcs-cache
phpcs.xml

Expand Down
2 changes: 1 addition & 1 deletion Command/ExtractTranslationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
2 changes: 1 addition & 1 deletion Command/ResourcesListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/console.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<services>
<service id="jms_translation.command.extract" class="JMS\TranslationBundle\Command\ExtractTranslationCommand" public="false">
<tag name="console.command" command="translation:extract" />
<tag name="console.command" command="jms:translation:extract" />
<argument type="service" id="jms_translation.config_factory"/>
<argument type="service" id="jms_translation.updater"/>
<argument>%jms_translation.locales%</argument>
</service>

<service id="jms_translation.command.list_resources" class="JMS\TranslationBundle\Command\ResourcesListCommand" public="false">
<tag name="console.command" command="translation:list-resources" />
<tag name="console.command" command="jms:translation:list-resources" />
<argument>%kernel.project_dir%</argument>
<argument>%kernel.bundles%</argument>
<argument type="expression">container.hasParameter('kernel.root_dir') ? parameter('kernel.root_dir') : null</argument>
Expand Down
31 changes: 21 additions & 10 deletions Tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -76,7 +87,7 @@ public function getLogDir(): string
return $this->getBaseDir() . '/logs';
}

public function getProjectDir()
public function getProjectDir(): string
{
return __DIR__;
}
Expand Down
15 changes: 11 additions & 4 deletions Tests/Functional/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions Tests/Functional/Command/ExtractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down Expand Up @@ -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')),
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Command/ResourcesListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testList(): void
{
$input = new ArgvInput([
'app/console',
'translation:list-resources',
'jms:translation:list-resources',
]);

$expectedOutput =
Expand All @@ -44,7 +44,7 @@ public function testListFiles(): void
{
$input = new ArgvInput([
'app/console',
'translation:list-resources',
'jms:translation:list-resources',
'--files',
]);

Expand Down
1 change: 0 additions & 1 deletion Tests/Functional/config/default_sf5.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
imports:
- { resource: framework.yml }
- { resource: twig.yml }
- { resource: bundle.yml }

Expand Down
16 changes: 16 additions & 0 deletions Tests/Functional/config/framework_sf6.yml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 0 additions & 3 deletions Tests/Functional/config/test_updating_translations.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
imports:
- { resource: default_sf5.yml }

parameters:
translation_output_dir: "%kernel.cache_dir%/translations"

6 changes: 3 additions & 3 deletions Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
31 changes: 26 additions & 5 deletions Translation/Loader/Symfony/XliffLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,12 +35,10 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
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)) {
Expand All @@ -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);
}
}
}
44 changes: 22 additions & 22 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,33 @@
"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",
"doctrine/coding-standard": "^8.2.1",
"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": {
Expand All @@ -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": {
Expand Down

0 comments on commit 1d73855

Please sign in to comment.