1. Go to this page and download the library: Download phpdot/container-swoole library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpdot / container-swoole example snippets
use PHPdot\Container\ContainerBuilder;
use PHPdot\Container\Swoole\SwooleContextProvider;
use function PHPdot\Container\singleton;
use function PHPdot\Container\scoped;
$container = (new ContainerBuilder())
->withContextProvider(new SwooleContextProvider())
->addDefinitions([
// Shared across all coroutines
Router::class => singleton(),
Redis::class => singleton(),
// Isolated per coroutine — fresh for each request
Session::class => scoped(),
SignalManager::class => scoped(),
])
->build();
use Swoole\Http\Server;
use PHPdot\Container\ContainerBuilder;
use PHPdot\Container\Swoole\SwooleContextProvider;
use function PHPdot\Container\singleton;
use function PHPdot\Container\scoped;
$container = (new ContainerBuilder())
->withContextProvider(new SwooleContextProvider())
->addDefinitions([
Config::class => singleton(),
Session::class => scoped(fn($c) => Session::fromRequest($c->get(Request::class))),
])
->build();
$server = new Server('0.0.0.0', 8080);
$server->on('request', function ($req, $res) use ($container) {
// Each request runs in its own coroutine.
// Scoped services are fresh. Singletons are shared.
$session = $container->get(Session::class);
$res->end('Hello ' . $session->user());
// Coroutine ends here — scoped instances destroyed automatically.
});
$server->start();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.