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

file_put_contents fails when vfsStream::setup() was invoked twice. #288

Open
yvoyer opened this issue Jan 26, 2024 · 2 comments
Open

file_put_contents fails when vfsStream::setup() was invoked twice. #288

yvoyer opened this issue Jan 26, 2024 · 2 comments

Comments

@yvoyer
Copy link

yvoyer commented Jan 26, 2024

I am using vfsStream to mock the file system as a service during the execution of a Symony app.

file_put_content() fails when creating another root dir was created.

Is there a way to have 2 root dirs, is it a bug or by design?

Steps to reproduce:

$root = vfsStream::setup('root');

mkdir($dir = $root->url() . '/test/code-65b3c45ed2883/64/1b/4e', 0777, true);
file_put_contents($dir . '/Works.txt', 'content');

$other = vfsStream::setup('other');

mkdir($dir = $root->url() . '/test/code-65b3c45ed2883/42/ff/91', 0777, true);

file_put_contents($dir . '/Fails.txt', 'content');
"PHP Warning:  file_put_contents(vfs://root/test/code-65b3c45ed2883/42/ff/91/Fails.txt): failed to open stream: "org\bovigo\vfs\vfsStreamWrapper::stream_open" call failed in ..."

Thank you for this great project.

@mikey179
Copy link
Member

mikey179 commented Jan 26, 2024

Basically by design: not intentional, but due to the limitations of user defined stream wrappers in PHP and the knowledge of the author when vfsStream was initially created*. You can only have one root dir at a time. The second call to vfsStream::setup() destroys the first representation of the file system, so reusing anything of that first one after that leads to undefined behavior. I'm not exactly sure, but I think the second call to mkdir() creates the new dir as a child of the "other" directory. In the second file_put_contents() call you are trying to put the file into vfs://root/test/code-65b3c45ed2883/42/ff/91/Fails.txt, but this directory doesn't exist then, as it would belong to the first file system which was abandoned, therefore the call fails.

*With the knowledge of today it could be changed in vfsStream, but not without a massive bc break.

@yvoyer
Copy link
Author

yvoyer commented Jan 26, 2024

@mikey179 would it be a feature you would be interested to add? I could check out how to implement it if you gave me some pointers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants