2017 © Pedro Peláez
 

library karma

Experimental HTTP server written in PHP

image

thinframe/karma

Experimental HTTP server written in PHP

  • Saturday, April 12, 2014
  • by unusorin
  • Repository
  • 4 Watchers
  • 2 Stars
  • 59 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Karma

New generation of PHP frameworks, (*1)

Latest Stable Version Latest Unstable Version License, (*2)

What is the problem with the current generation?

To explain the current problem let me start with a simple description on how PHP websites work:, (*3)

  1. A user makes a request from his browser to a certain website that is running using PHP, (*4)

  2. The HTTP server accepts that request and handles it in different ways:, (*5)

    • Apache executes the PHP script associated with the requested path directly and returns the output, (*6)

    • Nginx uses PHP-FPM to do basically the same thing., (*7)

  3. The HTTP server sends the response back to the client, (*8)

All good so far, but let's look a little bit deeper. When a PHP script is executed the followings things take place:, (*9)

  1. The necessary dependencies are loaded, lazy or all at once, and all the components are configured/initialized., (*10)

  2. Your logic is executed depending on the request and a response is returned., (*11)

But there is a problem in this process, because one of those two steps is executed every single time. Oh well ..., (*12)

Step 1 is executed for every single request, and from my point of view that is a bottleneck of PHP applications, since IO operations are very expensive., (*13)

Solutions that already exists

  • Use bootstrap files like Symfony2 do.
  • Investing in hardware. This is a good solution if you have the MONEY.
  • OPcache
  • And so on ...

A new aproach ... ?

Tom & David Kelley, the authors of the book Creative Confidence and owners of the company IDEO, say in their book that the best solution to an existing problem is to rethink the whole process that causes that problem. But the problem in this case is by design. PHP was made this way, right ?, (*14)

The solution

Some time ago, I came across ReactPHP, it is a PHP library that offers Event-driven, non-blocking I/O. It is inspired from NodeJS and it is awesome. I played with it, I did some tests and then an idea came to my mind:, (*15)

Instead of using two programs to do one thing, the HTTP server and the PHP, why don't we use a single one that will act as both?, (*16)

Basically, we merge the HTTP server, and the PHP framework that is behind the server, to make a single program written in PHP that will do both tasks. This way we will eliminate the recurrent loading/initialization time for PHP dependencies and also save some time that is lost between the HTTP server and PHP., (*17)

Painfull ? Not at all., (*18)

Karma

It is a PHP Application that acts as an HTTP server and a PHP framework, all at the same time. It provides an easy to use abstraction layer over anything that relates to socket work, so developers can focus on their work. It is fast, with a response time that is 80 to 90 percent faster than a conventional framework and it works with already existing PHP components, like Twig., (*19)

It provides built-in routing using Symfony Routing component and all the architecture is build around Symfony Dependency Injection Container. Most of the socket work is handled by the ReactPHP component. Basically, it is built with already existing PHP components., (*20)

Installation

composer create-project thinframe/karma-project <project_name> --stability=dev

If you want to use the command line component with completion support execute the following command in your project root:, (*21)

bash bin/thinframe-installer

From this point on, you can use the thinframe command in each karma project folder that you have. Otherwise, you can use bin/thinframe instead of thinframe., (*22)

Usage

To see all available commands execute the following command: thinframe help. Pretty simple., (*23)

  help                   - Show this list

  server run             - Start the HTTP server

  server run --daemon    - Start the HTTP server as a daemon

  server stop            - Stop the HTTP server

  server status          - Check HTTP server status

  server restart         - Restart the HTTP server

  server monitor         - Restart the HTTP server when source files are changed

  debug routes           - Show all routes

  debug applications     - Show all loaded applications

The test the setup, run thinframe server start and in your browser go to http://localhost:1337. If you see a default Karma page, then everything works., (*24)

By default, when you install karma-project, it creates a bootstrap project. All project related files are located in src/Acme/DemoApp., (*25)

[to be continued ...], (*26)

TODO's

  • Write unit tests and functional tests, (*27)

  • Optimize memory, (*28)

  • More documentation, (*29)

  • Implement PsySH, (*30)

  • More logging, (*31)

  • All kinds of features, (*32)

  • Test on all Linux distributions., (*33)

OS Support

So far it was tested on Ubuntu and Elementary OS. It should work on other Linux distributions pretty well., (*34)

Dependencies

  • PHP >= 5.4, (*35)

  • ext-inotify - http://www.php.net/manual/en/inotify.install.php, (*36)

Fair warning

This project is experimental. I recommend that it shouldn't be used on a production environment (yet)., (*37)

The Versions