1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Filesystem; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Provides a mechanism for determining the effective visibility of a set of assets (identified by |
7
|
|
|
* filename and hash), given their membership to objects of varying visibility. |
8
|
|
|
* |
9
|
|
|
* The effective visibility of assets is based on three rules: |
10
|
|
|
* - If an asset is attached to any public record, that asset is public. |
11
|
|
|
* - If an asset is not attached to any public record, but is attached to a protected record, |
12
|
|
|
* that asset is protected. |
13
|
|
|
* - If an asset is attached to a record being deleted, but not any existing public or protected |
14
|
|
|
* record, then that asset is marked for deletion. |
15
|
|
|
* |
16
|
|
|
* Variants are ignored for the purpose of determining visibility |
17
|
|
|
*/ |
18
|
|
|
class AssetManipulationList |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const STATE_PUBLIC = 'public'; |
22
|
|
|
|
23
|
|
|
const STATE_PROTECTED = 'protected'; |
24
|
|
|
|
25
|
|
|
const STATE_DELETED = 'deleted'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* List of public assets |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $public = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* List of protected assets |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $protected = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* List of deleted assets |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $deleted = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get an identifying key for a given filename and hash |
50
|
|
|
* |
51
|
|
|
* @param array $asset Asset tuple |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected function getAssetKey($asset) |
55
|
|
|
{ |
56
|
|
|
return $asset['Hash'] . '/' . $asset['Filename']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add asset with the given state |
61
|
|
|
* |
62
|
|
|
* @param array $asset Asset tuple |
63
|
|
|
* @param string $state One of the STATE_* const vars |
64
|
|
|
* @return bool True if the asset was added to the set matching the given state |
65
|
|
|
*/ |
66
|
|
|
public function addAsset($asset, $state) |
67
|
|
|
{ |
68
|
|
|
switch ($state) { |
69
|
|
|
case self::STATE_PUBLIC: |
70
|
|
|
return $this->addPublicAsset($asset); |
71
|
|
|
case self::STATE_PROTECTED: |
72
|
|
|
return $this->addProtectedAsset($asset); |
73
|
|
|
case self::STATE_DELETED: |
74
|
|
|
return $this->addDeletedAsset($asset); |
75
|
|
|
default: |
76
|
|
|
throw new \InvalidArgumentException("Invalid state {$state}"); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Mark a file as public |
82
|
|
|
* |
83
|
|
|
* @param array $asset Asset tuple |
84
|
|
|
* @return bool True if the asset was added to the public set |
85
|
|
|
*/ |
86
|
|
|
public function addPublicAsset($asset) |
87
|
|
|
{ |
88
|
|
|
// Remove from protected / deleted lists |
89
|
|
|
$key = $this->getAssetKey($asset); |
90
|
|
|
unset($this->protected[$key]); |
91
|
|
|
unset($this->deleted[$key]); |
92
|
|
|
// Skip if already public |
93
|
|
|
if(isset($this->public[$key])) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
unset($asset['Variant']); |
97
|
|
|
$this->public[$key] = $asset; |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Record an asset as protected |
103
|
|
|
* |
104
|
|
|
* @param array $asset Asset tuple |
105
|
|
|
* @return bool True if the asset was added to the protected set |
106
|
|
|
*/ |
107
|
|
|
public function addProtectedAsset($asset) |
108
|
|
|
{ |
109
|
|
|
$key = $this->getAssetKey($asset); |
110
|
|
|
// Don't demote from public |
111
|
|
|
if (isset($this->public[$key])) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
unset($this->deleted[$key]); |
115
|
|
|
// Skip if already protected |
116
|
|
|
if(isset($this->protected[$key])) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
unset($asset['Variant']); |
120
|
|
|
$this->protected[$key] = $asset; |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Record an asset as deleted |
126
|
|
|
* |
127
|
|
|
* @param array $asset Asset tuple |
128
|
|
|
* @return bool True if the asset was added to the deleted set |
129
|
|
|
*/ |
130
|
|
|
public function addDeletedAsset($asset) |
131
|
|
|
{ |
132
|
|
|
$key = $this->getAssetKey($asset); |
133
|
|
|
// Only delete if this doesn't exist in any non-deleted state |
134
|
|
|
if (isset($this->public[$key]) || isset($this->protected[$key])) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
// Skip if already deleted |
138
|
|
|
if(isset($this->deleted[$key])) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
unset($asset['Variant']); |
142
|
|
|
$this->deleted[$key] = $asset; |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get all public assets |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function getPublicAssets() |
152
|
|
|
{ |
153
|
|
|
return $this->public; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get protected assets |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
public function getProtectedAssets() |
162
|
|
|
{ |
163
|
|
|
return $this->protected; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get deleted assets |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function getDeletedAssets() |
172
|
|
|
{ |
173
|
|
|
return $this->deleted; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|