Skip to content

Commit 7d365cd

Browse files
committed
add mechanism to extend the os config
1 parent 2ec7178 commit 7d365cd

6 files changed

Lines changed: 101 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `Innmind\OperatingSystem\Config::map()`
8+
- `Innmind\OperatingSystem\Config\Logger`
9+
510
### Changed
611

712
- Requires `innmind/time-continuum:^4.1.1`

src/Config.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public static function of(): self
7272
);
7373
}
7474

75+
/**
76+
* @psalm-mutation-free
77+
*
78+
* @param callable(self): self $map
79+
*/
80+
public function map(callable $map): self
81+
{
82+
/** @psalm-suppress ImpureFunctionCall */
83+
return $map($this);
84+
}
85+
7586
/**
7687
* @psalm-mutation-free
7788
*/
@@ -123,6 +134,26 @@ public function haltProcessVia(Halt $halt): self
123134
);
124135
}
125136

137+
/**
138+
* @psalm-mutation-free
139+
*
140+
* @param callable(Halt): Halt $map
141+
*/
142+
public function maphalt(callable $map): self
143+
{
144+
/** @psalm-suppress ImpureFunctionCall */
145+
return new self(
146+
$this->clock,
147+
$this->caseSensitivity,
148+
$this->io,
149+
$map($this->halt),
150+
$this->path,
151+
$this->maxHttpConcurrency,
152+
$this->httpHeartbeat,
153+
$this->disableSSLVerification,
154+
);
155+
}
156+
126157
/**
127158
* @psalm-mutation-free
128159
*/

src/Config/Logger.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Innmind\OperatingSystem\Config;
5+
6+
use Innmind\OperatingSystem\Config;
7+
use Innmind\TimeWarp\Halt;
8+
use Psr\Log\LoggerInterface;
9+
10+
final class Logger
11+
{
12+
private function __construct(
13+
private LoggerInterface $logger,
14+
) {
15+
}
16+
17+
public function __invoke(Config $config): Config
18+
{
19+
return $config
20+
->mapHalt(fn($halt) => Halt\Logger::psr(
21+
$halt,
22+
$this->logger,
23+
));
24+
}
25+
26+
public static function psr(LoggerInterface $logger): self
27+
{
28+
return new self($logger);
29+
}
30+
}

src/CurrentProcess/Logger.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ public function signals(): Signals
5151
#[\Override]
5252
public function halt(Period $period): Attempt
5353
{
54-
$this->logger->debug('Halting current process...', ['period' => [
55-
'years' => $period->years(),
56-
'months' => $period->months(),
57-
'days' => $period->days(),
58-
'hours' => $period->hours(),
59-
'minutes' => $period->minutes(),
60-
'seconds' => $period->seconds(),
61-
'milliseconds' => $period->milliseconds(),
62-
]]);
63-
6454
return $this->process->halt($period);
6555
}
6656

src/OperatingSystem/Logger.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
Sockets,
1111
Remote,
1212
CurrentProcess,
13+
Config,
14+
Factory,
1315
};
1416
use Innmind\Server\Status;
1517
use Innmind\Server\Control;
@@ -29,7 +31,12 @@ private function __construct(OperatingSystem $os, LoggerInterface $logger)
2931

3032
public static function psr(OperatingSystem $os, LoggerInterface $logger): self
3133
{
32-
return new self($os, $logger);
34+
return new self(
35+
$os->map(static fn($_, $config) => Factory::build(
36+
$config->map(Config\Logger::psr($logger)),
37+
)),
38+
$logger,
39+
);
3340
}
3441

3542
#[\Override]

tests/CurrentProcess/GenericTest.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77
CurrentProcess\Generic,
88
CurrentProcess\Signals,
99
CurrentProcess,
10+
Factory,
11+
Config,
1012
};
1113
use Innmind\Server\Control\Server\Process\Pid;
1214
use Innmind\Server\Status\Server\Memory\Bytes;
1315
use Innmind\TimeContinuum\Period;
1416
use Innmind\TimeWarp\Halt;
1517
use Innmind\Immutable\SideEffect;
16-
use Innmind\BlackBox\PHPUnit\Framework\TestCase;
18+
use Psr\Log\NullLogger;
19+
use Innmind\BlackBox\{
20+
PHPUnit\BlackBox,
21+
PHPUnit\Framework\TestCase,
22+
Set,
23+
};
1724

1825
class GenericTest extends TestCase
1926
{
27+
use BlackBox;
28+
2029
public function testInterface()
2130
{
2231
$this->assertInstanceOf(
@@ -36,18 +45,25 @@ public function testId()
3645
);
3746
}
3847

39-
public function testHalt()
48+
public function testHalt(): BlackBox\Proof
4049
{
41-
$process = Generic::of(
42-
Halt\Usleep::new(),
43-
);
50+
return $this
51+
->forAll(Set::of(
52+
static fn($config) => $config,
53+
Config\Logger::psr(new NullLogger),
54+
))
55+
->prove(function($extension) {
56+
$process = Factory::build(
57+
Config::of()->map($extension),
58+
)->process();
4459

45-
$this->assertInstanceOf(
46-
SideEffect::class,
47-
$process
48-
->halt(Period::millisecond(1))
49-
->unwrap(),
50-
);
60+
$this->assertInstanceOf(
61+
SideEffect::class,
62+
$process
63+
->halt(Period::millisecond(1))
64+
->unwrap(),
65+
);
66+
});
5167
}
5268

5369
public function testSignals()

0 commit comments

Comments
 (0)