Adapterman

repository·master·Indexed 21 days ago

https://github.com/joanhey/adapterman

A tool that allows existing PHP applications, such as Symfony, Laravel, Slim, Drupal, and Bolt CMS, to run on the async, event-driven Workerman engine without modifying the application's source code. It provides high-performance, scalable capabilities by adapting the framework's Front Controller through a specialized server.php and start.php configuration.

Tokens
8.7K
Snippets
29
Records
37
Agent score
74%

What's inside adapterman

  1. How the Adapterman start command works

    master

    When you run ./vendor/bin/adapterman start, the following process occurs:

    1. The binary executes a PHP script with a custom configuration: /usr/bin/env php -c vendor/joanhey/adapterman/cli-php.ini vendor/joanhey/adapterman/src/start.php "$@".
    2. The -c vendor/joanhey/adapterman/cli-php.ini flag applies a specific PHP configuration. This configuration disables certain built-in PHP functions, which the Adapterman framework then re-implements. This ensures that functions that typically behave differently under FPM work correctly under the PHP CLI.
    3. The vendor/joanhey/adapterman/src/start.php script handles the server startup and performs automatic framework detection (referencing logic found in vendor/joanhey/adapterman/src/frameworks/index.php).
  2. Understand Adapterman's execution model

    master
    Adapterman functions as an App Server. A key architectural detail is that you do not need to manually include the Composer Autoload in your application code because the Workerman server handles the inclusion of the Composer Autoload automatically.
  3. Start the Adapterman server

    master

    After installation, you can start the server using the Adapterman CLI binary. The server automatically detects the framework being used (e.g., ThinkPHP) and starts the appropriate environment. Once running, the application is accessible at http://localhost:8080.

    ./vendor/bin/adapterman start
  4. Run a Slim application with Workerman

    master

    Once you have configured your start.php file with the required run() function and global $app variable, you can start the server using the server.php command.

    By default, the application will be accessible at http://localhost:8080.

    php server.php start
  5. Run your Laravel app with Adapterman

    master

    Once server.php and start.php are configured, start the application from the project root using the PHP CLI. The server will be accessible at the host and port defined in server.php (defaulting to http://localhost:8080).

    php server.php start
  6. Modify start.php for Workerman compatibility

    master

    When using Laravel with Adapterman, you must modify the start.php file (copied from public/index.php) to wrap the Laravel kernel handling inside a run() function. This function uses output buffering (ob_start() and ob_get_clean()) to capture the application response so it can be sent back through the Workerman connection.

    <?php
    
    $app = require_once __DIR__.'/bootstrap/app.php';
    
    global $kernel;
    
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    
    function run()
    {
        global $kernel;
    
        ob_start();
    
        $response = $kernel->handle(
            $request = Illuminate\Http\Request::capture()
        );
    
        $response->send();
    
        $kernel->terminate($request, $response);
        
        return ob_get_clean();
    }
  7. Configure Nginx as a reverse proxy for Adapterman apps

    master

    You can use Nginx to handle TLS termination, serve static files, and proxy requests to your Workerman-based applications under the same domain.

    To set this up, configure an Nginx server block that serves your static files from a public directory and uses a location @backend block to proxy all other requests to your application's IP and port.

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        # Change to your public dir
        root /var/www/html/your-app/public;
        index index.html index.htm;
    
        server_name localhost;
    
        location / {
            try_files $uri $uri/ @backend;
        }
    
        # Add the ip:port of your app
        location @backend {
             proxy_pass 127.0.0.1:8080; // or localhost:8080;
             proxy_http_version 1.1;
             proxy_set_header Connection "";
        }
    
        location ~ /\.\{ 
            deny all;
        }
    }
  8. Configure Bolt entry point for Workerman

    master

    When adapting app/index.php to a Workerman-compatible start.php, follow these steps:

    1. Disable runtime limit adjustments: Comment out or remove set_time_limit(0) and ini_set('memory_limit', ...) calls. It is recommended to set these values directly in your php.ini instead.
    2. Global Kernel: Declare the $kernel as a global variable and instantiate it outside the request loop.
    3. Implement run() function: Create a run(): string function that:
      • Uses ob_start() to begin output buffering.
      • Creates the request using Request::createFromGlobals().
      • Handles the request via $kernel->handle($request).
      • Sends the response via $response->send().
      • Terminates the request via $kernel->terminate($request, $response).
      • Returns the captured output using ob_get_clean().