Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class CLI
*/
protected Container $container;

protected ?Container $parentContainer = null;

/**
* Args
*
Expand Down Expand Up @@ -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');
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions tests/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Loading