Installation
composer require hyperf/engine-contract
Add to composer.json under autoload-dev if using for testing:
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
}
Basic Contract Definition
Define a contract interface (e.g., app/Contracts/EngineInterface.php):
namespace App\Contracts;
interface EngineInterface {
public function execute(string $command): string;
}
Register Contract in Hyperf
Bind the contract in config/autoload/containers.php:
'bindings' => [
\App\Contracts\EngineInterface::class => \App\Services\EngineService::class,
],
First Use Case Inject the contract into a service or controller:
use App\Contracts\EngineInterface;
class CommandHandler {
public function __construct(private EngineInterface $engine) {}
public function handle(string $input) {
return $this->engine->execute($input);
}
}
Hyperf\Engine\Contract\EngineContract to dynamically resolve engines:
$engine = app()->make(\Hyperf\Engine\Contract\EngineContract::class, [
'engine' => 'custom_engine',
]);
$container->bind(\App\Contracts\EngineInterface::class, function ($container) {
return new \App\Services\CustomEngine();
});
Hyperf\Engine\AbstractEngine for setup:
class CustomEngine extends AbstractEngine {
public function __construct() {
$this->init(); // Calls `initialize()` if defined
}
}
Hyperf\Engine\Contract\EngineInterface::shutdown() for teardown.Hyperf\Engine\Contract\EngineContract in tests:
$this->mock(\Hyperf\Engine\Contract\EngineContract::class, function ($mock) {
$mock->shouldReceive('execute')->andReturn('mocked_result');
});
Hyperf\Engine\EngineManager.Hyperf\Process\ProcessManager to manage long-running engines:
$process = Process::fromCallable(function () {
$engine = app()->make(\App\Contracts\EngineInterface::class);
$engine->execute('long_task');
});
Hyperf\Event\EventDispatcher:
event(new EngineExecuted($result));
$container->bind(\App\Contracts\EngineInterface::class, function ($c) {
return $c->make(\App\Services\EngineService::class);
});
Hyperf\Context\Context for shared data:
Context::set('engine_state', $state);
config/autoload/engines.php for engine-specific settings:
return [
'default' => 'custom_engine',
'engines' => [
'custom_engine' => [
'timeout' => 30,
],
],
];
Hyperf\Logger\LoggerFactory to log engine activity:
$logger = \Hyperf\Logger\LoggerFactory::get('engine');
$logger->info('Engine executed', ['command' => $command]);
Hyperf\Engine\Middleware\EngineProfilerMiddleware to track execution time:
$middleware = new EngineProfilerMiddleware();
$middleware->process($request, $next);
Hyperf\Engine\Contract\EngineContract for multi-engine support:
interface MultiEngineContract extends EngineContract {
public function getEngines(): array;
}
class EngineAuthMiddleware implements Middleware {
public function process(RequestInterface $request, Closure $next): ResponseInterface {
if (!$request->hasHeader('engine-token')) {
throw new \RuntimeException('Unauthorized');
}
return $next($request);
}
}
Hyperf\Engine\Contract\SerializableEngine for state persistence:
class SerializableEngine implements SerializableEngine {
public function serialize(): string {
return serialize($this->state);
}
public function unserialize(string $data): void {
$this->state = unserialize($data);
}
}
How can I help you explore Laravel packages today?