|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\Common\DI; |
|
6
|
|
|
|
|
7
|
|
|
use Kdyby; |
|
8
|
|
|
use Nette; |
|
9
|
|
|
use SixtyEightPublishers; |
|
10
|
|
|
|
|
11
|
|
|
final class CommonExtensionAdapter extends SixtyEightPublishers\User\DI\AbstractExtensionAdapter implements Kdyby\Doctrine\DI\ITargetEntityProvider |
|
12
|
|
|
{ |
|
13
|
|
|
const SHARED_DATA_USER_CLASS_NAME = 'common.user_class_name'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
protected static $defaults = [ |
|
17
|
|
|
'user' => [ |
|
18
|
|
|
'class' => '', |
|
19
|
|
|
'fields' => [], |
|
20
|
|
|
], |
|
21
|
|
|
'password_strategy' => SixtyEightPublishers\User\Common\PasswordHashStrategy\DefaultPasswordHashStrategy::class, |
|
22
|
|
|
'logger' => SixtyEightPublishers\User\Common\Logger\TracyLogger::class, |
|
23
|
|
|
'mail_sender' => SixtyEightPublishers\User\Common\Mail\NullMailSender::class, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function processConfig(array $config, \ArrayObject $sharedData): array |
|
30
|
|
|
{ |
|
31
|
|
|
Nette\Utils\Validators::assertField($config, 'user', 'array'); |
|
32
|
|
|
Nette\Utils\Validators::assertField($config['user'], 'class', 'string'); |
|
33
|
|
|
Nette\Utils\Validators::assertField($config['user'], 'fields', 'array'); |
|
34
|
|
|
|
|
35
|
|
|
Nette\Utils\Validators::assertField($config, 'password_strategy', 'string|' . Nette\DI\Statement::class); |
|
36
|
|
|
Nette\Utils\Validators::assertField($config, 'logger', 'string|' . Nette\DI\Statement::class); |
|
37
|
|
|
Nette\Utils\Validators::assertField($config, 'mail_sender', 'string|' . Nette\DI\Statement::class); |
|
38
|
|
|
|
|
39
|
|
|
if (!is_subclass_of($config['user']['class'], SixtyEightPublishers\User\Common\DoctrineEntity\IUser::class, TRUE)) { |
|
40
|
|
|
throw new SixtyEightPublishers\User\Common\Exception\ConfigurationException(sprintf( |
|
41
|
|
|
'Required setting %s.user_class_name must be valid classname of your User\'s entity that implements interface %s', |
|
42
|
|
|
$this->name, |
|
43
|
|
|
SixtyEightPublishers\User\Common\DoctrineEntity\IUser::class |
|
44
|
|
|
)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$sharedData[self::SHARED_DATA_USER_CLASS_NAME] = $config['user']['class']; |
|
48
|
|
|
|
|
49
|
|
|
return $config; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function loadConfiguration(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$config = $this->getConfig(); |
|
58
|
|
|
$builder = $this->getContainerBuilder(); |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
if (TRUE === $this->needRegister($config['password_strategy'])) { |
|
|
|
|
|
|
61
|
|
|
$builder->addDefinition($this->prefix('password_strategy')) |
|
62
|
|
|
->setType(SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy::class) |
|
63
|
|
|
->setFactory($config['password_strategy']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
if (TRUE === $this->needRegister($config['logger'])) { |
|
|
|
|
|
|
67
|
|
|
$builder->addDefinition($this->prefix('logger')) |
|
68
|
|
|
->setType(SixtyEightPublishers\User\Common\Logger\ILogger::class) |
|
69
|
|
|
->setFactory($config['logger']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
if (TRUE === $this->needRegister($config['mail_sender'])) { |
|
|
|
|
|
|
73
|
|
|
$builder->addDefinition($this->prefix('mail_sender')) |
|
74
|
|
|
->setType(SixtyEightPublishers\User\Common\Mail\IMailSender::class) |
|
75
|
|
|
->setFactory($config['mail_sender']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$fields = $config['user']['fields']; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($fields as $k => $v) { |
|
81
|
|
|
if (defined($k)) { |
|
82
|
|
|
unset($fields[$k]); |
|
83
|
|
|
$fields[constant($k)] = $v; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$builder->addDefinition($this->prefix('user_mapping')) |
|
88
|
|
|
->setType(SixtyEightPublishers\User\Common\UserMapping::class) |
|
89
|
|
|
->setArguments([ |
|
90
|
|
|
'className' => $config['user']['class'], |
|
91
|
|
|
'fields' => $fields, |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function beforeCompile(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$builder = $this->getContainerBuilder(); |
|
101
|
|
|
$translator = $builder->getByType(Nette\Localization\ITranslator::class, FALSE); |
|
102
|
|
|
|
|
103
|
|
|
if (NULL === $translator) { |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$translator = $builder->getDefinition($translator); |
|
108
|
|
|
|
|
109
|
|
|
/** @var \Nette\DI\ServiceDefinition[] $translatableServices */ |
|
110
|
|
|
$translatableServices = array_filter($builder->getDefinitions(), function (Nette\DI\ServiceDefinition $def) { |
|
111
|
|
|
return is_a($def->getImplement(), SixtyEightPublishers\User\Common\Translator\ITranslatorAware::class, TRUE) |
|
112
|
|
|
|| ($def->getImplementMode() !== $def::IMPLEMENT_MODE_GET && is_a($def->getType(), SixtyEightPublishers\User\Common\Translator\ITranslatorAware::class, TRUE)); |
|
113
|
|
|
}); |
|
114
|
|
|
|
|
115
|
|
|
foreach ($translatableServices as $translatableService) { |
|
116
|
|
|
$translatableService->addSetup('setTranslator', [ |
|
117
|
|
|
'translator' => $translator, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param mixed $what |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
private function needRegister($what): bool |
|
128
|
|
|
{ |
|
129
|
|
|
return (!is_string($what) || !Nette\Utils\Strings::startsWith($what, '@')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/ |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getTargetEntityMappings(): array |
|
138
|
|
|
{ |
|
139
|
|
|
return [ |
|
140
|
|
|
SixtyEightPublishers\User\Common\DoctrineEntity\IUser::class => $this->getSharedData(self::SHARED_DATA_USER_CLASS_NAME), |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.