From 8f0f7723be4753fa458f4ac36ebbe62d0ec5b5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Thu, 16 Jun 2022 15:30:33 +0200 Subject: [PATCH] neon with bom (#101) * neon with bom fix * Update TranslationExtensionTest.phpt * Update TranslationExtensionTest.phpt * Update TranslationExtensionTest.phpt * Update composer.json --- composer.json | 2 +- src/Loaders/DatabaseAbstract.php | 6 ++---- src/Loaders/Neon.php | 8 +++----- tests/Tests/DI/TranslationExtensionTest.phpt | 10 +--------- tests/Tests/TranslatorTest.phpt | 2 +- tests/lang/file_with_BOM.cs_CZ.neon | 2 ++ 6 files changed, 10 insertions(+), 20 deletions(-) create mode 100644 tests/lang/file_with_BOM.cs_CZ.neon diff --git a/composer.json b/composer.json index 40e7811..a82ab3e 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "nette/di": "^3.0.6", "nette/finder": "^2.5.2", "nette/http": "^3.0", - "nette/neon": "^3.0", + "nette/neon": "^3.3.1", "nette/schema": "^1.0", "nette/routing": "^3.0", "nette/utils": "^3.2.1", diff --git a/src/Loaders/DatabaseAbstract.php b/src/Loaders/DatabaseAbstract.php index 37b9c79..1963ccd 100644 --- a/src/Loaders/DatabaseAbstract.php +++ b/src/Loaders/DatabaseAbstract.php @@ -33,14 +33,12 @@ public function load( throw new InvalidArgument('Parameter resource must be string.'); } - $content = @file_get_contents($resource); // @ -> prevent E_WARNING and thrown an exception - - if ($content === false) { + if (!\is_readable($resource)) { throw new InvalidArgument('Something wrong with resource file "' . $resource . '".'); } /** @var array $settings */ - $settings = Neon::decode($content); + $settings = Neon::decodeFile($resource); $config = [ 'table' => $settings['table'] ?? $domain, diff --git a/src/Loaders/Neon.php b/src/Loaders/Neon.php index b493be8..e0ffd88 100644 --- a/src/Loaders/Neon.php +++ b/src/Loaders/Neon.php @@ -3,7 +3,7 @@ namespace Contributte\Translation\Loaders; use Contributte\Translation\Exceptions\InvalidArgument; -use Nette; +use Nette\Neon\Neon as NetteNeon; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\Loader\LoaderInterface; @@ -26,13 +26,11 @@ public function load( throw new InvalidArgument('Parameter resource must be string.'); } - $content = @file_get_contents($resource); // @ -> prevent E_WARNING and thrown an exception - - if ($content === false) { + if (!\is_readable($resource)) { throw new InvalidArgument('Something wrong with resource file "' . $resource . '".'); } - $messages = Nette\Neon\Neon::decode($content); + $messages = NetteNeon::decodeFile($resource); $catalogue = parent::load($messages ?? [], $locale, $domain); diff --git a/tests/Tests/DI/TranslationExtensionTest.phpt b/tests/Tests/DI/TranslationExtensionTest.phpt index c475db2..c33aa15 100644 --- a/tests/Tests/DI/TranslationExtensionTest.phpt +++ b/tests/Tests/DI/TranslationExtensionTest.phpt @@ -162,15 +162,7 @@ final class TranslationExtensionTest extends TestAbstract Assert::count(1, $tracyPanel->getIgnoredResources()); Assert::count(1, $panel->getIgnoredResources()); - $foo = $tracyPanel->getIgnoredResources(); - $foo = end($foo); - Assert::same('messages', end($foo)); - Assert::true(Strings::contains(key($foo), 'messages.cs_CZ.neon')); - - $foo = $panel->getIgnoredResources(); - $foo = end($foo); - Assert::same('messages', end($foo)); - Assert::true(Strings::contains(key($foo), 'messages.cs_CZ.neon')); + Assert::count(2, $panel->getIgnoredResources()['cs_CZ']); $symfonyTranslator = $container->getByType(TranslatorInterface::class); Assert::same($translator, $symfonyTranslator); diff --git a/tests/Tests/TranslatorTest.phpt b/tests/Tests/TranslatorTest.phpt index 04d620a..d2c921e 100644 --- a/tests/Tests/TranslatorTest.phpt +++ b/tests/Tests/TranslatorTest.phpt @@ -314,7 +314,7 @@ final class TranslatorTest extends TestAbstract Assert::count(1, $dom->find('tr[class="contributte-translation-missing-translation"]')); Assert::count(1, $dom->find('tr[class="contributte-translation-locale-resolvers"]')); Assert::count(4, $dom->find('tr[class="contributte-translation-resources"]'));// lang/another_domain.en_US.neon, lang/messages+intl-icu.en_US.neon, lang/messages.en_US.neon, lang_overloading/messages.en_US.neon - Assert::count(1, $dom->find('tr[class="contributte-translation-ignored-resources"]'));// lang/messages.cs_CZ.neon + Assert::count(2, $dom->find('tr[class="contributte-translation-ignored-resources"]'));// lang/messages.cs_CZ.neon $psrLogger = new class() extends AbstractLogger { diff --git a/tests/lang/file_with_BOM.cs_CZ.neon b/tests/lang/file_with_BOM.cs_CZ.neon new file mode 100644 index 0000000..3ce4e1b --- /dev/null +++ b/tests/lang/file_with_BOM.cs_CZ.neon @@ -0,0 +1,2 @@ +# comment +bom: 'BOM'