Treblle/treblle-php

Consider exposing more from builder

Opened this issue · 0 comments

Trebble factory exposes a single method: create() which is great when using SAPIs that support shutdown functions :) but causes a memory leak when the SAPI doesn't support shutdown functions. For example, FrankenPHP (disclosure: I'm a contributor on the project), doesn't support shutdown functions.

It would be great if the factory also exposed a a method that did the same thing as create() just without registering side-effects, so it could be used as such:

function handle_request(\Closure $afterRequest) {
  $treblle = TreblleFactory::onlyCreate($apiKey, $projectId);
  set_error_handler($treblle->onError(...));
  set_exception_handler($treblle->onException(...));
  $afterRequest($treblle->onShutdown(...));

  // do request
}

$running = true;
while($running) {
  $later = [];
  $afterRequest = static function ($callback) use (&$later) {
    $later[] = $callback;
  };
  
  $running = frankenphp_handle_request(fn() => handle_request($afterRequest));
  foreach($later as $cb) $cb();
}