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\Events\DI\EventsExtension; |
13
|
|
|
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable; |
14
|
|
|
use Knp\DoctrineBehaviors\ORM\Timestampable\TimestampableSubscriber; |
15
|
|
|
use Nette\Utils\AssertionException; |
16
|
|
|
use Nette\Utils\Validators; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
final class TimestampableExtension extends AbstractBehaviorExtension |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $default = [ |
26
|
|
|
'isRecursive' => TRUE, |
27
|
|
|
'trait' => Timestampable::class, |
28
|
|
|
'dbFieldType' => 'datetime', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
public function loadConfiguration() |
33
|
|
|
{ |
34
|
1 |
|
$config = $this->getConfig($this->default); |
35
|
1 |
|
$this->validateConfigTypes($config); |
|
|
|
|
36
|
1 |
|
$builder = $this->getContainerBuilder(); |
37
|
|
|
|
38
|
1 |
|
$builder->addDefinition($this->prefix('listener')) |
39
|
1 |
|
->setClass(TimestampableSubscriber::class, [ |
40
|
1 |
|
'@' . $this->getClassAnalyzer()->getClass(), |
41
|
1 |
|
$config['isRecursive'], |
42
|
1 |
|
$config['trait'], |
43
|
1 |
|
$config['dbFieldType'], |
44
|
|
|
]) |
45
|
1 |
|
->setAutowired(FALSE) |
46
|
1 |
|
->addTag(EventsExtension::TAG_SUBSCRIBER); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws AssertionException |
52
|
|
|
*/ |
53
|
1 |
|
private function validateConfigTypes(array $config) |
54
|
|
|
{ |
55
|
1 |
|
Validators::assertField($config, 'isRecursive', 'bool'); |
56
|
1 |
|
Validators::assertField($config, 'trait', 'type'); |
57
|
1 |
|
Validators::assertField($config, 'dbFieldType', 'string'); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
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.