1313use FastImageSize \FastImageSize ;
1414use phpbb \exception \runtime_exception ;
1515use phpbb \extension \manager as ext_manager ;
16- use phpbb \pwakit \storage \storage ;
16+ use phpbb \pwakit \storage \file_tracker ;
17+ use phpbb \storage \storage ;
1718use phpbb \storage \exception \storage_exception ;
1819use 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 )
0 commit comments