Skip to content

Commit 2e9540c

Browse files
authored
Merge pull request #32 from iMattPro/experiemental
Update storage and file tracking
2 parents 097e235 + 01552c8 commit 2e9540c

11 files changed

Lines changed: 171 additions & 113 deletions

File tree

adm/style/acp_pwakit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ <h1>{{ lang('ACP_PWA_KIT_APP_ICONS') }}</h1>
8484
{% set iconName = icon.src|split('/')|last %}
8585
<p style="position: relative;">
8686
<span class="pwa-icon-container">
87-
<img src="{{ icon.src }}" alt="{{ iconName|e("html") }}">
87+
<img src="{{ icon.src }}" alt="{{ iconName|e('html') }}">
8888
<button class="delete-btn" name="delete" value="{{ icon.src }}" title="{{ lang('ACP_PWA_IMG_DELETE') }}">
8989
<span class="fa-stack fa-2x">
9090
{{ Icon('font', 'circle', '', true, 'fa-stack-2x fa-inverse') }}

config/services.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ services:
1414
- '@ext.manager'
1515
- '@upload_imagesize'
1616
- '@phpbb.pwakit.storage'
17+
- '@phpbb.pwakit.file_tracker'
1718
- '@storage.helper'
1819
- '%core.root_path%'
1920

@@ -25,16 +26,18 @@ services:
2526
- '@phpbb.pwakit.storage'
2627

2728
phpbb.pwakit.storage:
28-
class: phpbb\pwakit\storage\storage
29+
class: phpbb\storage\storage
2930
arguments:
30-
- '@dbal.conn'
31-
- '@cache.driver'
3231
- '@storage.adapter.factory'
32+
- '@phpbb.pwakit.file_tracker'
3333
- 'phpbb_pwakit'
34-
- '%tables.storage%'
3534
tags:
3635
- { name: storage }
3736

37+
phpbb.pwakit.file_tracker:
38+
class: phpbb\pwakit\storage\file_tracker
39+
parent: storage.file_tracker
40+
3841
phpbb.pwakit.admin.controller:
3942
class: phpbb\pwakit\controller\admin_controller
4043
arguments:

helper/helper.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use FastImageSize\FastImageSize;
1414
use phpbb\exception\runtime_exception;
1515
use phpbb\extension\manager as ext_manager;
16-
use phpbb\pwakit\storage\storage;
16+
use phpbb\pwakit\storage\file_tracker;
17+
use phpbb\storage\storage;
1718
use phpbb\storage\exception\storage_exception;
1819
use phpbb\storage\helper as storage_helper;
1920

@@ -28,6 +29,9 @@ class helper
2829
/** @var storage */
2930
protected storage $storage;
3031

32+
/** @var file_tracker $file_tracker */
33+
protected file_tracker $file_tracker;
34+
3135
/** @var storage_helper */
3236
protected storage_helper $storage_helper;
3337

@@ -40,14 +44,16 @@ class helper
4044
* @param ext_manager $extension_manager
4145
* @param FastImageSize $imagesize
4246
* @param storage $storage
47+
* @param file_tracker $file_tracker
4348
* @param storage_helper $storage_helper
4449
* @param string $root_path
4550
*/
46-
public function __construct(ext_manager $extension_manager, FastImageSize $imagesize, storage $storage, storage_helper $storage_helper, string $root_path)
51+
public function __construct(ext_manager $extension_manager, FastImageSize $imagesize, storage $storage, file_tracker $file_tracker, storage_helper $storage_helper, string $root_path)
4752
{
4853
$this->extension_manager = $extension_manager;
4954
$this->imagesize = $imagesize;
5055
$this->storage = $storage;
56+
$this->file_tracker = $file_tracker;
5157
$this->storage_helper = $storage_helper;
5258
$this->root_path = $root_path;
5359
}
@@ -82,36 +88,39 @@ public function get_icons(string $use_path = ''): array
8288
public function resync_icons(): void
8389
{
8490
$path = $this->get_storage_path() . '/';
91+
$full_base_path = $this->root_path . $path;
8592

86-
// Get both arrays at once and pre-process paths
87-
$untracked_files = array_map(static function($file) use ($path) {
88-
return str_replace($path, '', $file);
89-
}, $this->get_images());
93+
// Create a single reusable callback function
94+
$remove_path = static fn($file) => str_replace($path, '', $file);
9095

91-
$tracked_files = array_map(static function($file) use ($path) {
92-
return str_replace($path, '', $file);
93-
}, $this->get_stored_images());
96+
// Get and process both arrays using the same callback
97+
$untracked_files = array_map($remove_path, $this->get_images());
98+
$tracked_files = array_map($remove_path, $this->get_stored_images());
9499

95100
// Process tracking changes
96101
$files_to_track = array_diff($untracked_files, $tracked_files);
97102
$files_to_untrack = array_diff($tracked_files, $untracked_files);
98103

99-
// Batch process tracking operations
100-
foreach ($files_to_track as $file)
104+
// Prepare batch tracking array with array_map instead of foreach
105+
$files = !empty($files_to_track) ? array_map(
106+
static fn($file) => [
107+
'file_path' => $file,
108+
'filesize' => filesize($full_base_path . $file)
109+
],
110+
$files_to_track
111+
) : [];
112+
113+
if ($files)
101114
{
102-
try
103-
{
104-
$this->storage->track_file($file);
105-
}
106-
catch (storage_exception)
107-
{
108-
// If file doesn't exist, don't track it
109-
}
115+
$this->file_tracker->track_files(file_tracker::STORAGE_NAME, $files);
110116
}
111117

112-
foreach ($files_to_untrack as $file)
118+
if ($files_to_untrack)
113119
{
114-
$this->storage->untrack_file($file);
120+
foreach ($files_to_untrack as $file)
121+
{
122+
$this->file_tracker->untrack_file(file_tracker::STORAGE_NAME, $file);
123+
}
115124
}
116125
}
117126

