Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add easy way to use simple closures as commands #58

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Cilex/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,27 @@ public function run(InputInterface $input = null, OutputInterface $output = null
*
* If a command with the same name already exists, it will be overridden.
*
* @param \Cilex\Command\Command $command A Command object
* @param Command $command A Command object
* @api
* @return void
*/
public function command(Command $command)
public function add(Command $command)
{
$this['console']->add($command);
}

/**
* @param string $name
* @param callable $callable
* @return Command
*/
public function command($name, $callable)
{
$command = new Command($name);
$command->setCode($callable);
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't setCode only influence the execute method of a Command? Unless I got it wrong I am missing a way to add argument and option definitions and a way to set the description for a command

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does, thats why the created command object is returned from the function. Its a shortcut, not the "best" way to create full commands.

In silex binding would be

$app->get('/', function () {})->bind('route');

which is the same as cilex

$app->command('my-name', function () {
})->addOoption(); // etc.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting notion, I hadn't thought of that option. Awesome!


$this->add($command);

return $command;
}
}
20 changes: 19 additions & 1 deletion tests/Cilex/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ public function testCustomInputOutput()

}

public function testClosureCommand()
{
$invoked = false;
$command = $this->app->command('closure-command', function () use (&$invoked) {
$invoked = true;
});

$this->assertInstanceOf('Symfony\Component\Console\Command\Command', $command);
$this->assertTrue($this->app['console']->has('closure-command'));

$command->run(
$this->getMock('Symfony\Component\Console\Input\InputInterface'),
$this->getMock('Symfony\Component\Console\Output\OutputInterface')
);

$this->assertTrue($invoked);
}

/**
* Tests the command method to see if the command is properly set and the
* Cilex application is added as container.
Expand All @@ -65,7 +83,7 @@ public function testCommand()
{
$this->assertFalse($this->app['console']->has('demo:greet'));

$this->app->command(new GreetCommand());
$this->app->add(new GreetCommand());

$this->assertTrue($this->app['console']->has('demo:greet'));

Expand Down
4 changes: 2 additions & 2 deletions tests/Cilex/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setUp()
public function testContainer()
{
$app = new Application('Test');
$app->command($this->fixture);
$app->add($this->fixture);

$this->assertSame($app, $this->fixture->getContainer());
}
Expand All @@ -52,7 +52,7 @@ public function testContainer()
public function testGetService()
{
$app = new Application('Test');
$app->command($this->fixture);
$app->add($this->fixture);

$this->assertInstanceOf('Symfony\Component\Console\Application', $this->fixture->getService('console'));
}
Expand Down