1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Zenify |
7
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zenify\DoctrineBehaviors\DI; |
11
|
|
|
|
12
|
|
|
use Kdyby; |
13
|
|
|
use Kdyby\Events\DI\EventsExtension; |
14
|
|
|
use Knp\DoctrineBehaviors\Model\Translatable\Translation; |
15
|
|
|
use Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber; |
16
|
|
|
use Nette\Utils\AssertionException; |
17
|
|
|
use Nette\Utils\Validators; |
18
|
|
|
use Zenify\DoctrineBehaviors\Entities\Attributes\Translatable; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
final class TranslatableExtension extends AbstractBehaviorExtension |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $default = [ |
28
|
|
|
'currentLocaleCallable' => NULL, |
29
|
|
|
'defaultLocaleCallable' => NULL, |
30
|
|
|
'translatableTrait' => Translatable::class, |
31
|
|
|
'translationTrait' => Translation::class, |
32
|
|
|
'translatableFetchMode' => 'LAZY', |
33
|
|
|
'translationFetchMode' => 'LAZY', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
public function loadConfiguration() |
38
|
|
|
{ |
39
|
1 |
|
$config = $this->getConfig($this->default); |
40
|
1 |
|
$this->validateConfigTypes($config); |
|
|
|
|
41
|
1 |
|
$builder = $this->getContainerBuilder(); |
42
|
|
|
|
43
|
1 |
|
$builder->addDefinition($this->prefix('listener')) |
44
|
1 |
|
->setClass(TranslatableSubscriber::class, [ |
45
|
1 |
|
'@' . $this->getClassAnalyzer()->getClass(), |
46
|
1 |
|
$config['currentLocaleCallable'], |
47
|
1 |
|
$config['defaultLocaleCallable'], |
48
|
1 |
|
$config['translatableTrait'], |
49
|
1 |
|
$config['translationTrait'], |
50
|
1 |
|
$config['translatableFetchMode'], |
51
|
1 |
|
$config['translationFetchMode'] |
52
|
|
|
]) |
53
|
1 |
|
->setAutowired(FALSE) |
54
|
1 |
|
->addTag(EventsExtension::TAG_SUBSCRIBER); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws AssertionException |
60
|
|
|
*/ |
61
|
1 |
|
private function validateConfigTypes(array $config) |
62
|
|
|
{ |
63
|
1 |
|
Validators::assertField($config, 'currentLocaleCallable', 'null|array'); |
64
|
1 |
|
Validators::assertField($config, 'translatableTrait', 'type'); |
65
|
1 |
|
Validators::assertField($config, 'translationTrait', 'type'); |
66
|
1 |
|
Validators::assertField($config, 'translatableFetchMode', 'string'); |
67
|
1 |
|
Validators::assertField($config, 'translationFetchMode', 'string'); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.