@@ -167,7 +176,7 @@ public function delete_icon(string $path): string
167176
protected function get_stored_images(): array
168177
{
169178
$path = $this->get_storage_path();
170-
$images = $this->storage->get_tracked_files();
179+
$images = $this->file_tracker->get_tracked_files();
171180

172181
$result = [];
173182
foreach ($images as $image)

migrations/m3_storage.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@
1010

1111
namespace phpbb\pwakit\migrations;
1212

13-
use phpbb\cache\driver\driver_interface;
1413
use phpbb\db\migration\container_aware_migration;
1514
use phpbb\extension\manager;
1615
use phpbb\pwakit\ext;
17-
use phpbb\pwakit\storage\storage;
18-
use phpbb\storage\adapter\adapter_interface;
19-
use phpbb\storage\adapter_factory;
20-
use phpbb\storage\exception\storage_exception;
16+
use phpbb\storage\file_tracker;
2117
use phpbb\storage\provider\local;
2218

2319
class m3_storage extends container_aware_migration
@@ -61,19 +57,8 @@ public function add_tracked_files(): void
6157
/** @var manager $extension_manager */
6258
$extension_manager = $this->container->get('ext.manager');
6359

64-
/** @var driver_interface $cache */
65-
$cache = $this->container->get('cache.driver');
66-
67-
/** @var adapter_interface|adapter_factory $factory */
68-
$factory = $this->container->get('storage.adapter.factory');
69-
70-
$storage = new storage(
71-
$this->db,
72-
$cache,
73-
$factory,
74-
'phpbb_pwakit',
75-
$this->tables['storage']
76-
);
60+
/** @var file_tracker $file_tracker */
61+
$file_tracker = $this->container->get('storage.file_tracker');
7762

7863
$storage_path = ext::PWA_ICON_DIR . '/';
7964

@@ -87,22 +72,14 @@ public function add_tracked_files(): void
8772
// Extract just the file paths relative to the storage dir
8873
$files = array_map(static function($image) use ($storage_path) {
8974
$pos = strpos($image, $storage_path);
90-
return $pos !== false ? substr($image, $pos + strlen($storage_path)) : $image;
75+
return [
76+
'file_path' => $pos !== false ? substr($image, $pos + strlen($storage_path)) : $image,
77+
'filesize' => filesize($image)
78+
];
9179
}, $files);
9280

93-
// Track each file
94-
foreach ($files as $file)
95-
{
96-
try
97-
{
98-
$storage->track_file($file);
99-
}
100-
catch (storage_exception)
101-
{
102-
// If file doesn't exist or other error, continue with next file
103-
continue;
104-
}
105-
}
81+
// Track files
82+
$file_tracker->track_files('phpbb_pwakit', $files);
10683
}
10784

