Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1400f4e

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

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,21 @@ 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+
$tester = new CommandTester($command);
307+
$tester->execute(array());
308+
$this->assertEquals('interact called'.PHP_EOL.'TestCommand'.PHP_EOL, $tester->getDisplay());
309+
}
310+
296311
public function testSetCodeWithNonClosureCallable()
297312
{
298313
$command = new \TestCommand();

0 commit comments

Comments
 (0)