8000 [Console] Bind the closure (code) to the Command if possible · symfony/symfony@b256312 · GitHub
[go: up one dir, main page]

Skip to content

Commit b256312

Browse files
committed
[Console] Bind the closure (code) to the Command if possible
1 parent 862bdf1 commit b256312

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Symfony/Component/Console/Command/Command.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ public function setCode($code)
284284
throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.');
285285
}
286286

287+
if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
288+
$code = \Closure::bind($code, $this);
289+
}
290+
287291
$this->code = $code;
288292

289293
return $this;

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ public function testSetCode()
293293
$this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
294294
}
295295

296+
public function testSetCodeBindToClosure()
297+
{
298+
if (PHP_VERSION_ID < 50400) {
299+
$this->markTestSkipped('Test skipped, for PHP 5.4+ only.');
300+
}
301+
302+
$command = new \TestCommand();
303+
$ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
304+
$output->writeln(get_class($this));
305+
});
306+
$this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
307+
$tester = new CommandTester($command);
308+
$tester->execute(array());
309+
$this->assertEquals('interact called'.PHP_EOL.'TestCommand'.PHP_EOL, $tester->getDisplay());
310+
}
311+
296312
public function testSetCodeWithNonClosureCallable()
297313
{
298314
$command = new \TestCommand();

0 commit comments

Comments
 (0)
0