close
Skip to content

[PHP] Add cp method to Universal PHP#3234

Merged
mho22 merged 3 commits intotrunkfrom
add-copy-method-to-universal-php
Feb 23, 2026
Merged

[PHP] Add cp method to Universal PHP#3234
mho22 merged 3 commits intotrunkfrom
add-copy-method-to-universal-php

Conversation

@mho22
Copy link
Copy Markdown
Collaborator

@mho22 mho22 commented Feb 2, 2026

Motivation for the change, related issues

Based on this issue

While playing with PHP.wasm, I found out it has a mv method, but no cp method.

Implementation details

  • Using directly FSHelpers.copyRecursive inside the php.cp() method
  • Adding tests
  • Keep consistency with the mv() when throwing errors
  • Throws a new error when copyRecursive is recursively copying inside himself.

Testing Instructions

CI

or manually :

cp.ts :

import { PHP } from '@php-wasm/universal';
import { loadNodeRuntime } from "@php-wasm/node";

const php = new PHP(await loadNodeRuntime('8.5'));

php.mkdir('test');

php.chdir('test');

php.writeFile('foo', `<?php echo "File copied successfully";`);

php.cp('foo', 'bar');

const response = await php.runStream({scriptPath: 'bar'});

console.log(await response.stdoutText, php.listFiles('/test'));

Output :

> node cp.ts

File copied successfully [ 'foo', 'bar' ]

Copy link
Copy Markdown
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add some tests like we have for mv() here:

it('mv() should move a file', () => {

@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 11, 2026

@brandonpayton I added tests and found out that if you copy a directory recursively inside himself, it freezes, so I also throw an error when it happens. Let me know if something looks wrong. And thank you for your review.

Copy link
Copy Markdown
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @mho22! This looks good to go except there appears to be one duplicate test. I left a note about that. Once that is fixed (if it is indeed an issue), let's merge!

});
});

it('cp() from NODEFS to MEMFS should work', () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a duplicate of the previous test. Was this meant to be a MEMFS-to-NODEFS variation?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I made a mistake here, I probably copy pasted it to work on the MEMFS to NODEFS later and forgot to implement it correctly. Done now.

@mho22 mho22 merged commit 6c49bef into trunk Feb 23, 2026
86 of 87 checks passed
@mho22 mho22 deleted the add-copy-method-to-universal-php branch February 23, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants