|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\CoreBundle\Installer\Setup; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
|
17
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
18
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
19
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Question\Question; |
|
23
|
|
|
use Symfony\Component\Intl\Intl; |
|
24
|
|
|
|
|
25
|
|
|
final class LocaleSetup implements LocaleSetupInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var RepositoryInterface */ |
|
28
|
|
|
private $localeRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** @var FactoryInterface */ |
|
31
|
|
|
private $localeFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $locale; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(RepositoryInterface $localeRepository, FactoryInterface $localeFactory, string $locale) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->localeRepository = $localeRepository; |
|
39
|
|
|
$this->localeFactory = $localeFactory; |
|
40
|
|
|
$this->locale = trim($locale); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setup(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper): LocaleInterface |
|
47
|
|
|
{ |
|
48
|
|
|
$code = $this->getLanguageCodeFromUser($input, $output, $questionHelper); |
|
49
|
|
|
|
|
50
|
|
|
$output->writeln(sprintf('Adding <info>%s</info> locale.', $code)); |
|
51
|
|
|
|
|
52
|
|
|
/** @var LocaleInterface $existingLocale */ |
|
53
|
|
|
$existingLocale = $this->localeRepository->findOneBy(['code' => $code]); |
|
54
|
|
|
if (null !== $existingLocale) { |
|
55
|
|
|
return $existingLocale; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @var LocaleInterface $locale */ |
|
59
|
|
|
$locale = $this->localeFactory->createNew(); |
|
60
|
|
|
$locale->setCode($code); |
|
61
|
|
|
|
|
62
|
|
|
$this->localeRepository->add($locale); |
|
63
|
|
|
|
|
64
|
|
|
return $locale; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function getLanguageCodeFromUser(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper): string |
|
68
|
|
|
{ |
|
69
|
|
|
$code = $this->getNewLanguageCode($input, $output, $questionHelper); |
|
70
|
|
|
$name = $this->getLanguageName($code); |
|
71
|
|
|
|
|
72
|
|
|
while (null === $name) { |
|
73
|
|
|
$output->writeln( |
|
74
|
|
|
sprintf('<comment>Language with code <info>%s</info> could not be resolved.</comment>', $code) |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$code = $this->getNewLanguageCode($input, $output, $questionHelper); |
|
78
|
|
|
$name = $this->getLanguageName($code); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$output->writeln(sprintf('Adding <info>%s</info> Language.', $name)); |
|
82
|
|
|
|
|
83
|
|
|
return $code; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function getNewLanguageCode(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper): string |
|
87
|
|
|
{ |
|
88
|
|
|
$question = new Question('Language (press enter to use ' . $this->locale . '): ', $this->locale); |
|
89
|
|
|
|
|
90
|
|
|
return trim($questionHelper->ask($input, $output, $question)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function getLanguageName(string $code): ?string |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$language = $code; |
|
96
|
|
|
$region = null; |
|
97
|
|
|
|
|
98
|
|
|
if (count(explode('_', $code, 2)) === 2) { |
|
99
|
|
|
[$language, $region] = explode('_', $code, 2); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return Intl::getLanguageBundle()->getLanguageName($language, $region); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.