1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codefocus\ManagedCache; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
class Condition |
9
|
|
|
{ |
10
|
|
|
const CACHE_TAG_PREFIX = 'ManagedCache:'; |
11
|
|
|
const CACHE_TAG_SEPARATOR = '-'; |
12
|
|
|
const CACHE_TAG_ID_OPEN = '('; |
13
|
|
|
const CACHE_TAG_ID_CLOSE = ')'; |
14
|
|
|
const CACHE_TAG_RELATED = '->'; |
15
|
|
|
|
16
|
|
|
protected $eventName = null; |
17
|
|
|
|
18
|
|
|
protected $modelName = null; |
19
|
|
|
|
20
|
|
|
protected $modelId = null; |
21
|
|
|
|
22
|
|
|
protected $relatedModelName = null; |
23
|
|
|
|
24
|
|
|
protected $relatedModelId = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $eventName |
30
|
|
|
* @param string|null $modelName (default: null) |
31
|
|
|
* @param int|null $modelId (default: null) |
32
|
|
|
* @param string|null $relatedModelName (default: null) |
33
|
|
|
* @param int|null $relatedModelId (default: null) |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
string $eventName, |
37
|
|
|
?string $modelName = null, |
38
|
|
|
?int $modelId = null, |
39
|
|
|
?string $relatedModelName = null, |
40
|
|
|
?int $relatedModelId = null |
41
|
|
|
) { |
42
|
|
|
$this->eventName = $eventName; |
43
|
|
|
$this->modelName = $modelName; |
44
|
|
|
$this->modelId = $modelId; |
45
|
|
|
$this->relatedModelName = $relatedModelName; |
46
|
|
|
$this->relatedModelId = $relatedModelId; |
47
|
|
|
|
48
|
|
|
if (null !== $this->modelName) { |
49
|
|
|
$this->assertIsModel($this->modelName); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return a string representation of this Condition. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function __toString(): string |
59
|
|
|
{ |
60
|
|
|
if (null === $this->modelName) { |
61
|
|
|
// All events of this type |
62
|
|
|
// trigger a flush. |
63
|
|
|
return |
64
|
|
|
self::CACHE_TAG_PREFIX . |
65
|
|
|
$this->eventName; |
66
|
|
|
} |
67
|
|
|
$modelTagPart = $this->getModelTagPart(); |
68
|
|
|
$relationTagPart = $this->getRelationTagPart(); |
69
|
|
|
if (null === $relationTagPart) { |
70
|
|
|
return |
71
|
|
|
self::CACHE_TAG_PREFIX . |
72
|
|
|
$this->eventName . |
73
|
|
|
self::CACHE_TAG_SEPARATOR . |
74
|
|
|
$modelTagPart . |
75
|
|
|
self::CACHE_TAG_SEPARATOR . |
76
|
|
|
$relationTagPart; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return |
80
|
|
|
self::CACHE_TAG_PREFIX . |
81
|
|
|
$this->eventName . |
82
|
|
|
self::CACHE_TAG_SEPARATOR . |
83
|
|
|
$modelTagPart; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get Relation Tag Part. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function getModelTagPart(): string |
92
|
|
|
{ |
93
|
|
|
if (null === $this->modelId) { |
94
|
|
|
// Any instance of this model. |
95
|
|
|
return $this->modelName; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Only the instance of this model with this id. |
99
|
|
|
return $this->modelName . |
100
|
|
|
self::CACHE_TAG_ID_OPEN . |
101
|
|
|
$this->modelId . |
102
|
|
|
self::CACHE_TAG_ID_CLOSE; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get Relation Tag Part. |
107
|
|
|
* |
108
|
|
|
* @return string|null |
109
|
|
|
*/ |
110
|
|
|
protected function getRelationTagPart(): ?string |
111
|
|
|
{ |
112
|
|
|
if (null === $this->relatedModelId) { |
113
|
|
|
if (null === $this->relatedModelName) { |
114
|
|
|
// No relation specified. |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
// Any instance of this related model. |
118
|
|
|
return $this->relatedModelName; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Only the instance of this related model with this id. |
122
|
|
|
return $this->relatedModelName . |
123
|
|
|
self::CACHE_TAG_ID_OPEN . |
124
|
|
|
$this->relatedModelId . |
125
|
|
|
self::CACHE_TAG_ID_CLOSE; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Throws an Exception if the specified class name is not an Eloquent Model. |
130
|
|
|
* |
131
|
|
|
* @param string $modelClassName |
132
|
|
|
*/ |
133
|
|
|
protected function assertIsModel(string $modelClassName): void |
134
|
|
|
{ |
135
|
|
|
if ( ! is_subclass_of($modelClassName, Model::class)) { |
136
|
|
|
throw new Exception($modelClassName . ' is not an Eloquent Model class name.'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|