Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blueprints: Simplify runPhpWithZipFunctions() setup #1434

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
68 changes: 67 additions & 1 deletion packages/playground/blueprints/src/lib/steps/zip-wp-content.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -73,3 +72,70 @@ export const zipWpContent = async (

return fileBuffer;
};

const zipFunctions = `<?php

function zipDir($root, $output, $options = array())
{
$root = rtrim($root, '/');
$additionalPaths = array_key_exists('additional_paths', $options) ? $options['additional_paths'] : array();
$excludePaths = array_key_exists('exclude_paths', $options) ? $options['exclude_paths'] : array();
$zip_root = array_key_exists('zip_root', $options) ? $options['zip_root'] : $root;

$zip = new ZipArchive;
$res = $zip->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,
});
}

This file was deleted.

121 changes: 20 additions & 101 deletions packages/playground/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -38,105 +35,27 @@ export const unzipFile = async (
zipPath,
extractToPath,
});
await runPhpWithZipFunctions(
php,
`unzip(${js.zipPath}, ${js.extractToPath});`
);
await php.run({
code: `<?php
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);
} else {
throw new Exception("Could not unzip file");
}
}
unzip(${js.zipPath}, ${js.extractToPath});
`,
});
if (await php.fileExists(tmpPath)) {
await php.unlink(tmpPath);
}
};

const zipFunctions = `<?php

function zipDir($root, $output, $options = array())
{
$root = rtrim($root, '/');
$additionalPaths = array_key_exists('additional_paths', $options) ? $options['additional_paths'] : array();
$excludePaths = array_key_exists('exclude_paths', $options) ? $options['exclude_paths'] : array();
$zip_root = array_key_exists('zip_root', $options) ? $options['zip_root'] : $root;

$zip = new ZipArchive;
$res = $zip->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,
});
}
Loading