From 7d365cd5a527416bbff4893c82d41f6d094af765 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 18 May 2025 12:21:26 +0200 Subject: [PATCH 01/41] add mechanism to extend the os config --- CHANGELOG.md | 5 ++++ src/Config.php | 31 +++++++++++++++++++++++ src/Config/Logger.php | 30 ++++++++++++++++++++++ src/CurrentProcess/Logger.php | 10 -------- src/OperatingSystem/Logger.php | 9 ++++++- tests/CurrentProcess/GenericTest.php | 38 ++++++++++++++++++++-------- 6 files changed, 101 insertions(+), 22 deletions(-) create mode 100644 src/Config/Logger.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ff432ef..e5df22a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## [Unreleased] +### Added + +- `Innmind\OperatingSystem\Config::map()` +- `Innmind\OperatingSystem\Config\Logger` + ### Changed - Requires `innmind/time-continuum:^4.1.1` diff --git a/src/Config.php b/src/Config.php index 286b725..fb642e2 100644 --- a/src/Config.php +++ b/src/Config.php @@ -72,6 +72,17 @@ public static function of(): self ); } + /** + * @psalm-mutation-free + * + * @param callable(self): self $map + */ + public function map(callable $map): self + { + /** @psalm-suppress ImpureFunctionCall */ + return $map($this); + } + /** * @psalm-mutation-free */ @@ -123,6 +134,26 @@ public function haltProcessVia(Halt $halt): self ); } + /** + * @psalm-mutation-free + * + * @param callable(Halt): Halt $map + */ + public function maphalt(callable $map): self + { + /** @psalm-suppress ImpureFunctionCall */ + return new self( + $this->clock, + $this->caseSensitivity, + $this->io, + $map($this->halt), + $this->path, + $this->maxHttpConcurrency, + $this->httpHeartbeat, + $this->disableSSLVerification, + ); + } + /** * @psalm-mutation-free */ diff --git a/src/Config/Logger.php b/src/Config/Logger.php new file mode 100644 index 0000000..42223d3 --- /dev/null +++ b/src/Config/Logger.php @@ -0,0 +1,30 @@ +mapHalt(fn($halt) => Halt\Logger::psr( + $halt, + $this->logger, + )); + } + + public static function psr(LoggerInterface $logger): self + { + return new self($logger); + } +} diff --git a/src/CurrentProcess/Logger.php b/src/CurrentProcess/Logger.php index 918d9a5..bbb20b8 100644 --- a/src/CurrentProcess/Logger.php +++ b/src/CurrentProcess/Logger.php @@ -51,16 +51,6 @@ public function signals(): Signals #[\Override] public function halt(Period $period): Attempt { - $this->logger->debug('Halting current process...', ['period' => [ - 'years' => $period->years(), - 'months' => $period->months(), - 'days' => $period->days(), - 'hours' => $period->hours(), - 'minutes' => $period->minutes(), - 'seconds' => $period->seconds(), - 'milliseconds' => $period->milliseconds(), - ]]); - return $this->process->halt($period); } diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php index 900dc03..ca6c391 100644 --- a/src/OperatingSystem/Logger.php +++ b/src/OperatingSystem/Logger.php @@ -10,6 +10,8 @@ Sockets, Remote, CurrentProcess, + Config, + Factory, }; use Innmind\Server\Status; use Innmind\Server\Control; @@ -29,7 +31,12 @@ private function __construct(OperatingSystem $os, LoggerInterface $logger) public static function psr(OperatingSystem $os, LoggerInterface $logger): self { - return new self($os, $logger); + return new self( + $os->map(static fn($_, $config) => Factory::build( + $config->map(Config\Logger::psr($logger)), + )), + $logger, + ); } #[\Override] diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcess/GenericTest.php index 30e825a..ba313a5 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcess/GenericTest.php @@ -7,16 +7,25 @@ CurrentProcess\Generic, CurrentProcess\Signals, CurrentProcess, + Factory, + Config, }; use Innmind\Server\Control\Server\Process\Pid; use Innmind\Server\Status\Server\Memory\Bytes; use Innmind\TimeContinuum\Period; use Innmind\TimeWarp\Halt; use Innmind\Immutable\SideEffect; -use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use Psr\Log\NullLogger; +use Innmind\BlackBox\{ + PHPUnit\BlackBox, + PHPUnit\Framework\TestCase, + Set, +}; class GenericTest extends TestCase { + use BlackBox; + public function testInterface() { $this->assertInstanceOf( @@ -36,18 +45,25 @@ public function testId() ); } - public function testHalt() + public function testHalt(): BlackBox\Proof { - $process = Generic::of( - Halt\Usleep::new(), - ); + return $this + ->forAll(Set::of( + static fn($config) => $config, + Config\Logger::psr(new NullLogger), + )) + ->prove(function($extension) { + $process = Factory::build( + Config::of()->map($extension), + )->process(); - $this->assertInstanceOf( - SideEffect::class, - $process - ->halt(Period::millisecond(1)) - ->unwrap(), - ); + $this->assertInstanceOf( + SideEffect::class, + $process + ->halt(Period::millisecond(1)) + ->unwrap(), + ); + }); } public function testSignals() From f12c0b04f6e6b8f6697a1cf0cb403268ddf7a21e Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 18 May 2025 12:28:06 +0200 Subject: [PATCH 02/41] use higher api to build the logger --- tests/CurrentProcess/GenericTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcess/GenericTest.php index ba313a5..a080f9e 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcess/GenericTest.php @@ -7,8 +7,8 @@ CurrentProcess\Generic, CurrentProcess\Signals, CurrentProcess, + OperatingSystem\Logger, Factory, - Config, }; use Innmind\Server\Control\Server\Process\Pid; use Innmind\Server\Status\Server\Memory\Bytes; @@ -47,15 +47,15 @@ public function testId() public function testHalt(): BlackBox\Proof { + $os = Factory::build(); + return $this ->forAll(Set::of( - static fn($config) => $config, - Config\Logger::psr(new NullLogger), + $os, + Logger::psr($os, new NullLogger), )) - ->prove(function($extension) { - $process = Factory::build( - Config::of()->map($extension), - )->process(); + ->prove(function($os) { + $process = $os->process(); $this->assertInstanceOf( SideEffect::class, From 58699bea08e979761bef41ce58066de226a3ae4a Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 18:54:34 +0200 Subject: [PATCH 03/41] make OperatingSystem a final class --- CHANGELOG.md | 1 + src/Factory.php | 2 +- src/OperatingSystem.php | 74 +++++++++++++++++++++---- src/OperatingSystem/Implementation.php | 35 ++++++++++++ src/OperatingSystem/Logger.php | 14 ++--- src/OperatingSystem/Resilient.php | 11 ++-- src/OperatingSystem/Unix.php | 5 +- tests/CurrentProcess/GenericTest.php | 4 +- tests/FactoryTest.php | 4 +- tests/OperatingSystem/ResilientTest.php | 5 +- tests/OperatingSystem/UnixTest.php | 2 - 11 files changed, 119 insertions(+), 38 deletions(-) create mode 100644 src/OperatingSystem/Implementation.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e5df22a..fac9f9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ - `Innmind\OperatingSystem\Sockets::open()` now returns an `Innmind\Immutable\Attempt` - `Innmind\OperatingSystem\Sockets::takeOver()` now returns an `Innmind\Immutable\Attempt` - `Innmind\OperatingSystem\Sockets::connectTo()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\OperatingSystem` is now a final class ### Fixed diff --git a/src/Factory.php b/src/Factory.php index 932ea38..ee9cc5a 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -10,7 +10,7 @@ public static function build(?Config $config = null): OperatingSystem switch (\PHP_OS) { case 'Darwin': case 'Linux': - return OperatingSystem\Unix::of($config); + return OperatingSystem::new($config); } throw new \LogicException('Unuspported operating system'); diff --git a/src/OperatingSystem.php b/src/OperatingSystem.php index 8e24a51..f71137a 100644 --- a/src/OperatingSystem.php +++ b/src/OperatingSystem.php @@ -3,25 +3,79 @@ namespace Innmind\OperatingSystem; +use Innmind\OperatingSystem\OperatingSystem\{ + Implementation, + Unix, +}; use Innmind\Server\Status\Server as ServerStatus; use Innmind\Server\Control\Server as ServerControl; use Innmind\TimeContinuum\Clock; -interface OperatingSystem +final class OperatingSystem { + private function __construct( + private Implementation $implementation, + ) { + } + + public static function new(?Config $config = null): self + { + return new self(Unix::of($config)); + } + /** * This method allows to change the underlying OS implementation while being * able to keep any decorators on top of it. * * @param callable(self, Config): self $map */ - public function map(callable $map): self; - public function clock(): Clock; - public function filesystem(): Filesystem; - public function status(): ServerStatus; - public function control(): ServerControl; - public function ports(): Ports; - public function sockets(): Sockets; - public function remote(): Remote; - public function process(): CurrentProcess; + public function map(callable $map): self + { + return new self($this->implementation->map( + static fn($implementation, $config) => $map( + new self($implementation), + $config, + )->implementation, + )); + } + + public function clock(): Clock + { + return $this->implementation->clock(); + } + + public function filesystem(): Filesystem + { + return $this->implementation->filesystem(); + } + + public function status(): ServerStatus + { + return $this->implementation->status(); + } + + public function control(): ServerControl + { + return $this->implementation->control(); + } + + public function ports(): Ports + { + return $this->implementation->ports(); + } + + public function sockets(): Sockets + { + return $this->implementation->sockets(); + } + + public function remote(): Remote + { + return $this->implementation->remote(); + } + + public function process(): CurrentProcess + { + return $this->implementation->process(); + } } diff --git a/src/OperatingSystem/Implementation.php b/src/OperatingSystem/Implementation.php new file mode 100644 index 0000000..a2799cb --- /dev/null +++ b/src/OperatingSystem/Implementation.php @@ -0,0 +1,35 @@ +os = $os; $this->logger = $logger; } - public static function psr(OperatingSystem $os, LoggerInterface $logger): self + public static function psr(Implementation $os, LoggerInterface $logger): self { return new self( - $os->map(static fn($_, $config) => Factory::build( + $os->map(static fn($_, $config) => Unix::of( $config->map(Config\Logger::psr($logger)), )), $logger, @@ -40,7 +38,7 @@ public static function psr(OperatingSystem $os, LoggerInterface $logger): self } #[\Override] - public function map(callable $map): OperatingSystem + public function map(callable $map): Implementation { return new self( $this->os->map($map), diff --git a/src/OperatingSystem/Resilient.php b/src/OperatingSystem/Resilient.php index b360067..f1f723a 100644 --- a/src/OperatingSystem/Resilient.php +++ b/src/OperatingSystem/Resilient.php @@ -4,7 +4,6 @@ namespace Innmind\OperatingSystem\OperatingSystem; use Innmind\OperatingSystem\{ - OperatingSystem, Filesystem, Ports, Sockets, @@ -18,22 +17,22 @@ /** * This decorator helps retry certain _safe_ operations on remote systems */ -final class Resilient implements OperatingSystem +final class Resilient implements Implementation { - private OperatingSystem $os; + private Implementation $os; - private function __construct(OperatingSystem $os) + private function __construct(Implementation $os) { $this->os = $os; } - public static function of(OperatingSystem $os): self + public static function of(Implementation $os): self { return new self($os); } #[\Override] - public function map(callable $map): OperatingSystem + public function map(callable $map): Implementation { return new self($this->os->map($map)); } diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index 04044ac..5504faf 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -4,7 +4,6 @@ namespace Innmind\OperatingSystem\OperatingSystem; use Innmind\OperatingSystem\{ - OperatingSystem, Filesystem, Ports, Sockets, @@ -22,7 +21,7 @@ }; use Innmind\TimeContinuum\Clock; -final class Unix implements OperatingSystem +final class Unix implements Implementation { private Config $config; private ?Filesystem $filesystem = null; @@ -44,7 +43,7 @@ public static function of(?Config $config = null): self } #[\Override] - public function map(callable $map): OperatingSystem + public function map(callable $map): Implementation { return $map($this, $this->config); } diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcess/GenericTest.php index a080f9e..b025686 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcess/GenericTest.php @@ -8,7 +8,7 @@ CurrentProcess\Signals, CurrentProcess, OperatingSystem\Logger, - Factory, + OperatingSystem\Unix, }; use Innmind\Server\Control\Server\Process\Pid; use Innmind\Server\Status\Server\Memory\Bytes; @@ -47,7 +47,7 @@ public function testId() public function testHalt(): BlackBox\Proof { - $os = Factory::build(); + $os = Unix::of(); return $this ->forAll(Set::of( diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 2dc8569..6632c91 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -5,7 +5,7 @@ use Innmind\OperatingSystem\{ Factory, - OperatingSystem\Unix, + OperatingSystem, Config, }; use Innmind\TimeContinuum\Clock; @@ -26,7 +26,7 @@ public function testBuild() $os = Factory::build(Config::of()->withClock($clock)); - $this->assertInstanceOf(Unix::class, $os); + $this->assertInstanceOf(OperatingSystem::class, $os); $this->assertSame($clock, $os->clock()); } diff --git a/tests/OperatingSystem/ResilientTest.php b/tests/OperatingSystem/ResilientTest.php index 9dba327..f2097ec 100644 --- a/tests/OperatingSystem/ResilientTest.php +++ b/tests/OperatingSystem/ResilientTest.php @@ -6,13 +6,11 @@ use Innmind\OperatingSystem\{ OperatingSystem\Resilient, OperatingSystem\Unix, - OperatingSystem, Filesystem, Ports, Sockets, Remote, CurrentProcess, - Factory, }; use Innmind\Server\Status\Server as ServerStatus; use Innmind\Server\Control\Server as ServerControl; @@ -23,9 +21,8 @@ class ResilientTest extends TestCase { public function testInterface() { - $os = Resilient::of(Factory::build()); + $os = Resilient::of(Unix::of()); - $this->assertInstanceOf(OperatingSystem::class, $os); $this->assertInstanceOf(Clock::class, $os->clock()); $this->assertInstanceOf(Filesystem::class, $os->filesystem()); $this->assertInstanceOf(ServerStatus::class, $os->status()); diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystem/UnixTest.php index f55e1e8..cee20e1 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystem/UnixTest.php @@ -5,7 +5,6 @@ use Innmind\OperatingSystem\{ OperatingSystem\Unix, - OperatingSystem, Filesystem, Ports, Sockets, @@ -26,7 +25,6 @@ public function testInterface() $os = Unix::of(Config::of()->withClock($clock)); - $this->assertInstanceOf(OperatingSystem::class, $os); $this->assertSame($clock, $os->clock()); $this->assertInstanceOf(Filesystem\Generic::class, $os->filesystem()); $this->assertInstanceOf(ServerStatus::class, $os->status()); From 0b6660fcc88e88ea7b99e3bb6c99b332012340b4 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 18:57:47 +0200 Subject: [PATCH 04/41] typo --- src/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Config.php b/src/Config.php index fb642e2..b38cd23 100644 --- a/src/Config.php +++ b/src/Config.php @@ -139,7 +139,7 @@ public function haltProcessVia(Halt $halt): self * * @param callable(Halt): Halt $map */ - public function maphalt(callable $map): self + public function mapHalt(callable $map): self { /** @psalm-suppress ImpureFunctionCall */ return new self( From 31b61711ca9b10d6e000e218bd1a61e52f2f0a30 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 18:59:03 +0200 Subject: [PATCH 05/41] use promoted properties --- src/Config.php | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/src/Config.php b/src/Config.php index b38cd23..1e33743 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,39 +15,20 @@ final class Config { - private Clock $clock; - private CaseSensitivity $caseSensitivity; - private IO $io; - private Halt $halt; - private EnvironmentPath $path; - /** @var Maybe */ - private Maybe $maxHttpConcurrency; - /** @var Maybe */ - private Maybe $httpHeartbeat; - private bool $disableSSLVerification; - /** * @param Maybe $maxHttpConcurrency * @param Maybe $httpHeartbeat */ private function __construct( - Clock $clock, - CaseSensitivity $caseSensitivity, - IO $io, - Halt $halt, - EnvironmentPath $path, - Maybe $maxHttpConcurrency, - Maybe $httpHeartbeat, - bool $disableSSLVerification, + private Clock $clock, + private CaseSensitivity $caseSensitivity, + private IO $io, + private Halt $halt, + private EnvironmentPath $path, + private Maybe $maxHttpConcurrency, + private Maybe $httpHeartbeat, + private bool $disableSSLVerification, ) { - $this->clock = $clock; - $this->caseSensitivity = $caseSensitivity; - $this->io = $io; - $this->halt = $halt; - $this->path = $path; - $this->maxHttpConcurrency = $maxHttpConcurrency; - $this->httpHeartbeat = $httpHeartbeat; - $this->disableSSLVerification = $disableSSLVerification; } public static function of(): self From ba7bb6330766d597388de82504ee50c2c6e0724c Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:11:31 +0200 Subject: [PATCH 06/41] add resilient config --- CHANGELOG.md | 1 + src/Config.php | 42 ++++++++++++++++++++++++++++++++++ src/Config/Resilient.php | 29 +++++++++++++++++++++++ src/Remote/Generic.php | 3 ++- tests/Remote/ResilientTest.php | 11 +++++---- 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/Config/Resilient.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fac9f9b..bdf0022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `Innmind\OperatingSystem\Config::map()` - `Innmind\OperatingSystem\Config\Logger` +- `Innmind\OperatingSystem\Config\Resilient` ### Changed diff --git a/src/Config.php b/src/Config.php index 1e33743..2878ab2 100644 --- a/src/Config.php +++ b/src/Config.php @@ -7,6 +7,7 @@ Clock, Period, }; +use Innmind\HttpTransport\Transport as HttpTransport; use Innmind\Filesystem\CaseSensitivity; use Innmind\Server\Status\EnvironmentPath; use Innmind\TimeWarp\Halt; @@ -18,6 +19,7 @@ final class Config /** * @param Maybe $maxHttpConcurrency * @param Maybe $httpHeartbeat + * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport */ private function __construct( private Clock $clock, @@ -27,6 +29,7 @@ private function __construct( private EnvironmentPath $path, private Maybe $maxHttpConcurrency, private Maybe $httpHeartbeat, + private \Closure $mapHttpTransport, private bool $disableSSLVerification, ) { } @@ -49,6 +52,7 @@ public static function of(): self }), $maxHttpConcurrency, $httpHeartbeat, + static fn(HttpTransport $transport) => $transport, false, ); } @@ -77,6 +81,7 @@ public function withClock(Clock $clock): self $this->path, $this->maxHttpConcurrency, $this->httpHeartbeat, + $this->mapHttpTransport, $this->disableSSLVerification, ); } @@ -94,6 +99,7 @@ public function caseInsensitiveFilesystem(): self $this->path, $this->maxHttpConcurrency, $this->httpHeartbeat, + $this->mapHttpTransport, $this->disableSSLVerification, ); } @@ -111,6 +117,7 @@ public function haltProcessVia(Halt $halt): self $this->path, $this->maxHttpConcurrency, $this->httpHeartbeat, + $this->mapHttpTransport, $this->disableSSLVerification, ); } @@ -131,6 +138,7 @@ public function mapHalt(callable $map): self $this->path, $this->maxHttpConcurrency, $this->httpHeartbeat, + $this->mapHttpTransport, $this->disableSSLVerification, ); } @@ -148,6 +156,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $path, $this->maxHttpConcurrency, $this->httpHeartbeat, + $this->mapHttpTransport, $this->disableSSLVerification, ); } @@ -167,6 +176,7 @@ public function limitHttpConcurrencyTo(int $max): self $this->path, Maybe::just($max), $this->httpHeartbeat, + $this->mapHttpTransport, $this->disableSSLVerification, ); } @@ -186,6 +196,27 @@ public function withHttpHeartbeat(Period $timeout, callable $heartbeat): self $this->path, $this->maxHttpConcurrency, Maybe::just([$timeout, $heartbeat]), + $this->mapHttpTransport, + $this->disableSSLVerification, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(HttpTransport): HttpTransport $map + */ + public function mapHttpTransport(\Closure $map): self + { + return new self( + $this->clock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->path, + $this->maxHttpConcurrency, + $this->httpHeartbeat, + $map, $this->disableSSLVerification, ); } @@ -203,6 +234,7 @@ public function disableSSLVerification(): self $this->path, $this->maxHttpConcurrency, $this->httpHeartbeat, + $this->mapHttpTransport, true, ); } @@ -267,6 +299,16 @@ public function httpHeartbeat(): Maybe return $this->httpHeartbeat; } + /** + * @internal + * + * @return \Closure(HttpTransport): HttpTransport + */ + public function httpTransportMapper(): \Closure + { + return $this->mapHttpTransport; + } + /** * @internal */ diff --git a/src/Config/Resilient.php b/src/Config/Resilient.php new file mode 100644 index 0000000..954d1de --- /dev/null +++ b/src/Config/Resilient.php @@ -0,0 +1,29 @@ +mapHttpTransport(static fn($transport) => ExponentialBackoff::of( + $transport, + $config->halt(), + )); + } + + /** + * This config helps retry certain _safe_ operations on remote systems + */ + public static function new(): self + { + return self::instance; + } +} diff --git a/src/Remote/Generic.php b/src/Remote/Generic.php index 46c484a..ec2a719 100644 --- a/src/Remote/Generic.php +++ b/src/Remote/Generic.php @@ -95,8 +95,9 @@ public function http(): HttpTransport true => $http->disableSSLVerification(), false => $http, }; + $map = $this->config->httpTransportMapper(); - return $this->http = $http; + return $this->http = $map($http); } #[\Override] diff --git a/tests/Remote/ResilientTest.php b/tests/Remote/ResilientTest.php index 1a76171..02905aa 100644 --- a/tests/Remote/ResilientTest.php +++ b/tests/Remote/ResilientTest.php @@ -4,8 +4,10 @@ namespace Tests\Innmind\OperatingSystem\Remote; use Innmind\OperatingSystem\{ + OperatingSystem, Remote\Resilient, Remote, + Config, Factory, }; use Innmind\HttpTransport\ExponentialBackoff; @@ -90,12 +92,13 @@ public function testSocket(): BlackBox\Proof public function testHttp() { - $remote = Resilient::of( - $this->os->remote(), - $this->os->process(), + $os = $this->os->map( + static fn($_, $config) => OperatingSystem::new( + $config->map(Config\Resilient::new()), + ), ); - $this->assertInstanceOf(ExponentialBackoff::class, $remote->http()); + $this->assertInstanceOf(ExponentialBackoff::class, $os->remote()->http()); } public function testSql(): BlackBox\Proof From 5c226068a8d9848c679e1f3604267a230f953272 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:14:17 +0200 Subject: [PATCH 07/41] remove useless classes --- CHANGELOG.md | 1 + src/OperatingSystem/Resilient.php | 90 ------------------------- src/Remote/Resilient.php | 78 --------------------- tests/OperatingSystem/ResilientTest.php | 50 -------------- tests/Remote/ResilientTest.php | 86 +---------------------- 5 files changed, 2 insertions(+), 303 deletions(-) delete mode 100644 src/OperatingSystem/Resilient.php delete mode 100644 src/Remote/Resilient.php delete mode 100644 tests/OperatingSystem/ResilientTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf0022..e29ba08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - `Innmind\OperatingSystem\Config::useStreamCapabilities()` - `Innmind\OperatingSystem\Sockets::watch()` +- `Innmind\OperatingSystem\OperatingSystem\Resilient` ## 5.2.0 - 2024-07-14 diff --git a/src/OperatingSystem/Resilient.php b/src/OperatingSystem/Resilient.php deleted file mode 100644 index f1f723a..0000000 --- a/src/OperatingSystem/Resilient.php +++ /dev/null @@ -1,90 +0,0 @@ -os = $os; - } - - public static function of(Implementation $os): self - { - return new self($os); - } - - #[\Override] - public function map(callable $map): Implementation - { - return new self($this->os->map($map)); - } - - #[\Override] - public function clock(): Clock - { - return $this->os->clock(); - } - - #[\Override] - public function filesystem(): Filesystem - { - return $this->os->filesystem(); - } - - #[\Override] - public function status(): ServerStatus - { - return $this->os->status(); - } - - #[\Override] - public function control(): ServerControl - { - return $this->os->control(); - } - - #[\Override] - public function ports(): Ports - { - return $this->os->ports(); - } - - #[\Override] - public function sockets(): Sockets - { - return $this->os->sockets(); - } - - #[\Override] - public function remote(): Remote - { - return Remote\Resilient::of( - $this->os->remote(), - $this->os->process(), - ); - } - - #[\Override] - public function process(): CurrentProcess - { - return $this->os->process(); - } -} diff --git a/src/Remote/Resilient.php b/src/Remote/Resilient.php deleted file mode 100644 index 9f0a86a..0000000 --- a/src/Remote/Resilient.php +++ /dev/null @@ -1,78 +0,0 @@ -remote = $remote; - $this->process = $process; - } - - public static function of(Remote $remote, CurrentProcess $process): self - { - return new self($remote, $process); - } - - #[\Override] - public function ssh(Url $server): Server - { - return $this->remote->ssh($server); - } - - #[\Override] - public function socket(Transport $transport, Authority $authority): Attempt - { - return $this->remote->socket($transport, $authority); - } - - #[\Override] - public function http(): HttpTransport - { - return ExponentialBackoff::of( - $this->remote->http(), - new class($this->process) implements Halt { - public function __construct( - private CurrentProcess $process, - ) { - } - - #[\Override] - public function __invoke(Period $period): Attempt - { - return $this->process->halt($period); - } - }, - ); - } - - #[\Override] - public function sql(Url $server): Connection - { - return $this->remote->sql($server); - } -} diff --git a/tests/OperatingSystem/ResilientTest.php b/tests/OperatingSystem/ResilientTest.php deleted file mode 100644 index f2097ec..0000000 --- a/tests/OperatingSystem/ResilientTest.php +++ /dev/null @@ -1,50 +0,0 @@ -assertInstanceOf(Clock::class, $os->clock()); - $this->assertInstanceOf(Filesystem::class, $os->filesystem()); - $this->assertInstanceOf(ServerStatus::class, $os->status()); - $this->assertInstanceOf(ServerControl::class, $os->control()); - $this->assertInstanceOf(Ports::class, $os->ports()); - $this->assertInstanceOf(Sockets::class, $os->sockets()); - $this->assertInstanceOf(Remote\Resilient::class, $os->remote()); - $this->assertInstanceOf(CurrentProcess::class, $os->process()); - } - - public function testMap() - { - $underlying = Unix::of(); - $os = Resilient::of($underlying); - - $result = $os->map(function($os) use ($underlying) { - $this->assertSame($underlying, $os); - - return Unix::of(); - }); - - $this->assertInstanceOf(Resilient::class, $result); - $this->assertNotSame($os, $result); - } -} diff --git a/tests/Remote/ResilientTest.php b/tests/Remote/ResilientTest.php index 02905aa..63d3799 100644 --- a/tests/Remote/ResilientTest.php +++ b/tests/Remote/ResilientTest.php @@ -5,30 +5,14 @@ use Innmind\OperatingSystem\{ OperatingSystem, - Remote\Resilient, - Remote, Config, Factory, }; use Innmind\HttpTransport\ExponentialBackoff; -use Innmind\Server\Control\Server; -use Innmind\IO\Sockets\Internet\Transport; -use Innmind\Immutable\Attempt; -use Formal\AccessLayer\Connection; -use Innmind\BlackBox\{ - PHPUnit\BlackBox, - PHPUnit\Framework\TestCase, - Set, -}; -use Fixtures\Innmind\Url\{ - Url, - Authority, -}; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ResilientTest extends TestCase { - use BlackBox; - private $os; public function setUp(): void @@ -36,60 +20,6 @@ public function setUp(): void $this->os = Factory::build(); } - public function testInterface() - { - $this->assertInstanceOf( - Remote::class, - Resilient::of( - $this->os->remote(), - $this->os->process(), - ), - ); - } - - public function testSsh(): BlackBox\Proof - { - return $this - ->forAll(Url::any()) - ->prove(function($url) { - $remote = Resilient::of( - $this->os->remote(), - $this->os->process(), - ); - - $this->assertInstanceOf( - Server::class, - $remote->ssh($url), - ); - }); - } - - public function testSocket(): BlackBox\Proof - { - return $this - ->forAll( - Set::of( - Transport::tcp(), - Transport::ssl(), - Transport::tls(), - Transport::tlsv10(), - Transport::tlsv12(), - ), - Authority::any(), - ) - ->prove(function($transport, $authority) { - $remote = Resilient::of( - $this->os->remote(), - $this->os->process(), - ); - - $this->assertInstanceOf( - Attempt::class, - $remote->socket($transport, $authority), - ); - }); - } - public function testHttp() { $os = $this->os->map( @@ -100,18 +30,4 @@ public function testHttp() $this->assertInstanceOf(ExponentialBackoff::class, $os->remote()->http()); } - - public function testSql(): BlackBox\Proof - { - return $this - ->forAll(Url::any()) - ->prove(function($server) { - $remote = Resilient::of( - $this->os->remote(), - $this->os->process(), - ); - - $this->assertInstanceOf(Connection::class, $remote->sql($server)); - }); - } } From 4f4908b80e9cb041b34b89aea39eabf6950eaaad Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:30:59 +0200 Subject: [PATCH 08/41] allow to change the default http transport --- CHANGELOG.md | 5 ++ src/Config.php | 131 +++++++---------------------------------- src/Remote/Generic.php | 29 +-------- 3 files changed, 27 insertions(+), 138 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e29ba08..378b393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - `Innmind\OperatingSystem\Config::map()` - `Innmind\OperatingSystem\Config\Logger` - `Innmind\OperatingSystem\Config\Resilient` +- `Innmind\OperatingSystem\Config::useHttpTransport()` +- `Innmind\OperatingSystem\Config::mapHttpTransport()` ### Changed @@ -40,6 +42,9 @@ - `Innmind\OperatingSystem\Config::useStreamCapabilities()` - `Innmind\OperatingSystem\Sockets::watch()` - `Innmind\OperatingSystem\OperatingSystem\Resilient` +- `Innmind\OperatingSystem\Config::limitHttpConcurrencyTo()` use `::useHttpTransport()` instead +- `Innmind\OperatingSystem\Config::withHttpHeartbeat()` use `::useHttpTransport()` instead +- `Innmind\OperatingSystem\Config::disableSSLVerification()` use `::useHttpTransport()` instead ## 5.2.0 - 2024-07-14 diff --git a/src/Config.php b/src/Config.php index 2878ab2..b89db97 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,22 +3,19 @@ namespace Innmind\OperatingSystem; -use Innmind\TimeContinuum\{ - Clock, - Period, +use Innmind\TimeContinuum\Clock; +use Innmind\HttpTransport\{ + Transport as HttpTransport, + Curl, }; -use Innmind\HttpTransport\Transport as HttpTransport; use Innmind\Filesystem\CaseSensitivity; use Innmind\Server\Status\EnvironmentPath; use Innmind\TimeWarp\Halt; use Innmind\IO\IO; -use Innmind\Immutable\Maybe; final class Config { /** - * @param Maybe $maxHttpConcurrency - * @param Maybe $httpHeartbeat * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport */ private function __construct( @@ -27,20 +24,13 @@ private function __construct( private IO $io, private Halt $halt, private EnvironmentPath $path, - private Maybe $maxHttpConcurrency, - private Maybe $httpHeartbeat, + private ?HttpTransport $httpTransport, private \Closure $mapHttpTransport, - private bool $disableSSLVerification, ) { } public static function of(): self { - /** @var Maybe */ - $maxHttpConcurrency = Maybe::nothing(); - /** @var Maybe */ - $httpHeartbeat = Maybe::nothing(); - return new self( Clock::live(), CaseSensitivity::sensitive, @@ -50,10 +40,8 @@ public static function of(): self false => '', default => $path, }), - $maxHttpConcurrency, - $httpHeartbeat, + null, static fn(HttpTransport $transport) => $transport, - false, ); } @@ -79,10 +67,8 @@ public function withClock(Clock $clock): self $this->io, $this->halt, $this->path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, + $this->httpTransport, $this->mapHttpTransport, - $this->disableSSLVerification, ); } @@ -97,10 +83,8 @@ public function caseInsensitiveFilesystem(): self $this->io, $this->halt, $this->path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, + $this->httpTransport, $this->mapHttpTransport, - $this->disableSSLVerification, ); } @@ -115,10 +99,8 @@ public function haltProcessVia(Halt $halt): self $this->io, $halt, $this->path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, + $this->httpTransport, $this->mapHttpTransport, - $this->disableSSLVerification, ); } @@ -136,10 +118,8 @@ public function mapHalt(callable $map): self $this->io, $map($this->halt), $this->path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, + $this->httpTransport, $this->mapHttpTransport, - $this->disableSSLVerification, ); } @@ -154,19 +134,15 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->io, $this->halt, $path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, + $this->httpTransport, $this->mapHttpTransport, - $this->disableSSLVerification, ); } /** * @psalm-mutation-free - * - * @param positive-int $max */ - public function limitHttpConcurrencyTo(int $max): self + public function useHttpTransport(HttpTransport $transport): self { return new self( $this->clock, @@ -174,30 +150,8 @@ public function limitHttpConcurrencyTo(int $max): self $this->io, $this->halt, $this->path, - Maybe::just($max), - $this->httpHeartbeat, + $transport, $this->mapHttpTransport, - $this->disableSSLVerification, - ); - } - - /** - * @psalm-mutation-free - * - * @param callable(): void $heartbeat - */ - public function withHttpHeartbeat(Period $timeout, callable $heartbeat): self - { - return new self( - $this->clock, - $this->caseSensitivity, - $this->io, - $this->halt, - $this->path, - $this->maxHttpConcurrency, - Maybe::just([$timeout, $heartbeat]), - $this->mapHttpTransport, - $this->disableSSLVerification, ); } @@ -214,28 +168,8 @@ public function mapHttpTransport(\Closure $map): self $this->io, $this->halt, $this->path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, + $this->httpTransport, $map, - $this->disableSSLVerification, - ); - } - - /** - * @psalm-mutation-free - */ - public function disableSSLVerification(): self - { - return new self( - $this->clock, - $this->caseSensitivity, - $this->io, - $this->halt, - $this->path, - $this->maxHttpConcurrency, - $this->httpHeartbeat, - $this->mapHttpTransport, - true, ); } @@ -281,39 +215,14 @@ public function environmentPath(): EnvironmentPath /** * @internal - * - * @return Maybe - */ - public function maxHttpConcurrency(): Maybe - { - return $this->maxHttpConcurrency; - } - - /** - * @internal - * - * @return Maybe */ - public function httpHeartbeat(): Maybe + public function httpTransport(): HttpTransport { - return $this->httpHeartbeat; - } - - /** - * @internal - * - * @return \Closure(HttpTransport): HttpTransport - */ - public function httpTransportMapper(): \Closure - { - return $this->mapHttpTransport; - } + $transport = $this->httpTransport ?? Curl::of( + $this->clock, + $this->io, + ); - /** - * @internal - */ - public function mustDisableSSLVerification(): bool - { - return $this->disableSSLVerification; + return ($this->mapHttpTransport)($transport); } } diff --git a/src/Remote/Generic.php b/src/Remote/Generic.php index ec2a719..7c31886 100644 --- a/src/Remote/Generic.php +++ b/src/Remote/Generic.php @@ -17,10 +17,7 @@ Authority, Authority\Port, }; -use Innmind\HttpTransport\{ - Transport as HttpTransport, - Curl, -}; +use Innmind\HttpTransport\Transport as HttpTransport; use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; @@ -75,29 +72,7 @@ public function socket(Transport $transport, Authority $authority): Attempt #[\Override] public function http(): HttpTransport { - if ($this->http) { - return $this->http; - } - - $http = Curl::of( - $this->config->clock(), - $this->config->io(), - ); - $http = $this->config->maxHttpConcurrency()->match( - static fn($max) => $http->maxConcurrency($max), - static fn() => $http, - ); - $http = $this->config->httpHeartbeat()->match( - static fn($config) => $http->heartbeat($config[0], $config[1]), - static fn() => $http, - ); - $http = match ($this->config->mustDisableSSLVerification()) { - true => $http->disableSSLVerification(), - false => $http, - }; - $map = $this->config->httpTransportMapper(); - - return $this->http = $map($http); + return $this->http ??= $this->config->httpTransport(); } #[\Override] From 075fa5b771fad061e7ae30167d8e31349375e0ef Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:33:20 +0200 Subject: [PATCH 09/41] map the halt object only when retrieved --- src/Config.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Config.php b/src/Config.php index b89db97..d68226e 100644 --- a/src/Config.php +++ b/src/Config.php @@ -16,6 +16,7 @@ final class Config { /** + * @param \Closure(Halt): Halt $mapHalt * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport */ private function __construct( @@ -23,6 +24,7 @@ private function __construct( private CaseSensitivity $caseSensitivity, private IO $io, private Halt $halt, + private \Closure $mapHalt, private EnvironmentPath $path, private ?HttpTransport $httpTransport, private \Closure $mapHttpTransport, @@ -36,6 +38,7 @@ public static function of(): self CaseSensitivity::sensitive, IO::fromAmbientAuthority(), Halt\Usleep::new(), + static fn(Halt $halt) => $halt, EnvironmentPath::of(match ($path = \getenv('PATH')) { false => '', default => $path, @@ -66,6 +69,7 @@ public function withClock(Clock $clock): self $this->caseSensitivity, $this->io, $this->halt, + $this->mapHalt, $this->path, $this->httpTransport, $this->mapHttpTransport, @@ -82,6 +86,7 @@ public function caseInsensitiveFilesystem(): self CaseSensitivity::insensitive, $this->io, $this->halt, + $this->mapHalt, $this->path, $this->httpTransport, $this->mapHttpTransport, @@ -98,6 +103,7 @@ public function haltProcessVia(Halt $halt): self $this->caseSensitivity, $this->io, $halt, + $this->mapHalt, $this->path, $this->httpTransport, $this->mapHttpTransport, @@ -107,16 +113,17 @@ public function haltProcessVia(Halt $halt): self /** * @psalm-mutation-free * - * @param callable(Halt): Halt $map + * @param \Closure(Halt): Halt $map */ - public function mapHalt(callable $map): self + public function mapHalt(\Closure $map): self { /** @psalm-suppress ImpureFunctionCall */ return new self( $this->clock, $this->caseSensitivity, $this->io, - $map($this->halt), + $this->halt, + $map, $this->path, $this->httpTransport, $this->mapHttpTransport, @@ -133,6 +140,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->caseSensitivity, $this->io, $this->halt, + $this->mapHalt, $path, $this->httpTransport, $this->mapHttpTransport, @@ -149,6 +157,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->caseSensitivity, $this->io, $this->halt, + $this->mapHalt, $this->path, $transport, $this->mapHttpTransport, @@ -167,6 +176,7 @@ public function mapHttpTransport(\Closure $map): self $this->caseSensitivity, $this->io, $this->halt, + $this->mapHalt, $this->path, $this->httpTransport, $map, @@ -202,7 +212,7 @@ public function io(): IO */ public function halt(): Halt { - return $this->halt; + return ($this->mapHalt)($this->halt); } /** From ebe2d050e5558ee6a61b3121d06f1903a4817506 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:36:01 +0200 Subject: [PATCH 10/41] compose config mappers --- src/Config.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Config.php b/src/Config.php index d68226e..9e75607 100644 --- a/src/Config.php +++ b/src/Config.php @@ -117,13 +117,15 @@ public function haltProcessVia(Halt $halt): self */ public function mapHalt(\Closure $map): self { + $previous = $this->mapHalt; + /** @psalm-suppress ImpureFunctionCall */ return new self( $this->clock, $this->caseSensitivity, $this->io, $this->halt, - $map, + static fn(Halt $halt) => $map($previous($halt)), $this->path, $this->httpTransport, $this->mapHttpTransport, @@ -171,6 +173,8 @@ public function useHttpTransport(HttpTransport $transport): self */ public function mapHttpTransport(\Closure $map): self { + $previous = $this->mapHttpTransport; + return new self( $this->clock, $this->caseSensitivity, @@ -179,7 +183,7 @@ public function mapHttpTransport(\Closure $map): self $this->mapHalt, $this->path, $this->httpTransport, - $map, + static fn(HttpTransport $transport) => $map($previous($transport)), ); } From 270f66b5a19938513c708940b59a1fd5b76c5de4 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:43:09 +0200 Subject: [PATCH 11/41] apply http transport logger through the config --- src/Config/Logger.php | 5 +++++ src/Remote/Logger.php | 5 +---- tests/Remote/GenericTest.php | 27 ++++++++++++++++++--------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Config/Logger.php b/src/Config/Logger.php index 42223d3..38274b9 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -4,6 +4,7 @@ namespace Innmind\OperatingSystem\Config; use Innmind\OperatingSystem\Config; +use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; use Psr\Log\LoggerInterface; @@ -20,6 +21,10 @@ public function __invoke(Config $config): Config ->mapHalt(fn($halt) => Halt\Logger::psr( $halt, $this->logger, + )) + ->mapHttpTransport(fn($transport) => HttpTransport\Logger::psr( + $transport, + $this->logger, )); } diff --git a/src/Remote/Logger.php b/src/Remote/Logger.php index 9189ce2..6fa8d73 100644 --- a/src/Remote/Logger.php +++ b/src/Remote/Logger.php @@ -60,10 +60,7 @@ public function socket(Transport $transport, Authority $authority): Attempt #[\Override] public function http(): HttpTransport\Transport { - return HttpTransport\Logger::psr( - $this->remote->http(), - $this->logger, - ); + return $this->remote->http(); } #[\Override] diff --git a/tests/Remote/GenericTest.php b/tests/Remote/GenericTest.php index 60429c4..43ed2f7 100644 --- a/tests/Remote/GenericTest.php +++ b/tests/Remote/GenericTest.php @@ -5,6 +5,8 @@ use Innmind\OperatingSystem\{ Remote\Generic, + OperatingSystem\Logger, + OperatingSystem\Unix, Remote, Config, Factory, @@ -33,8 +35,10 @@ use Innmind\BlackBox\{ PHPUnit\BlackBox, PHPUnit\Framework\TestCase, + Set, }; use Fixtures\Innmind\Url\Url as FUrl; +use Psr\Log\NullLogger; class GenericTest extends TestCase { @@ -104,17 +108,22 @@ public function testSocket() $socket->close(); } - public function testHttp() + public function testHttp(): BlackBox\Proof { - $remote = Generic::of( - $this->server(), - Config::of(), - ); + $os = Unix::of(); - $http = $remote->http(); - - $this->assertInstanceOf(HttpTransport::class, $http); - $this->assertSame($http, $remote->http()); + return $this + ->forAll(Set::of( + $os, + Logger::psr($os, new NullLogger), + )) + ->prove(function($os) { + $remote = $os->remote(); + $http = $remote->http(); + + $this->assertInstanceOf(HttpTransport::class, $http); + $this->assertSame($http, $remote->http()); + }); } public function testSql(): BlackBox\Proof From 19fd7ee82c3702ee4526c42211beb56ab4188fae Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:50:03 +0200 Subject: [PATCH 12/41] remove logs that could be expressed via underlying abstractions --- CHANGELOG.md | 9 ++++ src/CurrentProcess/Logger.php | 69 ------------------------- src/CurrentProcess/Signals/Logger.php | 72 --------------------------- src/Filesystem/Logger.php | 14 +----- src/OperatingSystem/Logger.php | 15 ++---- src/Ports/Logger.php | 46 ----------------- src/Remote/Logger.php | 11 ---- src/Sockets/Logger.php | 59 ---------------------- 8 files changed, 14 insertions(+), 281 deletions(-) delete mode 100644 src/CurrentProcess/Logger.php delete mode 100644 src/CurrentProcess/Signals/Logger.php delete mode 100644 src/Ports/Logger.php delete mode 100644 src/Sockets/Logger.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 378b393..3cf354a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,15 @@ - `Innmind\OperatingSystem\Config::limitHttpConcurrencyTo()` use `::useHttpTransport()` instead - `Innmind\OperatingSystem\Config::withHttpHeartbeat()` use `::useHttpTransport()` instead - `Innmind\OperatingSystem\Config::disableSSLVerification()` use `::useHttpTransport()` instead +- The following informations are no longer logged: + - the current process id + - the current process memory + - the signals listener being added/removed + - the signals received by the current process + - temporary files being created + - opened ports + - opened remote sockets + - opened sockets ## 5.2.0 - 2024-07-14 diff --git a/src/CurrentProcess/Logger.php b/src/CurrentProcess/Logger.php deleted file mode 100644 index bbb20b8..0000000 --- a/src/CurrentProcess/Logger.php +++ /dev/null @@ -1,69 +0,0 @@ -process = $process; - $this->logger = $logger; - } - - public static function psr(CurrentProcess $process, LoggerInterface $logger): self - { - return new self($process, $logger); - } - - #[\Override] - public function id(): Attempt - { - return $this->process->id()->map(function($pid) { - $this->logger->debug( - 'Current process id is {pid}', - ['pid' => $pid->toInt()], - ); - - return $pid; - }); - } - - #[\Override] - public function signals(): Signals - { - return $this->signals ??= Signals\Logger::psr( - $this->process->signals(), - $this->logger, - ); - } - - #[\Override] - public function halt(Period $period): Attempt - { - return $this->process->halt($period); - } - - #[\Override] - public function memory(): Bytes - { - $memory = $this->process->memory(); - - $this->logger->debug( - 'Current process memory at {memory}', - ['memory' => $memory->toString()], - ); - - return $memory; - } -} diff --git a/src/CurrentProcess/Signals/Logger.php b/src/CurrentProcess/Signals/Logger.php deleted file mode 100644 index 375ced1..0000000 --- a/src/CurrentProcess/Signals/Logger.php +++ /dev/null @@ -1,72 +0,0 @@ - */ - private Map $decorated; - - private function __construct(Signals $signals, LoggerInterface $logger) - { - $this->signals = $signals; - $this->logger = $logger; - /** @var Map */ - $this->decorated = Map::of(); - } - - public static function psr(Signals $signals, LoggerInterface $logger): self - { - return new self($signals, $logger); - } - - #[\Override] - public function listen(Signal $signal, callable $listener): void - { - $this->logger->debug( - 'Registering a listener for signal {signal}', - ['signal' => $signal->toInt()], - ); - - $decorated = function(Signal $signal, Info $info) use ($listener): void { - $this->logger->debug( - 'Handling signal {signal}', - ['signal' => $signal->toInt()], - ); - - $listener($signal, $info); - }; - $this->decorated = ($this->decorated)($listener, $decorated); - - $this->signals->listen($signal, $decorated); - } - - #[\Override] - public function remove(callable $listener): void - { - $this->logger->debug('Removing a signal listener'); - - // by default we alias the user listener as the decorated in case he - // found a way to install his listener from another way than from self::listen() - $decorated = $this - ->decorated - ->get($listener) - ->match( - static fn($decorated) => $decorated, - static fn() => $listener, - ); - $this->signals->remove($decorated); - $this->decorated = $this->decorated->remove($decorated); - } -} diff --git a/src/Filesystem/Logger.php b/src/Filesystem/Logger.php index 6152594..2d89429 100644 --- a/src/Filesystem/Logger.php +++ b/src/Filesystem/Logger.php @@ -4,10 +4,7 @@ namespace Innmind\OperatingSystem\Filesystem; use Innmind\OperatingSystem\Filesystem; -use Innmind\Filesystem\{ - Adapter, - File\Content, -}; +use Innmind\Filesystem\Adapter; use Innmind\Url\Path; use Innmind\FileWatch\Ping; use Innmind\Immutable\{ @@ -87,13 +84,6 @@ public function watch(Path $path): Ping #[\Override] public function temporary(Sequence $chunks): Attempt { - return $this - ->filesystem - ->temporary($chunks) - ->map(function(Content $content) { - $this->logger->debug('Temporary file created'); - - return $content; - }); + return $this->filesystem->temporary($chunks); } } diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php index 73d9319..2831a86 100644 --- a/src/OperatingSystem/Logger.php +++ b/src/OperatingSystem/Logger.php @@ -85,19 +85,13 @@ public function control(): Control\Server #[\Override] public function ports(): Ports { - return Ports\Logger::psr( - $this->os->ports(), - $this->logger, - ); + return $this->os->ports(); } #[\Override] public function sockets(): Sockets { - return Sockets\Logger::psr( - $this->os->sockets(), - $this->logger, - ); + return $this->os->sockets(); } #[\Override] @@ -112,9 +106,6 @@ public function remote(): Remote #[\Override] public function process(): CurrentProcess { - return CurrentProcess\Logger::psr( - $this->os->process(), - $this->logger, - ); + return $this->os->process(); } } diff --git a/src/Ports/Logger.php b/src/Ports/Logger.php deleted file mode 100644 index 6891152..0000000 --- a/src/Ports/Logger.php +++ /dev/null @@ -1,46 +0,0 @@ -ports = $ports; - $this->logger = $logger; - } - - public static function psr(Ports $ports, LoggerInterface $logger): self - { - return new self($ports, $logger); - } - - #[\Override] - public function open(Transport $transport, IP $ip, Port $port): Attempt - { - $this->logger->debug( - 'Opening new port at {address}', - [ - 'address' => \sprintf( - '%s://%s:%s', - $transport->toString(), - $ip->toString(), - $port->toString(), - ), - ], - ); - - return $this->ports->open($transport, $ip, $port); - } -} diff --git a/src/Remote/Logger.php b/src/Remote/Logger.php index 6fa8d73..8414df1 100644 --- a/src/Remote/Logger.php +++ b/src/Remote/Logger.php @@ -43,17 +43,6 @@ public function ssh(Url $server): Control\Server #[\Override] public function socket(Transport $transport, Authority $authority): Attempt { - $this->logger->debug( - 'Opening remote socket at {address}', - [ - 'address' => \sprintf( - '%s://%s', - $transport->toString(), - $authority->toString(), - ), - ], - ); - return $this->remote->socket($transport, $authority); } diff --git a/src/Sockets/Logger.php b/src/Sockets/Logger.php deleted file mode 100644 index 9ba4cf0..0000000 --- a/src/Sockets/Logger.php +++ /dev/null @@ -1,59 +0,0 @@ -sockets = $sockets; - $this->logger = $logger; - } - - public static function psr(Sockets $sockets, LoggerInterface $logger): self - { - return new self($sockets, $logger); - } - - #[\Override] - public function open(Address $address): Attempt - { - $this->logger->debug( - 'Opening socket at {address}', - ['address' => $address->toString()], - ); - - return $this->sockets->open($address); - } - - #[\Override] - public function takeOver(Address $address): Attempt - { - $this->logger->debug( - 'Taking over the socket at {address}', - ['address' => $address->toString()], - ); - - return $this->sockets->takeOver($address); - } - - #[\Override] - public function connectTo(Address $address): Attempt - { - $this->logger->debug( - 'Connecting to socket at {address}', - ['address' => $address->toString()], - ); - - return $this->sockets->connectTo($address); - } -} From fe0f8575b866428da3f59668376718ce3e0401b8 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 19:57:01 +0200 Subject: [PATCH 13/41] allow to change the way to open sql connections --- CHANGELOG.md | 1 + src/Config.php | 42 ++++++++++++++++++++++++++++++++++++++++++ src/Remote/Generic.php | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cf354a..90bbf90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `Innmind\OperatingSystem\Config\Resilient` - `Innmind\OperatingSystem\Config::useHttpTransport()` - `Innmind\OperatingSystem\Config::mapHttpTransport()` +- `Innmind\OperatingSystem\Config::openSQLConnectionVia()` ### Changed diff --git a/src/Config.php b/src/Config.php index 9e75607..433a71a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -12,12 +12,15 @@ use Innmind\Server\Status\EnvironmentPath; use Innmind\TimeWarp\Halt; use Innmind\IO\IO; +use Innmind\Url\Url; +use Formal\AccessLayer; final class Config { /** * @param \Closure(Halt): Halt $mapHalt * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport + * @param \Closure(Url): AccessLayer\Connection $sql */ private function __construct( private Clock $clock, @@ -28,6 +31,7 @@ private function __construct( private EnvironmentPath $path, private ?HttpTransport $httpTransport, private \Closure $mapHttpTransport, + private \Closure $sql, ) { } @@ -45,6 +49,9 @@ public static function of(): self }), null, static fn(HttpTransport $transport) => $transport, + static fn(Url $server) => AccessLayer\Connection\Lazy::of( + static fn() => AccessLayer\Connection\PDO::of($server), + ), ); } @@ -73,6 +80,7 @@ public function withClock(Clock $clock): self $this->path, $this->httpTransport, $this->mapHttpTransport, + $this->sql, ); } @@ -90,6 +98,7 @@ public function caseInsensitiveFilesystem(): self $this->path, $this->httpTransport, $this->mapHttpTransport, + $this->sql, ); } @@ -107,6 +116,7 @@ public function haltProcessVia(Halt $halt): self $this->path, $this->httpTransport, $this->mapHttpTransport, + $this->sql, ); } @@ -129,6 +139,7 @@ public function mapHalt(\Closure $map): self $this->path, $this->httpTransport, $this->mapHttpTransport, + $this->sql, ); } @@ -146,6 +157,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $path, $this->httpTransport, $this->mapHttpTransport, + $this->sql, ); } @@ -163,6 +175,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->path, $transport, $this->mapHttpTransport, + $this->sql, ); } @@ -184,6 +197,27 @@ public function mapHttpTransport(\Closure $map): self $this->path, $this->httpTransport, static fn(HttpTransport $transport) => $map($previous($transport)), + $this->sql, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Url): AccessLayer\Connection $sql + */ + public function openSQLConnectionVia(\Closure $sql): self + { + return new self( + $this->clock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $sql, ); } @@ -239,4 +273,12 @@ public function httpTransport(): HttpTransport return ($this->mapHttpTransport)($transport); } + + /** + * @internal + */ + public function sql(Url $url): AccessLayer\Connection + { + return ($this->sql)($url); + } } diff --git a/src/Remote/Generic.php b/src/Remote/Generic.php index 7c31886..bf22e0b 100644 --- a/src/Remote/Generic.php +++ b/src/Remote/Generic.php @@ -78,6 +78,6 @@ public function http(): HttpTransport #[\Override] public function sql(Url $server): Connection { - return Connection\Lazy::of(static fn() => Connection\PDO::of($server)); + return $this->config->sql($server); } } From 08aab59d91d0607219bd3db1f03a5f6f6f1279be Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 20:03:26 +0200 Subject: [PATCH 14/41] add Config::mapSQLConnection() --- CHANGELOG.md | 1 + src/Config.php | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90bbf90..14bcf42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `Innmind\OperatingSystem\Config::useHttpTransport()` - `Innmind\OperatingSystem\Config::mapHttpTransport()` - `Innmind\OperatingSystem\Config::openSQLConnectionVia()` +- `Innmind\OperatingSystem\Config::mapSQLConnection()` ### Changed diff --git a/src/Config.php b/src/Config.php index 433a71a..42036a5 100644 --- a/src/Config.php +++ b/src/Config.php @@ -21,6 +21,7 @@ final class Config * @param \Closure(Halt): Halt $mapHalt * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport * @param \Closure(Url): AccessLayer\Connection $sql + * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $mapSql */ private function __construct( private Clock $clock, @@ -32,6 +33,7 @@ private function __construct( private ?HttpTransport $httpTransport, private \Closure $mapHttpTransport, private \Closure $sql, + private \Closure $mapSql, ) { } @@ -52,6 +54,7 @@ public static function of(): self static fn(Url $server) => AccessLayer\Connection\Lazy::of( static fn() => AccessLayer\Connection\PDO::of($server), ), + static fn(AccessLayer\Connection $connection) => $connection, ); } @@ -81,6 +84,7 @@ public function withClock(Clock $clock): self $this->httpTransport, $this->mapHttpTransport, $this->sql, + $this->mapSql, ); } @@ -99,6 +103,7 @@ public function caseInsensitiveFilesystem(): self $this->httpTransport, $this->mapHttpTransport, $this->sql, + $this->mapSql, ); } @@ -117,6 +122,7 @@ public function haltProcessVia(Halt $halt): self $this->httpTransport, $this->mapHttpTransport, $this->sql, + $this->mapSql, ); } @@ -140,6 +146,7 @@ public function mapHalt(\Closure $map): self $this->httpTransport, $this->mapHttpTransport, $this->sql, + $this->mapSql, ); } @@ -158,6 +165,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->httpTransport, $this->mapHttpTransport, $this->sql, + $this->mapSql, ); } @@ -176,6 +184,7 @@ public function useHttpTransport(HttpTransport $transport): self $transport, $this->mapHttpTransport, $this->sql, + $this->mapSql, ); } @@ -198,6 +207,7 @@ public function mapHttpTransport(\Closure $map): self $this->httpTransport, static fn(HttpTransport $transport) => $map($previous($transport)), $this->sql, + $this->mapSql, ); } @@ -218,6 +228,30 @@ public function openSQLConnectionVia(\Closure $sql): self $this->httpTransport, $this->mapHttpTransport, $sql, + $this->mapSql, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $map + */ + public function mapSQLConnection(\Closure $map): self + { + $previous = $this->mapSql; + + return new self( + $this->clock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + static fn(AccessLayer\Connection $connection) => $map($previous($connection)), ); } @@ -279,6 +313,8 @@ public function httpTransport(): HttpTransport */ public function sql(Url $url): AccessLayer\Connection { - return ($this->sql)($url); + return ($this->mapSql)( + ($this->sql)($url), + ); } } From 14650d2fc4a3e6e4dc14d4d6122502c77fb0fedb Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 20:03:39 +0200 Subject: [PATCH 15/41] apply sql logger via the config --- src/Config/Logger.php | 5 +++++ src/Remote/Logger.php | 5 +---- tests/Remote/GenericTest.php | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Config/Logger.php b/src/Config/Logger.php index 38274b9..426ffc9 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -6,6 +6,7 @@ use Innmind\OperatingSystem\Config; use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; +use Formal\AccessLayer\Connection; use Psr\Log\LoggerInterface; final class Logger @@ -25,6 +26,10 @@ public function __invoke(Config $config): Config ->mapHttpTransport(fn($transport) => HttpTransport\Logger::psr( $transport, $this->logger, + )) + ->mapSQLConnection(fn($connection) => Connection\Logger::psr( + $connection, + $this->logger, )); } diff --git a/src/Remote/Logger.php b/src/Remote/Logger.php index 8414df1..77884bd 100644 --- a/src/Remote/Logger.php +++ b/src/Remote/Logger.php @@ -55,9 +55,6 @@ public function http(): HttpTransport\Transport #[\Override] public function sql(Url $server): Connection { - return Connection\Logger::psr( - $this->remote->sql($server), - $this->logger, - ); + return $this->remote->sql($server); } } diff --git a/tests/Remote/GenericTest.php b/tests/Remote/GenericTest.php index 43ed2f7..5248f23 100644 --- a/tests/Remote/GenericTest.php +++ b/tests/Remote/GenericTest.php @@ -128,15 +128,18 @@ public function testHttp(): BlackBox\Proof public function testSql(): BlackBox\Proof { + $os = Unix::of(); + return $this - ->forAll(FUrl::any()) - ->prove(function($server) { - $remote = Generic::of( - $this->server(), - Config::of(), - ); - - $sql = $remote->sql($server); + ->forAll( + FUrl::any(), + Set::of( + $os, + Logger::psr($os, new NullLogger), + ), + ) + ->prove(function($server, $os) { + $sql = $os->remote()->sql($server); $this->assertInstanceOf(Connection::class, $sql); }); From 02eda6cb06b9187d7ad1ca950d7256c16d9279e9 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 20:09:14 +0200 Subject: [PATCH 16/41] add Config::mapServerControl() --- CHANGELOG.md | 1 + src/Config.php | 47 ++++++++++++++++++++++++++++++++++++ src/OperatingSystem/Unix.php | 4 +-- src/Remote/Generic.php | 4 +-- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14bcf42..373b340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `Innmind\OperatingSystem\Config::mapHttpTransport()` - `Innmind\OperatingSystem\Config::openSQLConnectionVia()` - `Innmind\OperatingSystem\Config::mapSQLConnection()` +- `Innmind\OperatingSystem\Config::mapServerControl()` ### Changed diff --git a/src/Config.php b/src/Config.php index 42036a5..dcda826 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,6 +3,7 @@ namespace Innmind\OperatingSystem; +use Innmind\Server\Control; use Innmind\TimeContinuum\Clock; use Innmind\HttpTransport\{ Transport as HttpTransport, @@ -22,6 +23,7 @@ final class Config * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport * @param \Closure(Url): AccessLayer\Connection $sql * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $mapSql + * @param \Closure(Control\Server): Control\Server $mapServerControl */ private function __construct( private Clock $clock, @@ -34,6 +36,7 @@ private function __construct( private \Closure $mapHttpTransport, private \Closure $sql, private \Closure $mapSql, + private \Closure $mapServerControl, ) { } @@ -55,6 +58,7 @@ public static function of(): self static fn() => AccessLayer\Connection\PDO::of($server), ), static fn(AccessLayer\Connection $connection) => $connection, + static fn(Control\Server $server) => $server, ); } @@ -85,6 +89,7 @@ public function withClock(Clock $clock): self $this->mapHttpTransport, $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -104,6 +109,7 @@ public function caseInsensitiveFilesystem(): self $this->mapHttpTransport, $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -123,6 +129,7 @@ public function haltProcessVia(Halt $halt): self $this->mapHttpTransport, $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -147,6 +154,7 @@ public function mapHalt(\Closure $map): self $this->mapHttpTransport, $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -166,6 +174,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->mapHttpTransport, $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -185,6 +194,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->mapHttpTransport, $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -208,6 +218,7 @@ public function mapHttpTransport(\Closure $map): self static fn(HttpTransport $transport) => $map($previous($transport)), $this->sql, $this->mapSql, + $this->mapServerControl, ); } @@ -229,6 +240,7 @@ public function openSQLConnectionVia(\Closure $sql): self $this->mapHttpTransport, $sql, $this->mapSql, + $this->mapServerControl, ); } @@ -252,6 +264,31 @@ public function mapSQLConnection(\Closure $map): self $this->mapHttpTransport, $this->sql, static fn(AccessLayer\Connection $connection) => $map($previous($connection)), + $this->mapServerControl, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Control\Server): Control\Server $map + */ + public function mapServerControl(\Closure $map): self + { + $previous = $this->mapServerControl; + + return new self( + $this->clock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + $this->mapSql, + static fn(Control\Server $server) => $map($previous($server)), ); } @@ -317,4 +354,14 @@ public function sql(Url $url): AccessLayer\Connection ($this->sql)($url), ); } + + /** + * @internal + * + * @return \Closure(Control\Server): Control\Server + */ + public function serverControlMapper(): \Closure + { + return $this->mapServerControl; + } } diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index 5504faf..4eee63c 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -76,11 +76,11 @@ public function status(): ServerStatus #[\Override] public function control(): ServerControl { - return $this->control ??= Servers\Unix::of( + return $this->control ??= $this->config->serverControlMapper()(Servers\Unix::of( $this->clock(), $this->config->io(), $this->config->halt(), - ); + )); } #[\Override] diff --git a/src/Remote/Generic.php b/src/Remote/Generic.php index bf22e0b..10d71b8 100644 --- a/src/Remote/Generic.php +++ b/src/Remote/Generic.php @@ -50,12 +50,12 @@ public function ssh(Url $server): Server $port = $server->authority()->port(); } - return Servers\Remote::of( + return $this->config->serverControlMapper()(Servers\Remote::of( $this->server, $server->authority()->userInformation()->user(), $server->authority()->host(), $port, - ); + )); } #[\Override] From f8eb06236fdbf96677c0424ef0356fd8882652b2 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 23 May 2025 20:16:27 +0200 Subject: [PATCH 17/41] apply server control logger via the config --- src/Config/Logger.php | 5 +++ src/OperatingSystem/Logger.php | 10 ++---- src/Remote/Logger.php | 60 ---------------------------------- tests/Remote/GenericTest.php | 16 +++++++++ 4 files changed, 23 insertions(+), 68 deletions(-) delete mode 100644 src/Remote/Logger.php diff --git a/src/Config/Logger.php b/src/Config/Logger.php index 426ffc9..8b63197 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -4,6 +4,7 @@ namespace Innmind\OperatingSystem\Config; use Innmind\OperatingSystem\Config; +use Innmind\Server\Control; use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; use Formal\AccessLayer\Connection; @@ -30,6 +31,10 @@ public function __invoke(Config $config): Config ->mapSQLConnection(fn($connection) => Connection\Logger::psr( $connection, $this->logger, + )) + ->mapServerControl(fn($server) => Control\Servers\Logger::psr( + $server, + $this->logger, )); } diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php index 2831a86..4e00c99 100644 --- a/src/OperatingSystem/Logger.php +++ b/src/OperatingSystem/Logger.php @@ -76,10 +76,7 @@ public function status(): Status\Server #[\Override] public function control(): Control\Server { - return Control\Servers\Logger::psr( - $this->os->control(), - $this->logger, - ); + return $this->os->control(); } #[\Override] @@ -97,10 +94,7 @@ public function sockets(): Sockets #[\Override] public function remote(): Remote { - return Remote\Logger::psr( - $this->os->remote(), - $this->logger, - ); + return $this->os->remote(); } #[\Override] diff --git a/src/Remote/Logger.php b/src/Remote/Logger.php deleted file mode 100644 index 77884bd..0000000 --- a/src/Remote/Logger.php +++ /dev/null @@ -1,60 +0,0 @@ -remote = $remote; - $this->logger = $logger; - } - - public static function psr(Remote $remote, LoggerInterface $logger): self - { - return new self($remote, $logger); - } - - #[\Override] - public function ssh(Url $server): Control\Server - { - return Control\Servers\Logger::psr( - $this->remote->ssh($server), - $this->logger, - ); - } - - #[\Override] - public function socket(Transport $transport, Authority $authority): Attempt - { - return $this->remote->socket($transport, $authority); - } - - #[\Override] - public function http(): HttpTransport\Transport - { - return $this->remote->http(); - } - - #[\Override] - public function sql(Url $server): Connection - { - return $this->remote->sql($server); - } -} diff --git a/tests/Remote/GenericTest.php b/tests/Remote/GenericTest.php index 5248f23..6a548d1 100644 --- a/tests/Remote/GenericTest.php +++ b/tests/Remote/GenericTest.php @@ -71,6 +71,22 @@ public function testSsh() ->unwrap(); } + public function testSshLogger() + { + $remote = Generic::of( + $this->server("ssh '-p' '42' 'user@my-vps' 'ls'"), + Config::of()->map(Config\Logger::psr(new NullLogger)), + ); + + $remoteServer = $remote->ssh(Url::of('ssh://user@my-vps:42/')); + + $this->assertInstanceOf(Servers\Logger::class, $remoteServer); + $remoteServer + ->processes() + ->execute(Command::foreground('ls')) + ->unwrap(); + } + public function testSshWithoutPort() { $remote = Generic::of( From 4e7bb96fc37f2cd416a0846f6e5813264d120c5e Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 09:49:43 +0200 Subject: [PATCH 18/41] add Config::mapServerStatus() --- CHANGELOG.md | 1 + src/Config.php | 49 ++++++++++++++++++++++++++++++++++++ src/OperatingSystem/Unix.php | 4 +-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 373b340..0a1775e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - `Innmind\OperatingSystem\Config::openSQLConnectionVia()` - `Innmind\OperatingSystem\Config::mapSQLConnection()` - `Innmind\OperatingSystem\Config::mapServerControl()` +- `Innmind\OperatingSystem\Config::mapServerStatus()` ### Changed diff --git a/src/Config.php b/src/Config.php index dcda826..3edd2bd 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,6 +4,7 @@ namespace Innmind\OperatingSystem; use Innmind\Server\Control; +use Innmind\Server\Status; use Innmind\TimeContinuum\Clock; use Innmind\HttpTransport\{ Transport as HttpTransport, @@ -24,6 +25,7 @@ final class Config * @param \Closure(Url): AccessLayer\Connection $sql * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $mapSql * @param \Closure(Control\Server): Control\Server $mapServerControl + * @param \Closure(Status\Server): Status\Server $mapServerStatus */ private function __construct( private Clock $clock, @@ -37,6 +39,7 @@ private function __construct( private \Closure $sql, private \Closure $mapSql, private \Closure $mapServerControl, + private \Closure $mapServerStatus, ) { } @@ -59,6 +62,7 @@ public static function of(): self ), static fn(AccessLayer\Connection $connection) => $connection, static fn(Control\Server $server) => $server, + static fn(Status\Server $server) => $server, ); } @@ -90,6 +94,7 @@ public function withClock(Clock $clock): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -110,6 +115,7 @@ public function caseInsensitiveFilesystem(): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -130,6 +136,7 @@ public function haltProcessVia(Halt $halt): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -155,6 +162,7 @@ public function mapHalt(\Closure $map): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -175,6 +183,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -195,6 +204,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -219,6 +229,7 @@ public function mapHttpTransport(\Closure $map): self $this->sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -241,6 +252,7 @@ public function openSQLConnectionVia(\Closure $sql): self $sql, $this->mapSql, $this->mapServerControl, + $this->mapServerStatus, ); } @@ -265,6 +277,7 @@ public function mapSQLConnection(\Closure $map): self $this->sql, static fn(AccessLayer\Connection $connection) => $map($previous($connection)), $this->mapServerControl, + $this->mapServerStatus, ); } @@ -289,6 +302,32 @@ public function mapServerControl(\Closure $map): self $this->sql, $this->mapSql, static fn(Control\Server $server) => $map($previous($server)), + $this->mapServerStatus, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Status\Server): Status\Server $map + */ + public function mapServerStatus(\Closure $map): self + { + $previous = $this->mapServerStatus; + + return new self( + $this->clock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + $this->mapSql, + $this->mapServerControl, + static fn(Status\Server $server) => $map($previous($server)), ); } @@ -364,4 +403,14 @@ public function serverControlMapper(): \Closure { return $this->mapServerControl; } + + /** + * @internal + * + * @return \Closure(Status\Server): Status\Server + */ + public function serverStatusMapper(): \Closure + { + return $this->mapServerStatus; + } } diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index 4eee63c..54697b4 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -66,11 +66,11 @@ public function filesystem(): Filesystem #[\Override] public function status(): ServerStatus { - return $this->status ??= ServerFactory::build( + return $this->status ??= $this->config->serverStatusMapper()(ServerFactory::build( $this->clock(), $this->control(), $this->config->environmentPath(), - ); + )); } #[\Override] From 2739b55fd359d779fdcee5d23c54242c4258a14e Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 09:51:58 +0200 Subject: [PATCH 19/41] apply server status logger via the config --- src/Config/Logger.php | 5 +++++ src/OperatingSystem/Logger.php | 5 +---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Config/Logger.php b/src/Config/Logger.php index 8b63197..b99f96c 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -5,6 +5,7 @@ use Innmind\OperatingSystem\Config; use Innmind\Server\Control; +use Innmind\Server\Status; use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; use Formal\AccessLayer\Connection; @@ -35,6 +36,10 @@ public function __invoke(Config $config): Config ->mapServerControl(fn($server) => Control\Servers\Logger::psr( $server, $this->logger, + )) + ->mapServerStatus(fn($server) => Status\Servers\Logger::of( + $server, + $this->logger, )); } diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php index 4e00c99..86ef1de 100644 --- a/src/OperatingSystem/Logger.php +++ b/src/OperatingSystem/Logger.php @@ -67,10 +67,7 @@ public function filesystem(): Filesystem #[\Override] public function status(): Status\Server { - return Status\Servers\Logger::of( - $this->os->status(), - $this->logger, - ); + return $this->os->status(); } #[\Override] From 16875ca0d6eba70c95d8f3ee1fa7d02d6e15554c Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 09:56:41 +0200 Subject: [PATCH 20/41] add Config::mapClock() --- CHANGELOG.md | 1 + src/Config.php | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a1775e..4058f43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `Innmind\OperatingSystem\Config::mapSQLConnection()` - `Innmind\OperatingSystem\Config::mapServerControl()` - `Innmind\OperatingSystem\Config::mapServerStatus()` +- `Innmind\OperatingSystem\Config::mapClock()` ### Changed diff --git a/src/Config.php b/src/Config.php index 3edd2bd..6507e1c 100644 --- a/src/Config.php +++ b/src/Config.php @@ -20,6 +20,7 @@ final class Config { /** + * @param \Closure(Clock): Clock $mapClock * @param \Closure(Halt): Halt $mapHalt * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport * @param \Closure(Url): AccessLayer\Connection $sql @@ -29,6 +30,7 @@ final class Config */ private function __construct( private Clock $clock, + private \Closure $mapClock, private CaseSensitivity $caseSensitivity, private IO $io, private Halt $halt, @@ -47,6 +49,7 @@ public static function of(): self { return new self( Clock::live(), + static fn(Clock $clock) => $clock, CaseSensitivity::sensitive, IO::fromAmbientAuthority(), Halt\Usleep::new(), @@ -84,6 +87,33 @@ public function withClock(Clock $clock): self { return new self( $clock, + $this->mapClock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + $this->mapSql, + $this->mapServerControl, + $this->mapServerStatus, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Clock): Clock $map + */ + public function mapClock(\Closure $map): self + { + $previous = $this->mapClock; + + return new self( + $this->clock, + static fn(Clock $clock) => $map($previous($clock)), $this->caseSensitivity, $this->io, $this->halt, @@ -105,6 +135,7 @@ public function caseInsensitiveFilesystem(): self { return new self( $this->clock, + $this->mapClock, CaseSensitivity::insensitive, $this->io, $this->halt, @@ -126,6 +157,7 @@ public function haltProcessVia(Halt $halt): self { return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $halt, @@ -152,6 +184,7 @@ public function mapHalt(\Closure $map): self /** @psalm-suppress ImpureFunctionCall */ return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -173,6 +206,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self { return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -194,6 +228,7 @@ public function useHttpTransport(HttpTransport $transport): self { return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -219,6 +254,7 @@ public function mapHttpTransport(\Closure $map): self return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -242,6 +278,7 @@ public function openSQLConnectionVia(\Closure $sql): self { return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -267,6 +304,7 @@ public function mapSQLConnection(\Closure $map): self return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -292,6 +330,7 @@ public function mapServerControl(\Closure $map): self return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -317,6 +356,7 @@ public function mapServerStatus(\Closure $map): self return new self( $this->clock, + $this->mapClock, $this->caseSensitivity, $this->io, $this->halt, @@ -336,7 +376,7 @@ public function mapServerStatus(\Closure $map): self */ public function clock(): Clock { - return $this->clock; + return ($this->mapClock)($this->clock); } /** From 08d99060b7bfa9b882cc37e412d7f4ec961457f3 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 09:57:49 +0200 Subject: [PATCH 21/41] apply clock logger via the config --- src/Config/Logger.php | 4 +++- src/OperatingSystem/Logger.php | 5 +---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Config/Logger.php b/src/Config/Logger.php index b99f96c..7787afe 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -6,6 +6,7 @@ use Innmind\OperatingSystem\Config; use Innmind\Server\Control; use Innmind\Server\Status; +use Innmind\TimeContinuum\Clock; use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; use Formal\AccessLayer\Connection; @@ -40,7 +41,8 @@ public function __invoke(Config $config): Config ->mapServerStatus(fn($server) => Status\Servers\Logger::of( $server, $this->logger, - )); + )) + ->mapClock(fn($clock) => Clock::logger($clock, $this->logger)); } public static function psr(LoggerInterface $logger): self diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php index 86ef1de..0395154 100644 --- a/src/OperatingSystem/Logger.php +++ b/src/OperatingSystem/Logger.php @@ -49,10 +49,7 @@ public function map(callable $map): Implementation #[\Override] public function clock(): TimeContinuum\Clock { - return TimeContinuum\Clock::logger( - $this->os->clock(), - $this->logger, - ); + return $this->os->clock(); } #[\Override] From 16542a9089c679234a17a4fd47e373313071c171 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:02:52 +0200 Subject: [PATCH 22/41] add Config::mapFileWatch() --- CHANGELOG.md | 1 + src/Config.php | 53 ++++++++++++++++++++++++++++++++++++++ src/Filesystem/Generic.php | 4 ++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4058f43..6f33d17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `Innmind\OperatingSystem\Config::mapServerControl()` - `Innmind\OperatingSystem\Config::mapServerStatus()` - `Innmind\OperatingSystem\Config::mapClock()` +- `Innmind\OperatingSystem\Config::mapFileWatch()` ### Changed diff --git a/src/Config.php b/src/Config.php index 6507e1c..b745262 100644 --- a/src/Config.php +++ b/src/Config.php @@ -11,6 +11,7 @@ Curl, }; use Innmind\Filesystem\CaseSensitivity; +use Innmind\FileWatch\Watch; use Innmind\Server\Status\EnvironmentPath; use Innmind\TimeWarp\Halt; use Innmind\IO\IO; @@ -27,6 +28,7 @@ final class Config * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $mapSql * @param \Closure(Control\Server): Control\Server $mapServerControl * @param \Closure(Status\Server): Status\Server $mapServerStatus + * @param \Closure(Watch): Watch $mapFileWatch */ private function __construct( private Clock $clock, @@ -42,6 +44,7 @@ private function __construct( private \Closure $mapSql, private \Closure $mapServerControl, private \Closure $mapServerStatus, + private \Closure $mapFileWatch, ) { } @@ -66,6 +69,7 @@ public static function of(): self static fn(AccessLayer\Connection $connection) => $connection, static fn(Control\Server $server) => $server, static fn(Status\Server $server) => $server, + static fn(Watch $watch) => $watch, ); } @@ -99,6 +103,7 @@ public function withClock(Clock $clock): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -125,6 +130,7 @@ public function mapClock(\Closure $map): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -147,6 +153,7 @@ public function caseInsensitiveFilesystem(): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -169,6 +176,7 @@ public function haltProcessVia(Halt $halt): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -196,6 +204,7 @@ public function mapHalt(\Closure $map): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -218,6 +227,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -240,6 +250,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -266,6 +277,7 @@ public function mapHttpTransport(\Closure $map): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -290,6 +302,7 @@ public function openSQLConnectionVia(\Closure $sql): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -316,6 +329,7 @@ public function mapSQLConnection(\Closure $map): self static fn(AccessLayer\Connection $connection) => $map($previous($connection)), $this->mapServerControl, $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -342,6 +356,7 @@ public function mapServerControl(\Closure $map): self $this->mapSql, static fn(Control\Server $server) => $map($previous($server)), $this->mapServerStatus, + $this->mapFileWatch, ); } @@ -368,6 +383,34 @@ public function mapServerStatus(\Closure $map): self $this->mapSql, $this->mapServerControl, static fn(Status\Server $server) => $map($previous($server)), + $this->mapFileWatch, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Watch): Watch $map + */ + public function mapFileWatch(\Closure $map): self + { + $previous = $this->mapFileWatch; + + return new self( + $this->clock, + $this->mapClock, + $this->caseSensitivity, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + $this->mapSql, + $this->mapServerControl, + $this->mapServerStatus, + static fn(Watch $watch) => $map($previous($watch)), ); } @@ -453,4 +496,14 @@ public function serverStatusMapper(): \Closure { return $this->mapServerStatus; } + + /** + * @internal + * + * @return \Closure(Watch): Watch + */ + public function fileWatchMapper(): \Closure + { + return $this->mapFileWatch; + } } diff --git a/src/Filesystem/Generic.php b/src/Filesystem/Generic.php index f1f0aed..4d0de26 100644 --- a/src/Filesystem/Generic.php +++ b/src/Filesystem/Generic.php @@ -33,7 +33,9 @@ final class Generic implements Filesystem private function __construct(Processes $processes, Config $config) { - $this->watch = Factory::build($processes, $config->halt()); + $this->watch = $config->fileWatchMapper()( + Factory::build($processes, $config->halt()), + ); $this->config = $config; /** @var \WeakMap */ $this->mounted = new \WeakMap; From 1dc3c360851551125b6f0ebec13af551069958c0 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:04:16 +0200 Subject: [PATCH 23/41] apply file watch logger via the config --- src/Config/Logger.php | 7 ++++++- src/Filesystem/Logger.php | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Config/Logger.php b/src/Config/Logger.php index 7787afe..4743cb0 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -7,6 +7,7 @@ use Innmind\Server\Control; use Innmind\Server\Status; use Innmind\TimeContinuum\Clock; +use Innmind\FileWatch\Watch; use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; use Formal\AccessLayer\Connection; @@ -42,7 +43,11 @@ public function __invoke(Config $config): Config $server, $this->logger, )) - ->mapClock(fn($clock) => Clock::logger($clock, $this->logger)); + ->mapClock(fn($clock) => Clock::logger($clock, $this->logger)) + ->mapFileWatch(fn($watch) => Watch::logger( + $watch, + $this->logger, + )); } public static function psr(LoggerInterface $logger): self diff --git a/src/Filesystem/Logger.php b/src/Filesystem/Logger.php index 2d89429..5974034 100644 --- a/src/Filesystem/Logger.php +++ b/src/Filesystem/Logger.php @@ -77,7 +77,6 @@ public function require(Path $path): Maybe #[\Override] public function watch(Path $path): Ping { - // todo bring back the ping logger return $this->filesystem->watch($path); } From 57d7bc4cf34e10d300d662f729992e38f2121425 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:25:12 +0200 Subject: [PATCH 24/41] remove Config::caseInsensitiveFilesystem() and add Config::mountFilesystemVia() --- CHANGELOG.md | 2 + src/Config.php | 99 ++++++++++++++++++++++---------------- src/Filesystem/Generic.php | 18 +++---- tests/FactoryTest.php | 14 +++++- 4 files changed, 80 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f33d17..7eb0bf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `Innmind\OperatingSystem\Config::mapServerStatus()` - `Innmind\OperatingSystem\Config::mapClock()` - `Innmind\OperatingSystem\Config::mapFileWatch()` +- `Innmind\OperatingSystem\Config::mountFilesystemVia()` ### Changed @@ -51,6 +52,7 @@ - `Innmind\OperatingSystem\Config::limitHttpConcurrencyTo()` use `::useHttpTransport()` instead - `Innmind\OperatingSystem\Config::withHttpHeartbeat()` use `::useHttpTransport()` instead - `Innmind\OperatingSystem\Config::disableSSLVerification()` use `::useHttpTransport()` instead +- `Innmind\OperatingSystem\Config::caseInsensitiveFilesystem()` use `::mountFilesystemVia()` instead - The following informations are no longer logged: - the current process id - the current process memory diff --git a/src/Config.php b/src/Config.php index b745262..4a530b1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -10,12 +10,19 @@ Transport as HttpTransport, Curl, }; -use Innmind\Filesystem\CaseSensitivity; +use Innmind\Filesystem\{ + Adapter as Filesystem, + CaseSensitivity +}; use Innmind\FileWatch\Watch; use Innmind\Server\Status\EnvironmentPath; use Innmind\TimeWarp\Halt; use Innmind\IO\IO; -use Innmind\Url\Url; +use Innmind\Url\{ + Url, + Path, +}; +use Innmind\Immutable\Attempt; use Formal\AccessLayer; final class Config @@ -29,11 +36,11 @@ final class Config * @param \Closure(Control\Server): Control\Server $mapServerControl * @param \Closure(Status\Server): Status\Server $mapServerStatus * @param \Closure(Watch): Watch $mapFileWatch + * @param \Closure(Path, self): Attempt $filesystem */ private function __construct( private Clock $clock, private \Closure $mapClock, - private CaseSensitivity $caseSensitivity, private IO $io, private Halt $halt, private \Closure $mapHalt, @@ -45,6 +52,7 @@ private function __construct( private \Closure $mapServerControl, private \Closure $mapServerStatus, private \Closure $mapFileWatch, + private \Closure $filesystem, ) { } @@ -53,7 +61,6 @@ public static function of(): self return new self( Clock::live(), static fn(Clock $clock) => $clock, - CaseSensitivity::sensitive, IO::fromAmbientAuthority(), Halt\Usleep::new(), static fn(Halt $halt) => $halt, @@ -70,6 +77,12 @@ public static function of(): self static fn(Control\Server $server) => $server, static fn(Status\Server $server) => $server, static fn(Watch $watch) => $watch, + static fn(Path $path, self $config) => Attempt::of( + static fn() => Filesystem\Filesystem::mount( + $path, + $config->io(), + )->withCaseSensitivity(CaseSensitivity::sensitive), + ), ); } @@ -92,7 +105,6 @@ public function withClock(Clock $clock): self return new self( $clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -104,6 +116,7 @@ public function withClock(Clock $clock): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -119,30 +132,6 @@ public function mapClock(\Closure $map): self return new self( $this->clock, static fn(Clock $clock) => $map($previous($clock)), - $this->caseSensitivity, - $this->io, - $this->halt, - $this->mapHalt, - $this->path, - $this->httpTransport, - $this->mapHttpTransport, - $this->sql, - $this->mapSql, - $this->mapServerControl, - $this->mapServerStatus, - $this->mapFileWatch, - ); - } - - /** - * @psalm-mutation-free - */ - public function caseInsensitiveFilesystem(): self - { - return new self( - $this->clock, - $this->mapClock, - CaseSensitivity::insensitive, $this->io, $this->halt, $this->mapHalt, @@ -154,6 +143,7 @@ public function caseInsensitiveFilesystem(): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -165,7 +155,6 @@ public function haltProcessVia(Halt $halt): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $halt, $this->mapHalt, @@ -177,6 +166,7 @@ public function haltProcessVia(Halt $halt): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -193,7 +183,6 @@ public function mapHalt(\Closure $map): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, static fn(Halt $halt) => $map($previous($halt)), @@ -205,6 +194,7 @@ public function mapHalt(\Closure $map): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -216,7 +206,6 @@ public function withEnvironmentPath(EnvironmentPath $path): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -228,6 +217,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -239,7 +229,6 @@ public function useHttpTransport(HttpTransport $transport): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -251,6 +240,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -266,7 +256,6 @@ public function mapHttpTransport(\Closure $map): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -278,6 +267,7 @@ public function mapHttpTransport(\Closure $map): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -291,7 +281,6 @@ public function openSQLConnectionVia(\Closure $sql): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -303,6 +292,7 @@ public function openSQLConnectionVia(\Closure $sql): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -318,7 +308,6 @@ public function mapSQLConnection(\Closure $map): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -330,6 +319,7 @@ public function mapSQLConnection(\Closure $map): self $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -345,7 +335,6 @@ public function mapServerControl(\Closure $map): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -357,6 +346,7 @@ public function mapServerControl(\Closure $map): self static fn(Control\Server $server) => $map($previous($server)), $this->mapServerStatus, $this->mapFileWatch, + $this->filesystem, ); } @@ -372,7 +362,6 @@ public function mapServerStatus(\Closure $map): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -384,6 +373,7 @@ public function mapServerStatus(\Closure $map): self $this->mapServerControl, static fn(Status\Server $server) => $map($previous($server)), $this->mapFileWatch, + $this->filesystem, ); } @@ -399,7 +389,6 @@ public function mapFileWatch(\Closure $map): self return new self( $this->clock, $this->mapClock, - $this->caseSensitivity, $this->io, $this->halt, $this->mapHalt, @@ -411,6 +400,32 @@ public function mapFileWatch(\Closure $map): self $this->mapServerControl, $this->mapServerStatus, static fn(Watch $watch) => $map($previous($watch)), + $this->filesystem, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Path, self): Attempt $filesystem + */ + public function mountFilesystemVia(\Closure $filesystem): self + { + return new self( + $this->clock, + $this->mapClock, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + $this->mapSql, + $this->mapServerControl, + $this->mapServerStatus, + $this->mapFileWatch, + $filesystem, ); } @@ -424,10 +439,12 @@ public function clock(): Clock /** * @internal + * + * @return Attempt */ - public function filesystemCaseSensitivity(): CaseSensitivity + public function filesystem(Path $path): Attempt { - return $this->caseSensitivity; + return ($this->filesystem)($path, $this); } /** diff --git a/src/Filesystem/Generic.php b/src/Filesystem/Generic.php index 4d0de26..f99d787 100644 --- a/src/Filesystem/Generic.php +++ b/src/Filesystem/Generic.php @@ -62,18 +62,14 @@ public function mount(Path $path): Attempt } } - return Attempt::of(function() use ($path) { - $adapter = Adapter\Filesystem::mount( - $path, - $this->config->io(), - ) - ->withCaseSensitivity( - $this->config->filesystemCaseSensitivity(), - ); - $this->mounted[$adapter] = $path->toString(); + return $this + ->config + ->filesystem($path) + ->map(function($adapter) use ($path) { + $this->mounted[$adapter] = $path->toString(); - return $adapter; - }); + return $adapter; + }); } #[\Override] diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 6632c91..6dfd32d 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -10,11 +10,14 @@ }; use Innmind\TimeContinuum\Clock; use Innmind\Filesystem\{ + Adapter\Filesystem, File, File\Content, Directory, + CaseSensitivity, }; use Innmind\Url\Path; +use Innmind\Immutable\Attempt; use Symfony\Component\Filesystem\Filesystem as FS; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -48,7 +51,16 @@ public function testPersistingFileOnCaseInsensitiveFilesystem() $path = \sys_get_temp_dir().'/innmind/filesystem/'; (new FS)->remove($path); - $os = Factory::build(Config::of()->caseInsensitiveFilesystem()); + $os = Factory::build( + Config::of()->mountFilesystemVia( + static fn($path, $config) => Attempt::of( + static fn() => Filesystem::mount( + $path, + $config->io(), + )->withCaseSensitivity(CaseSensitivity::insensitive), + ), + ), + ); $adapter = $os ->filesystem() ->mount(Path::of($path)) From 3fc3c41972443ab75e870439640440de46206fc9 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:28:27 +0200 Subject: [PATCH 25/41] add Config::mapFilesystem() --- CHANGELOG.md | 1 + src/Config.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eb0bf9..2c9fad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - `Innmind\OperatingSystem\Config::mapClock()` - `Innmind\OperatingSystem\Config::mapFileWatch()` - `Innmind\OperatingSystem\Config::mountFilesystemVia()` +- `Innmind\OperatingSystem\Config::mapFilesystem()` ### Changed diff --git a/src/Config.php b/src/Config.php index 4a530b1..c53b7de 100644 --- a/src/Config.php +++ b/src/Config.php @@ -37,6 +37,7 @@ final class Config * @param \Closure(Status\Server): Status\Server $mapServerStatus * @param \Closure(Watch): Watch $mapFileWatch * @param \Closure(Path, self): Attempt $filesystem + * @param \Closure(Filesystem): Filesystem $mapFilesystem */ private function __construct( private Clock $clock, @@ -53,6 +54,7 @@ private function __construct( private \Closure $mapServerStatus, private \Closure $mapFileWatch, private \Closure $filesystem, + private \Closure $mapFilesystem, ) { } @@ -83,6 +85,7 @@ public static function of(): self $config->io(), )->withCaseSensitivity(CaseSensitivity::sensitive), ), + static fn(Filesystem $filesystem) => $filesystem, ); } @@ -117,6 +120,7 @@ public function withClock(Clock $clock): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -144,6 +148,7 @@ public function mapClock(\Closure $map): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -167,6 +172,7 @@ public function haltProcessVia(Halt $halt): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -195,6 +201,7 @@ public function mapHalt(\Closure $map): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -218,6 +225,7 @@ public function withEnvironmentPath(EnvironmentPath $path): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -241,6 +249,7 @@ public function useHttpTransport(HttpTransport $transport): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -268,6 +277,7 @@ public function mapHttpTransport(\Closure $map): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -293,6 +303,7 @@ public function openSQLConnectionVia(\Closure $sql): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -320,6 +331,7 @@ public function mapSQLConnection(\Closure $map): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -347,6 +359,7 @@ public function mapServerControl(\Closure $map): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -374,6 +387,7 @@ public function mapServerStatus(\Closure $map): self static fn(Status\Server $server) => $map($previous($server)), $this->mapFileWatch, $this->filesystem, + $this->mapFilesystem, ); } @@ -401,6 +415,7 @@ public function mapFileWatch(\Closure $map): self $this->mapServerStatus, static fn(Watch $watch) => $map($previous($watch)), $this->filesystem, + $this->mapFilesystem, ); } @@ -426,6 +441,35 @@ public function mountFilesystemVia(\Closure $filesystem): self $this->mapServerStatus, $this->mapFileWatch, $filesystem, + $this->mapFilesystem, + ); + } + + /** + * @psalm-mutation-free + * + * @param \Closure(Filesystem): Filesystem $map + */ + public function mapFilesystem(\Closure $map): self + { + $previous = $this->mapFilesystem; + + return new self( + $this->clock, + $this->mapClock, + $this->io, + $this->halt, + $this->mapHalt, + $this->path, + $this->httpTransport, + $this->mapHttpTransport, + $this->sql, + $this->mapSql, + $this->mapServerControl, + $this->mapServerStatus, + $this->mapFileWatch, + $this->filesystem, + static fn(Filesystem $filesystem) => $map($previous($filesystem)), ); } @@ -444,7 +488,9 @@ public function clock(): Clock */ public function filesystem(Path $path): Attempt { - return ($this->filesystem)($path, $this); + return ($this->filesystem)($path, $this)->map( + $this->mapFilesystem, + ); } /** From a17c2694b0265cea05fb3b97b294d0709265d6de Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:30:10 +0200 Subject: [PATCH 26/41] apply filesystem logger via the config --- src/Config/Logger.php | 5 +++++ src/Filesystem/Logger.php | 8 +------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Config/Logger.php b/src/Config/Logger.php index 4743cb0..56dcdc2 100644 --- a/src/Config/Logger.php +++ b/src/Config/Logger.php @@ -8,6 +8,7 @@ use Innmind\Server\Status; use Innmind\TimeContinuum\Clock; use Innmind\FileWatch\Watch; +use Innmind\Filesystem\Adapter as Filesystem; use Innmind\HttpTransport; use Innmind\TimeWarp\Halt; use Formal\AccessLayer\Connection; @@ -47,6 +48,10 @@ public function __invoke(Config $config): Config ->mapFileWatch(fn($watch) => Watch::logger( $watch, $this->logger, + )) + ->mapFilesystem(fn($filesystem) => Filesystem\Logger::psr( + $filesystem, + $this->logger, )); } diff --git a/src/Filesystem/Logger.php b/src/Filesystem/Logger.php index 5974034..7aae2f0 100644 --- a/src/Filesystem/Logger.php +++ b/src/Filesystem/Logger.php @@ -4,7 +4,6 @@ namespace Innmind\OperatingSystem\Filesystem; use Innmind\OperatingSystem\Filesystem; -use Innmind\Filesystem\Adapter; use Innmind\Url\Path; use Innmind\FileWatch\Ping; use Innmind\Immutable\{ @@ -37,12 +36,7 @@ public static function psr( #[\Override] public function mount(Path $path): Attempt { - return $this->filesystem->mount($path)->map( - fn($adapter) => Adapter\Logger::psr( - $adapter, - $this->logger, - ), - ); + return $this->filesystem->mount($path); } #[\Override] From f254519ecde2d9608e861842defeac3c35eb18e0 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:33:16 +0200 Subject: [PATCH 27/41] remove logging if a file/directory exists and when a php file is loaded --- CHANGELOG.md | 2 + src/Filesystem/Logger.php | 82 ---------------------------------- src/OperatingSystem/Logger.php | 5 +-- 3 files changed, 3 insertions(+), 86 deletions(-) delete mode 100644 src/Filesystem/Logger.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c9fad5..eb09b73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ - opened ports - opened remote sockets - opened sockets + - if a file/directory exists or not + - when a PHP file is loaded in memory ## 5.2.0 - 2024-07-14 diff --git a/src/Filesystem/Logger.php b/src/Filesystem/Logger.php deleted file mode 100644 index 7aae2f0..0000000 --- a/src/Filesystem/Logger.php +++ /dev/null @@ -1,82 +0,0 @@ -filesystem = $filesystem; - $this->logger = $logger; - } - - public static function psr( - Filesystem $filesystem, - LoggerInterface $logger, - ): self { - return new self($filesystem, $logger); - } - - #[\Override] - public function mount(Path $path): Attempt - { - return $this->filesystem->mount($path); - } - - #[\Override] - public function contains(Path $path): bool - { - $contains = $this->filesystem->contains($path); - - $this->logger->debug( - 'Checking if {path} exists, answer: {answer}', - ['answer' => $contains ? 'yes' : 'no'], - ); - - return $contains; - } - - #[\Override] - public function require(Path $path): Maybe - { - return $this - ->filesystem - ->require($path) - ->map(function(mixed $value) use ($path): mixed { - $this->logger->debug( - 'PHP file located at {path} loaded in memory', - ['path' => $path->toString()], - ); - - return $value; - }); - } - - #[\Override] - public function watch(Path $path): Ping - { - return $this->filesystem->watch($path); - } - - #[\Override] - public function temporary(Sequence $chunks): Attempt - { - return $this->filesystem->temporary($chunks); - } -} diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php index 0395154..9ba24f0 100644 --- a/src/OperatingSystem/Logger.php +++ b/src/OperatingSystem/Logger.php @@ -55,10 +55,7 @@ public function clock(): TimeContinuum\Clock #[\Override] public function filesystem(): Filesystem { - return Filesystem\Logger::psr( - $this->os->filesystem(), - $this->logger, - ); + return $this->os->filesystem(); } #[\Override] From d18cdb398d56d056ffa78e4826d49a8297856815 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:37:24 +0200 Subject: [PATCH 28/41] remove logger os --- CHANGELOG.md | 1 + src/OperatingSystem/Logger.php | 96 ---------------------------- tests/CurrentProcess/GenericTest.php | 8 +-- tests/Remote/GenericTest.php | 13 ++-- 4 files changed, 8 insertions(+), 110 deletions(-) delete mode 100644 src/OperatingSystem/Logger.php diff --git a/CHANGELOG.md b/CHANGELOG.md index eb09b73..424330b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ - `Innmind\OperatingSystem\Config::useStreamCapabilities()` - `Innmind\OperatingSystem\Sockets::watch()` - `Innmind\OperatingSystem\OperatingSystem\Resilient` +- `Innmind\OperatingSystem\OperatingSystem\Logger` - `Innmind\OperatingSystem\Config::limitHttpConcurrencyTo()` use `::useHttpTransport()` instead - `Innmind\OperatingSystem\Config::withHttpHeartbeat()` use `::useHttpTransport()` instead - `Innmind\OperatingSystem\Config::disableSSLVerification()` use `::useHttpTransport()` instead diff --git a/src/OperatingSystem/Logger.php b/src/OperatingSystem/Logger.php deleted file mode 100644 index 9ba24f0..0000000 --- a/src/OperatingSystem/Logger.php +++ /dev/null @@ -1,96 +0,0 @@ -os = $os; - $this->logger = $logger; - } - - public static function psr(Implementation $os, LoggerInterface $logger): self - { - return new self( - $os->map(static fn($_, $config) => Unix::of( - $config->map(Config\Logger::psr($logger)), - )), - $logger, - ); - } - - #[\Override] - public function map(callable $map): Implementation - { - return new self( - $this->os->map($map), - $this->logger, - ); - } - - #[\Override] - public function clock(): TimeContinuum\Clock - { - return $this->os->clock(); - } - - #[\Override] - public function filesystem(): Filesystem - { - return $this->os->filesystem(); - } - - #[\Override] - public function status(): Status\Server - { - return $this->os->status(); - } - - #[\Override] - public function control(): Control\Server - { - return $this->os->control(); - } - - #[\Override] - public function ports(): Ports - { - return $this->os->ports(); - } - - #[\Override] - public function sockets(): Sockets - { - return $this->os->sockets(); - } - - #[\Override] - public function remote(): Remote - { - return $this->os->remote(); - } - - #[\Override] - public function process(): CurrentProcess - { - return $this->os->process(); - } -} diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcess/GenericTest.php index b025686..7b5c6ba 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcess/GenericTest.php @@ -7,8 +7,8 @@ CurrentProcess\Generic, CurrentProcess\Signals, CurrentProcess, - OperatingSystem\Logger, OperatingSystem\Unix, + Config, }; use Innmind\Server\Control\Server\Process\Pid; use Innmind\Server\Status\Server\Memory\Bytes; @@ -47,12 +47,10 @@ public function testId() public function testHalt(): BlackBox\Proof { - $os = Unix::of(); - return $this ->forAll(Set::of( - $os, - Logger::psr($os, new NullLogger), + Unix::of(), + Unix::of(Config::of()->map(Config\Logger::psr(new NullLogger))), )) ->prove(function($os) { $process = $os->process(); diff --git a/tests/Remote/GenericTest.php b/tests/Remote/GenericTest.php index 6a548d1..6450346 100644 --- a/tests/Remote/GenericTest.php +++ b/tests/Remote/GenericTest.php @@ -5,7 +5,6 @@ use Innmind\OperatingSystem\{ Remote\Generic, - OperatingSystem\Logger, OperatingSystem\Unix, Remote, Config, @@ -126,12 +125,10 @@ public function testSocket() public function testHttp(): BlackBox\Proof { - $os = Unix::of(); - return $this ->forAll(Set::of( - $os, - Logger::psr($os, new NullLogger), + Unix::of(), + Unix::of(Config::of()->map(Config\Logger::psr(new NullLogger))), )) ->prove(function($os) { $remote = $os->remote(); @@ -144,14 +141,12 @@ public function testHttp(): BlackBox\Proof public function testSql(): BlackBox\Proof { - $os = Unix::of(); - return $this ->forAll( FUrl::any(), Set::of( - $os, - Logger::psr($os, new NullLogger), + Unix::of(), + Unix::of(Config::of()->map(Config\Logger::psr(new NullLogger))), ), ) ->prove(function($server, $os) { From eb8d0ac248cd8f678e9fac0033239181578ae9ff Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:41:17 +0200 Subject: [PATCH 29/41] make Sockets a final class --- CHANGELOG.md | 1 + src/OperatingSystem/Unix.php | 2 +- src/Sockets.php | 47 ++++++++++++-- src/Sockets/Unix.php | 62 ------------------- tests/OperatingSystem/UnixTest.php | 2 +- .../{Sockets/UnixTest.php => SocketsTest.php} | 16 ++--- 6 files changed, 51 insertions(+), 79 deletions(-) delete mode 100644 src/Sockets/Unix.php rename tests/{Sockets/UnixTest.php => SocketsTest.php} (83%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 424330b..119b19b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ - `Innmind\OperatingSystem\Sockets::takeOver()` now returns an `Innmind\Immutable\Attempt` - `Innmind\OperatingSystem\Sockets::connectTo()` now returns an `Innmind\Immutable\Attempt` - `Innmind\OperatingSystem\OperatingSystem` is now a final class +- `Innmind\OperatingSystem\Sockets` is now a final class ### Fixed diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index 54697b4..d80e8a7 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -92,7 +92,7 @@ public function ports(): Ports #[\Override] public function sockets(): Sockets { - return $this->sockets ??= Sockets\Unix::of($this->config); + return $this->sockets ??= Sockets::of($this->config); } #[\Override] diff --git a/src/Sockets.php b/src/Sockets.php index cb136e5..a743622 100644 --- a/src/Sockets.php +++ b/src/Sockets.php @@ -10,24 +10,63 @@ }; use Innmind\Immutable\Attempt; -interface Sockets +final class Sockets { + private Config $config; + + private function __construct(Config $config) + { + $this->config = $config; + } + + /** + * @internal + */ + public static function of(Config $config): self + { + return new self($config); + } + /** * This method will fail if the socket already exist * * @return Attempt */ - public function open(Address $address): Attempt; + public function open(Address $address): Attempt + { + return $this + ->config + ->io() + ->sockets() + ->servers() + ->unix($address); + } /** * This will take control of the socket if it already exist (use carefully) * * @return Attempt */ - public function takeOver(Address $address): Attempt; + public function takeOver(Address $address): Attempt + { + return $this + ->config + ->io() + ->sockets() + ->servers() + ->takeOver($address); + } /** * @return Attempt */ - public function connectTo(Address $address): Attempt; + public function connectTo(Address $address): Attempt + { + return $this + ->config + ->io() + ->sockets() + ->clients() + ->unix($address); + } } diff --git a/src/Sockets/Unix.php b/src/Sockets/Unix.php deleted file mode 100644 index 03ee3da..0000000 --- a/src/Sockets/Unix.php +++ /dev/null @@ -1,62 +0,0 @@ -config = $config; - } - - /** - * @internal - */ - public static function of(Config $config): self - { - return new self($config); - } - - #[\Override] - public function open(Address $address): Attempt - { - return $this - ->config - ->io() - ->sockets() - ->servers() - ->unix($address); - } - - #[\Override] - public function takeOver(Address $address): Attempt - { - return $this - ->config - ->io() - ->sockets() - ->servers() - ->takeOver($address); - } - - #[\Override] - public function connectTo(Address $address): Attempt - { - return $this - ->config - ->io() - ->sockets() - ->clients() - ->unix($address); - } -} diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystem/UnixTest.php index cee20e1..11e5c8b 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystem/UnixTest.php @@ -30,7 +30,7 @@ public function testInterface() $this->assertInstanceOf(ServerStatus::class, $os->status()); $this->assertInstanceOf(ServerControl::class, $os->control()); $this->assertInstanceOf(Ports\Unix::class, $os->ports()); - $this->assertInstanceOf(Sockets\Unix::class, $os->sockets()); + $this->assertInstanceOf(Sockets::class, $os->sockets()); $this->assertInstanceOf(Remote\Generic::class, $os->remote()); $this->assertInstanceOf(CurrentProcess\Generic::class, $os->process()); $this->assertSame($os->filesystem(), $os->filesystem()); diff --git a/tests/Sockets/UnixTest.php b/tests/SocketsTest.php similarity index 83% rename from tests/Sockets/UnixTest.php rename to tests/SocketsTest.php index 6cc61d2..6a47fd9 100644 --- a/tests/Sockets/UnixTest.php +++ b/tests/SocketsTest.php @@ -1,10 +1,9 @@ assertInstanceOf(Sockets::class, Unix::of(Config::of())); - } - public function testOpen() { - $sockets = Unix::of(Config::of()); + $sockets = Sockets::of(Config::of()); $socket = $sockets->open(Address::of(Path::of('/tmp/foo')))->match( static fn($server) => $server, @@ -44,7 +38,7 @@ public function testOpen() public function testTakeOver() { - $sockets = Unix::of(Config::of()); + $sockets = Sockets::of(Config::of()); $socket = $sockets->open(Address::of(Path::of('/tmp/foo')))->match( static fn($server) => $server, @@ -62,7 +56,7 @@ public function testTakeOver() public function testConnectTo() { - $sockets = Unix::of(Config::of()); + $sockets = Sockets::of(Config::of()); $server = $sockets->open(Address::of(Path::of('/tmp/foo')))->match( static fn($server) => $server, From 6c4ec58153e88d94b2e6d13bf751e1a810dda554 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:44:24 +0200 Subject: [PATCH 30/41] make Remote a final class --- CHANGELOG.md | 1 + src/OperatingSystem/Unix.php | 2 +- src/Remote.php | 64 ++++++++++++-- src/Remote/Generic.php | 83 ------------------- tests/OperatingSystem/UnixTest.php | 2 +- .../GenericTest.php => RemoteTest.php} | 26 ++---- 6 files changed, 68 insertions(+), 110 deletions(-) delete mode 100644 src/Remote/Generic.php rename tests/{Remote/GenericTest.php => RemoteTest.php} (92%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 119b19b..e7e8332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ - `Innmind\OperatingSystem\Sockets::connectTo()` now returns an `Innmind\Immutable\Attempt` - `Innmind\OperatingSystem\OperatingSystem` is now a final class - `Innmind\OperatingSystem\Sockets` is now a final class +- `Innmind\OperatingSystem\Remote` is now a final class ### Fixed diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index d80e8a7..409cb91 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -98,7 +98,7 @@ public function sockets(): Sockets #[\Override] public function remote(): Remote { - return $this->remote ??= Remote\Generic::of( + return $this->remote ??= Remote::of( $this->control(), $this->config, ); diff --git a/src/Remote.php b/src/Remote.php index a97518d..54b52fd 100644 --- a/src/Remote.php +++ b/src/Remote.php @@ -3,7 +3,10 @@ namespace Innmind\OperatingSystem; -use Innmind\Server\Control\Server; +use Innmind\Server\Control\{ + Server, + Servers, +}; use Innmind\IO\{ Sockets\Clients\Client, Sockets\Internet\Transport, @@ -11,19 +14,68 @@ use Innmind\Url\{ Url, Authority, + Authority\Port, }; use Innmind\HttpTransport\Transport as HttpTransport; use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; -interface Remote +final class Remote { - public function ssh(Url $server): Server; + private Server $server; + private Config $config; + private ?HttpTransport $http = null; + + private function __construct(Server $server, Config $config) + { + $this->server = $server; + $this->config = $config; + } + + /** + * @internal + */ + public static function of(Server $server, Config $config): self + { + return new self($server, $config); + } + + public function ssh(Url $server): Server + { + $port = null; + + if ($server->authority()->port()->value() !== Port::none()->value()) { + $port = $server->authority()->port(); + } + + return $this->config->serverControlMapper()(Servers\Remote::of( + $this->server, + $server->authority()->userInformation()->user(), + $server->authority()->host(), + $port, + )); + } /** * @return Attempt */ - public function socket(Transport $transport, Authority $authority): Attempt; - public function http(): HttpTransport; - public function sql(Url $server): Connection; + public function socket(Transport $transport, Authority $authority): Attempt + { + return $this + ->config + ->io() + ->sockets() + ->clients() + ->internet($transport, $authority); + } + + public function http(): HttpTransport + { + return $this->http ??= $this->config->httpTransport(); + } + + public function sql(Url $server): Connection + { + return $this->config->sql($server); + } } diff --git a/src/Remote/Generic.php b/src/Remote/Generic.php deleted file mode 100644 index 10d71b8..0000000 --- a/src/Remote/Generic.php +++ /dev/null @@ -1,83 +0,0 @@ -server = $server; - $this->config = $config; - } - - /** - * @internal - */ - public static function of(Server $server, Config $config): self - { - return new self($server, $config); - } - - #[\Override] - public function ssh(Url $server): Server - { - $port = null; - - if ($server->authority()->port()->value() !== Port::none()->value()) { - $port = $server->authority()->port(); - } - - return $this->config->serverControlMapper()(Servers\Remote::of( - $this->server, - $server->authority()->userInformation()->user(), - $server->authority()->host(), - $port, - )); - } - - #[\Override] - public function socket(Transport $transport, Authority $authority): Attempt - { - return $this - ->config - ->io() - ->sockets() - ->clients() - ->internet($transport, $authority); - } - - #[\Override] - public function http(): HttpTransport - { - return $this->http ??= $this->config->httpTransport(); - } - - #[\Override] - public function sql(Url $server): Connection - { - return $this->config->sql($server); - } -} diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystem/UnixTest.php index 11e5c8b..f32a8a5 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystem/UnixTest.php @@ -31,7 +31,7 @@ public function testInterface() $this->assertInstanceOf(ServerControl::class, $os->control()); $this->assertInstanceOf(Ports\Unix::class, $os->ports()); $this->assertInstanceOf(Sockets::class, $os->sockets()); - $this->assertInstanceOf(Remote\Generic::class, $os->remote()); + $this->assertInstanceOf(Remote::class, $os->remote()); $this->assertInstanceOf(CurrentProcess\Generic::class, $os->process()); $this->assertSame($os->filesystem(), $os->filesystem()); $this->assertSame($os->status(), $os->status()); diff --git a/tests/Remote/GenericTest.php b/tests/RemoteTest.php similarity index 92% rename from tests/Remote/GenericTest.php rename to tests/RemoteTest.php index 6450346..d8f7bc7 100644 --- a/tests/Remote/GenericTest.php +++ b/tests/RemoteTest.php @@ -1,12 +1,11 @@ assertInstanceOf( - Remote::class, - Generic::of( - $this->server(), - Config::of(), - ), - ); - } - public function testSsh() { - $remote = Generic::of( + $remote = Remote::of( $this->server("ssh '-p' '42' 'user@my-vps' 'ls'"), Config::of(), ); @@ -72,7 +60,7 @@ public function testSsh() public function testSshLogger() { - $remote = Generic::of( + $remote = Remote::of( $this->server("ssh '-p' '42' 'user@my-vps' 'ls'"), Config::of()->map(Config\Logger::psr(new NullLogger)), ); @@ -88,7 +76,7 @@ public function testSshLogger() public function testSshWithoutPort() { - $remote = Generic::of( + $remote = Remote::of( $this->server("ssh 'user@my-vps' 'ls'"), Config::of(), ); @@ -101,7 +89,7 @@ public function testSshWithoutPort() public function testSocket() { - $remote = Generic::of( + $remote = Remote::of( $this->server(), Config::of(), ); From 7cb57b56a469a73118919c11afc6804303115f04 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:46:52 +0200 Subject: [PATCH 31/41] make Ports a final class --- CHANGELOG.md | 1 + src/OperatingSystem/Unix.php | 2 +- src/Ports.php | 27 ++++++++++++- src/Ports/Unix.php | 42 --------------------- tests/OperatingSystem/UnixTest.php | 2 +- tests/{Ports/UnixTest.php => PortsTest.php} | 12 ++---- 6 files changed, 31 insertions(+), 55 deletions(-) delete mode 100644 src/Ports/Unix.php rename tests/{Ports/UnixTest.php => PortsTest.php} (71%) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7e8332..e68d10a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ - `Innmind\OperatingSystem\OperatingSystem` is now a final class - `Innmind\OperatingSystem\Sockets` is now a final class - `Innmind\OperatingSystem\Remote` is now a final class +- `Innmind\OperatingSystem\Ports` is now a final class ### Fixed diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index 409cb91..4016d6a 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -86,7 +86,7 @@ public function control(): ServerControl #[\Override] public function ports(): Ports { - return $this->ports ??= Ports\Unix::of($this->config); + return $this->ports ??= Ports::of($this->config); } #[\Override] diff --git a/src/Ports.php b/src/Ports.php index 4c3b5e1..5794216 100644 --- a/src/Ports.php +++ b/src/Ports.php @@ -11,10 +11,33 @@ use Innmind\IP\IP; use Innmind\Immutable\Attempt; -interface Ports +final class Ports { + private Config $config; + + private function __construct(Config $config) + { + $this->config = $config; + } + + /** + * @internal + */ + public static function of(Config $config): self + { + return new self($config); + } + /** * @return Attempt */ - public function open(Transport $transport, IP $ip, Port $port): Attempt; + public function open(Transport $transport, IP $ip, Port $port): Attempt + { + return $this + ->config + ->io() + ->sockets() + ->servers() + ->internet($transport, $ip, $port); + } } diff --git a/src/Ports/Unix.php b/src/Ports/Unix.php deleted file mode 100644 index e131cfc..0000000 --- a/src/Ports/Unix.php +++ /dev/null @@ -1,42 +0,0 @@ -config = $config; - } - - /** - * @internal - */ - public static function of(Config $config): self - { - return new self($config); - } - - #[\Override] - public function open(Transport $transport, IP $ip, Port $port): Attempt - { - return $this - ->config - ->io() - ->sockets() - ->servers() - ->internet($transport, $ip, $port); - } -} diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystem/UnixTest.php index f32a8a5..134b646 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystem/UnixTest.php @@ -29,7 +29,7 @@ public function testInterface() $this->assertInstanceOf(Filesystem\Generic::class, $os->filesystem()); $this->assertInstanceOf(ServerStatus::class, $os->status()); $this->assertInstanceOf(ServerControl::class, $os->control()); - $this->assertInstanceOf(Ports\Unix::class, $os->ports()); + $this->assertInstanceOf(Ports::class, $os->ports()); $this->assertInstanceOf(Sockets::class, $os->sockets()); $this->assertInstanceOf(Remote::class, $os->remote()); $this->assertInstanceOf(CurrentProcess\Generic::class, $os->process()); diff --git a/tests/Ports/UnixTest.php b/tests/PortsTest.php similarity index 71% rename from tests/Ports/UnixTest.php rename to tests/PortsTest.php index e967b39..8bd6a9b 100644 --- a/tests/Ports/UnixTest.php +++ b/tests/PortsTest.php @@ -1,10 +1,9 @@ assertInstanceOf(Ports::class, Unix::of(Config::of())); - } - public function testOpen() { - $ports = Unix::of(Config::of()); + $ports = Ports::of(Config::of()); $socket = $ports->open( Transport::tlsv12(), From 84c582a3a7fa23715447ca70f652542c7915fe39 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:51:28 +0200 Subject: [PATCH 32/41] make Filesystem a final class --- CHANGELOG.md | 1 + src/Filesystem.php | 116 +++++++++++++-- src/Filesystem/Generic.php | 134 ------------------ src/OperatingSystem/Unix.php | 2 +- .../GenericTest.php => FilesystemTest.php} | 34 ++--- tests/OperatingSystem/UnixTest.php | 2 +- tests/{Filesystem => }/fixture.php | 0 7 files changed, 121 insertions(+), 168 deletions(-) delete mode 100644 src/Filesystem/Generic.php rename tests/{Filesystem/GenericTest.php => FilesystemTest.php} (87%) rename tests/{Filesystem => }/fixture.php (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index e68d10a..af0576b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ - `Innmind\OperatingSystem\Sockets` is now a final class - `Innmind\OperatingSystem\Remote` is now a final class - `Innmind\OperatingSystem\Ports` is now a final class +- `Innmind\OperatingSystem\Filesystem` is now a final class ### Fixed diff --git a/src/Filesystem.php b/src/Filesystem.php index 85ce603..d1da96a 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -8,27 +8,106 @@ File\Content, }; use Innmind\Url\Path; -use Innmind\FileWatch\Ping; +use Innmind\Server\Control\Server\Processes; +use Innmind\FileWatch\{ + Ping, + Factory, + Watch, +}; use Innmind\Immutable\{ - Attempt, Maybe, - Str, + Attempt, Sequence, + Str, }; -interface Filesystem +final class Filesystem { + private Watch $watch; + private Config $config; + /** @var \WeakMap */ + private \WeakMap $mounted; + + private function __construct(Processes $processes, Config $config) + { + $this->watch = $config->fileWatchMapper()( + Factory::build($processes, $config->halt()), + ); + $this->config = $config; + /** @var \WeakMap */ + $this->mounted = new \WeakMap; + } + + /** + * @internal + */ + public static function of(Processes $processes, Config $config): self + { + return new self($processes, $config); + } + /** * @return Attempt */ - public function mount(Path $path): Attempt; - public function contains(Path $path): bool; + public function mount(Path $path): Attempt + { + /** + * @var Adapter $adapter + * @var string $mounted + */ + foreach ($this->mounted as $adapter => $mounted) { + if ($path->toString() === $mounted) { + return Attempt::result($adapter); + } + } + + return $this + ->config + ->filesystem($path) + ->map(function($adapter) use ($path) { + $this->mounted[$adapter] = $path->toString(); + + return $adapter; + }); + } + + public function contains(Path $path): bool + { + if (!\file_exists($path->toString())) { + return false; + } + + if ($path->directory() && !\is_dir($path->toString())) { + return false; + } + + return true; + } /** * @return Maybe Return the value returned by the file or nothing if the file doesn't exist */ - public function require(Path $path): Maybe; - public function watch(Path $path): Ping; + public function require(Path $path): Maybe + { + $path = $path->toString(); + + if (!\file_exists($path) || \is_dir($path)) { + /** @var Maybe */ + return Maybe::nothing(); + } + + /** + * @psalm-suppress UnresolvableInclude + * @psalm-suppress MixedArgument + * @var Maybe + */ + return Maybe::just(require $path); + } + + public function watch(Path $path): Ping + { + return ($this->watch)($path); + } /** * This method is to be used to generate a temporary file content even if it @@ -42,5 +121,24 @@ public function watch(Path $path): Ping; * * @return Attempt */ - public function temporary(Sequence $chunks): Attempt; + public function temporary(Sequence $chunks): Attempt + { + return $this + ->config + ->io() + ->files() + ->temporary(Sequence::of()) + ->flatMap( + static fn($tmp) => $chunks + ->sink($tmp->push()->chunk(...)) + ->attempt( + static fn($push, $chunk) => $chunk + ->attempt(static fn() => new \RuntimeException('Failed to load chunk')) + ->flatMap($push) + ->map(static fn() => $push), + ) + ->map(static fn() => $tmp->read()), + ) + ->map(Content::io(...)); + } } diff --git a/src/Filesystem/Generic.php b/src/Filesystem/Generic.php deleted file mode 100644 index f99d787..0000000 --- a/src/Filesystem/Generic.php +++ /dev/null @@ -1,134 +0,0 @@ - */ - private \WeakMap $mounted; - - private function __construct(Processes $processes, Config $config) - { - $this->watch = $config->fileWatchMapper()( - Factory::build($processes, $config->halt()), - ); - $this->config = $config; - /** @var \WeakMap */ - $this->mounted = new \WeakMap; - } - - /** - * @internal - */ - public static function of(Processes $processes, Config $config): self - { - return new self($processes, $config); - } - - #[\Override] - public function mount(Path $path): Attempt - { - /** - * @var Adapter $adapter - * @var string $mounted - */ - foreach ($this->mounted as $adapter => $mounted) { - if ($path->toString() === $mounted) { - return Attempt::result($adapter); - } - } - - return $this - ->config - ->filesystem($path) - ->map(function($adapter) use ($path) { - $this->mounted[$adapter] = $path->toString(); - - return $adapter; - }); - } - - #[\Override] - public function contains(Path $path): bool - { - if (!\file_exists($path->toString())) { - return false; - } - - if ($path->directory() && !\is_dir($path->toString())) { - return false; - } - - return true; - } - - #[\Override] - public function require(Path $path): Maybe - { - $path = $path->toString(); - - if (!\file_exists($path) || \is_dir($path)) { - /** @var Maybe */ - return Maybe::nothing(); - } - - /** - * @psalm-suppress UnresolvableInclude - * @psalm-suppress MixedArgument - * @var Maybe - */ - return Maybe::just(require $path); - } - - #[\Override] - public function watch(Path $path): Ping - { - return ($this->watch)($path); - } - - #[\Override] - public function temporary(Sequence $chunks): Attempt - { - return $this - ->config - ->io() - ->files() - ->temporary(Sequence::of()) - ->flatMap( - static fn($tmp) => $chunks - ->sink($tmp->push()->chunk(...)) - ->attempt( - static fn($push, $chunk) => $chunk - ->attempt(static fn() => new \RuntimeException('Failed to load chunk')) - ->flatMap($push) - ->map(static fn() => $push), - ) - ->map(static fn() => $tmp->read()), - ) - ->map(Content::io(...)); - } -} diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index 4016d6a..f9c1648 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -57,7 +57,7 @@ public function clock(): Clock #[\Override] public function filesystem(): Filesystem { - return $this->filesystem ??= Filesystem\Generic::of( + return $this->filesystem ??= Filesystem::of( $this->control()->processes(), $this->config, ); diff --git a/tests/Filesystem/GenericTest.php b/tests/FilesystemTest.php similarity index 87% rename from tests/Filesystem/GenericTest.php rename to tests/FilesystemTest.php index 3b604e6..057d218 100644 --- a/tests/Filesystem/GenericTest.php +++ b/tests/FilesystemTest.php @@ -1,10 +1,9 @@ assertInstanceOf( - Filesystem::class, - Generic::of( - Factory::build()->control()->processes(), - Config::of(), - ), - ); - } - public function testMount() { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -56,7 +44,7 @@ public function testMount() public function testMountingTheSamePathTwiceReturnsTheSameAdapter() { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -68,7 +56,7 @@ public function testMountingTheSamePathTwiceReturnsTheSameAdapter() public function testContainsFile() { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -81,7 +69,7 @@ public function testContainsFile() public function testContainsDirectory() { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -94,7 +82,7 @@ public function testContainsDirectory() public function testWatch() { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -107,7 +95,7 @@ public function testRequireUnknownFile(): BlackBox\Proof return $this ->forAll(FPath::any()) ->prove(function($path) { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -121,7 +109,7 @@ public function testRequireUnknownFile(): BlackBox\Proof public function testRequireFile() { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -137,7 +125,7 @@ public function testCreateTemporaryFile() $this ->forAll(Set::sequence(Set::strings()->unicode())) ->then(function($chunks) { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); @@ -169,7 +157,7 @@ public function testCreateTemporaryFileFailure() Set::sequence(Set::strings()->unicode())->between(0, 20), // upper bound to fit in memory ) ->then(function($leading, $trailing) { - $filesystem = Generic::of( + $filesystem = Filesystem::of( Factory::build()->control()->processes(), Config::of(), ); diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystem/UnixTest.php index 134b646..0c9d127 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystem/UnixTest.php @@ -26,7 +26,7 @@ public function testInterface() $os = Unix::of(Config::of()->withClock($clock)); $this->assertSame($clock, $os->clock()); - $this->assertInstanceOf(Filesystem\Generic::class, $os->filesystem()); + $this->assertInstanceOf(Filesystem::class, $os->filesystem()); $this->assertInstanceOf(ServerStatus::class, $os->status()); $this->assertInstanceOf(ServerControl::class, $os->control()); $this->assertInstanceOf(Ports::class, $os->ports()); diff --git a/tests/Filesystem/fixture.php b/tests/fixture.php similarity index 100% rename from tests/Filesystem/fixture.php rename to tests/fixture.php From a8aaab429b2ad2ad1fae771f99902640003fd01c Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:55:23 +0200 Subject: [PATCH 33/41] make Signals a final class --- CHANGELOG.md | 1 + src/CurrentProcess/Generic.php | 4 +- src/CurrentProcess/Signals.php | 25 +++++++++++-- src/CurrentProcess/Signals/Wrapper.php | 37 ------------------- tests/CurrentProcess/GenericTest.php | 2 +- .../WrapperTest.php => SignalsTest.php} | 21 +++-------- 6 files changed, 31 insertions(+), 59 deletions(-) delete mode 100644 src/CurrentProcess/Signals/Wrapper.php rename tests/CurrentProcess/{Signals/WrapperTest.php => SignalsTest.php} (79%) diff --git a/CHANGELOG.md b/CHANGELOG.md index af0576b..90db8fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ - `Innmind\OperatingSystem\Remote` is now a final class - `Innmind\OperatingSystem\Ports` is now a final class - `Innmind\OperatingSystem\Filesystem` is now a final class +- `Innmind\OperatingSystem\CurrentProcess\Signals` is now a final class ### Fixed diff --git a/src/CurrentProcess/Generic.php b/src/CurrentProcess/Generic.php index 71ea177..77af6be 100644 --- a/src/CurrentProcess/Generic.php +++ b/src/CurrentProcess/Generic.php @@ -14,7 +14,7 @@ final class Generic implements CurrentProcess { private Halt $halt; - private ?Signals\Wrapper $signals = null; + private ?Signals $signals = null; private function __construct(Halt $halt) { @@ -44,7 +44,7 @@ public function id(): Attempt #[\Override] public function signals(): Signals { - return $this->signals ??= Signals\Wrapper::of(new Handler); + return $this->signals ??= Signals::of(new Handler); } #[\Override] diff --git a/src/CurrentProcess/Signals.php b/src/CurrentProcess/Signals.php index b5dcfac..74b0d44 100644 --- a/src/CurrentProcess/Signals.php +++ b/src/CurrentProcess/Signals.php @@ -4,19 +4,38 @@ namespace Innmind\OperatingSystem\CurrentProcess; use Innmind\Signals\{ + Handler, Signal, Info, }; -interface Signals +final class Signals { + private Handler $handler; + + private function __construct(Handler $handler) + { + $this->handler = $handler; + } + + public static function of(Handler $handler): self + { + return new self($handler); + } + /** * @param callable(Signal, Info): void $listener */ - public function listen(Signal $signal, callable $listener): void; + public function listen(Signal $signal, callable $listener): void + { + $this->handler->listen($signal, $listener); + } /** * @param callable(Signal, Info): void $listener */ - public function remove(callable $listener): void; + public function remove(callable $listener): void + { + $this->handler->remove($listener); + } } diff --git a/src/CurrentProcess/Signals/Wrapper.php b/src/CurrentProcess/Signals/Wrapper.php deleted file mode 100644 index 3539de3..0000000 --- a/src/CurrentProcess/Signals/Wrapper.php +++ /dev/null @@ -1,37 +0,0 @@ -handler = $handler; - } - - public static function of(Handler $handler): self - { - return new self($handler); - } - - #[\Override] - public function listen(Signal $signal, callable $listener): void - { - $this->handler->listen($signal, $listener); - } - - #[\Override] - public function remove(callable $listener): void - { - $this->handler->remove($listener); - } -} diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcess/GenericTest.php index 7b5c6ba..fce6bcc 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcess/GenericTest.php @@ -68,7 +68,7 @@ public function testSignals() { $process = Generic::of(Halt\Usleep::new()); - $this->assertInstanceOf(Signals\Wrapper::class, $process->signals()); + $this->assertInstanceOf(Signals::class, $process->signals()); $this->assertSame($process->signals(), $process->signals()); } diff --git a/tests/CurrentProcess/Signals/WrapperTest.php b/tests/CurrentProcess/SignalsTest.php similarity index 79% rename from tests/CurrentProcess/Signals/WrapperTest.php rename to tests/CurrentProcess/SignalsTest.php index 304b96b..a3130ec 100644 --- a/tests/CurrentProcess/Signals/WrapperTest.php +++ b/tests/CurrentProcess/SignalsTest.php @@ -1,31 +1,20 @@ assertInstanceOf( - Signals::class, - Wrapper::of(new Handler), - ); - } - public function testListen() { - $signals = Wrapper::of(new Handler); + $signals = Signals::of(new Handler); $order = []; $count = 0; @@ -50,7 +39,7 @@ public function testListen() public function testRemoveSignal() { - $signals = Wrapper::of(new Handler); + $signals = Signals::of(new Handler); $order = []; $count = 0; From 0fb5dc60f8292a512a8d081a42da930e192a961d Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 10:58:31 +0200 Subject: [PATCH 34/41] make CurrentProcess a final class --- CHANGELOG.md | 1 + src/CurrentProcess.php | 49 +++++++++++++-- src/CurrentProcess/Generic.php | 62 ------------------- src/OperatingSystem/Unix.php | 2 +- ...GenericTest.php => CurrentProcessTest.php} | 21 ++----- tests/OperatingSystem/UnixTest.php | 2 +- 6 files changed, 53 insertions(+), 84 deletions(-) delete mode 100644 src/CurrentProcess/Generic.php rename tests/{CurrentProcess/GenericTest.php => CurrentProcessTest.php} (78%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90db8fb..edcf2e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - `Innmind\OperatingSystem\Ports` is now a final class - `Innmind\OperatingSystem\Filesystem` is now a final class - `Innmind\OperatingSystem\CurrentProcess\Signals` is now a final class +- `Innmind\OperatingSystem\CurrentProcess` is now a final class ### Fixed diff --git a/src/CurrentProcess.php b/src/CurrentProcess.php index c1d0eb1..3e8b203 100644 --- a/src/CurrentProcess.php +++ b/src/CurrentProcess.php @@ -7,22 +7,61 @@ use Innmind\Server\Control\Server\Process\Pid; use Innmind\Server\Status\Server\Memory\Bytes; use Innmind\TimeContinuum\Period; +use Innmind\TimeWarp\Halt; +use Innmind\Signals\Handler; use Innmind\Immutable\{ Attempt, SideEffect, }; -interface CurrentProcess +final class CurrentProcess { + private Halt $halt; + private ?Signals $signals = null; + + private function __construct(Halt $halt) + { + $this->halt = $halt; + } + + /** + * @internal + */ + public static function of(Halt $halt): self + { + return new self($halt); + } + /** * @return Attempt */ - public function id(): Attempt; - public function signals(): Signals; + public function id(): Attempt + { + $pid = \getmypid(); + + /** @psalm-suppress ArgumentTypeCoercion */ + return match ($pid) { + false => Attempt::error(new \RuntimeException('Failed to retrieve process id')), + default => Attempt::result(new Pid($pid)), + }; + } + + public function signals(): Signals + { + return $this->signals ??= Signals::of(new Handler); + } /** * @return Attempt */ - public function halt(Period $period): Attempt; - public function memory(): Bytes; + public function halt(Period $period): Attempt + { + return ($this->halt)($period); + } + + public function memory(): Bytes + { + /** @psalm-suppress ArgumentTypeCoercion */ + return Bytes::of(\memory_get_usage()); + } } diff --git a/src/CurrentProcess/Generic.php b/src/CurrentProcess/Generic.php deleted file mode 100644 index 77af6be..0000000 --- a/src/CurrentProcess/Generic.php +++ /dev/null @@ -1,62 +0,0 @@ -halt = $halt; - } - - /** - * @internal - */ - public static function of(Halt $halt): self - { - return new self($halt); - } - - #[\Override] - public function id(): Attempt - { - $pid = \getmypid(); - - /** @psalm-suppress ArgumentTypeCoercion */ - return match ($pid) { - false => Attempt::error(new \RuntimeException('Failed to retrieve process id')), - default => Attempt::result(new Pid($pid)), - }; - } - - #[\Override] - public function signals(): Signals - { - return $this->signals ??= Signals::of(new Handler); - } - - #[\Override] - public function halt(Period $period): Attempt - { - return ($this->halt)($period); - } - - #[\Override] - public function memory(): Bytes - { - /** @psalm-suppress ArgumentTypeCoercion */ - return Bytes::of(\memory_get_usage()); - } -} diff --git a/src/OperatingSystem/Unix.php b/src/OperatingSystem/Unix.php index f9c1648..7595f2e 100644 --- a/src/OperatingSystem/Unix.php +++ b/src/OperatingSystem/Unix.php @@ -107,6 +107,6 @@ public function remote(): Remote #[\Override] public function process(): CurrentProcess { - return $this->process ??= CurrentProcess\Generic::of($this->config->halt()); + return $this->process ??= CurrentProcess::of($this->config->halt()); } } diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcessTest.php similarity index 78% rename from tests/CurrentProcess/GenericTest.php rename to tests/CurrentProcessTest.php index fce6bcc..8b54032 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcessTest.php @@ -1,12 +1,11 @@ assertInstanceOf( - CurrentProcess::class, - Generic::of(Halt\Usleep::new()), - ); - } - public function testId() { - $process = Generic::of(Halt\Usleep::new()); + $process = CurrentProcess::of(Halt\Usleep::new()); $this->assertInstanceOf(Pid::class, $process->id()->unwrap()); $this->assertSame( @@ -66,7 +57,7 @@ public function testHalt(): BlackBox\Proof public function testSignals() { - $process = Generic::of(Halt\Usleep::new()); + $process = CurrentProcess::of(Halt\Usleep::new()); $this->assertInstanceOf(Signals::class, $process->signals()); $this->assertSame($process->signals(), $process->signals()); @@ -74,7 +65,7 @@ public function testSignals() public function testMemory() { - $process = Generic::of(Halt\Usleep::new()); + $process = CurrentProcess::of(Halt\Usleep::new()); $this->assertInstanceOf(Bytes::class, $process->memory()); $this->assertGreaterThan(3_000_000, $process->memory()->toInt()); // ~3MB diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystem/UnixTest.php index 0c9d127..b297305 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystem/UnixTest.php @@ -32,7 +32,7 @@ public function testInterface() $this->assertInstanceOf(Ports::class, $os->ports()); $this->assertInstanceOf(Sockets::class, $os->sockets()); $this->assertInstanceOf(Remote::class, $os->remote()); - $this->assertInstanceOf(CurrentProcess\Generic::class, $os->process()); + $this->assertInstanceOf(CurrentProcess::class, $os->process()); $this->assertSame($os->filesystem(), $os->filesystem()); $this->assertSame($os->status(), $os->status()); $this->assertSame($os->control(), $os->control()); From da695e5bf532fda4a2c72d4b6bb7263329519304 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:05:27 +0200 Subject: [PATCH 35/41] remove useless indirection --- src/OperatingSystem.php | 66 +++++++---- src/OperatingSystem/Implementation.php | 35 ------ src/OperatingSystem/Unix.php | 112 ------------------ tests/CurrentProcessTest.php | 6 +- .../UnixTest.php => OperatingSystemTest.php} | 12 +- tests/RemoteTest.php | 10 +- 6 files changed, 57 insertions(+), 184 deletions(-) delete mode 100644 src/OperatingSystem/Implementation.php delete mode 100644 src/OperatingSystem/Unix.php rename tests/{OperatingSystem/UnixTest.php => OperatingSystemTest.php} (85%) diff --git a/src/OperatingSystem.php b/src/OperatingSystem.php index f71137a..758c2e8 100644 --- a/src/OperatingSystem.php +++ b/src/OperatingSystem.php @@ -3,24 +3,35 @@ namespace Innmind\OperatingSystem; -use Innmind\OperatingSystem\OperatingSystem\{ - Implementation, - Unix, +use Innmind\Server\Status\{ + Server as ServerStatus, + ServerFactory, +}; +use Innmind\Server\Control\{ + Server as ServerControl, + Servers, }; -use Innmind\Server\Status\Server as ServerStatus; -use Innmind\Server\Control\Server as ServerControl; use Innmind\TimeContinuum\Clock; final class OperatingSystem { - private function __construct( - private Implementation $implementation, - ) { + private Config $config; + private ?Filesystem $filesystem = null; + private ?ServerStatus $status = null; + private ?ServerControl $control = null; + private ?Ports $ports = null; + private ?Sockets $sockets = null; + private ?Remote $remote = null; + private ?CurrentProcess $process = null; + + private function __construct(Config $config) + { + $this->config = $config; } public static function new(?Config $config = null): self { - return new self(Unix::of($config)); + return new self($config ?? Config::of()); } /** @@ -31,51 +42,60 @@ public static function new(?Config $config = null): self */ public function map(callable $map): self { - return new self($this->implementation->map( - static fn($implementation, $config) => $map( - new self($implementation), - $config, - )->implementation, - )); + return $map($this, $this->config); } public function clock(): Clock { - return $this->implementation->clock(); + return $this->config->clock(); } public function filesystem(): Filesystem { - return $this->implementation->filesystem(); + return $this->filesystem ??= Filesystem::of( + $this->control()->processes(), + $this->config, + ); } public function status(): ServerStatus { - return $this->implementation->status(); + return $this->status ??= $this->config->serverStatusMapper()(ServerFactory::build( + $this->clock(), + $this->control(), + $this->config->environmentPath(), + )); } public function control(): ServerControl { - return $this->implementation->control(); + return $this->control ??= $this->config->serverControlMapper()(Servers\Unix::of( + $this->clock(), + $this->config->io(), + $this->config->halt(), + )); } public function ports(): Ports { - return $this->implementation->ports(); + return $this->ports ??= Ports::of($this->config); } public function sockets(): Sockets { - return $this->implementation->sockets(); + return $this->sockets ??= Sockets::of($this->config); } public function remote(): Remote { - return $this->implementation->remote(); + return $this->remote ??= Remote::of( + $this->control(), + $this->config, + ); } public function process(): CurrentProcess { - return $this->implementation->process(); + return $this->process ??= CurrentProcess::of($this->config->halt()); } } diff --git a/src/OperatingSystem/Implementation.php b/src/OperatingSystem/Implementation.php deleted file mode 100644 index a2799cb..0000000 --- a/src/OperatingSystem/Implementation.php +++ /dev/null @@ -1,35 +0,0 @@ -config = $config; - } - - public static function of(?Config $config = null): self - { - return new self($config ?? Config::of()); - } - - #[\Override] - public function map(callable $map): Implementation - { - return $map($this, $this->config); - } - - #[\Override] - public function clock(): Clock - { - return $this->config->clock(); - } - - #[\Override] - public function filesystem(): Filesystem - { - return $this->filesystem ??= Filesystem::of( - $this->control()->processes(), - $this->config, - ); - } - - #[\Override] - public function status(): ServerStatus - { - return $this->status ??= $this->config->serverStatusMapper()(ServerFactory::build( - $this->clock(), - $this->control(), - $this->config->environmentPath(), - )); - } - - #[\Override] - public function control(): ServerControl - { - return $this->control ??= $this->config->serverControlMapper()(Servers\Unix::of( - $this->clock(), - $this->config->io(), - $this->config->halt(), - )); - } - - #[\Override] - public function ports(): Ports - { - return $this->ports ??= Ports::of($this->config); - } - - #[\Override] - public function sockets(): Sockets - { - return $this->sockets ??= Sockets::of($this->config); - } - - #[\Override] - public function remote(): Remote - { - return $this->remote ??= Remote::of( - $this->control(), - $this->config, - ); - } - - #[\Override] - public function process(): CurrentProcess - { - return $this->process ??= CurrentProcess::of($this->config->halt()); - } -} diff --git a/tests/CurrentProcessTest.php b/tests/CurrentProcessTest.php index 8b54032..d76c543 100644 --- a/tests/CurrentProcessTest.php +++ b/tests/CurrentProcessTest.php @@ -6,7 +6,7 @@ use Innmind\OperatingSystem\{ CurrentProcess, CurrentProcess\Signals, - OperatingSystem\Unix, + OperatingSystem, Config, }; use Innmind\Server\Control\Server\Process\Pid; @@ -40,8 +40,8 @@ public function testHalt(): BlackBox\Proof { return $this ->forAll(Set::of( - Unix::of(), - Unix::of(Config::of()->map(Config\Logger::psr(new NullLogger))), + OperatingSystem::new(), + OperatingSystem::new(Config::of()->map(Config\Logger::psr(new NullLogger))), )) ->prove(function($os) { $process = $os->process(); diff --git a/tests/OperatingSystem/UnixTest.php b/tests/OperatingSystemTest.php similarity index 85% rename from tests/OperatingSystem/UnixTest.php rename to tests/OperatingSystemTest.php index b297305..e2d53ae 100644 --- a/tests/OperatingSystem/UnixTest.php +++ b/tests/OperatingSystemTest.php @@ -1,10 +1,10 @@ withClock($clock)); + $os = OperatingSystem::new(Config::of()->withClock($clock)); $this->assertSame($clock, $os->clock()); $this->assertInstanceOf(Filesystem::class, $os->filesystem()); @@ -44,8 +44,8 @@ public function testInterface() public function testMap() { - $os = Unix::of($config = Config::of()); - $expected = Unix::of(); + $os = OperatingSystem::new($config = Config::of()); + $expected = OperatingSystem::new(); $result = $os->map(function($os_, $config_) use ($os, $config, $expected) { $this->assertSame($os, $os_); diff --git a/tests/RemoteTest.php b/tests/RemoteTest.php index d8f7bc7..de9f1e1 100644 --- a/tests/RemoteTest.php +++ b/tests/RemoteTest.php @@ -5,7 +5,7 @@ use Innmind\OperatingSystem\{ Remote, - OperatingSystem\Unix, + OperatingSystem, Config, Factory, }; @@ -115,8 +115,8 @@ public function testHttp(): BlackBox\Proof { return $this ->forAll(Set::of( - Unix::of(), - Unix::of(Config::of()->map(Config\Logger::psr(new NullLogger))), + OperatingSystem::new(), + OperatingSystem::new(Config::of()->map(Config\Logger::psr(new NullLogger))), )) ->prove(function($os) { $remote = $os->remote(); @@ -133,8 +133,8 @@ public function testSql(): BlackBox\Proof ->forAll( FUrl::any(), Set::of( - Unix::of(), - Unix::of(Config::of()->map(Config\Logger::psr(new NullLogger))), + OperatingSystem::new(), + OperatingSystem::new(Config::of()->map(Config\Logger::psr(new NullLogger))), ), ) ->prove(function($server, $os) { From 9a13bbea4c2b9f29cf535ebdd0cbfff7e81b84f8 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:09:09 +0200 Subject: [PATCH 36/41] simplify OperatingSystem::map() by only modifying the Config --- CHANGELOG.md | 1 + src/OperatingSystem.php | 4 ++-- tests/OperatingSystemTest.php | 8 +++----- tests/Remote/ResilientTest.php | 5 +---- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edcf2e6..19cfd3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ - `Innmind\OperatingSystem\Filesystem` is now a final class - `Innmind\OperatingSystem\CurrentProcess\Signals` is now a final class - `Innmind\OperatingSystem\CurrentProcess` is now a final class +- `Innmind\OperatingSystem\OperatingSystem::map()` callable must now return a `Config` ### Fixed diff --git a/src/OperatingSystem.php b/src/OperatingSystem.php index 758c2e8..627e7d2 100644 --- a/src/OperatingSystem.php +++ b/src/OperatingSystem.php @@ -38,11 +38,11 @@ public static function new(?Config $config = null): self * This method allows to change the underlying OS implementation while being * able to keep any decorators on top of it. * - * @param callable(self, Config): self $map + * @param callable(Config): Config $map */ public function map(callable $map): self { - return $map($this, $this->config); + return new self($map($this->config)); } public function clock(): Clock diff --git a/tests/OperatingSystemTest.php b/tests/OperatingSystemTest.php index e2d53ae..a6ba479 100644 --- a/tests/OperatingSystemTest.php +++ b/tests/OperatingSystemTest.php @@ -45,15 +45,13 @@ public function testInterface() public function testMap() { $os = OperatingSystem::new($config = Config::of()); - $expected = OperatingSystem::new(); - $result = $os->map(function($os_, $config_) use ($os, $config, $expected) { - $this->assertSame($os, $os_); + $result = $os->map(function($config_) use ($config) { $this->assertSame($config, $config_); - return $expected; + return Config::of(); }); - $this->assertSame($expected, $result); + $this->assertNotSame($os, $result); } } diff --git a/tests/Remote/ResilientTest.php b/tests/Remote/ResilientTest.php index 63d3799..6a9f805 100644 --- a/tests/Remote/ResilientTest.php +++ b/tests/Remote/ResilientTest.php @@ -4,7 +4,6 @@ namespace Tests\Innmind\OperatingSystem\Remote; use Innmind\OperatingSystem\{ - OperatingSystem, Config, Factory, }; @@ -23,9 +22,7 @@ public function setUp(): void public function testHttp() { $os = $this->os->map( - static fn($_, $config) => OperatingSystem::new( - $config->map(Config\Resilient::new()), - ), + Config\Resilient::new(), ); $this->assertInstanceOf(ExponentialBackoff::class, $os->remote()->http()); From 9fdbfe7ec9b13be5941000cb51d234d9aa79793c Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:18:40 +0200 Subject: [PATCH 37/41] defer the access to the config in mappers to give access to the latest config --- src/Config.php | 101 ++++++++++++++++++++++++--------------- src/Config/Resilient.php | 2 +- src/Filesystem.php | 1 + src/OperatingSystem.php | 26 ++++++---- src/Remote.php | 15 +++--- 5 files changed, 90 insertions(+), 55 deletions(-) diff --git a/src/Config.php b/src/Config.php index c53b7de..ef0e095 100644 --- a/src/Config.php +++ b/src/Config.php @@ -28,16 +28,16 @@ final class Config { /** - * @param \Closure(Clock): Clock $mapClock - * @param \Closure(Halt): Halt $mapHalt - * @param \Closure(HttpTransport): HttpTransport $mapHttpTransport + * @param \Closure(Clock, self): Clock $mapClock + * @param \Closure(Halt, self): Halt $mapHalt + * @param \Closure(HttpTransport, self): HttpTransport $mapHttpTransport * @param \Closure(Url): AccessLayer\Connection $sql - * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $mapSql - * @param \Closure(Control\Server): Control\Server $mapServerControl - * @param \Closure(Status\Server): Status\Server $mapServerStatus - * @param \Closure(Watch): Watch $mapFileWatch + * @param \Closure(AccessLayer\Connection, self): AccessLayer\Connection $mapSql + * @param \Closure(Control\Server, self): Control\Server $mapServerControl + * @param \Closure(Status\Server, self): Status\Server $mapServerStatus + * @param \Closure(Watch, self): Watch $mapFileWatch * @param \Closure(Path, self): Attempt $filesystem - * @param \Closure(Filesystem): Filesystem $mapFilesystem + * @param \Closure(Filesystem, self): Filesystem $mapFilesystem */ private function __construct( private Clock $clock, @@ -65,27 +65,27 @@ public static function of(): self static fn(Clock $clock) => $clock, IO::fromAmbientAuthority(), Halt\Usleep::new(), - static fn(Halt $halt) => $halt, + static fn(Halt $halt, self $config) => $halt, EnvironmentPath::of(match ($path = \getenv('PATH')) { false => '', default => $path, }), null, - static fn(HttpTransport $transport) => $transport, + static fn(HttpTransport $transport, self $config) => $transport, static fn(Url $server) => AccessLayer\Connection\Lazy::of( static fn() => AccessLayer\Connection\PDO::of($server), ), - static fn(AccessLayer\Connection $connection) => $connection, - static fn(Control\Server $server) => $server, - static fn(Status\Server $server) => $server, - static fn(Watch $watch) => $watch, + static fn(AccessLayer\Connection $connection, self $config) => $connection, + static fn(Control\Server $server, self $config) => $server, + static fn(Status\Server $server, self $config) => $server, + static fn(Watch $watch, self $config) => $watch, static fn(Path $path, self $config) => Attempt::of( static fn() => Filesystem\Filesystem::mount( $path, $config->io(), )->withCaseSensitivity(CaseSensitivity::sensitive), ), - static fn(Filesystem $filesystem) => $filesystem, + static fn(Filesystem $filesystem, self $config) => $filesystem, ); } @@ -127,7 +127,7 @@ public function withClock(Clock $clock): self /** * @psalm-mutation-free * - * @param \Closure(Clock): Clock $map + * @param \Closure(Clock, self): Clock $map */ public function mapClock(\Closure $map): self { @@ -135,7 +135,10 @@ public function mapClock(\Closure $map): self return new self( $this->clock, - static fn(Clock $clock) => $map($previous($clock)), + static fn(Clock $clock, self $config) => $map( + $previous($clock, $config), + $config, + ), $this->io, $this->halt, $this->mapHalt, @@ -179,7 +182,7 @@ public function haltProcessVia(Halt $halt): self /** * @psalm-mutation-free * - * @param \Closure(Halt): Halt $map + * @param \Closure(Halt, self): Halt $map */ public function mapHalt(\Closure $map): self { @@ -191,7 +194,10 @@ public function mapHalt(\Closure $map): self $this->mapClock, $this->io, $this->halt, - static fn(Halt $halt) => $map($previous($halt)), + static fn(Halt $halt, self $config) => $map( + $previous($halt, $config), + $config, + ), $this->path, $this->httpTransport, $this->mapHttpTransport, @@ -256,7 +262,7 @@ public function useHttpTransport(HttpTransport $transport): self /** * @psalm-mutation-free * - * @param \Closure(HttpTransport): HttpTransport $map + * @param \Closure(HttpTransport, self): HttpTransport $map */ public function mapHttpTransport(\Closure $map): self { @@ -270,7 +276,10 @@ public function mapHttpTransport(\Closure $map): self $this->mapHalt, $this->path, $this->httpTransport, - static fn(HttpTransport $transport) => $map($previous($transport)), + static fn(HttpTransport $transport, self $config) => $map( + $previous($transport, $config), + $config, + ), $this->sql, $this->mapSql, $this->mapServerControl, @@ -310,7 +319,7 @@ public function openSQLConnectionVia(\Closure $sql): self /** * @psalm-mutation-free * - * @param \Closure(AccessLayer\Connection): AccessLayer\Connection $map + * @param \Closure(AccessLayer\Connection, self): AccessLayer\Connection $map */ public function mapSQLConnection(\Closure $map): self { @@ -326,7 +335,10 @@ public function mapSQLConnection(\Closure $map): self $this->httpTransport, $this->mapHttpTransport, $this->sql, - static fn(AccessLayer\Connection $connection) => $map($previous($connection)), + static fn(AccessLayer\Connection $connection, self $config) => $map( + $previous($connection, $config), + $config, + ), $this->mapServerControl, $this->mapServerStatus, $this->mapFileWatch, @@ -338,7 +350,7 @@ public function mapSQLConnection(\Closure $map): self /** * @psalm-mutation-free * - * @param \Closure(Control\Server): Control\Server $map + * @param \Closure(Control\Server, self): Control\Server $map */ public function mapServerControl(\Closure $map): self { @@ -355,7 +367,10 @@ public function mapServerControl(\Closure $map): self $this->mapHttpTransport, $this->sql, $this->mapSql, - static fn(Control\Server $server) => $map($previous($server)), + static fn(Control\Server $server, self $config) => $map( + $previous($server, $config), + $config, + ), $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, @@ -366,7 +381,7 @@ public function mapServerControl(\Closure $map): self /** * @psalm-mutation-free * - * @param \Closure(Status\Server): Status\Server $map + * @param \Closure(Status\Server, self): Status\Server $map */ public function mapServerStatus(\Closure $map): self { @@ -384,7 +399,10 @@ public function mapServerStatus(\Closure $map): self $this->sql, $this->mapSql, $this->mapServerControl, - static fn(Status\Server $server) => $map($previous($server)), + static fn(Status\Server $server, self $config) => $map( + $previous($server, $config), + $config, + ), $this->mapFileWatch, $this->filesystem, $this->mapFilesystem, @@ -394,7 +412,7 @@ public function mapServerStatus(\Closure $map): self /** * @psalm-mutation-free * - * @param \Closure(Watch): Watch $map + * @param \Closure(Watch, self): Watch $map */ public function mapFileWatch(\Closure $map): self { @@ -413,7 +431,10 @@ public function mapFileWatch(\Closure $map): self $this->mapSql, $this->mapServerControl, $this->mapServerStatus, - static fn(Watch $watch) => $map($previous($watch)), + static fn(Watch $watch, self $config) => $map( + $previous($watch, $config), + $config, + ), $this->filesystem, $this->mapFilesystem, ); @@ -448,7 +469,7 @@ public function mountFilesystemVia(\Closure $filesystem): self /** * @psalm-mutation-free * - * @param \Closure(Filesystem): Filesystem $map + * @param \Closure(Filesystem, self): Filesystem $map */ public function mapFilesystem(\Closure $map): self { @@ -469,7 +490,10 @@ public function mapFilesystem(\Closure $map): self $this->mapServerStatus, $this->mapFileWatch, $this->filesystem, - static fn(Filesystem $filesystem) => $map($previous($filesystem)), + static fn(Filesystem $filesystem, self $config) => $map( + $previous($filesystem, $config), + $config, + ), ); } @@ -478,7 +502,7 @@ public function mapFilesystem(\Closure $map): self */ public function clock(): Clock { - return ($this->mapClock)($this->clock); + return ($this->mapClock)($this->clock, $this); } /** @@ -489,7 +513,7 @@ public function clock(): Clock public function filesystem(Path $path): Attempt { return ($this->filesystem)($path, $this)->map( - $this->mapFilesystem, + fn($adapter) => ($this->mapFilesystem)($adapter, $this), ); } @@ -506,7 +530,7 @@ public function io(): IO */ public function halt(): Halt { - return ($this->mapHalt)($this->halt); + return ($this->mapHalt)($this->halt, $this); } /** @@ -527,7 +551,7 @@ public function httpTransport(): HttpTransport $this->io, ); - return ($this->mapHttpTransport)($transport); + return ($this->mapHttpTransport)($transport, $this); } /** @@ -537,13 +561,14 @@ public function sql(Url $url): AccessLayer\Connection { return ($this->mapSql)( ($this->sql)($url), + $this, ); } /** * @internal * - * @return \Closure(Control\Server): Control\Server + * @return \Closure(Control\Server, self): Control\Server */ public function serverControlMapper(): \Closure { @@ -553,7 +578,7 @@ public function serverControlMapper(): \Closure /** * @internal * - * @return \Closure(Status\Server): Status\Server + * @return \Closure(Status\Server, self): Status\Server */ public function serverStatusMapper(): \Closure { @@ -563,7 +588,7 @@ public function serverStatusMapper(): \Closure /** * @internal * - * @return \Closure(Watch): Watch + * @return \Closure(Watch, self): Watch */ public function fileWatchMapper(): \Closure { diff --git a/src/Config/Resilient.php b/src/Config/Resilient.php index 954d1de..dd04ba6 100644 --- a/src/Config/Resilient.php +++ b/src/Config/Resilient.php @@ -13,7 +13,7 @@ enum Resilient public function __invoke(Config $config): Config { return $config - ->mapHttpTransport(static fn($transport) => ExponentialBackoff::of( + ->mapHttpTransport(static fn($transport, $config) => ExponentialBackoff::of( $transport, $config->halt(), )); diff --git a/src/Filesystem.php b/src/Filesystem.php index d1da96a..a01ab48 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -32,6 +32,7 @@ private function __construct(Processes $processes, Config $config) { $this->watch = $config->fileWatchMapper()( Factory::build($processes, $config->halt()), + $config, ); $this->config = $config; /** @var \WeakMap */ diff --git a/src/OperatingSystem.php b/src/OperatingSystem.php index 627e7d2..e85e46a 100644 --- a/src/OperatingSystem.php +++ b/src/OperatingSystem.php @@ -60,20 +60,26 @@ public function filesystem(): Filesystem public function status(): ServerStatus { - return $this->status ??= $this->config->serverStatusMapper()(ServerFactory::build( - $this->clock(), - $this->control(), - $this->config->environmentPath(), - )); + return $this->status ??= $this->config->serverStatusMapper()( + ServerFactory::build( + $this->clock(), + $this->control(), + $this->config->environmentPath(), + ), + $this->config, + ); } public function control(): ServerControl { - return $this->control ??= $this->config->serverControlMapper()(Servers\Unix::of( - $this->clock(), - $this->config->io(), - $this->config->halt(), - )); + return $this->control ??= $this->config->serverControlMapper()( + Servers\Unix::of( + $this->clock(), + $this->config->io(), + $this->config->halt(), + ), + $this->config, + ); } public function ports(): Ports diff --git a/src/Remote.php b/src/Remote.php index 54b52fd..18f0b31 100644 --- a/src/Remote.php +++ b/src/Remote.php @@ -48,12 +48,15 @@ public function ssh(Url $server): Server $port = $server->authority()->port(); } - return $this->config->serverControlMapper()(Servers\Remote::of( - $this->server, - $server->authority()->userInformation()->user(), - $server->authority()->host(), - $port, - )); + return $this->config->serverControlMapper()( + Servers\Remote::of( + $this->server, + $server->authority()->userInformation()->user(), + $server->authority()->host(), + $port, + ), + $this->config, + ); } /** From 77491c93f8d445b20c569a4bcb68d40f969e36dd Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:21:31 +0200 Subject: [PATCH 38/41] do not expose internal details of the config --- src/Config.php | 18 ++++++------------ src/Filesystem.php | 3 +-- src/OperatingSystem.php | 6 ++---- src/Remote.php | 3 +-- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/Config.php b/src/Config.php index ef0e095..82795c1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -567,31 +567,25 @@ public function sql(Url $url): AccessLayer\Connection /** * @internal - * - * @return \Closure(Control\Server, self): Control\Server */ - public function serverControlMapper(): \Closure + public function serverControl(Control\Server $server): Control\Server { - return $this->mapServerControl; + return ($this->mapServerControl)($server, $this); } /** * @internal - * - * @return \Closure(Status\Server, self): Status\Server */ - public function serverStatusMapper(): \Closure + public function serverStatus(Status\Server $server): Status\Server { - return $this->mapServerStatus; + return ($this->mapServerStatus)($server, $this); } /** * @internal - * - * @return \Closure(Watch, self): Watch */ - public function fileWatchMapper(): \Closure + public function fileWatch(Watch $watch): Watch { - return $this->mapFileWatch; + return ($this->mapFileWatch)($watch, $this); } } diff --git a/src/Filesystem.php b/src/Filesystem.php index a01ab48..9b378ac 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -30,9 +30,8 @@ final class Filesystem private function __construct(Processes $processes, Config $config) { - $this->watch = $config->fileWatchMapper()( + $this->watch = $config->fileWatch( Factory::build($processes, $config->halt()), - $config, ); $this->config = $config; /** @var \WeakMap */ diff --git a/src/OperatingSystem.php b/src/OperatingSystem.php index e85e46a..d2c7b83 100644 --- a/src/OperatingSystem.php +++ b/src/OperatingSystem.php @@ -60,25 +60,23 @@ public function filesystem(): Filesystem public function status(): ServerStatus { - return $this->status ??= $this->config->serverStatusMapper()( + return $this->status ??= $this->config->serverStatus( ServerFactory::build( $this->clock(), $this->control(), $this->config->environmentPath(), ), - $this->config, ); } public function control(): ServerControl { - return $this->control ??= $this->config->serverControlMapper()( + return $this->control ??= $this->config->serverControl( Servers\Unix::of( $this->clock(), $this->config->io(), $this->config->halt(), ), - $this->config, ); } diff --git a/src/Remote.php b/src/Remote.php index 18f0b31..e20ef77 100644 --- a/src/Remote.php +++ b/src/Remote.php @@ -48,14 +48,13 @@ public function ssh(Url $server): Server $port = $server->authority()->port(); } - return $this->config->serverControlMapper()( + return $this->config->serverControl( Servers\Remote::of( $this->server, $server->authority()->userInformation()->user(), $server->authority()->host(), $port, ), - $this->config, ); } From 7eb35a14d11d78879c3e242ea88613012b53e464 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:24:23 +0200 Subject: [PATCH 39/41] rename Config::of() to ::new() to better identify it is brand new --- CHANGELOG.md | 1 + src/Config.php | 2 +- src/OperatingSystem.php | 2 +- tests/CurrentProcessTest.php | 2 +- tests/FactoryTest.php | 4 ++-- tests/FilesystemTest.php | 18 +++++++++--------- tests/OperatingSystemTest.php | 6 +++--- tests/PortsTest.php | 2 +- tests/RemoteTest.php | 12 ++++++------ tests/SocketsTest.php | 6 +++--- 10 files changed, 28 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19cfd3c..e31b6f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ - `Innmind\OperatingSystem\CurrentProcess\Signals` is now a final class - `Innmind\OperatingSystem\CurrentProcess` is now a final class - `Innmind\OperatingSystem\OperatingSystem::map()` callable must now return a `Config` +- `Innmind\OperatingSystem\Config::of()` has been renamed `::new()` ### Fixed diff --git a/src/Config.php b/src/Config.php index 82795c1..d8b279d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -58,7 +58,7 @@ private function __construct( ) { } - public static function of(): self + public static function new(): self { return new self( Clock::live(), diff --git a/src/OperatingSystem.php b/src/OperatingSystem.php index d2c7b83..406f7d0 100644 --- a/src/OperatingSystem.php +++ b/src/OperatingSystem.php @@ -31,7 +31,7 @@ private function __construct(Config $config) public static function new(?Config $config = null): self { - return new self($config ?? Config::of()); + return new self($config ?? Config::new()); } /** diff --git a/tests/CurrentProcessTest.php b/tests/CurrentProcessTest.php index d76c543..5b819d6 100644 --- a/tests/CurrentProcessTest.php +++ b/tests/CurrentProcessTest.php @@ -41,7 +41,7 @@ public function testHalt(): BlackBox\Proof return $this ->forAll(Set::of( OperatingSystem::new(), - OperatingSystem::new(Config::of()->map(Config\Logger::psr(new NullLogger))), + OperatingSystem::new(Config::new()->map(Config\Logger::psr(new NullLogger))), )) ->prove(function($os) { $process = $os->process(); diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 6dfd32d..1df7a75 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -27,7 +27,7 @@ public function testBuild() { $clock = Clock::live(); - $os = Factory::build(Config::of()->withClock($clock)); + $os = Factory::build(Config::new()->withClock($clock)); $this->assertInstanceOf(OperatingSystem::class, $os); $this->assertSame($clock, $os->clock()); @@ -52,7 +52,7 @@ public function testPersistingFileOnCaseInsensitiveFilesystem() (new FS)->remove($path); $os = Factory::build( - Config::of()->mountFilesystemVia( + Config::new()->mountFilesystemVia( static fn($path, $config) => Attempt::of( static fn() => Filesystem::mount( $path, diff --git a/tests/FilesystemTest.php b/tests/FilesystemTest.php index 057d218..20b4ae6 100644 --- a/tests/FilesystemTest.php +++ b/tests/FilesystemTest.php @@ -34,7 +34,7 @@ public function testMount() { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $adapter = $filesystem->mount(Path::of('/tmp/'))->unwrap(); @@ -46,7 +46,7 @@ public function testMountingTheSamePathTwiceReturnsTheSameAdapter() { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $adapter = $filesystem->mount(Path::of('/tmp/'))->unwrap(); @@ -58,7 +58,7 @@ public function testContainsFile() { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $this->assertFalse($filesystem->contains(Path::of('/tmp/foo'))); @@ -71,7 +71,7 @@ public function testContainsDirectory() { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $this->assertFalse($filesystem->contains(Path::of('/tmp/some-dir/'))); @@ -84,7 +84,7 @@ public function testWatch() { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $this->assertInstanceOf(Ping::class, $filesystem->watch(Path::of('/somewhere'))); @@ -97,7 +97,7 @@ public function testRequireUnknownFile(): BlackBox\Proof ->prove(function($path) { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $this->assertFalse($filesystem->require($path)->match( @@ -111,7 +111,7 @@ public function testRequireFile() { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $this->assertSame(42, $filesystem->require(Path::of(__DIR__.'/fixture.php'))->match( @@ -127,7 +127,7 @@ public function testCreateTemporaryFile() ->then(function($chunks) { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $content = $filesystem @@ -159,7 +159,7 @@ public function testCreateTemporaryFileFailure() ->then(function($leading, $trailing) { $filesystem = Filesystem::of( Factory::build()->control()->processes(), - Config::of(), + Config::new(), ); $content = $filesystem diff --git a/tests/OperatingSystemTest.php b/tests/OperatingSystemTest.php index a6ba479..6500da9 100644 --- a/tests/OperatingSystemTest.php +++ b/tests/OperatingSystemTest.php @@ -23,7 +23,7 @@ public function testInterface() { $clock = Clock::live(); - $os = OperatingSystem::new(Config::of()->withClock($clock)); + $os = OperatingSystem::new(Config::new()->withClock($clock)); $this->assertSame($clock, $os->clock()); $this->assertInstanceOf(Filesystem::class, $os->filesystem()); @@ -44,12 +44,12 @@ public function testInterface() public function testMap() { - $os = OperatingSystem::new($config = Config::of()); + $os = OperatingSystem::new($config = Config::new()); $result = $os->map(function($config_) use ($config) { $this->assertSame($config, $config_); - return Config::of(); + return Config::new(); }); $this->assertNotSame($os, $result); diff --git a/tests/PortsTest.php b/tests/PortsTest.php index 8bd6a9b..a0db87d 100644 --- a/tests/PortsTest.php +++ b/tests/PortsTest.php @@ -19,7 +19,7 @@ class PortsTest extends TestCase { public function testOpen() { - $ports = Ports::of(Config::of()); + $ports = Ports::of(Config::new()); $socket = $ports->open( Transport::tlsv12(), diff --git a/tests/RemoteTest.php b/tests/RemoteTest.php index de9f1e1..c9a5d1d 100644 --- a/tests/RemoteTest.php +++ b/tests/RemoteTest.php @@ -46,7 +46,7 @@ public function testSsh() { $remote = Remote::of( $this->server("ssh '-p' '42' 'user@my-vps' 'ls'"), - Config::of(), + Config::new(), ); $remoteServer = $remote->ssh(Url::of('ssh://user@my-vps:42/')); @@ -62,7 +62,7 @@ public function testSshLogger() { $remote = Remote::of( $this->server("ssh '-p' '42' 'user@my-vps' 'ls'"), - Config::of()->map(Config\Logger::psr(new NullLogger)), + Config::new()->map(Config\Logger::psr(new NullLogger)), ); $remoteServer = $remote->ssh(Url::of('ssh://user@my-vps:42/')); @@ -78,7 +78,7 @@ public function testSshWithoutPort() { $remote = Remote::of( $this->server("ssh 'user@my-vps' 'ls'"), - Config::of(), + Config::new(), ); $remoteServer = $remote->ssh(Url::of('ssh://user@my-vps/')); @@ -91,7 +91,7 @@ public function testSocket() { $remote = Remote::of( $this->server(), - Config::of(), + Config::new(), ); $server = Factory::build() ->ports() @@ -116,7 +116,7 @@ public function testHttp(): BlackBox\Proof return $this ->forAll(Set::of( OperatingSystem::new(), - OperatingSystem::new(Config::of()->map(Config\Logger::psr(new NullLogger))), + OperatingSystem::new(Config::new()->map(Config\Logger::psr(new NullLogger))), )) ->prove(function($os) { $remote = $os->remote(); @@ -134,7 +134,7 @@ public function testSql(): BlackBox\Proof FUrl::any(), Set::of( OperatingSystem::new(), - OperatingSystem::new(Config::of()->map(Config\Logger::psr(new NullLogger))), + OperatingSystem::new(Config::new()->map(Config\Logger::psr(new NullLogger))), ), ) ->prove(function($server, $os) { diff --git a/tests/SocketsTest.php b/tests/SocketsTest.php index 6a47fd9..29039f6 100644 --- a/tests/SocketsTest.php +++ b/tests/SocketsTest.php @@ -19,7 +19,7 @@ class SocketsTest extends TestCase { public function testOpen() { - $sockets = Sockets::of(Config::of()); + $sockets = Sockets::of(Config::new()); $socket = $sockets->open(Address::of(Path::of('/tmp/foo')))->match( static fn($server) => $server, @@ -38,7 +38,7 @@ public function testOpen() public function testTakeOver() { - $sockets = Sockets::of(Config::of()); + $sockets = Sockets::of(Config::new()); $socket = $sockets->open(Address::of(Path::of('/tmp/foo')))->match( static fn($server) => $server, @@ -56,7 +56,7 @@ public function testTakeOver() public function testConnectTo() { - $sockets = Sockets::of(Config::of()); + $sockets = Sockets::of(Config::new()); $server = $sockets->open(Address::of(Path::of('/tmp/foo')))->match( static fn($server) => $server, From 4176f6aba3541933ac26f54428e7565aaf826a1e Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:25:22 +0200 Subject: [PATCH 40/41] add missing internal flag --- src/CurrentProcess/Signals.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/CurrentProcess/Signals.php b/src/CurrentProcess/Signals.php index 74b0d44..1a5c961 100644 --- a/src/CurrentProcess/Signals.php +++ b/src/CurrentProcess/Signals.php @@ -18,6 +18,9 @@ private function __construct(Handler $handler) $this->handler = $handler; } + /** + * @internal + */ public static function of(Handler $handler): self { return new self($handler); From ec74cbe4a977af452bd5126e74435b9ad1919df0 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 24 May 2025 11:26:53 +0200 Subject: [PATCH 41/41] create the handler inside the wrapper to not expose implementation detail --- src/CurrentProcess.php | 3 +-- src/CurrentProcess/Signals.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/CurrentProcess.php b/src/CurrentProcess.php index 3e8b203..fa2d194 100644 --- a/src/CurrentProcess.php +++ b/src/CurrentProcess.php @@ -8,7 +8,6 @@ use Innmind\Server\Status\Server\Memory\Bytes; use Innmind\TimeContinuum\Period; use Innmind\TimeWarp\Halt; -use Innmind\Signals\Handler; use Innmind\Immutable\{ Attempt, SideEffect, @@ -48,7 +47,7 @@ public function id(): Attempt public function signals(): Signals { - return $this->signals ??= Signals::of(new Handler); + return $this->signals ??= Signals::of(); } /** diff --git a/src/CurrentProcess/Signals.php b/src/CurrentProcess/Signals.php index 1a5c961..a22d7d9 100644 --- a/src/CurrentProcess/Signals.php +++ b/src/CurrentProcess/Signals.php @@ -21,9 +21,9 @@ private function __construct(Handler $handler) /** * @internal */ - public static function of(Handler $handler): self + public static function of(): self { - return new self($handler); + return new self(new Handler); } /**