8000 [Console] Add completion to debug:translation command · symfony/symfony@2d1f985 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d1f985

Browse files
[Console] Add completion to debug:translation command
1 parent 540ee0a commit 2d1f985

File tree

2 files changed

+113
-19
lines changed

2 files changed

+113
-19
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Input\InputArgument;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Input\InputOption;
1921
use Symfony\Component\Console\Output\OutputInterface;
2022
use Symfony\Component\Console\Style\SymfonyStyle;
2123
use Symfony\Component\HttpKernel\KernelInterface;
24+
use Symfony\Component\Intl\Locales;
2225
use Symfony\Component\Translation\Catalogue\MergeOperation;
2326
use Symfony\Component\Translation\DataCollectorTranslator;
2427
use Symfony\Component\Translation\Extractor\ExtractorInterface;
@@ -135,15 +138,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
135138
$kernel = $this->getApplication()->getKernel();
136139

137140
// Define Root Paths
138-
$transPaths = $this->transPaths;
139-
if ($this->defaultTransPath) {
140-
$transPaths[] = $this->defaultTransPath;
141-
}
142-
$codePaths = $this->codePaths;
143-
$codePaths[] = $kernel->getProjectDir().'/src';
144-
if ($this->defaultViewsPath) {
145-
$codePaths[] = $this->defaultViewsPath;
146-
}
141+
$transPaths = $this->getRootTransPaths();
142+
$codePaths = $this->getRootCodePaths($kernel);
147143

148144
// Override with provided Bundle info
149145
if (null !== $input->getArgument('bundle')) {
@@ -259,6 +255,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
259255
return $exitCode;
260256
}
261257

258+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
259+
{
260+
if ($input->mustSuggestArgumentValuesFor('locale')) {
261+
$suggestions->suggestValues(Locales::getLocales());
262+
263+
return;
264+
}
265+
266+
if ($input->mustSuggestOptionValuesFor('domain')) {
267+
$locale = $input->getArgument('locale');
268+
/** @var KernelInterface $kernel */
269+
$kernel = $this->getApplication()->getKernel();
270+
271+
$mergeOperation = new MergeOperation(
272+
$this->extractMessages($locale, $this->getRootCodePaths($kernel)),
273+
$this->loadCurrentMessages($locale, $this->getRootTransPaths())
274+
);
275+
276+
$suggestions->suggestValues($mergeOperation->getDomains());
277+
}
278+
}
279+
262280
private function formatState(int $state): string
263281
{
264282
if (self::MESSAGE_MISSING === $state) {
@@ -354,4 +372,25 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra
354372

355373
return $fallbackCatalogues;
356374
}
375+
376+
private function getRootTransPaths(): array
377+
{
378+
$transPaths = $this->transPaths;
379+
if ($this->defaultTransPath) {
380+
$transPaths[] = $this->defaultTransPath;
381+
}
382+
383+
return $transPaths;
384+
}
385+
386+
private function getRootCodePaths(KernelInterface $kernel): array
387+
{
388+
$codePaths = $this->codePaths;
389+
$codePaths[] = $kernel->getProjectDir().'/src';
390+
if ($this->defaultViewsPath) {
391+
$codePaths[] = $this->defaultViewsPath;
392+
}
393+
394+
return $codePaths;
395+
}
357396
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1718
use Symfony\Component\Console\Tester\CommandTester;
1819
use Symfony\Component\DependencyInjection\Container;
1920
use Symfony\Component\Filesystem\Filesystem;
2021
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2122
use Symfony\Component\HttpKernel\KernelInterface;
23+
use Symfony\Component\Intl\Locales;
2224
use Symfony\Component\Translation\Extractor\ExtractorInterface;
2325
use Symfony\Component\Translation\Reader\TranslationReader;
2426
use Symfony\Component\Translation\Translator;
@@ -140,22 +142,29 @@ protected function tearDown(): void
140142
}
141143

142144
private function createCommandTester($extractedMessages = [], $loadedMessages = [], $kernel = null, array $transPaths = [], array $codePaths = []): CommandTester
145+
{
146+
return new CommandTester($this->createCommand($extractedMessages, $loadedMessages, $kernel, $transPaths, $codePaths));
147+
}
148+
149+
private function createCommand($extractedMessages = [], $loadedMessages = [], $kernel = null, array $transPaths = [], array $codePaths = [], ExtractorInterface $extractor = null): TranslationDebugCommand
143150
{
144151
$translator = $this->createMock(Translator::class);
145152
$translator
146153
->expects($this->any())
147154
->method('getFallbackLocales')
148155
->willReturn(['en']);
149156

150-
$extractor = $this->createMock(ExtractorInterface::class);
151-
$extractor
152-
->expects($this->any())
153-
->method('extract')
154-
->willReturnCallback(
155-
function ($path, $catalogue) use ($extractedMessages) {
156-
$catalogue->add($extractedMessages);
157-
}
158-
);
157+
if (!$extractor) {
158+
$extractor = $this->createMock(ExtractorInterface::class);
159+
$extractor
160+
->expects($this->any())
161+
->method('extract')
162+
->willReturnCallback(
163+
function ($path, $catalogue) use ($extractedMessages) {
164+
$catalogue->add($extractedMessages);
165+
}
166+
);
167+
}
159168

160169
$loader = $this->createMock(TranslationReader::class);
161170
$loader
@@ -195,7 +204,7 @@ function ($path, $catalogue) use ($loadedMessages) {
195204
$application = new Application($kernel);
196205
$application->add($command);
197206

198-
return new CommandTester($application->find('debug:translation'));
207+
return $application->find('debug:translation');
199208
}
200209

201210
private function getBundle($path)
@@ -209,4 +218,50 @@ private function getBundle($path)
209218

210219
return $bundle;
211220
}
221+
222+
/**
223+
* @dataProvider provideCompletionSuggestions
224+
*/
225+
public function testComplete(array $input, array $expectedSuggestions)
226+
{
227+
$extractedMessagesWithDomains = [
228+
'messages' => [
229+
'foo' => 'foo',
230+
],
231+
'validators' => [
232+
'foo' => 'foo',
233+
],
234+
'custom_domain' => [
235+
'foo' => 'foo',
236+
],
237+
];
238+
$extractor = $this->createMock(ExtractorInterface::class);
239+
$extractor
240+
->expects($this->any())
241+
->method('extract')
242+
->willReturnCallback(
243+
function ($path, $catalogue) use ($extractedMessagesWithDomains) {
244+
foreach ($extractedMessagesWithDomains as $domain => $message) {
245+
$catalogue->add($message, $domain);
246+
}
247+
}
248+
);
249+
250+
$tester = new CommandCompletionTester($this->createCommand([], [], null, [], [], $extractor));
251+
$suggestions = $tester->complete($input);
252+
$this->assertSame($expectedSuggestions, $suggestions);
253+
}
254+
255+
public function provideCompletionSuggestions()
256+
{
257+
yield 'locale' => [
258+
[''],
259+
Locales::getLocales(),
260+
];
261+
262+
yield 'option --domain' => [
263+
['en', '--domain', ''],
264+
['messages', 'validators', 'custom_domain'],
265+
];
266+
}
212267
}

0 commit comments

Comments
 (0)
0