Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1400f4e

Browse files
committed
[Console] Bind the closure (code) to the Command if possible
1 parent862bdf1 commit1400f4e

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
thrownew \InvalidArgumentException('Invalid callable provided to Command::setCode.');
285285
}
286286

287+
if (PHP_VERSION_ID >=50400 &&$codeinstanceof \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+
publicfunctiontestSetCodeBindToClosure()
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 =newCommandTester($command);
307+
$tester->execute(array());
308+
$this->assertEquals('interact called'.PHP_EOL.'TestCommand'.PHP_EOL,$tester->getDisplay());
309+
}
310+
296311
publicfunctiontestSetCodeWithNonClosureCallable()
297312
{
298313
$command =new \TestCommand();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp