Skip to content

Commit dda9ec7

Browse files
committed
Reapply "fix: preserve injected container on reset"
This reverts commit 7a206f1.
1 parent 7a206f1 commit dda9ec7

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/CLI/CLI.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class CLI
3333
*/
3434
protected Container $container;
3535

36+
protected ?Container $parentContainer = null;
37+
3638
/**
3739
* Args
3840
*
@@ -98,7 +100,8 @@ public function __construct(?Adapter $adapter = null, array $args = [], ?Contain
98100
@\cli_set_process_title($this->command);
99101

100102
$this->adapter = $adapter ?? new Generic();
101-
$this->container = $container ?? new Container();
103+
$this->parentContainer = $container;
104+
$this->container = new Container($container);
102105
}
103106

104107
/**
@@ -409,14 +412,15 @@ protected function validate(string $key, array $param, $value): void
409412

410413
public function setContainer(Container $container): self
411414
{
412-
$this->container = $container;
415+
$this->parentContainer = $container;
416+
$this->container = new Container($container);
413417

414418
return $this;
415419
}
416420

417421
public function reset(): void
418422
{
419-
$this->container = new Container();
423+
$this->container = new Container($this->parentContainer);
420424
}
421425

422426
private function camelCaseIt($key): string

tests/CLI/CLITest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ public function testProvidedContainer()
211211

212212
$cli = new CLI(new Generic(), ['test.php', 'build'], $container);
213213

214-
$this->assertSame($container, $cli->getContainer());
214+
$this->assertNotSame($container, $cli->getContainer());
215+
$this->assertEquals('test-value', $cli->getResource('test'));
215216

216217
$cli->task('build')
217218
->inject('test')
@@ -226,6 +227,25 @@ public function testProvidedContainer()
226227
$this->assertEquals('test-value', $result);
227228
}
228229

230+
public function testResetPreservesInjectedContainer()
231+
{
232+
$container = new Container();
233+
$container->set('base', fn () => 'base-value');
234+
235+
$cli = new CLI(new Generic(), ['test.php', 'build'], $container);
236+
$cli->setResource('runtime', fn () => 'runtime-value');
237+
238+
$this->assertEquals('base-value', $cli->getResource('base'));
239+
$this->assertEquals('runtime-value', $cli->getResource('runtime'));
240+
241+
$cli->reset();
242+
243+
$this->assertEquals('base-value', $cli->getResource('base'));
244+
245+
$this->expectException(\Exception::class);
246+
$cli->getResource('runtime');
247+
}
248+
229249
public function testMatch()
230250
{
231251
$cli = new CLI(new Generic(), ['test.php', 'build2', '--email=me@example.com', '--list=item1', '--list=item2']); // Mock command request

0 commit comments

Comments
 (0)