Skip to content

Commit

Permalink
Replace kernel.root_dir with kernel.project_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Feb 26, 2020
1 parent 0cee3ab commit 0f594cd
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 29 deletions.
27 changes: 20 additions & 7 deletions Command/ResourcesListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<info>Directories list :</info>');
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));
}

Expand All @@ -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!');
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
<service id="jms_translation.config_factory" class="%jms_translation.config_factory.class%" public="true"/>

<service id="jms_translation.file_source_factory" class="%jms_translation.file_source_factory.class%">
<argument>%kernel.root_dir%</argument>
<argument type="expression">container.hasParameter('kernel.root_dir') ? parameter('kernel.root_dir') : parameter('kernel.project_dir')</argument>
<argument>%kernel.project_dir%</argument>
</service>

<service id="jms_translation.file_writer" class="%jms_translation.file_writer.class%" public="false" />
Expand Down
6 changes: 3 additions & 3 deletions Resources/doc/cookbook/config_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
keep: false
6 changes: 3 additions & 3 deletions Resources/doc/cookbook/extraction_configs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion Tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
59 changes: 59 additions & 0 deletions Tests/Functional/Command/ResourcesListCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* 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());
}
}
6 changes: 3 additions & 3 deletions Tests/Functional/config/bundle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
4 changes: 2 additions & 2 deletions Tests/Functional/config/framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
resource: "%kernel.project_dir%/config/routing.yml"
39 changes: 37 additions & 2 deletions Tests/Translation/FileSourceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -26,29 +26,64 @@ 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',
),

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',
),

);
}
}
18 changes: 13 additions & 5 deletions Translation/FileSourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.38 || ^2.7"
"twig/twig": "^1.42.4 || ^2.12.5"
},
"require-dev": {
"doctrine/annotations": "^1.8",
Expand All @@ -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
Expand Down

0 comments on commit 0f594cd

Please sign in to comment.