1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\Common\DI; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Nette\Schema\Expect; |
9
|
|
|
use Nette\Schema\Schema; |
10
|
|
|
use Nette\DI\CompilerExtension; |
11
|
|
|
use Nette\DI\Definitions\Statement; |
12
|
|
|
use SixtyEightPublishers\User\Common\UserMapping; |
13
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\DatabaseType; |
14
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntity; |
15
|
|
|
use SixtyEightPublishers\User\Common\Logger\TracyLogger; |
16
|
|
|
use SixtyEightPublishers\User\Common\Mail\NullMailSender; |
17
|
|
|
use SixtyEightPublishers\User\Common\Entity\UserInterface; |
18
|
|
|
use SixtyEightPublishers\User\Common\Logger\LoggerInterface; |
19
|
|
|
use SixtyEightPublishers\User\Common\Mail\MailSenderInterface; |
20
|
|
|
use SixtyEightPublishers\User\DI\AbstractCompilerExtensionPass; |
21
|
|
|
use SixtyEightPublishers\User\Common\DbalType\Password\PasswordType; |
22
|
|
|
use SixtyEightPublishers\User\Common\Exception\ConfigurationException; |
23
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\DatabaseTypeProviderInterface; |
24
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntityProviderInterface; |
25
|
|
|
use SixtyEightPublishers\User\Common\DbalType\Password\PasswordInterface; |
26
|
|
|
use SixtyEightPublishers\User\Common\PasswordHashStrategy\DefaultPasswordHashStrategy; |
27
|
|
|
use SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface; |
28
|
|
|
|
29
|
|
|
final class CommonExtension extends AbstractCompilerExtensionPass implements TargetEntityProviderInterface, DatabaseTypeProviderInterface |
30
|
|
|
{ |
31
|
|
|
public const SHARED_DATA_USER_CLASS_NAME = 'common.user_class_name'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function attach(CompilerExtension $extension, ArrayObject $sharedData): void |
37
|
|
|
{ |
38
|
|
|
parent::attach($extension, $sharedData); |
39
|
|
|
|
40
|
|
|
if (!is_subclass_of($this->config->user->class, UserInterface::class, TRUE)) { |
41
|
|
|
throw new ConfigurationException(sprintf( |
42
|
|
|
'Required setting %s.user.class must be valid classname of your User\'s entity that implements interface %s', |
43
|
|
|
$this->name, |
44
|
|
|
UserInterface::class |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$sharedData[self::SHARED_DATA_USER_CLASS_NAME] = $this->config->user->class; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function getConfigSchema(): Schema |
55
|
|
|
{ |
56
|
|
|
return Expect::structure([ |
57
|
|
|
'user' => Expect::structure([ |
58
|
|
|
'class' => Expect::string()->required(), |
59
|
|
|
'fields' => Expect::array(), |
60
|
|
|
]), |
61
|
|
|
'password_strategy' => Expect::anyOf(Expect::string(), Expect::type(Statement::class)) |
62
|
|
|
->default(DefaultPasswordHashStrategy::class) |
63
|
|
|
->before(static function ($def) { |
64
|
|
|
return $def instanceof Statement ? $def : new Statement($def); |
|
|
|
|
65
|
|
|
}), |
66
|
|
|
'logger' => Expect::anyOf(Expect::string(), Expect::type(Statement::class)) |
67
|
|
|
->default(TracyLogger::class) |
68
|
|
|
->before(static function ($def) { |
69
|
|
|
return $def instanceof Statement ? $def : new Statement($def); |
|
|
|
|
70
|
|
|
}), |
71
|
|
|
'mail_sender' => Expect::anyOf(Expect::string(), Expect::type(Statement::class)) |
72
|
|
|
->default(NullMailSender::class) |
73
|
|
|
->before(static function ($def) { |
74
|
|
|
return $def instanceof Statement ? $def : new Statement($def); |
|
|
|
|
75
|
|
|
}), |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
|
|
public function loadConfiguration(): void |
83
|
|
|
{ |
84
|
|
|
$builder = $this->getContainerBuilder(); |
85
|
|
|
|
86
|
|
|
$builder->addDefinition($this->prefix('password_strategy')) |
87
|
|
|
->setType(PasswordHashStrategyInterface::class) |
88
|
|
|
->setFactory($this->config->password_strategy); |
89
|
|
|
|
90
|
|
|
$builder->addDefinition($this->prefix('logger')) |
91
|
|
|
->setType(LoggerInterface::class) |
92
|
|
|
->setFactory($this->config->logger); |
93
|
|
|
|
94
|
|
|
$builder->addDefinition($this->prefix('mail_sender')) |
95
|
|
|
->setType(MailSenderInterface::class) |
96
|
|
|
->setFactory($this->config->mail_sender); |
97
|
|
|
|
98
|
|
|
$fields = $this->config->user->fields; |
99
|
|
|
|
100
|
|
|
foreach ($fields as $k => $v) { |
101
|
|
|
if (defined($k)) { |
102
|
|
|
unset($fields[$k]); |
103
|
|
|
$fields[constant($k)] = $v; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$builder->addDefinition($this->prefix('user_mapping')) |
108
|
|
|
->setType(UserMapping::class) |
109
|
|
|
->setArguments([$this->config->user->class, $fields]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getTargetEntities(): array |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
new TargetEntity(UserInterface::class, $this->sharedData[self::SHARED_DATA_USER_CLASS_NAME]), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getDatabaseTypes(): array |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
new DatabaseType(PasswordInterface::class, PasswordType::class), |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.