开发者

php include directive from fifo (named pipe) files

开发者 https://www.devze.com 2023-04-02 19:56 出处:网络
I was looking to find a way to use \"include\" statement using a fifo (named pipe) file created with posix_mkfifo. As my expectations were high I got very depressed to see the page blocked, waiting fo

I was looking to find a way to use "include" statement using a fifo (named pipe) file created with posix_mkfifo. As my expectations were high I got very depressed to see the page blocked, waiting for an action that I was sure it won't ever happen.

So... my logic dictated that as "include" surely use some kind of "fopen"-family function, and fifos can be opened this way, my only thing to do was: to create one, then write in it, maybe keep the resource alive until it was included, include the file, close the handler, delete the file. But of course it was nothing like that. I suppose that include opens the file in a blocking manner and this behavior is the cause of my problem. Having worked with fifos in c, I noticed that in a non-blocking open method the information in fifo is preserved until a reader decides to read it. I was hoping on the same behavior.

Although I tried to open another page to write to the same fifo, so that the reader can continue, that was bound to fail also. Furthermore, not only the page did not respond anymore, but it failed to answer to the user abort also, fact due to I had my sessions blocked for several minutes, as session_start() would tell me that there cannot be used by two processes at once ( I might be wrong here about the error message, I can remember well, and I have deleted the test files. )

My question is if there is any way to achieve this effect: of including using a fifo.

In the end, to answer all the questions about why the hell am I doing this I will say that my system puts itself between the real code and it's processing with the purpose of modifying it on need in a dynamic manner, completely reversible. And for that it needs to read the original file, check the code, change it when it seems to and then开发者_如何学编程 write it in a file, in a new "root" folder, keep the folder and files hierarchy and then "include" the file. At the moment, I replaced the use of fifos with the one of real files, but I need to filter a lot of code. And eval, although overused in my functions, is not a solution as it do not keep the current directory and the script name, or anything specific to an actual file, besides that it is very sensible with many things, especially with relative paths, and weird uses of php syntax. So I am still stuck with writing in files.

Because of that the system is a terrible time consumer and a bad resource manager, my original idea was to write in fifos that are more light, kept in memory, volatile, and really made for the only purpose of IPC.

With respect, paul


As suggested in a comment, I'll elaborate it a bit: Write your own stream wrapper and then you can register it on top of a protocol:

stream_wrapper_register("myfilter", "WrapCode");

include('myfilter://path-to-include');

# or

include('myfilter://file://path-to-include');

That done, inside your WrapCode stream wrapper class, you can open a stream to the original resource and then process it on the file transparently. This would be on-the-fly, so you don't need to care about storing your files somewhere (but you still can do so).

The PHP Manual offers an example with a simple stream wrapper class which shows how it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消