Completed
Pull Request — master (#4329)
by Craig
10:07 queued 04:38
created

TranslationKeyToValueCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 45
c 2
b 0
f 1
dl 0
loc 85
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 7 1
C execute() 0 49 11
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle\Command;
15
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Exception\InvalidArgumentException;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
use Symfony\Component\Filesystem\Filesystem;
23
use Symfony\Component\Finder\Finder;
24
use Symfony\Component\HttpKernel\KernelInterface;
25
use Symfony\Component\Yaml\Yaml;
26
27
class TranslationKeyToValueCommand extends Command
28
{
29
    protected static $defaultName = 'zikula:translation:keytovalue';
30
31
    private $defaultTransPath;
32
33
    public function __construct(string $defaultTransPath = null)
34
    {
35
        parent::__construct();
36
        $this->defaultTransPath = $defaultTransPath;
37
    }
38
39
    protected function configure()
40
    {
41
        $this
42
            ->setDescription('update translation files to remove null values and replace them with the key')
43
            ->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages')
44
            ->setHelp(
45
                <<<'EOF'
46
The <info>%command.name%</info> command transforms translation strings of a given
47
bundle or the default translations directory. It sets null messages to the value of the key.
48
49
It is recommended to run <info>php bin/console translation:extract</info> first.
50
51
Example running against a Bundle (AcmeBundle)
52
53
  <info>php %command.full_name% AcmeBundle</info>
54
55
Example running against default messages directory
56
57
  <info>php %command.full_name%</info>
58
EOF
59
            )
60
        ;
61
    }
62
63
    protected function execute(InputInterface $input, OutputInterface $output): int
64
    {
65
        $io = new SymfonyStyle($input, $output);
66
67
        /** @var KernelInterface $kernel */
68
        $kernel = $this->getApplication()->getKernel();
0 ignored issues
show
Bug introduced by
The method getKernel() does not exist on Symfony\Component\Console\Application. It seems like you code against a sub-type of Symfony\Component\Console\Application such as Symfony\Bundle\FrameworkBundle\Console\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $kernel = $this->getApplication()->/** @scrutinizer ignore-call */ getKernel();
Loading history...
69
        $transPaths = [];
70
        if ($this->defaultTransPath) {
71
            $transPaths[] = $this->defaultTransPath;
72
        }
73
        $currentName = 'default directory';
74
        // Override with provided Bundle info (this section copied from symfony's translation:update command)
75
        if (null !== $input->getArgument('bundle')) {
76
            try {
77
                $foundBundle = $kernel->getBundle($input->getArgument('bundle'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('bundle') can also be of type null and string[]; however, parameter $name of Symfony\Component\HttpKe...lInterface::getBundle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
                $foundBundle = $kernel->getBundle(/** @scrutinizer ignore-type */ $input->getArgument('bundle'));
Loading history...
78
                $bundleDir = $foundBundle->getPath();
79
                $transPaths = [is_dir($bundleDir . '/Resources/translations') ? $bundleDir . '/Resources/translations' : $bundleDir . '/translations'];
80
                if ($this->defaultTransPath) {
81
                    $transPaths[] = $this->defaultTransPath;
82
                }
83
                $currentName = $foundBundle->getName();
84
            } catch (\InvalidArgumentException $e) {
85
                // such a bundle does not exist, so treat the argument as path
86
                $path = $input->getArgument('bundle');
87
                $transPaths = [$path . '/translations'];
0 ignored issues
show
Bug introduced by
Are you sure $path of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
                $transPaths = [/** @scrutinizer ignore-type */ $path . '/translations'];
Loading history...
88
                if (!is_dir($transPaths[0]) && !isset($transPaths[1])) {
89
                    throw new InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0]));
90
                }
91
            }
92
        }
93
        $io->title('Translation Messages Key to Value Transformer');
94
        $io->comment(sprintf('Transforming translation files for "<info>%s</info>"', $currentName));
95
        $finder = new Finder();
96
        $fs = new Filesystem();
97
        foreach ($finder->files()->in($transPaths)->name(['*.yaml', '*.yml']) as $file) {
98
            $io->text(sprintf('<comment>Parsing %s</comment>', $file->getBasename()));
99
            $messages = Yaml::parseFile($file->getRealPath());
100
            foreach ($messages as $key => $message) {
101
                if (null === $message) {
102
                    $messages[$key] = $key;
103
                }
104
            }
105
            $io->text(sprintf('<info>Dumping %s</info>', $file->getBasename()));
106
            $fs->dumpFile($file->getRealPath(), Yaml::dump($messages));
107
        }
108
109
        $io->success('Success!');
110
111
        return Command::SUCCESS;
112
    }
113
}
114