Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Command/ImportTranslationsCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Command;
4
5
use Kunstmaan\TranslatorBundle\Model\Import\ImportCommand;
6
use Kunstmaan\TranslatorBundle\Service\Command\Importer\ImportCommandHandler;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @final      since 5.1
14
 *
15
 * @deprecated since 5.1
16
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
17
 * NEXT_MAJOR file will be renamed
18
 *
19
 * Class ImportTranslationsFromCodeCommand
20
 */
21
class ImportTranslationsCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
    /**
24
     * @var ImportCommandHandler
25
     */
26
    private $importCommandHandler;
27
28
    /**
29
     * @var string
30
     */
31
    private $defaultBundle;
32
33
    /**
34
     * @var array
35
     */
36
    private $bundles;
37
38
    /**
39
     * ImportTranslationsCommand constructor.
40
     *
41
     * @param ImportCommandHandler $importCommandHandler
42
     */
43 View Code Duplication
    public function __construct(/* ImportCommandHandler */
44
        $importCommandHandler = null,
45
        string $defaultBundle,
46
        array $bundles
47
    ) {
48
        parent::__construct();
49
50
        if (!$importCommandHandler instanceof ImportCommandHandler) {
51
            @trigger_error(
52
                sprintf(
53
                    'Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ',
54
                    __METHOD__
55
                ),
56
                E_USER_DEPRECATED
57
            );
58
59
            $this->setName(null === $importCommandHandler ? 'kuma:translator:import' : $importCommandHandler);
60
61
            return;
62
        }
63
64
        $this->importCommandHandler = $importCommandHandler;
65
        $this->defaultBundle = $defaultBundle;
66
        $this->bundles = $bundles;
67
    }
68
69
    /**
70
     * Configures this command.
71
     */
72
    protected function configure()
73
    {
74
        $this
75
            ->setName('kuma:translator:import')
76
            ->setDescription('Import translation files into database')
77
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import, overwrite all existing database entries')
78
            ->addOption('locales', 'l', InputOption::VALUE_REQUIRED, 'Language import, only import a specific locale')
79
            ->addOption('globals', 'g', InputOption::VALUE_NONE, 'Global app import, import the global translations of your app')
80
            ->addOption(
81
                'defaultbundle',
82
                'd',
83
                InputOption::VALUE_REQUIRED,
84
                'Import the translations for specific bundles, use "own", "all" or "custom"'
85
            )
86
            ->addOption(
87
                'bundles',
88
                'b',
89
                InputOption::VALUE_OPTIONAL,
90
                'A list of bundle names that need to be imported (comma delimited) , only used When "defaultbundle" is set to "custom"'
91
            );
92
    }
93
94
    protected function execute(InputInterface $input, OutputInterface $output)
95
    {
96
        $force = $input->getOption('force');
97
        $locales = $input->getOption('locales');
98
        $globals = $input->getOption('globals');
99
        $defaultBundle = $input->getOption('defaultbundle') ?: $this->defaultBundle;
100
        $bundles = $input->getOption('bundles') ? array_map('trim', explode(',', $input->getOption('bundles'))) : $this->bundles;
101
        if (null === $this->importCommandHandler) {
102
            $this->importCommandHandler = $this->getContainer()->get('kunstmaan_translator.service.importer.command_handler');
103
        }
104
105
        $importCommand = new ImportCommand();
106
        $importCommand
107
            ->setForce($force)
108
            ->setLocales($locales)
109
            ->setGlobals($globals)
110
            ->setDefaultBundle($defaultBundle)
111
            ->setBundles($bundles);
112
113
        $imported = $this->importCommandHandler->executeImportCommand($importCommand);
114
115
        $output->writeln(sprintf('Translation imported: %d', $imported));
116
117
        return 0;
118
    }
119
}
120