Skip to content

Commit b3b8f8d

Browse files
committed
Review add_to_zip() method
1 parent 83ccb05 commit b3b8f8d

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

console/command/extension/language.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private function package($zip_path)
178178
throw new \Exception($this->language->lang('CLI_EXTENSION_LANGUAGE_FILE_NOT_FOUND', $zip_path));
179179
}
180180

181-
// These are the language directories (and license) we want to extract from phpBB in order to form British English
181+
// These are the language directories (and license) we want to extract from phpBB to build British English
182182
$language_directories = [
183183
'docs/LICENSE.txt',
184184
'ext/phpbb/viglink/language/en',
@@ -226,23 +226,32 @@ private function package($zip_path)
226226
*/
227227
private function add_to_zip(ZipArchive $source_zip, ZipArchive $dest_zip, $save_version, $source_path)
228228
{
229-
for ($i = 0; $i < $source_zip->numFiles; $i++)
229+
$prefix_length = strlen('phpBB3/');
230+
231+
for ($i = 0, $n = $source_zip->numFiles; $i < $n; $i++)
230232
{
231233
$stat = $source_zip->statIndex($i);
232-
if (str_starts_with($stat['name'], $source_path))
234+
$entry_name = $stat['name'];
235+
236+
if (!str_starts_with($entry_name, $source_path))
237+
{
238+
continue;
239+
}
240+
241+
$relative_path = substr($entry_name, $prefix_length);
242+
$dest_path = $save_version . '/' . $relative_path;
243+
244+
// Check if it's a directory (size is 0, and the name ends with '/')
245+
if ($stat['size'] === 0 && str_ends_with($entry_name, '/'))
246+
{
247+
$dest_zip->addEmptyDir($dest_path);
248+
continue;
249+
}
250+
251+
$file_contents = $source_zip->getFromName($entry_name);
252+
if ($file_contents !== false)
233253
{
234-
if ($stat['size'] <= 0 && strpos($stat['name'], '/', strlen($source_path)))
235-
{
236-
$dest_zip->addEmptyDir($save_version . '/' . substr($stat['name'], strlen('phpBB3/')));
237-
continue;
238-
}
239-
240-
$file_contents = $source_zip->getFromName($stat['name']);
241-
if ($file_contents !== false)
242-
{
243-
$local_path = $save_version . '/' . substr($stat['name'], strlen('phpBB3/'));
244-
$dest_zip->addFromString($local_path, $file_contents);
245-
}
254+
$dest_zip->addFromString($dest_path, $file_contents);
246255
}
247256
}
248257
}

0 commit comments

Comments
 (0)