diff --git a/examples/context.php b/examples/context.php index 32d6dd37..a267045b 100644 --- a/examples/context.php +++ b/examples/context.php @@ -99,7 +99,7 @@ function contextFromPath(): Context } #[AsTask(description: 'Displays information about the context', name: 'context')] -function contextInfo(): void +function contextInfo(bool $test = false): void { $context = context(); io()->writeln('context name: ' . variable('name', 'N/A')); diff --git a/src/Kernel.php b/src/Kernel.php index 379fc13b..ace84302 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -11,6 +11,7 @@ use Castor\Event\BeforeBootEvent; use Castor\Event\FunctionsResolvedEvent; use Castor\Exception\CouldNotFindEntrypointException; +use Castor\Factory\TaskCommandFactory; use Castor\Function\FunctionLoader; use Castor\Function\FunctionResolver; use Castor\Helper\PlatformHelper; @@ -46,6 +47,7 @@ public function __construct( private readonly FunctionResolver $functionResolver, private readonly FunctionLoader $functionLoader, private readonly ContextRegistry $contextRegistry, + private readonly TaskCommandFactory $taskCommandFactory, ) { } @@ -123,6 +125,25 @@ private function load( $this->functionLoader->loadListeners($descriptorsCollection->listenerDescriptors); + $taskCommand = null; + foreach ($descriptorsCollection->taskDescriptors as $taskDescriptor) { + $namespace = $taskDescriptor->taskAttribute->namespace; + $taskName = $taskDescriptor->taskAttribute->name; + $fullTaskName = $namespace ? $namespace . ':' . $taskName : $taskName; + if ($fullTaskName === $input->getFirstArgument()) { + $taskCommand = $this->taskCommandFactory->createTask($taskDescriptor); + + break; + } + } + + $taskDefinition = $taskCommand?->getDefinition(); + + if (null !== $taskDefinition) { + $this->application->getDefinition()->addOptions($taskDefinition->getOptions()); + $this->application->getDefinition()->addArguments($taskDefinition->getArguments()); + } + // Must load contexts before tasks, because tasks can be disabled // depending on the context. And it must be before executing // listeners too, to get the context there.