Skip to content

Commit 8d1955b

Browse files
authored
Merge pull request #51 from utopia-php/codex/task-container-injection
feat: allow cli container injection
2 parents 4efef26 + dda9ec7 commit 8d1955b

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/CLI/CLI.php

Lines changed: 15 additions & 5 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
*
@@ -83,10 +85,11 @@ class CLI
8385
*
8486
* @param Adapter|null $adapter
8587
* @param array $args
88+
* @param Container|null $container
8689
*
8790
* @throws Exception
8891
*/
89-
public function __construct(?Adapter $adapter = null, array $args = [])
92+
public function __construct(?Adapter $adapter = null, array $args = [], ?Container $container = null)
9093
{
9194
if (\php_sapi_name() !== 'cli') {
9295
throw new Exception('CLI tasks can only work from the command line');
@@ -97,7 +100,8 @@ public function __construct(?Adapter $adapter = null, array $args = [])
97100
@\cli_set_process_title($this->command);
98101

99102
$this->adapter = $adapter ?? new Generic();
100-
$this->container = new Container();
103+
$this->parentContainer = $container;
104+
$this->container = new Container($container);
101105
}
102106

103107
/**
@@ -213,6 +217,11 @@ public function setResource(string $name, callable $callback, array $dependencie
213217
$this->container->set($name, $callback, $dependencies);
214218
}
215219

220+
public function getContainer(): Container
221+
{
222+
return $this->container;
223+
}
224+
216225
/**
217226
* task-name --foo=test
218227
*
@@ -401,16 +410,17 @@ protected function validate(string $key, array $param, $value): void
401410
}
402411
}
403412

404-
public function setContainer($container): self
413+
public function setContainer(Container $container): self
405414
{
406-
$this->container = $container;
415+
$this->parentContainer = $container;
416+
$this->container = new Container($container);
407417

408418
return $this;
409419
}
410420

411421
public function reset(): void
412422
{
413-
$this->container = new Container();
423+
$this->container = new Container($this->parentContainer);
414424
}
415425

416426
private function camelCaseIt($key): string

tests/CLI/CLITest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Utopia\CLI\Adapters\Generic;
77
use Utopia\CLI\CLI;
8+
use Utopia\DI\Container;
89
use Utopia\Validator\ArrayList;
910
use Utopia\Validator\Text;
1011

@@ -201,6 +202,50 @@ public function testInjection()
201202
$this->assertEquals('test-value-me@example.com', $result);
202203
}
203204

205+
public function testProvidedContainer()
206+
{
207+
ob_start();
208+
209+
$container = new Container();
210+
$container->set('test', fn () => 'test-value');
211+
212+
$cli = new CLI(new Generic(), ['test.php', 'build'], $container);
213+
214+
$this->assertNotSame($container, $cli->getContainer());
215+
$this->assertEquals('test-value', $cli->getResource('test'));
216+
217+
$cli->task('build')
218+
->inject('test')
219+
->action(function ($test) {
220+
echo $test;
221+
});
222+
223+
$cli->run();
224+
225+
$result = ob_get_clean();
226+
227+
$this->assertEquals('test-value', $result);
228+
}
229+
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+
204249
public function testMatch()
205250
{
206251
$cli = new CLI(new Generic(), ['test.php', 'build2', '--email=me@example.com', '--list=item1', '--list=item2']); // Mock command request

0 commit comments

Comments
 (0)