Lately, you might have noticed that streams are getting more important in the PHP scene. They are for example widely used in PSR-7 and implemented in Diactoros. Yesterday I was scrolling down the issues list of Phpspec and bumped in on this pull request. This pull request shows that it is possible to convert code in-memory and later require the modified content to be executed. Wow, that is awesome! Let's take a look at it.
The concept
You probably already used some built-in PHP streams like regular files, `php://memory` or `data://...` in combination with the `fopen()` method or the `StdFileObject` class. Beside those built-in streams, it is possible to configure your own stream wrappers. For this post I will be creating a blacklist stream that will remove blacklisted methods from your code. The blacklist stream wrapper will read a PHP file and replaces blacklisted methods from the code. When the file is required through the blacklist wrapper, all blacklisted methods will be removed from the code.
Show me some code
Usage
To use the stream wrapper, you have to register it with the name blacklist. Next you can require the file with the new stream URL. Internally PHP will open the file with your custom stream reader. This means that you can alter the content of the PHP file in an in-memory stream which will be used by PHP. Here is what the file looks like.
The above code needs a file test.php. This file looks like this:
When executing the run.php
you will see the result:
What the hell did I just read…
I know, most projects won't need in-memory code adjustments. The blacklist wrapper in this post will probably never be needed, but it is used to show you what is possible. Streams are pretty cool and will probably become more important in the future.