diff --git a/Command/ResourcesListCommand.php b/Command/ResourcesListCommand.php index d9e9c741..51898288 100644 --- a/Command/ResourcesListCommand.php +++ b/Command/ResourcesListCommand.php @@ -55,16 +55,23 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $rootPath = realpath($this->getContainer()->getParameter('kernel.root_dir')); - $basePath = realpath($this->getContainer()->getParameter('kernel.root_dir').'/..'); + $directoriesToSearch = []; + + // TODO: Remove this block when dropping support of Symfony 4 as it will always be false + if ($this->getContainer()->hasParameter('kernel.root_dir')) { + $directoriesToSearch[] = realpath($this->getContainer()->getParameter('kernel.root_dir')); + } + + $basePath = realpath($this->getContainer()->getParameter('kernel.project_dir')); + + $directoriesToSearch[] = $basePath; $dirs = $this->retrieveDirs(); if (!$input->hasParameterOption('--files')) { $output->writeln('Directories list :'); foreach ($dirs as $dir) { - $path = str_replace($rootPath, '%kernel.root_dir%', $dir); - $path = str_replace($basePath, '%kernel.root_dir%/..', $path); + $path = str_replace($directoriesToSearch, ['%kernel.root_dir%', '%kernel.project_dir%'], $dir); $output->writeln(sprintf(' - %s', $path)); } @@ -78,8 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $files = $this->retrieveFiles($dirs); foreach ($files as $file) { - $path = str_replace($basePath, '%kernel.root_dir%', $file); - $output->writeln(sprintf(' - %s', $path)); + $path = str_replace($basePath, '%kernel.project_dir%', $file); + $output->writeln(sprintf(' - %s', $path)); } $output->writeln('done!'); @@ -120,7 +127,13 @@ private function retrieveDirs() } } - if (is_dir($dir = $this->getContainer()->getParameter('kernel.root_dir').'/Resources/translations')) { + // TODO: Remove this block when dropping support of Symfony 4 + if ($this->getContainer()->hasParameter('kernel.root_dir') && + is_dir($dir = $this->getContainer()->getParameter('kernel.root_dir').'/Resources/translations')) { + $dirs[] = $dir; + } + + if (is_dir($dir = $this->getContainer()->getParameter('kernel.project_dir').'/translations')) { $dirs[] = $dir; } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index a37b6dd2..c7de8031 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -65,7 +65,8 @@ - %kernel.root_dir% + container.hasParameter('kernel.root_dir') ? parameter('kernel.root_dir') : parameter('kernel.project_dir') + %kernel.project_dir% diff --git a/Resources/doc/cookbook/config_reference.rst b/Resources/doc/cookbook/config_reference.rst index 8abaceab..1307410a 100644 --- a/Resources/doc/cookbook/config_reference.rst +++ b/Resources/doc/cookbook/config_reference.rst @@ -14,10 +14,10 @@ On this page you will find all available configuration options and their meaning # Create a configuration named "app" app: # List of directories we should extract translations keys from - dirs: ["%kernel.root_dir%", "%kernel.root_dir%/../src"] + dirs: ["%kernel.project_dir%/src", "%kernel.project_dir%/templates"] # Where to write the translation files - output_dir: "%kernel.root_dir%/Resources/translations" + output_dir: "%kernel.project_dir%/translations" # Whitelist domains domains: ["messages"] @@ -46,4 +46,4 @@ On this page you will find all available configuration options and their meaning # If true, we will never remove messages from the translation files. # If false, the translation files are up to date with the source. - keep: false \ No newline at end of file + keep: false diff --git a/Resources/doc/cookbook/extraction_configs.rst b/Resources/doc/cookbook/extraction_configs.rst index bf0db8de..a94f48dc 100644 --- a/Resources/doc/cookbook/extraction_configs.rst +++ b/Resources/doc/cookbook/extraction_configs.rst @@ -10,8 +10,8 @@ also set-up some pre-defined settings via the configuration: jms_translation: configs: app: - dirs: ["%kernel.root_dir%", "%kernel.root_dir%/../src"] - output_dir: "%kernel.root_dir%/Resources/translations" + dirs: ["%kernel.project_dir%/templates", "%kernel.project_dir%/src"] + output_dir: "%kernel.project_dir%/translations" ignored_domains: [routes] excluded_names: ["*TestCase.php", "*Test.php"] excluded_dirs: [cache, data, logs] @@ -26,7 +26,7 @@ You can then run the extraction process with this configuration with the followi .. code-block :: bash php app/console translation:extract de --config=app - + The ``--config`` option also supports overriding via command-line options. Let's assume that you would like to change the output format that has been defined in the config, but leave all other settings the same, you would run: diff --git a/Tests/Functional/AppKernel.php b/Tests/Functional/AppKernel.php index 28d08bae..f6ba04f7 100644 --- a/Tests/Functional/AppKernel.php +++ b/Tests/Functional/AppKernel.php @@ -59,7 +59,22 @@ public function registerContainerConfiguration(LoaderInterface $loader) $loader->load($this->config); } - public function getCacheDir() + public function getCacheDir(): string + { + return $this->getBaseDir().'/cache'; + } + + public function getLogDir(): string + { + return $this->getBaseDir().'/logs'; + } + + public function getProjectDir() + { + return __DIR__; + } + + private function getBaseDir(): string { return sys_get_temp_dir().'/JMSTranslationBundle'; } diff --git a/Tests/Functional/Command/ResourcesListCommandTest.php b/Tests/Functional/Command/ResourcesListCommandTest.php new file mode 100644 index 00000000..f6269bc3 --- /dev/null +++ b/Tests/Functional/Command/ResourcesListCommandTest.php @@ -0,0 +1,59 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace JMS\TranslationBundle\Tests\Functional\Command; + +use Symfony\Component\Console\Input\ArgvInput; + +final class ResourcesListCommandTest extends BaseCommandTestCase +{ + public function testList(): void + { + $input = new ArgvInput(array( + 'app/console', + 'translation:list-resources', + )); + + $expectedOutput = + 'Directories list :'."\n" + .' - %kernel.root_dir%/Fixture/TestBundle/Resources/translations'."\n" + .'done!'."\n" + ; + + $this->getApp()->run($input, $output = new Output()); + $this->assertEquals($expectedOutput, $output->getContent()); + } + + public function testListFiles(): void + { + $input = new ArgvInput(array( + 'app/console', + 'translation:list-resources', + '--files' + )); + + $expectedOutput = + 'Resources list :'."\n" + .' - %kernel.project_dir%/Fixture/TestBundle/Resources/translations/messages.en.php'."\n" + .'done!'."\n" + ; + + $this->getApp()->run($input, $output = new Output()); + $this->assertEquals($expectedOutput, $output->getContent()); + } +} diff --git a/Tests/Functional/Controller/ApiControllerTest.php b/Tests/Functional/Controller/ApiControllerTest.php index 85aa6522..166821b1 100644 --- a/Tests/Functional/Controller/ApiControllerTest.php +++ b/Tests/Functional/Controller/ApiControllerTest.php @@ -23,9 +23,6 @@ public function testUpdateAction() // Start application $client = static::createClient(); $client->request('POST', '/_trans/api/configs/app/domains/navigation/locales/en/messages?id=main.home', array('_method'=>'PUT', 'message'=>'Away')); -if($client->getResponse()->getStatusCode() !== 200) { - file_put_contents('/tmp/test-update.html',$client->getResponse()); -} $this->assertEquals(200, $client->getResponse()->getStatusCode()); // Verify that the file has new content diff --git a/Tests/Functional/Controller/TranslateControllerTest.php b/Tests/Functional/Controller/TranslateControllerTest.php index 4401e203..3fb0bff7 100644 --- a/Tests/Functional/Controller/TranslateControllerTest.php +++ b/Tests/Functional/Controller/TranslateControllerTest.php @@ -13,10 +13,6 @@ public function testIndexAction() { $client = static::createClient(); $crawler = $client->request('GET', '/_trans/'); -if($client->getResponse()->getStatusCode() !== 200) { -file_put_contents('/tmp/test-index.html',$client->getResponse()); -} - $this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertGreaterThan( 0, diff --git a/Tests/Functional/config/bundle.yml b/Tests/Functional/config/bundle.yml index 1ae6f7aa..6d47d638 100644 --- a/Tests/Functional/config/bundle.yml +++ b/Tests/Functional/config/bundle.yml @@ -2,9 +2,9 @@ jms_translation: configs: app: dirs: - - "%kernel.root_dir%" - - "%kernel.root_dir%/Fixture/TestBundle" - output_dir: "%kernel.root_dir%/Fixture/TestBundle/Resources/translations" + - "%kernel.project_dir%" + - "%kernel.project_dir%/Fixture/TestBundle" + output_dir: "%kernel.project_dir%/Fixture/TestBundle/Resources/translations" ignored_domains: [routes] excluded_names: ["*TestCase.php", "*Test.php"] excluded_dirs: [cache, data, logs] diff --git a/Tests/Functional/config/framework.yml b/Tests/Functional/config/framework.yml index 131f2437..47295065 100644 --- a/Tests/Functional/config/framework.yml +++ b/Tests/Functional/config/framework.yml @@ -5,10 +5,10 @@ framework: storage_id: session.storage.mock_file form: true csrf_protection: true - validation: + validation: enabled: true enable_annotations: true translator: enabled: true router: - resource: "%kernel.root_dir%/config/routing.yml" \ No newline at end of file + resource: "%kernel.project_dir%/config/routing.yml" diff --git a/Tests/Translation/FileSourceFactoryTest.php b/Tests/Translation/FileSourceFactoryTest.php index 3b13ba3a..cb14a819 100644 --- a/Tests/Translation/FileSourceFactoryTest.php +++ b/Tests/Translation/FileSourceFactoryTest.php @@ -13,9 +13,9 @@ class FileSourceFactoryTest extends TestCase * * @dataProvider pathProvider */ - public function testGetRelativePath($root, $file, $expected, $message = '') + public function testGetRelativePath($root, $projectRoot, $file, $expected, $message = '') { - $factory = new FileSourceFactory($root); + $factory = new FileSourceFactory($root, $projectRoot); $result = NSA::invokeMethod($factory, 'getRelativePath', $file); $this->assertEquals($expected, $result, $message); @@ -26,18 +26,21 @@ public function pathProvider() return array( array( '/user/foo/application/app', + null, '/user/foo/application/src/bundle/controller/index.php', '/../src/bundle/controller/index.php', ), array( '/user/foo/application/app/foo/bar', + null, '/user/foo/application/src/bundle/controller/index.php', '/../../../src/bundle/controller/index.php', ), array( '/user/foo/application/app', + null, '/user/foo/application/app/../src/AppBundle/Controller/DefaultController.php', '/../src/AppBundle/Controller/DefaultController.php', 'Test with "/../" in the file path', @@ -45,10 +48,42 @@ public function pathProvider() array( '/user/foo/application/app/foo/bar/baz/biz/foo', + null, '/user/foo/application/src/bundle/controller/index.php', '/../../../../../../src/bundle/controller/index.php', 'Test when the root path is longer that file path', ), + + array( + '/user/foo/application/app', + '/user/foo/application', + '/user/foo/application/src/bundle/controller/index.php', + '/src/bundle/controller/index.php', + ), + + array( + '/user/foo/application/app/foo/bar', + '/user/foo/application/src/foo/bar', + '/user/foo/application/src/bundle/controller/index.php', + '/../../bundle/controller/index.php', + ), + + array( + '/user/foo/application/app', + '/user/foo/application', + '/user/foo/application/app/../src/AppBundle/Controller/DefaultController.php', + '/app/../src/AppBundle/Controller/DefaultController.php', + 'Test with "/../" in the file path', + ), + + array( + '/user/foo/application/app/foo/bar/baz/biz/foo', + '/user/foo/application/src/foo/bar/baz/biz/foo', + '/user/foo/application/src/bundle/controller/index.php', + '/../../../../../bundle/controller/index.php', + 'Test when the root path is longer that file path', + ), + ); } } diff --git a/Translation/FileSourceFactory.php b/Translation/FileSourceFactory.php index adcd6982..bf496228 100644 --- a/Translation/FileSourceFactory.php +++ b/Translation/FileSourceFactory.php @@ -24,17 +24,25 @@ class FileSourceFactory { /** * @var string + * + * @deprecated Will be removed in 2.0. Use $baseDir instead. */ protected $kernelRoot; + /** + * @var string + */ + protected $baseDir; + /** * FileSourceFactory constructor. * * @param string $kernelRoot */ - public function __construct($kernelRoot) + public function __construct($kernelRoot, string $baseDir = null) { $this->kernelRoot = $kernelRoot; + $this->baseDir = $baseDir ?? $kernelRoot; } /** @@ -58,15 +66,15 @@ public function create(\SplFileInfo $file, $line = null, $column = null) */ private function getRelativePath($path) { - if (0 === strpos($path, $this->kernelRoot)) { - return substr($path, strlen($this->kernelRoot)); + if (0 === strpos($path, $this->baseDir)) { + return substr($path, strlen($this->baseDir)); } $relativePath = $ds = DIRECTORY_SEPARATOR; - $rootArray = explode($ds, $this->kernelRoot); + $rootArray = explode($ds, $this->baseDir); $pathArray = explode($ds, $path); - // Take the first directory in the kernelRoot tree + // Take the first directory in the baseDir tree foreach ($rootArray as $rootCurrentDirectory) { // Take the first directory from the path tree $pathCurrentDirectory = array_shift($pathArray); diff --git a/composer.json b/composer.json index 05b905b7..9cc68ce8 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "symfony/console": "^3.4 || ^4.3", "symfony/framework-bundle": "^3.4.31 || ^4.3", "symfony/validator": "^3.4 || ^4.3", - "twig/twig": "^1.42.3 || ^2.12" + "twig/twig": "^1.42.4 || ^2.12.5" }, "require-dev": { "doctrine/annotations": "^1.8", @@ -43,7 +43,7 @@ "symfony/security-csrf": "^3.4 || ^4.3", "symfony/templating": "^3.4 || ^4.3", "symfony/translation": "^3.4 || ^4.3", - "symfony/twig-bundle": "^3.4 || ^4.3" + "symfony/twig-bundle": "^3.4.37 || ^4.3.11" }, "config": { "sort-packages": true