Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include or require a vfsStream file

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.

like image 434
thom Avatar asked Apr 27 '26 01:04

thom


2 Answers

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.

like image 108
Frank Kleine Avatar answered Apr 28 '26 15:04

Frank Kleine


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

like image 25
Michael Avatar answered Apr 28 '26 13:04

Michael