A standalone cli server enhanced by the JIT compiler and hopefuly a decent first meaningful contribution from me

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #1793

    When I was younger I started getting into learning Php for the first time and since then I’ve always been learning more of Php, but always lurking without giving back much.

    So I hope this is a good start.


    I’m publishing here a codebase I’ve been working on for some time, it’s a php cli server that makes use of the new JIT compiler in php 8 (and php 7.4.8 to some extent) and manages Http and WebSocket requests through an event driven design like express while also giving you the option of using OOP to handle these requests like you would using controllers.

    The idea actually dates back 4-5 years, when I first started learning Java out of spite to be honest.

    I wanted to learn a programming language “for big boys” as they used to say around… well jokes on me, I actually started learning Java and since then I’m in love with it.

    By the time I was done learning OOP and all the goods of Java, I got back into php properly and a lot of new excited stuff was waiting for me: classes, interfaces, traits, typed properties and a lot more.

    To me that was a sign that Php could become so much more that a simple scripting language that sits under a web server, it was starting to develop more.

    Design approaches I could follow in Java through OOP I could now also follow in php with the extra perk that I could be lazy when I wanted to!


    So I wrote a server that borrows some things from other projects like spring boot like dependency injection and quarkus’ panache entity system.

    There’s not much documentation on the internal stuff, but most of the tools do have comments and you should get intellisense docs.


    This is the repository: [https://github.com/tncrazvan/catpaw](https://github.com/tncrazvan/catpaw)

    And this is a working template using it: [https://github.com/tncrazvan/catpaw-template](https://github.com/tncrazvan/catpaw-template)

    Here’s another template, this one’s using Svelte for the fe: [https://github.com/tncrazvan/catpaw-svelte-template](https://github.com/tncrazvan/catpaw-svelte-template)


    I did throw some documentation out there, here’s a guide on how to set it up with some custom example: [http://razshare.net/projects/catpaw](http://razshare.net/projects/catpaw)


    The guide doesn’t specify how to setup a JIT compiler for php, in order to do that I’d suggest following: [https://medium.com/jp-tech/try-out-jit-compiler-with-php-8-0-a020e6aeb3e5](https://medium.com/jp-tech/try-out-jit-compiler-with-php-8-0-a020e6aeb3e5)

    You’ll have to build php8 from source and enable a few options.

    I should highlight the fact that you do need to use a JIT compiler for it to be fast enough to be decent.


    In short, after you’ve setup your php jit, run

    composer create-project tncrazvan/catpaw-template ProjectName

    and then

    composer run dev

    your server entry point is `src/main.php`


    There’s a demo file uploading form in there with 2 kinds of request managers:

    1. A default one that waits for the POST request to be completely loaded in memory before executing any code and serving a response (similar to what you would do using apache for example).
    2. And a second method of doing things that executes code as the data is being recieved from the client using a HttpConsumer class injection.This avoids loading whole files in memory before saving them to disk.

    In case you’re wondering about the event loop, each request is being read in chunks of bytes of set size, so when it comes to reading requests there is a time sharing mechanism, when it comes to executing code you can use the `yield` keyword in your events/methods, the main loop will detect the generator and keep consuming it until your events/methods return (or end without a return).

    Here’s an example using the HttpConsumer api: [https://github.com/tncrazvan/catpaw-template/blob/master/src/api/http/post_file.php](https://github.com/tncrazvan/catpaw-template/blob/master/src/api/http/post_file.php)

    It offers more things like

    1. automatic multipart forms detection and parsing,
    2. automatic serialization of object responses,
    3. the WebSocket API,
    4. Autoinjection directly into object properties like Spring Boot,
    5. an Orm similar to Quarkus’ PanacheEntity system

    and so on , but I don’t think this is the place to discuss all of them.

    here are some of the standalone repositories though:

    * Panache-like-orm and query builder: [https://github.com/tncrazvan/php-orm](https://github.com/tncrazvan/php-orm) or on composer found as [tncrazvan/orm](https://packagist.org/packages/tncrazvan/orm)
    * Autowire (dependency injection): [https://github.com/tncrazvan/php-autowire](https://github.com/tncrazvan/php-autowire) or on composer found as [tncrazvan/autowire](https://packagist.org/packages/tncrazvan/autowire)


    This is a good time for Php, the JIT can actually give a great performance boost if you allow it to actually compile stuff and not kill the process right away.


    I tried it using php with and without the jit compler, and the difference is pretty noticeable from what I’ve tested so far.


    I should mention that all of this is meant to run on Linux.I haven’t properly tested it on windows, but some stuff will not work on windows, at least not as intended.

    For example http sessions are saved into a mounted ram disk (that you can enable or disable in its configuration), and windows does not support such a thing it should simply save your serialized sessions in a simple directory (I do plan to allow customization through a callback to save all that stuff anywhere).


    Don’t take this code too seriously, I hope you find it interesting and fun to play with.

    Stay safe!


    seems interesting, I will try it as soon I get some time


    Noice. Can you benchmark it with other options such as reactphp, swoole, etc?


    Really cool sounding async framework!! I hope I find some time to take a look and learn something- thanks for sharing



    Can you please test this using my PHP 8 docker image?

    composer require –dev phpexperts/dockerize-php8

    Make sure that `./vendor/bin` is in your PATH and your user is in the `docker` group and it should just auto-work.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.