File size: 5,040 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Command;

use Mautic\CoreBundle\Factory\TransifexFactory;
use Mautic\CoreBundle\Helper\LanguageHelper;
use Mautic\CoreBundle\Helper\UrlHelper;
use Mautic\Transifex\Connector\Resources;
use Mautic\Transifex\Exception\InvalidConfigurationException;
use Mautic\Transifex\Exception\ResponseException;
use Mautic\Transifex\Exception\TransifexException;
use Mautic\Transifex\Promise;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * CLI Command to push language resources to Transifex.
 */
class PushTransifexCommand extends Command
{
    public const NAME = 'mautic:transifex:push';

    public function __construct(
        private TransifexFactory $transifexFactory,
        private TranslatorInterface $translator,
        private LanguageHelper $languageHelper
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->setName(self::NAME)
            ->addOption('bundle', null, InputOption::VALUE_OPTIONAL, 'Optional bundle to pull. Example value: WebhookBundle', null)
            ->setHelp(<<<'EOT'
The <info>%command.name%</info> command is used to push translation resources to Transifex

<info>php %command.full_name%</info>

You can optionally choose to update resources for one bundle only with the --bundle option:

<info>php %command.full_name% --bundle AssetBundle</info>
EOT
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $bundleFilter = $input->getOption('bundle');
        $files        = $this->languageHelper->getLanguageFiles($bundleFilter ? [$bundleFilter] : []);

        try {
            $transifex = $this->transifexFactory->getTransifex();
        } catch (InvalidConfigurationException) {
            $output->writeln($this->translator->trans(
                'mautic.core.command.transifex_no_credentials')
            );

            return Command::FAILURE;
        }

        $resources = $transifex->getConnector(Resources::class);
        \assert($resources instanceof Resources);

        $existingResources = json_decode((string) $resources->getAll()->getBody(), true);
        $promises          = new \SplQueue();

        foreach ($files as $bundle => $stringFiles) {
            foreach ($stringFiles as $file) {
                $name    = $bundle.' '.str_replace('.ini', '', basename($file));
                $alias   = UrlHelper::stringURLUnicodeSlug($name);
                $content = file_get_contents($file);
                $output->writeln(
                    $this->translator->trans(
                        'mautic.core.command.transifex_processing_resource',
                        ['%resource%' => $name]
                    )
                );

                try {
                    if (false === $content) {
                        throw new \RuntimeException('Unable to read file '.$file);
                    }

                    if (!$resources->resourceExists($existingResources['data'], $alias)) {
                        $resources->create($name, $alias, 'INI');
                        $output->writeln(
                            $this->translator->trans('mautic.core.command.transifex_resource_created')
                        );
                    }

                    $promise = $transifex->getApiConnector()->createPromise(
                        $resources->uploadContent($alias, $content, true)
                    );
                    $promise->setFilePath($file);
                    $promises->enqueue($promise);
                } catch (TransifexException $exception) {
                    $output->writeln(
                        $this->translator->trans(
                            'mautic.core.command.transifex_error_pushing_data',
                            ['%message%' => $exception->getMessage()]
                        )
                    );
                }
            }
        }

        $transifex->getApiConnector()->fulfillPromises(
            $promises,
            function (ResponseInterface $response, Promise $promise) use ($output): void {
                $output->writeln(
                    $this->translator->trans(
                        'mautic.core.command.transifex_resource_updated',
                        ['%file%' => $promise->getFilePath()]
                    )
                );
            },
            function (ResponseException $exception, Promise $promise) use ($output): void {
                $output->writeln($promise->getFilePath());
                $output->writeln($exception->getMessage());
            }
        );

        return Command::SUCCESS;
    }

    protected static $defaultDescription = 'Pushes Mautic translation resources to Transifex';
}