diff --git a/src/CLI/CLI.php b/src/CLI/CLI.php index ee2f5da..0611d29 100644 --- a/src/CLI/CLI.php +++ b/src/CLI/CLI.php @@ -33,6 +33,8 @@ class CLI */ protected Container $container; + protected ?Container $parentContainer = null; + /** * Args * @@ -83,10 +85,11 @@ class CLI * * @param Adapter|null $adapter * @param array $args + * @param Container|null $container * * @throws Exception */ - public function __construct(?Adapter $adapter = null, array $args = []) + public function __construct(?Adapter $adapter = null, array $args = [], ?Container $container = null) { if (\php_sapi_name() !== 'cli') { throw new Exception('CLI tasks can only work from the command line'); @@ -97,7 +100,8 @@ public function __construct(?Adapter $adapter = null, array $args = []) @\cli_set_process_title($this->command); $this->adapter = $adapter ?? new Generic(); - $this->container = new Container(); + $this->parentContainer = $container; + $this->container = new Container($container); } /** @@ -213,6 +217,11 @@ public function setResource(string $name, callable $callback, array $dependencie $this->container->set($name, $callback, $dependencies); } + public function getContainer(): Container + { + return $this->container; + } + /** * task-name --foo=test * @@ -401,16 +410,17 @@ protected function validate(string $key, array $param, $value): void } } - public function setContainer($container): self + public function setContainer(Container $container): self { - $this->container = $container; + $this->parentContainer = $container; + $this->container = new Container($container); return $this; } public function reset(): void { - $this->container = new Container(); + $this->container = new Container($this->parentContainer); } private function camelCaseIt($key): string diff --git a/tests/CLI/CLITest.php b/tests/CLI/CLITest.php index a72d43c..078536f 100755 --- a/tests/CLI/CLITest.php +++ b/tests/CLI/CLITest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Utopia\CLI\Adapters\Generic; use Utopia\CLI\CLI; +use Utopia\DI\Container; use Utopia\Validator\ArrayList; use Utopia\Validator\Text; @@ -201,6 +202,50 @@ public function testInjection() $this->assertEquals('test-value-me@example.com', $result); } + public function testProvidedContainer() + { + ob_start(); + + $container = new Container(); + $container->set('test', fn () => 'test-value'); + + $cli = new CLI(new Generic(), ['test.php', 'build'], $container); + + $this->assertNotSame($container, $cli->getContainer()); + $this->assertEquals('test-value', $cli->getResource('test')); + + $cli->task('build') + ->inject('test') + ->action(function ($test) { + echo $test; + }); + + $cli->run(); + + $result = ob_get_clean(); + + $this->assertEquals('test-value', $result); + } + + public function testResetPreservesInjectedContainer() + { + $container = new Container(); + $container->set('base', fn () => 'base-value'); + + $cli = new CLI(new Generic(), ['test.php', 'build'], $container); + $cli->setResource('runtime', fn () => 'runtime-value'); + + $this->assertEquals('base-value', $cli->getResource('base')); + $this->assertEquals('runtime-value', $cli->getResource('runtime')); + + $cli->reset(); + + $this->assertEquals('base-value', $cli->getResource('base')); + + $this->expectException(\Exception::class); + $cli->getResource('runtime'); + } + public function testMatch() { $cli = new CLI(new Generic(), ['test.php', 'build2', '--email=me@example.com', '--list=item1', '--list=item2']); // Mock command request