Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 60 additions & 42 deletions Command/TranslationDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\DataCollectorTranslator;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function __construct(TranslatorInterface $translator, TranslationReaderIn
$this->enabledLocales = $enabledLocales;
}

protected function configure()
protected function configure(): void
{
$this
->setDefinition([
Expand Down Expand Up @@ -131,40 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$kernel = $this->getApplication()->getKernel();

// Define Root Paths
$transPaths = $this->getRootTransPaths();
$codePaths = $this->getRootCodePaths($kernel);

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
try {
$bundle = $kernel->getBundle($input->getArgument('bundle'));
$bundleDir = $bundle->getPath();
$transPaths = [is_dir($bundleDir.'/Resources/translations') ? $bundleDir.'/Resources/translations' : $bundleDir.'/translations'];
$codePaths = [is_dir($bundleDir.'/Resources/views') ? $bundleDir.'/Resources/views' : $bundleDir.'/templates'];
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
if ($this->defaultViewsPath) {
$codePaths[] = $this->defaultViewsPath;
}
} catch (\InvalidArgumentException) {
// such a bundle does not exist, so treat the argument as path
$path = $input->getArgument('bundle');

$transPaths = [$path.'/translations'];
$codePaths = [$path.'/templates'];

if (!is_dir($transPaths[0])) {
throw new InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0]));
}
}
} elseif ($input->getOption('all')) {
foreach ($kernel->getBundles() as $bundle) {
$bundleDir = $bundle->getPath();
$transPaths[] = is_dir($bundleDir.'/Resources/translations') ? $bundleDir.'/Resources/translations' : $bundle->getPath().'/translations';
$codePaths[] = is_dir($bundleDir.'/Resources/views') ? $bundleDir.'/Resources/views' : $bundle->getPath().'/templates';
}
}
[$transPaths, $codePaths] = $this->defineRootPaths($kernel, $input);

// Extract used messages
$extractedCatalogue = $this->extractMessages($locale, $codePaths);
Expand Down Expand Up @@ -192,6 +160,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::EXIT_CODE_GENERAL_ERROR;
}

$onlyMissing = $input->getOption('only-missing');
$onlyUnused = $input->getOption('only-unused');

// Load the fallback catalogues
$fallbackCatalogues = $this->loadFallbackCatalogues($locale, $transPaths);

Expand All @@ -211,20 +182,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$currentCatalogue->defines($messageId, $domain)) {
$states[] = self::MESSAGE_MISSING;

if (!$input->getOption('only-unused')) {
$exitCode = $exitCode | self::EXIT_CODE_MISSING;
if (!$onlyUnused) {
$exitCode |= self::EXIT_CODE_MISSING;
}
}
} elseif ($currentCatalogue->defines($messageId, $domain)) {
$states[] = self::MESSAGE_UNUSED;

if (!$input->getOption('only-missing')) {
$exitCode = $exitCode | self::EXIT_CODE_UNUSED;
if (!$onlyMissing) {
$exitCode |= self::EXIT_CODE_UNUSED;
}
}

if (!\in_array(self::MESSAGE_UNUSED, $states) && $input->getOption('only-unused')
|| !\in_array(self::MESSAGE_MISSING, $states) && $input->getOption('only-missing')
if (($onlyUnused && !\in_array(self::MESSAGE_UNUSED, $states, true))
|| ($onlyMissing && !\in_array(self::MESSAGE_MISSING, $states, true))
) {
continue;
}
Expand All @@ -233,7 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
$states[] = self::MESSAGE_EQUALS_FALLBACK;

$exitCode = $exitCode | self::EXIT_CODE_FALLBACK;
$exitCode |= self::EXIT_CODE_FALLBACK;

break;
}
Expand Down Expand Up @@ -387,6 +358,9 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra
return $fallbackCatalogues;
}

/**
* @return array<int,string>
*/
private function getRootTransPaths(): array
{
$transPaths = $this->transPaths;
Expand All @@ -397,6 +371,9 @@ private function getRootTransPaths(): array
return $transPaths;
}

/**
* @return array<int,string>
*/
private function getRootCodePaths(KernelInterface $kernel): array
{
$codePaths = $this->codePaths;
Expand All @@ -407,4 +384,45 @@ private function getRootCodePaths(KernelInterface $kernel): array

return $codePaths;
}

/**
* @return array<int,array<int,string>>
*/
private function defineRootPaths(KernelInterface $kernel, InputInterface $input): array
{
$transPaths = $this->getRootTransPaths();
$codePaths = $this->getRootCodePaths($kernel);

try {
foreach ($this->iterateBundles($kernel, $input) as $bundle) {
$bundleDir = $bundle->getPath();
$transPaths[] = is_dir($bundleDir . '/Resources/translations') ? $bundleDir . '/Resources/translations' : $bundleDir . '/translations';
$codePaths[] = is_dir($bundleDir . '/Resources/views') ? $bundleDir . '/Resources/views' : $bundleDir . '/templates';
}
} catch (\InvalidArgumentException) {
// such a bundle does not exist, so treat the argument as path
$path = $input->getArgument('bundle');

$transPaths = [$path . '/translations'];
$codePaths = [$path . '/templates'];

if (!is_dir($transPaths[0])) {
throw new InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0]));
}
}

return [$transPaths, $codePaths];
}

/**
* @return iterable<BundleInterface>
*/
private function iterateBundles(KernelInterface $kernel, InputInterface $input): iterable
{
if (null !== ($bundle = $input->getArgument('bundle'))) {
yield $kernel->getBundle($bundle);
} elseif ($input->getOption('all')) {
yield from $kernel->getBundles();
}
}
}