diff --git a/src/Server/Socket/SocketFactory.php b/src/Server/Socket/SocketFactory.php new file mode 100644 index 0000000..5625aa7 --- /dev/null +++ b/src/Server/Socket/SocketFactory.php @@ -0,0 +1,31 @@ + self::createReuseOrFallback(), + ProxyType::FORK_SHARED => new ForkSharedProxy(), + ProxyType::MASTER => new MasterProxy(), + }; + } + + private static function createReuseOrFallback(): SocketProxyInterface + { + $proxy = new ReusePortProxy(); + + if ($proxy->isSupported()) { + return $proxy; + } + + return new ForkSharedProxy(); + } +} diff --git a/tests/Server/Socket/SocketFactoryTest.php b/tests/Server/Socket/SocketFactoryTest.php new file mode 100644 index 0000000..0851fda --- /dev/null +++ b/tests/Server/Socket/SocketFactoryTest.php @@ -0,0 +1,52 @@ +assertInstanceOf(MasterProxy::class, $proxy); + } + + public function testCreateForkSharedReturnsForkSharedProxy(): void + { + $proxy = SocketFactory::create(ProxyType::FORK_SHARED, 8080, ProtocolType::HTTP); + + $this->assertInstanceOf(ForkSharedProxy::class, $proxy); + } + + public function testCreateReusePortReturnsReusePortWhenSupported(): void + { + if (!(new ReusePortProxy())->isSupported()) { + $this->markTestSkipped('SO_REUSEPORT not supported on this platform'); + } + + $proxy = SocketFactory::create(ProxyType::REUSE_PORT, 8080, ProtocolType::HTTP); + + $this->assertInstanceOf(ReusePortProxy::class, $proxy); + } + + public function testCreateReusePortFallsBackToForkSharedWhenNotSupported(): void + { + if ((new ReusePortProxy())->isSupported()) { + $this->markTestSkipped('SO_REUSEPORT is supported on this platform'); + } + + $proxy = SocketFactory::create(ProxyType::REUSE_PORT, 8080, ProtocolType::HTTP); + + $this->assertInstanceOf(ForkSharedProxy::class, $proxy); + } +}