10885
/**
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
namespace phpbb\pwakit\storage;
1212

13-
class storage extends \phpbb\storage\storage
13+
class file_tracker extends \phpbb\storage\file_tracker
1414
{
15+
public const STORAGE_NAME = 'phpbb_pwakit';
16+
1517
/**
1618
* Gets tracked files in the storage table
1719
*
@@ -20,7 +22,7 @@ class storage extends \phpbb\storage\storage
2022
public function get_tracked_files(): array
2123
{
2224
$sql = 'SELECT file_path FROM ' . $this->storage_table . "
23-
WHERE storage = '" . $this->db->sql_escape($this->get_name()) . "'
25+
WHERE storage = '" . self::STORAGE_NAME . "'
2426
ORDER BY file_path";
2527
$result = $this->db->sql_query($sql);
2628
$files = $this->db->sql_fetchrowset($result);

tests/unit/acp_module_test.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ public function test_module_info()
7777
public function module_auth_test_data(): array
7878
{
7979
return [
80-
// module_auth, expected result
81-
['ext_foo/bar', false],
82-
['ext_phpbb/pwakit', true],
80+
'invalid auth' => ['ext_foo/bar', false],
81+
'valid auth' => ['ext_phpbb/pwakit', true],
8382
];
8483
}
8584

@@ -94,7 +93,7 @@ public function test_module_auth($module_auth, $expected)
9493
public function main_module_test_data(): array
9594
{
9695
return [
97-
['settings'],
96+
'valid mode' => ['settings'],
9897
];
9998
}
10099

tests/unit/admin_controller_test.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ protected function setUp(): void
115115
public function module_access_test_data(): array
116116
{
117117
return [
118-
['settings', true],
119-
['foobar', false],
118+
'correct mode' => ['settings', true],
119+
'incorrect mode' => ['foobar', false],
120120
];
121121
}
122122

@@ -138,9 +138,9 @@ public function test_module_access($mode, $expected)
138138
public function form_checks_data(): array
139139
{
140140
return [
141-
['submit'],
142-
['upload'],
143-
['resync'],
141+
'submit test' => ['submit'],
142+
'upload test' => ['upload'],
143+
'resync test' => ['resync'],
144144
];
145145
}
146146

@@ -162,7 +162,7 @@ public function test_form_checks($action)
162162
public function display_settings_test_data(): array
163163
{
164164
return [
165-
[
165+
'site name and short name' => [
166166
[
167167
'sitename' => 'phpBB',
168168
'sitename_short' => 'phpBB',
@@ -172,7 +172,7 @@ public function display_settings_test_data(): array
172172
'sitename_short' => 'phpBB',
173173
],
174174
],
175-
[
175+
'site name only' => [
176176
[
177177
'sitename' => 'phpBB',
178178
'sitename_short' => '',
@@ -182,7 +182,7 @@ public function display_settings_test_data(): array
182182
'sitename_short' => 'phpBB',
183183
],
184184
],
185-
[
185+
'long site name only' => [
186186
[
187187
'sitename' => 'phpBB Long Site Name',
188188
'sitename_short' => '',
@@ -192,7 +192,7 @@ public function display_settings_test_data(): array
192192
'sitename_short' => 'phpBB Long S',
193193
],
194194
],
195-
[
195+
'long mb site name only' => [
196196
[
197197
'sitename' => utf8_encode_ucr('phpBB™ 😂😂😂😂😂😂😂😂'),
198198
'sitename_short' => '',
@@ -263,7 +263,7 @@ public function test_display_settings($configs, $expected)
263263
public function submit_test_data(): array
264264
{
265265
return [
266-
[ // all good inputs
266+
'all good inputs' => [
267267
[
268268
'pwa_bg_color_1' => '#000000',
269269
'pwa_theme_color_1' => '#ffffff',
@@ -276,7 +276,7 @@ public function submit_test_data(): array
276276
],
277277
'CONFIG_UPDATED',
278278
],
279-
[ // one style with good inputs
279+
'one style with good inputs #1' => [
280280
[
281281
'pwa_bg_color_1' => '#000000',
282282
'pwa_theme_color_1' => '#ffffff',
@@ -289,7 +289,7 @@ public function submit_test_data(): array
289289
],
290290
'CONFIG_UPDATED',
291291
],
292-
[ // one style with good inputs
292+
'one style with good inputs #2' => [
293293
[
294294
'pwa_bg_color_1' => '#000000',
295295
'pwa_theme_color_1' => '',
@@ -302,7 +302,7 @@ public function submit_test_data(): array
302302
],
303303
'CONFIG_UPDATED',
304304
],
305-
[ // one bad input
305+
'one bad input' => [
306306
[
307307
'pwa_bg_color_1' => '#000000',
308308
'pwa_theme_color_1' => 'fff',
@@ -315,7 +315,7 @@ public function submit_test_data(): array
315315
],
316316
'ACP_PWA_INVALID_COLOR',
317317
],
318-
[ // all bad inputs
318+
'all bad inputs' => [
319319
[
320320
'pwa_bg_color_1' => 'foo',
321321
'pwa_theme_color_1' => 'bar',
@@ -328,7 +328,7 @@ public function submit_test_data(): array
328328
],
329329
'ACP_PWA_INVALID_COLOR<br>ACP_PWA_INVALID_COLOR<br>ACP_PWA_INVALID_COLOR<br>ACP_PWA_INVALID_COLOR',
330330
],
331-
[ // all empty inputs
331+
'all empty inputs' => [
332332
[
333333
'pwa_bg_color_1' => '',
334334
'pwa_theme_color_1' => '',
@@ -446,9 +446,21 @@ public function test_resync()
446446
public function delete_test_data(): array
447447
{
448448
return [
449-
['foo.png', false, false], // not confirmed yet
450-
['foo.png', true, false], // confirmed and valid data, no errors
451-
['', true, true], // confirmed with invalid data, errors
449+
'not confirmed' => [
450+
'foo.png',
451+
false,
452+
false
453+
],
454+
'confirmed valid data' => [
455+
'foo.png',
456+
true,
457+
false
458+
],
459+
'confirmed invalid data' => [
460+
'',
461+
true,
462+
true
463+
],
452464
];
453465
}
454466

0 commit comments

Comments
 (0)