1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CoreBundle\Doctrine\EventSubscriber; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\EventSubscriber; |
16
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
17
|
|
|
use Doctrine\ORM\Events; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class TimestampableSubscriber |
21
|
|
|
* |
22
|
|
|
* @author Adam Piotrowski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class TimestampableSubscriber implements EventSubscriber |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array Timestampable fields |
28
|
|
|
*/ |
29
|
|
|
protected $fields = ['createdAt', 'updatedAt']; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Doctrine\ORM\Mapping\ClassMetadataInfo |
33
|
|
|
*/ |
34
|
|
|
protected $classMetadata; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns subscribed events |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function getSubscribedEvents() |
42
|
|
|
{ |
43
|
|
|
return [Events::loadClassMetadata]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Event triggered during metadata loading |
48
|
|
|
* |
49
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
50
|
|
|
*/ |
51
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
52
|
|
|
{ |
53
|
|
|
$this->classMetadata = $eventArgs->getClassMetadata(); |
|
|
|
|
54
|
|
|
$reflectionClass = $this->classMetadata->getReflectionClass(); |
55
|
|
|
|
56
|
|
|
if (null === $reflectionClass) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($this->hasMethod($reflectionClass, 'updateTimestamps')) { |
61
|
|
|
$this->addLifecycleCallbacks(); |
62
|
|
|
$this->mapFields(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds timestampable lifecycle callbacks to entity |
68
|
|
|
*/ |
69
|
|
|
protected function addLifecycleCallbacks() |
70
|
|
|
{ |
71
|
|
|
foreach ($this->getLifecycleCallbacks() as $callback) { |
72
|
|
|
$this->classMetadata->addLifecycleCallback('updateTimestamps', $callback); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getLifecycleCallbacks() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
Events::prePersist, |
80
|
|
|
Events::preUpdate |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Iterates through fields and adds mapping to them |
86
|
|
|
*/ |
87
|
|
|
protected function mapFields() |
88
|
|
|
{ |
89
|
|
|
foreach ($this->fields as $field) { |
90
|
|
|
$this->mapField($field); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds mapping to single field |
96
|
|
|
* |
97
|
|
|
* @param string $field |
98
|
|
|
*/ |
99
|
|
|
protected function mapField($field) |
100
|
|
|
{ |
101
|
|
|
if (!$this->classMetadata->hasField($field)) { |
102
|
|
|
$this->classMetadata->mapField([ |
103
|
|
|
'fieldName' => $field, |
104
|
|
|
'type' => 'datetime', |
105
|
|
|
'nullable' => true, |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Checks whether the class contains such a method |
112
|
|
|
* |
113
|
|
|
* @param \ReflectionClass $class |
114
|
|
|
* @param string $methodName |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function hasMethod(\ReflectionClass $class, $methodName) |
119
|
|
|
{ |
120
|
|
|
return $class->hasMethod($methodName); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..