|
1
|
|
|
<?php |
|
2
|
|
|
namespace l10nNetteTranslator\ApplicationDI; |
|
3
|
|
|
|
|
4
|
|
|
use Nette\DI\CompilerExtension; |
|
5
|
|
|
use Nette\InvalidStateException; |
|
6
|
|
|
use Nette\PhpGenerator\ClassType; |
|
7
|
|
|
|
|
8
|
|
|
class Extension extends CompilerExtension { |
|
9
|
12 |
|
public function loadConfiguration() { |
|
10
|
12 |
|
$config = $this->getConfig(); |
|
11
|
|
|
|
|
12
|
12 |
|
if (empty($config['languages']) || !is_array($config['languages'])) { |
|
13
|
9 |
|
throw new InvalidStateException("Languages must be set, must be array and can't be empty"); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** @var \Nette\DI\ContainerBuilder $builder */ |
|
17
|
3 |
|
$builder = $this->getContainerBuilder(); |
|
18
|
|
|
|
|
19
|
3 |
|
$translator = $builder->addDefinition('l10n_nette_translator.translator'); |
|
20
|
3 |
|
$translator->setClass('l10nNetteTranslator\Translator'); |
|
21
|
|
|
|
|
22
|
3 |
|
foreach ($config['languages'] as $language) { |
|
23
|
3 |
|
if (empty($language['lang'])) { |
|
24
|
1 |
|
throw new InvalidStateException('Key "lang" must be set'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
2 |
|
$lang = $builder->literal("new $language[lang]"); |
|
|
|
|
|
|
28
|
2 |
|
$plural = isset($language['plural']) ? $builder->literal("new $language[plural]") : $lang; |
|
|
|
|
|
|
29
|
2 |
|
$default = !empty($language['default']); |
|
30
|
|
|
|
|
31
|
2 |
|
$translator->addSetup('addLanguageAndPlural', [$lang, $plural, $default]); |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
if (!empty($config['storage'])) { |
|
35
|
1 |
|
$translator->addSetup('setStorage', [$config['storage']]); |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
$panel = $builder->addDefinition('l10n_nette_translator.panel'); |
|
39
|
2 |
|
$panel->setClass('l10nNetteTranslator\Panel'); |
|
40
|
|
|
|
|
41
|
2 |
|
$processor = $builder->addDefinition('l10n_nette_translator.processor'); |
|
42
|
2 |
|
$processor->setClass('l10nNetteTranslator\TranslatorProcessor'); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function afterCompile(ClassType $class) { |
|
46
|
1 |
|
$initialize = $class->getMethod('initialize'); |
|
47
|
1 |
|
$initialize->addBody('$this->getService("tracy.bar")->addPanel($this->getService("l10n_nette_translator.panel"));'); |
|
48
|
1 |
|
$initialize->addBody('$response = $this->getService("l10n_nette_translator.processor")->run();'); |
|
49
|
1 |
|
$initialize->addBody('if($response instanceof Nette\Application\IResponse) {'); |
|
50
|
1 |
|
$initialize->addBody(' $response->send($this->getByType("Nette\Http\IRequest"), $this->getByType("Nette\Http\IResponse"));'); |
|
51
|
1 |
|
$initialize->addBody(' exit();'); |
|
52
|
1 |
|
$initialize->addBody('}'); |
|
53
|
1 |
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.