Skip to content

[codex] Allow task container injection during platform init#57

Closed
ChiragAgg5k wants to merge 1 commit intomainfrom
codex/task-container-init
Closed

[codex] Allow task container injection during platform init#57
ChiragAgg5k wants to merge 1 commit intomainfrom
codex/task-container-init

Conversation

@ChiragAgg5k
Copy link
Copy Markdown
Member

Summary

  • allow Platform::init() task bootstrapping to accept a provided container param
  • forward that container into the CLI instance before task hooks and actions are registered
  • add an end-to-end test proving an internally created CLI can resolve injected task dependencies from the supplied container

Why

Appwrite needs to register task resources directly on a shared DI container in app/cli.php, but utopia-php/platform currently creates the CLI without exposing a way to supply the task container during init(Service::TYPE_TASK).

This keeps the change minimal and compatible with the existing utopia-php/cli API by reusing CLI::setContainer().

Validation

  • composer test
  • composer lint
  • composer format src/Platform/Platform.php tests/Platform/TestServiceCLI.php tests/Platform/TestActionCLIInjection.php tests/e2e/CLITest.php

Impact

Users can now do:

$container = new Container();
$platform->init(Service::TYPE_TASK, [
    'container' => $container,
]);

and register CLI task resources directly on that container.

@ChiragAgg5k ChiragAgg5k marked this pull request as ready for review April 5, 2026 14:48
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 5, 2026

Greptile Summary

This PR adds a minimal, well-scoped feature to Platform::init() for TYPE_TASK that allows callers to supply a pre-built DI Container, which is forwarded to the CLI instance via setContainer() before tasks are registered. The implementation is clean and backward-compatible. An end-to-end test is included. A couple of minor test-reliability issues are worth addressing.

  • src/Platform/Platform.php: New container key in $params is checked with isset() and forwarded to $this->cli->setContainer() before initTasks() is called — correct ordering.
  • tests/e2e/CLITest.php: New test validates the feature end-to-end, but the output buffer opened with ob_start() is not closed in the finally block, and $result is only assigned inside the try block yet used outside it.
  • tests/Platform/TestServiceCLI.php / tests/Platform/TestActionCLIInjection.php: Straightforward additions of a new injectable action and its registration in the test service.

Confidence Score: 5/5

Safe to merge; all findings are non-blocking test quality issues.

The production change is correct and minimal. The only issues are in the test helper code (output buffer leak, potentially undefined variable), which are P2 and do not affect correctness of the feature itself.

tests/e2e/CLITest.php — the ob_start/ob_get_clean imbalance and undefined $result on exception paths should be cleaned up.

Important Files Changed

Filename Overview
src/Platform/Platform.php Adds container forwarding to CLI before task registration; correct ordering, backward-compatible, no logic issues.
tests/e2e/CLITest.php New test covers the feature but has an output-buffer leak and a potentially-undefined $result on exception paths.
tests/Platform/TestActionCLIInjection.php New test fixture action that injects a 'test' dependency and echoes its value — straightforward.
tests/Platform/TestServiceCLI.php Registers the new injectable action under the 'inject' key; no issues.

Reviews (1): Last reviewed commit: "feat: allow task container injection" | Re-trigger Greptile

Comment on lines +44 to +63
try {
$_SERVER['argv'] = ['test.php', 'inject'];
ob_start();

$container = new Container();
$container->set('test', fn () => 'test-value');

$platform = new TestPlatform();
$platform->init(Service::TYPE_TASK, [
'adapter' => new Generic(),
'container' => $container,
]);

$cli = $platform->getCli();
$cli->run();

$result = ob_get_clean();
} finally {
$_SERVER['argv'] = $argv;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Output buffer not closed on exception path

ob_start() is called inside the try block, but ob_get_clean() only runs on the happy path. If $cli->run() throws, the buffer is never flushed or discarded, which can corrupt output for every subsequent test in the suite. Additionally, $result is referenced after the try/finally block but is only assigned inside the try — if an exception escapes, $result would be undefined at the assertEquals call.

Moving the buffer teardown into finally fixes both issues:

$argv = $_SERVER['argv'] ?? [];
$result = '';

try {
    $_SERVER['argv'] = ['test.php', 'inject'];
    ob_start();

    $container = new Container();
    $container->set('test', fn () => 'test-value');

    $platform = new TestPlatform();
    $platform->init(Service::TYPE_TASK, [
        'adapter' => new Generic(),
        'container' => $container,
    ]);

    $cli = $platform->getCli();
    $cli->run();
} finally {
    $result = ob_get_clean();
    $_SERVER['argv'] = $argv;
}

$this->assertEquals('test-value', $result);

@ChiragAgg5k
Copy link
Copy Markdown
Member Author

Superseded by utopia-php/cli#51. Closing this in favor of the CLI-owned container approach.

@ChiragAgg5k ChiragAgg5k closed this Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant