diff --git a/packages/playground/blueprints/src/lib/steps/install-theme.spec.ts b/packages/playground/blueprints/src/lib/steps/install-theme.spec.ts index 6e8c598742..6b1660193f 100644 --- a/packages/playground/blueprints/src/lib/steps/install-theme.spec.ts +++ b/packages/playground/blueprints/src/lib/steps/install-theme.spec.ts @@ -92,7 +92,7 @@ describe('Blueprint step installTheme', () => { it('ifAlreadyInstalled=skip should skip the theme if the theme already exists', async () => { await installTheme(php, { themeZipFile: new File( - ['invalid zip bytes, unpacking should not attempted'], + [php.readFileAsBuffer(zipFilePath)], zipFileName ), ifAlreadyInstalled: 'skip', diff --git a/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts b/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts index 9870ff76d7..81080cac48 100644 --- a/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts +++ b/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts @@ -1,7 +1,6 @@ import { joinPaths, phpVars } from '@php-wasm/util'; import { UniversalPHP } from '@php-wasm/universal'; import { wpContentFilesExcludedFromExport } from '../utils/wp-content-files-excluded-from-exports'; -import { runPhpWithZipFunctions } from '../utils/run-php-with-zip-functions'; interface ZipWpContentOptions { /** @@ -73,3 +72,70 @@ export const zipWpContent = async ( return fileBuffer; }; + +const zipFunctions = `open($output, ZipArchive::CREATE); + if ($res === TRUE) { + $directories = array( + $root . '/' + ); + while (sizeof($directories)) { + $current_dir = array_pop($directories); + + if ($handle = opendir($current_dir)) { + while (false !== ($entry = readdir($handle))) { + if ($entry == '.' || $entry == '..') { + continue; + } + + $entry = join_paths($current_dir, $entry); + if (in_array($entry, $excludePaths)) { + continue; + } + + if (is_dir($entry)) { + $directory_path = $entry . '/'; + array_push($directories, $directory_path); + } else if (is_file($entry)) { + $zip->addFile($entry, substr($entry, strlen($zip_root))); + } + } + closedir($handle); + } + } + foreach ($additionalPaths as $disk_path => $zip_path) { + $zip->addFile($disk_path, $zip_path); + } + $zip->close(); + chmod($output, 0777); + } +} + +function join_paths() +{ + $paths = array(); + + foreach (func_get_args() as $arg) { + if ($arg !== '') { + $paths[] = $arg; + } + } + + return preg_replace('#/+#', '/', join('/', $paths)); +} +`; + +async function runPhpWithZipFunctions(playground: UniversalPHP, code: string) { + return await playground.run({ + code: zipFunctions + code, + }); +} diff --git a/packages/playground/blueprints/src/lib/utils/run-php-with-zip-functions.ts b/packages/playground/blueprints/src/lib/utils/run-php-with-zip-functions.ts deleted file mode 100644 index f70f23ac2b..0000000000 --- a/packages/playground/blueprints/src/lib/utils/run-php-with-zip-functions.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { UniversalPHP } from '@php-wasm/universal'; - -const zipFunctions = `open($output, ZipArchive::CREATE); - if ($res === TRUE) { - $directories = array( - $root . '/' - ); - while (sizeof($directories)) { - $current_dir = array_pop($directories); - - if ($handle = opendir($current_dir)) { - while (false !== ($entry = readdir($handle))) { - if ($entry == '.' || $entry == '..') { - continue; - } - - $entry = join_paths($current_dir, $entry); - if (in_array($entry, $excludePaths)) { - continue; - } - - if (is_dir($entry)) { - $directory_path = $entry . '/'; - array_push($directories, $directory_path); - } else if (is_file($entry)) { - $zip->addFile($entry, substr($entry, strlen($zip_root))); - } - } - closedir($handle); - } - } - foreach ($additionalPaths as $disk_path => $zip_path) { - $zip->addFile($disk_path, $zip_path); - } - $zip->close(); - chmod($output, 0777); - } -} - -function join_paths() -{ - $paths = array(); - - foreach (func_get_args() as $arg) { - if ($arg !== '') { - $paths[] = $arg; - } - } - - return preg_replace('#/+#', '/', join('/', $paths)); -} - -function unzip($zipPath, $extractTo, $overwrite = true) -{ - if (!is_dir($extractTo)) { - mkdir($extractTo, 0777, true); - } - $zip = new ZipArchive; - $res = $zip->open($zipPath); - if ($res === TRUE) { - $zip->extractTo($extractTo); - $zip->close(); - chmod($extractTo, 0777); - } -} - - -function delTree($dir) -{ - $files = array_diff(scandir($dir), array('.', '..')); - foreach ($files as $file) { - (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); - } - return rmdir($dir); -} -`; - -export async function runPhpWithZipFunctions( - playground: UniversalPHP, - code: string -) { - return await playground.run({ - code: zipFunctions + code, - }); -} diff --git a/packages/playground/common/src/index.ts b/packages/playground/common/src/index.ts index 5b0f65f5f0..5aae83fc81 100644 --- a/packages/playground/common/src/index.ts +++ b/packages/playground/common/src/index.ts @@ -14,9 +14,6 @@ import { phpVars } from '@php-wasm/util'; export const RecommendedPHPVersion = '8.0'; -// @TODO Make these ZIP functions more versatile and -// move them to one of the @php-wasm packages. - /** * Unzip a zip file inside Playground. */ @@ -38,105 +35,27 @@ export const unzipFile = async ( zipPath, extractToPath, }); - await runPhpWithZipFunctions( - php, - `unzip(${js.zipPath}, ${js.extractToPath});` - ); + await php.run({ + code: `open($zipPath); + if ($res === TRUE) { + $zip->extractTo($extractTo); + $zip->close(); + chmod($extractTo, 0777); + } else { + throw new Exception("Could not unzip file"); + } + } + unzip(${js.zipPath}, ${js.extractToPath}); + `, + }); if (await php.fileExists(tmpPath)) { await php.unlink(tmpPath); } }; - -const zipFunctions = `open($output, ZipArchive::CREATE); - if ($res === TRUE) { - $directories = array( - $root . '/' - ); - while (sizeof($directories)) { - $current_dir = array_pop($directories); - - if ($handle = opendir($current_dir)) { - while (false !== ($entry = readdir($handle))) { - if ($entry == '.' || $entry == '..') { - continue; - } - - $entry = join_paths($current_dir, $entry); - if (in_array($entry, $excludePaths)) { - continue; - } - - if (is_dir($entry)) { - $directory_path = $entry . '/'; - array_push($directories, $directory_path); - } else if (is_file($entry)) { - $zip->addFile($entry, substr($entry, strlen($zip_root))); - } - } - closedir($handle); - } - } - foreach ($additionalPaths as $disk_path => $zip_path) { - $zip->addFile($disk_path, $zip_path); - } - $zip->close(); - chmod($output, 0777); - } -} - -function join_paths() -{ - $paths = array(); - - foreach (func_get_args() as $arg) { - if ($arg !== '') { - $paths[] = $arg; - } - } - - return preg_replace('#/+#', '/', join('/', $paths)); -} - -function unzip($zipPath, $extractTo, $overwrite = true) -{ - if (!is_dir($extractTo)) { - mkdir($extractTo, 0777, true); - } - $zip = new ZipArchive; - $res = $zip->open($zipPath); - if ($res === TRUE) { - $zip->extractTo($extractTo); - $zip->close(); - chmod($extractTo, 0777); - } -} - - -function delTree($dir) -{ - $files = array_diff(scandir($dir), array('.', '..')); - foreach ($files as $file) { - (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); - } - return rmdir($dir); -} -`; - -export async function runPhpWithZipFunctions( - playground: UniversalPHP, - code: string -) { - return await playground.run({ - code: zipFunctions + code, - }); -}