Using vfsStream, am I able to require or include a virtual file?
$structure = array(
'classes' => array('Foo.php' => '<?php class Foo {} ?>')
);
\vfsStream::create($structure);
require_once(\vfsStream::url('classes').DIRECTORY_SEPARATOR.'Foo.php');
The code above fails silently under PHPUnit.
Thanks.
Have you tried
require_once(\vfsStream::url('root/classes').DIRECTORY_SEPARATOR.'Foo.php');
? The call to vfsStream::create($structure); creates the root directory, and does not use the first entry in $structures as the root directory as there might be more then one element in this array. See also documentation at https://github.com/mikey179/vfsStream/wiki/Createcomplexstructures.
In addition to Frank's answer about the incorrect use of url(), there may be a configuration issue. In a stock PHP installation, you have to make sure allow_url_fopen is enabled in your php.ini and allow_url_include is enabled in configuration or your script.
In my case, however, I'm running the Suhosin extension, which ignores these parameters and completely disables url_fopen by default. In order to include/require a vfsStream file, you need to add the vfs:// scheme to Suhosin's whitelist in php.ini: suhosin.executor.include.whitelist = "vfs://"
Thanks to Frank Kleine, the vfsStream maintainer, for helping me track this down.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With