1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\RawValue; |
15
|
|
|
use Phalcon\Encryption\Security; |
|
|
|
|
16
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior; |
17
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractInjectable; |
18
|
|
|
use Zemit\Mvc\Model\Behavior\Transformable; |
19
|
|
|
|
20
|
|
|
trait Uuid |
21
|
|
|
{ |
22
|
|
|
use AbstractBehavior; |
23
|
|
|
use AbstractInjectable; |
24
|
|
|
use Options; |
25
|
|
|
use Behavior; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializing Uuid |
29
|
|
|
*/ |
30
|
2 |
|
public function initializeUuid(?array $options = null): void |
31
|
|
|
{ |
32
|
2 |
|
$options ??= $this->getOptionsManager()->get('uuid') ?? []; |
33
|
|
|
|
34
|
2 |
|
$field = $options['field'] ?? 'uuid'; |
35
|
2 |
|
$native = $options['native'] ?? true; |
36
|
2 |
|
$binary = $options['binary'] ?? true; |
37
|
|
|
|
38
|
2 |
|
$security = $this->getDI()->get('security'); |
39
|
2 |
|
assert($security instanceof Security); |
40
|
|
|
|
41
|
2 |
|
$this->setUuidBehavior(new Transformable([ |
42
|
2 |
|
'beforeValidationOnCreate' => [ |
43
|
2 |
|
$field => function ($model, $field) use ($security, $native, $binary) { |
44
|
|
|
return $model->getAttribute($field) ?? $native |
45
|
|
|
? ($binary |
46
|
|
|
? new RawValue('UUID_TO_BIN(UUID())') |
47
|
|
|
: new RawValue('UUID()') |
48
|
|
|
) |
49
|
|
|
: ($binary |
50
|
|
|
? $this->getBinaryUuid($security->getRandom()->uuid()) |
51
|
|
|
: $security->getRandom()->uuid() |
52
|
|
|
); |
53
|
2 |
|
}, |
54
|
2 |
|
], |
55
|
2 |
|
'afterFetch' => [ |
56
|
2 |
|
$field => function ($model, $field) use ($binary) { |
57
|
|
|
// $native not yet supported while fetching |
58
|
|
|
$value = $model->getAttribute($field); |
59
|
|
|
if ($binary && !empty($value)) { |
60
|
|
|
$hex = bin2hex($value); |
61
|
|
|
$uuid = sprintf( |
62
|
|
|
'%s-%s-%s-%s-%s', |
63
|
|
|
substr($hex, 0, 8), |
64
|
|
|
substr($hex, 8, 4), |
65
|
|
|
substr($hex, 12, 4), |
66
|
|
|
substr($hex, 16, 4), |
67
|
|
|
substr($hex, 20, 12) |
68
|
|
|
); |
69
|
|
|
$this->setAttribute($field, $uuid); |
|
|
|
|
70
|
|
|
return $uuid; |
71
|
|
|
} |
72
|
2 |
|
}, |
73
|
2 |
|
], |
74
|
2 |
|
])); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get a binary representation of UUID |
79
|
|
|
* |
80
|
|
|
* @param string $uuid |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
private function getBinaryUuid($uuid) |
84
|
|
|
{ |
85
|
|
|
return pack('h*', str_replace('-', '', $uuid)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set Uuid Behavior |
90
|
|
|
*/ |
91
|
2 |
|
public function setUuidBehavior(Transformable $uuidBehavior): void |
92
|
|
|
{ |
93
|
2 |
|
$this->setBehavior('uuid', $uuidBehavior); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get Uuid Behavior |
98
|
|
|
*/ |
99
|
|
|
public function getUuidBehavior(): Transformable |
100
|
|
|
{ |
101
|
|
|
$behavior = $this->getBehavior('uuid'); |
102
|
|
|
assert($behavior instanceof Transformable); |
103
|
|
|
return $behavior; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: