Hamlet Framework



Hamlet Core

Build Status

Some notes

Bootstrapping an application with a Web Server

require_once __DIR__ . '/../../vendor/autoload.php';

$application = new new \Example\Application;

Bootstrapping an application with Swoole Http Server

require_once __DIR__ . '/../../vendor/autoload.php';

$application = new new \Example\Application;
\Hamlet\Bootstraps\SwooleBootstrap::run('', 8080, $application);

Bootstrapping an application with ReactPHP

require_once __DIR__ . '/../../vendor/autoload.php';

$application = new new \Example\Application;
\Hamlet\Bootstraps\ReactBootstrap::run('', 8080, $application);

Session support

If you're using React of Swoole to run your application and would like to have Session support, you'll need to implement SessionHandlerInterface and override AbstractApplication::sessionHandler to return an instance of your session handler.

How to use database package:

Start with two tables

    id   INTEGER,
    name VARCHAR(255)

CREATE TABLE addresses (
    user_id INTEGER,
    address VARCHAR(255)

We can map both tables to a single immutable entity like following:

class User implements \Hamlet\Database\Entity
    private $id, $name, $addresses;
    public function id(): int 
        return $this->id;
    public function name(): string
        return $this->name;
     * @return string[]
    public function addresses(): array
        return $this->addresses;
    public function __toString(): string
        return $this->name . ' [' . join(', ', $this->addresses . ']'; 

The repository method that fetches User entity by ID would look like this:

$database = Database::sqlite3(tempnam(sys_get_temp_dir(), '.sqlite'));

$procedure = $database->prepare('
    SELECT id, name, address
      FROM users
           JOIN addresses
             ON users.id = addresses.user_id
     WHERE id = ?

$users = $procedure->processAll()
foreach ($users as $user) {
    echo $user . PHP_EOL;

And Bob's your uncle. Unless you need to chew through tens of 1000s of rows, in which case you'll need to process entities in a stream:


return $procedure->stream()
    ->forEach(function (User $user) {
        echo $user . PHP_EOL;

To Do:

  • Support for WebSockets (swoole and react)
  • Add test coverage for alternative DB engines (https://docs.travis-ci.com/user/docker/)
  • Extend for easy addition of a CLI layer to any application (should work with shyaml)
  • Support for HTTP/2.0
  • Support for OAuth server (PHP League)
  • Support for GraphQL
  • Support for Alexa
  • Submit all reports to Psalm with regards to false positives