Skip to content

Commit 675aad2

Browse files
committed
wip
1 parent fdb63d0 commit 675aad2

File tree

6 files changed

+74
-35
lines changed

6 files changed

+74
-35
lines changed

src/Command.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Commands/CreateRequestHandlerCommand.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/InvokableCommand.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Narration\Console;
4+
5+
use function call_user_func;
6+
use Symfony\Component\Console\Command\Command as BaseCommand;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class InvokableCommand extends BaseCommand
11+
{
12+
/**
13+
* @var \callable
14+
*/
15+
private $callable;
16+
17+
/**
18+
* InvokableCommand constructor.
19+
*
20+
* @param string $name
21+
* @param callable $callable
22+
*/
23+
public function __construct(string $name, callable $callable)
24+
{
25+
parent::__construct($name);
26+
27+
$this->callable = $callable;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function execute(InputInterface $input, OutputInterface $output): void
34+
{
35+
call_user_func($this->callable, $input, $output);
36+
}
37+
}

src/Kernel.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ private function __construct(Application $application)
3434
}
3535

3636
/**
37-
* @param \Narration\Console\Command[] $commands
37+
* @param mixed $commands
3838
*
3939
* @return \Narration\Console\Kernel
4040
*/
41-
public static function attach(array $commands): self
41+
public static function withCommands(array $commands): self
4242
{
4343
$application = new Application('Narrative', '@dev');
4444

4545
foreach ($application->all() as $command) {
4646
$command->setHidden(true);
4747
}
4848

49-
$application->addCommands($commands);
49+
foreach ($commands as $name => $callable) {
50+
$application->add(new InvokableCommand($name, $callable));
51+
}
5052

5153
return new self($application);
5254
}

tests/Unit/InvokableCommandTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Narration\Console\InvokableCommand;
6+
use Narration\Console\Kernel;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Input\ArrayInput;
9+
use Symfony\Component\Console\Output\BufferedOutput;
10+
11+
final class InvokableCommandTest extends TestCase
12+
{
13+
public function testExecutable(): void
14+
{
15+
$input = new ArrayInput([]);
16+
17+
$output = new BufferedOutput();
18+
19+
$injectedInput = null;
20+
$injectedOutput = null;
21+
22+
$command = new InvokableCommand('foo', function ($input, $output) use (&$injectedInput, &$injectedOutput) {
23+
$injectedInput = $input;
24+
$injectedOutput = $output;
25+
});
26+
$command->run($input, $output);
27+
28+
$this->assertSame($input, $injectedInput);
29+
$this->assertSame($output, $injectedOutput);
30+
}
31+
}
32+

tests/Unit/KernelTest.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)