Storage.php
3.68 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
<?php
namespace ACP\Service;
use AC;
use AC\ListScreenRepository\Storage\ListScreenRepository;
use AC\Registerable;
use ACP\ListScreenRepository\Collection;
use ACP\ListScreenRepository\FileFactory;
use ACP\Storage\Directory;
use ACP\Storage\ListScreen\LegacyCollectionDecoder;
use ACP\Storage\ListScreen\SerializerTypes;
use ACP\Storage\ListScreenRepositoryFactory;
final class Storage implements Registerable {
/**
* @var AC\ListScreenRepository\Storage
*/
private $storage;
/**
* @var FileFactory
*/
private $file_factory;
/**
* @var AC\EncodedListScreenDataFactory
*/
private $encoded_list_screen_data_factory;
/**
* @var LegacyCollectionDecoder
*/
private $collection_decoder;
public function __construct(
AC\ListScreenRepository\Storage $storage,
FileFactory $file_factory,
AC\EncodedListScreenDataFactory $encoded_list_screen_data_factory,
LegacyCollectionDecoder $collection_decoder
) {
$this->storage = $storage;
$this->file_factory = $file_factory;
$this->encoded_list_screen_data_factory = $encoded_list_screen_data_factory;
$this->collection_decoder = $collection_decoder;
}
public function register() {
add_action( 'acp/ready', [ $this, 'configure' ], 20 );
}
public function configure(): void {
$repositories = $this->storage->get_repositories();
$this->configure_file_storage( $repositories );
$repositories = apply_filters( 'acp/storage/repositories',
$repositories,
new ListScreenRepositoryFactory( $this->file_factory )
);
$this->configure_api_storage( $repositories );
$this->storage->set_repositories( $repositories );
}
private function configure_api_storage( array &$repositories ): void {
$collection = new AC\ListScreenCollection();
foreach ( $this->encoded_list_screen_data_factory->create() as $data ) {
if ( ! $this->collection_decoder->can_decode( $data ) ) {
continue;
}
foreach ( $this->collection_decoder->decode( $data ) as $list_screen ) {
$collection->add( $list_screen );
}
}
if ( ! $collection->count() ) {
return;
}
$repositories['acp-collection'] = new ListScreenRepository(
new Collection( $collection ),
false
);
}
private function configure_file_storage( array &$repositories ): void {
if ( apply_filters( 'acp/storage/file/enable_for_multisite', false ) && is_multisite() ) {
return;
}
$path = apply_filters( 'acp/storage/file/directory', null );
if ( ! is_string( $path ) || $path === '' ) {
return;
}
$directory = new Directory( $path );
if ( ! $directory->exists() && $directory->has_path( WP_CONTENT_DIR ) ) {
$directory->create();
}
$file = new ListScreenRepository(
$this->file_factory->create(
SerializerTypes::PHP,
$directory
),
(bool) apply_filters( 'acp/storage/file/directory/writable', true )
);
$repositories['acp-file'] = $file;
if ( ! $file->is_writable() || ! $this->storage->has_repository( 'acp-database' ) ) {
return;
}
$database = $this->storage->get_repository( 'acp-database' );
if ( apply_filters( 'acp/storage/file/directory/migrate', false ) ) {
$this->run_migration( $database, $file );
}
if ( apply_filters( 'acp/storage/file/directory/copy', false ) ) {
$this->run_copy( $database, $file );
}
$repositories['acp-database'] = $database->with_writable( false );
}
private function run_migration( ListScreenRepository $from, ListScreenRepository $to ): void {
foreach ( $from->with_writable( true )->find_all() as $list_screen ) {
$to->save( $list_screen );
$from->delete( $list_screen );
}
}
private function run_copy( ListScreenRepository $from, ListScreenRepository $to ): void {
foreach ( $from->find_all() as $list_screen ) {
$to->save( $list_screen );
}
}
}