File.php
5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare( strict_types=1 );
namespace ACP\ListScreenRepository;
use AC;
use AC\Exception\MissingListScreenIdException;
use AC\Exception\SourceNotAvailableException;
use AC\ListScreen;
use AC\ListScreenCollection;
use AC\ListScreenRepository\Filter;
use AC\ListScreenRepository\ListScreenPermissionTrait;
use AC\ListScreenRepository\Sort;
use AC\ListScreenRepository\SourceAware;
use AC\OpCacheInvalidateTrait;
use AC\Type\ListScreenId;
use ACP\Exception\DecoderNotFoundException;
use ACP\Exception\DirectoryNotWritableException;
use ACP\Exception\FileNotWritableException;
use ACP\Storage\Directory;
use ACP\Storage\ListScreen\DecoderFactory;
use ACP\Storage\ListScreen\Encoder;
use ACP\Storage\ListScreen\Serializer;
use ACP\Storage\ListScreen\Unserializer;
use LogicException;
use SplFileInfo;
use WP_User;
final class File implements AC\ListScreenRepositoryWritable, SourceAware {
use OpCacheInvalidateTrait;
use ListScreenPermissionTrait;
/**
* @var Directory
*/
private $directory;
/**
* @var null
*/
private $extension;
/**
* @var DecoderFactory
*/
private $decoder_factory;
/**
* @var Unserializer|null
*/
private $unserializer;
/**
* @var Encoder
*/
private $encoder;
/**
* @var Serializer
*/
private $serializer;
public function __construct(
Directory $directory,
string $extension,
Encoder $encoder,
DecoderFactory $decoder_factory,
Serializer $serializer,
Unserializer $unserializer = null
) {
$this->directory = $directory;
$this->extension = $extension;
$this->encoder = $encoder;
$this->decoder_factory = $decoder_factory;
$this->serializer = $serializer;
$this->unserializer = $unserializer;
$this->validate();
}
private function validate(): void {
if ( $this->extension !== null && ! preg_match( '/^[a-z0-9]{2,4}$/', $this->extension ) ) {
throw new LogicException( 'Invalid extension found.' );
}
}
public function find_all( Sort $sort = null ): ListScreenCollection {
$list_screens = $this->find_all_from_files();
return $sort
? $sort->sort( $list_screens )
: $list_screens;
}
public function find( ListScreenId $id ): ?ListScreen {
$list_screens = ( new Filter\ListId( $id ) )->filter(
$this->find_all_from_files()
);
return $list_screens->get_first() ?: null;
}
public function find_by_user( ListScreenId $id, WP_User $user ): ?ListScreen {
$list_screen = $this->find( $id );
return $list_screen && $this->user_can_view_list_screen( $list_screen, $user )
? $list_screen
: null;
}
public function find_all_by_key( string $key, Sort $sort = null ): ListScreenCollection {
$list_screens = ( new Filter\ListKey( $key ) )->filter(
$this->find_all_from_files()
);
return $sort
? $sort->sort( $list_screens )
: $list_screens;
}
public function exists( ListScreenId $id ): bool {
return null !== $this->find( $id );
}
private function find_all_from_files(): ListScreenCollection {
$list_screens = new ListScreenCollection();
foreach ( $this->get_files() as $file ) {
$encoded_list_screen = $this->unserializer
? $this->unserializer->unserialize( $file->openFile()->fread( $file->getSize() ) )
: require( $file->getRealPath() );
try {
$decoder = $this->decoder_factory->create( $encoded_list_screen );
} catch ( DecoderNotFoundException $e ) {
continue;
}
if ( ! $decoder->can_decode( $encoded_list_screen ) ) {
continue;
}
$list_screen = $decoder->decode( $encoded_list_screen );
$list_screens->add( $list_screen );
}
return $list_screens;
}
public function find_all_by_user( string $key, WP_User $user, Sort $sort = null ): ListScreenCollection {
$list_screens = $this->find_all_by_key( $key, $sort );
return ( new Filter\User( $user ) )->filter( $list_screens );
}
public function save( ListScreen $list_screen ): void {
if ( ! $this->directory->exists() ) {
$this->directory->create();
}
if ( ! $this->directory->get_info()->isWritable() ) {
throw new DirectoryNotWritableException( $this->directory->get_path() );
}
if ( ! $list_screen->has_id() ) {
throw MissingListScreenIdException::from_saving_list_screen();
}
$file = $this->create_file_name(
$this->directory->get_path(),
$list_screen->get_id()
);
$result = file_put_contents(
$file,
$this->serializer->serialize( $this->encoder->encode( $list_screen ) )
);
if ( $result === false ) {
throw FileNotWritableException::from_saving_list_screen( $list_screen );
}
$this->opcache_invalidate( $file );
}
public function delete( ListScreen $list_screen ): void {
$file = $this->create_file_name(
$this->directory->get_path(),
$list_screen->get_id()
);
$this->opcache_invalidate( $file );
$result = unlink( $file );
if ( $result === false ) {
throw FileNotWritableException::from_removing_list_screen( $list_screen );
}
}
/**
* Get all files and do superficial checks on them
* @return SplFileInfo[]
*/
private function get_files(): array {
$files = [];
if ( $this->directory->is_readable() ) {
foreach ( $this->directory->get_files() as $file ) {
if ( ! $file->isFile() || ! $file->isReadable() || $file->getSize() === 0 ) {
continue;
}
if ( $this->extension !== null && $this->extension !== $file->getExtension() ) {
continue;
}
$files[] = $file->getFileInfo();
}
}
return $files;
}
private function create_file_name( string $path, ListScreenId $id ): string {
return sprintf( '%s/%s.%s', $path, $id->get_id(), $this->extension );
}
public function get_directory(): Directory {
return $this->directory;
}
public function get_source( ListScreenId $id ): string {
if ( ! $this->has_source( $id ) ) {
throw new SourceNotAvailableException();
}
return $this->create_file_name(
$this->directory->get_path(),
$id
);
}
public function has_source( ListScreenId $id ): bool {
return $this->exists( $id );
}
}