📜 ⬆️ ⬇️

phpDaemon and runkit: new level

Today I will tell you about the new tasty phpDaemon buns, including those provided by the runkit PHP module.

I am glad to announce that the code style in phpDaemon is adapted to the preferences of most earth programmers, and not just harsh aliens. Thank you for this silentroach . Also, comments on the style and readability of the code are now adequately perceived.

Runkit history

PECL / runkit is a very remarkable PHP module, the story of which is laid by a female programmer Sara Golemon back in August 2005. Its purpose is to allow the constants to be changed during the execution of the script, to override functions and classes, and to perform other manipulations in PHP like black magic ...
However, the project was very raw, it took a lot of time to fix bugs and maintain compatibility with new versions of PHP. And Sara lost interest in developing the project, for reasons we can only guess. But she did the main thing - showed that all this is possible - a matter of technology. After that pajoye, johannes, sfox, sebastian, mlively, and others made changes. However, for example, for the whole of 2007 there were only 4 commits, and the module continued to harshly and lag behind the main PHP branch. There have been several attempts at fork by third-party developers, but only one in my opinion was a success.
Our compatriot Dmitry Zenovich ( dzenovich ) gave him a new life, at github.com/zenovich/runkit : The runkit that works!

Update code on the fly

Previously, when the option --auto-reload = 5s was enabled in phpDaemon, the demon once every 5 seconds checked the modification dates of all the connected PHP files, and when they changed it went into a smooth restart. Now it is enough to enable --auto-reimport and instead of a smooth restart, the modified file will be imported directly into the live process. This greatly simplifies life, I must say. Thank you for this Dmitry Zenovich! (and a little bit to me, for tampering with it).


In addition to manipulating classes, constants, and functions, runkit allows you to create a sandbox. Looks like this:

$sandbox = new Runkit_Sandbox(array( 'safe_mode' => TRUE, .... 'output_handler' => array($this,'out') )); $sandbox->call_user_func(function() { echo "Hello World!"; }); 

At the same time, the code executed in the sandbox cannot affect the genitive context, and the Fatal error / Parse error will not cause the main script to crash. You can also set any ini-options for the sandbox.

Override native PHP functions

Runkit allows you to override not only user-defined, but also native PHP functions. Currently phpDaemon overloads header () and register_shutdown_function () and routes calls to the appropriate methods of the current request.

The create_function () function is a separate story. Maybe someone does not know the inside story of this function, but in reality it represents this:
 function create_function($args,$code) { static $c = 0; $name = "\x00lambda_".$c++; eval('function '.$name.'('.$args.') {'.$code.'}'); return $name; } 

That is, it creates the most ordinary and ordinary function and returns its name as a string. Of course, there is no question of removing unnecessary lambda functions; they live to the death of the script. I myself did not pay attention to this problem because I have not used create_function for a long time, but Ilya Kantor ( algo ) in his article (True FastCGI for PHP - migration and tests) drew attention to this and suggested memorizing the names of all lambda functions created during the execution of a query, and at the end clean it up. This solution is suitable for its task, but in general it is understandably unacceptable for phpDaemon.
Therefore, I invented a different solution, in my opinion more elegant and universal. My create_function () returns a DestructableLambda object, in the destructor of which the lambda function is deleted via runkit. In addition, there is an LRU cache that stores the most frequently compiled lambda functions, which reduces compilation costs. Source code

If you have any ideas what other features can be improved, we will be glad to hear.

The end

Well, perhaps that's all. Thank you all for your attention.

Github: kakserpom / phpdaemon
Homepage: http://phpdaemon.net
Group: groups.google.com/group/phpdaemon

Source: https://habr.com/ru/post/104811/

All Articles