|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\UUDI\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
|
|
|
|
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
8
|
|
|
use SilverStripe\Forms\FieldList; |
|
9
|
|
|
use SilverStripe\Forms\ReadonlyField; |
|
10
|
|
|
use SilverStripe\ORM\DataExtension; |
|
11
|
|
|
use SilverStripe\ORM\DataObject; |
|
12
|
|
|
use Sunnysideup\UUDI\Api\HashCreator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class \Sunnysideup\UUDI\Extensions\UUIDExtension. |
|
16
|
|
|
* |
|
17
|
|
|
* @property string $UUID |
|
18
|
|
|
* @property string $PublicUUID |
|
19
|
|
|
*/ |
|
20
|
|
|
class UUIDExtension extends DataExtension |
|
21
|
|
|
{ |
|
22
|
|
|
private static $db = [ |
|
23
|
|
|
'RequiresUUID' => 'Boolean(0)', //32 + 1 + 32 |
|
24
|
|
|
'UUID' => 'Varchar(65)', //32 + 1 + 32 |
|
25
|
|
|
'PublicUUID' => 'Varchar(12)', //32 + 1 + 32 |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
private static $indexes = [ |
|
29
|
|
|
'RequiresUUID' => true, |
|
30
|
|
|
'UUID' => false, |
|
31
|
|
|
'PublicUUID' => true, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
private $UUIDNeverAgainRaceCondition = false; |
|
35
|
|
|
|
|
36
|
|
|
public function onBeforeWrite() |
|
37
|
|
|
{ |
|
38
|
|
|
$owner = $this->getOwner(); |
|
39
|
|
|
if ($this->requiresUUID()) { |
|
40
|
|
|
if(!$owner->UUID) { |
|
41
|
|
|
$owner->UUID = $this->getHashID(); |
|
42
|
|
|
} |
|
43
|
|
|
} else { |
|
44
|
|
|
$this->UUIDNeverAgainRaceCondition = true; |
|
45
|
|
|
$owner->UUID = ''; |
|
46
|
|
|
} |
|
47
|
|
|
if (! $owner->PublicUUID || 'ERROR' === $owner->PublicUUID) { |
|
48
|
|
|
$owner->PublicUUID = $this->calculatePublicUUID(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function onAfterWrite() |
|
53
|
|
|
{ |
|
54
|
|
|
$owner = $this->getOwner(); |
|
55
|
|
|
if (! $owner->UUID && false === $this->UUIDNeverAgainRaceCondition) { |
|
56
|
|
|
$this->UUIDNeverAgainRaceCondition = true; |
|
57
|
|
|
$owner->write(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function calculatePublicUUID(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return HashCreator::generate_hash(12); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function updateCMSFields(FieldList $fields) |
|
67
|
|
|
{ |
|
68
|
|
|
$owner = $this->owner; |
|
69
|
|
|
if (! ($owner instanceof SiteTree)) { |
|
70
|
|
|
$this->updateCMSFieldsForHashId($fields); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function updateSettingsFields(FieldList $fields) |
|
75
|
|
|
{ |
|
76
|
|
|
$owner = $this->owner; |
|
77
|
|
|
if ($owner instanceof SiteTree) { |
|
78
|
|
|
$this->updateCMSFieldsForHashId($fields); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function updateCMSFieldsForHashId(FieldList $fields) |
|
83
|
|
|
{ |
|
84
|
|
|
/** @var DataObject $owner */ |
|
85
|
|
|
$owner = $this->owner; |
|
86
|
|
|
$fields->removeByName( |
|
87
|
|
|
[ |
|
88
|
|
|
'RequiresUUID', |
|
89
|
|
|
'UUID', |
|
90
|
|
|
'PublicUUID', |
|
91
|
|
|
] |
|
92
|
|
|
); |
|
93
|
|
|
if ($owner->hasMethod('ShowUUIDInCMS')) { |
|
94
|
|
|
$tab = 'Root.UUID'; |
|
95
|
|
|
if ($owner->hasMethod('UUIDTabInCMS')) { |
|
96
|
|
|
$tab = $owner->UUIDTabInCMS(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$fields->addFieldsToTab( |
|
100
|
|
|
$tab, |
|
101
|
|
|
[ |
|
102
|
|
|
// ReadonlyField::create('MyUUID', 'Private UUID', $owner->UUID), |
|
103
|
|
|
CheckboxField::create('RequiresUUID', 'Has UUID', $owner->PublicUUID)->performDisabledTransformation, |
|
104
|
|
|
ReadonlyField::create('MyPublicUUID', 'Public UUID', $owner->PublicUUID), |
|
105
|
|
|
] |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Gets a truly unique identifier to the classname and ID. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getHashID(): ?string |
|
114
|
|
|
{ |
|
115
|
|
|
$owner = $this->getOwner(); |
|
116
|
|
|
if ($owner->ID) { |
|
117
|
|
|
return HashCreator::create_hash_id($owner->ClassName, $owner->ID) . '_' . HashCreator::generate_hash(32); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return ''; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function requiresUUID(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
$owner = $this->getOwner(); |
|
126
|
|
|
return $owner->RequiresUUID || Config::inst()->get(HashCreator::class, 'always_require_uuid'); |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths