Skip to content

Commit 3247308

Browse files
Andy Rainesfabpot
authored andcommitted
[Console] fixed BC issue with static closures
1 parent 04595dc commit 3247308

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,15 @@ public function setCode($code)
283283
if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
284284
$r = new \ReflectionFunction($code);
285285
if (null === $r->getClosureThis()) {
286-
$code = \Closure::bind($code, $this);
286+
if (PHP_VERSION_ID < 70000) {
287+
// Bug in PHP5: https://bugs.php.net/bug.php?id=64761
288+
// This means that we cannot bind static closures and therefore we must
289+
// ignore any errors here. There is no way to test if the closure is
290+
// bindable.
291+
$code = @\Closure::bind($code, $this);
292+
} else {
293+
$code = \Closure::bind($code, $this);
294+
}
287295
}
288296
}
289297

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,29 @@ public function testSetCodeBindToClosure($previouslyBound, $expected)
335335
$this->assertEquals('interact called'.PHP_EOL.$expected.PHP_EOL, $tester->getDisplay());
336336
}
337337

338+
public function testSetCodeWithStaticClosure()
339+
{
340+
$command = new \TestCommand();
341+
$command->setCode(self::createClosure());
342+
$tester = new CommandTester($command);
343+
$tester->execute(array());
344+
345+
if (PHP_VERSION_ID < 70000) {
346+
// Cannot bind static closures in PHP 5
347+
$this->assertEquals('interact called'.PHP_EOL.'not bound'.PHP_EOL, $tester->getDisplay());
348+
} else {
349+
// Can bind static closures in PHP 7
350+
$this->assertEquals('interact called'.PHP_EOL.'bound'.PHP_EOL, $tester->getDisplay());
351+
}
352+
}
353+
354+
private static function createClosure()
355+
{
356+
return function (InputInterface $input, OutputInterface $output) {
357+
$output->writeln(isset($this) ? 'bound' : 'not bound');
358+
};
359+
}
360+
338361
public function testSetCodeWithNonClosureCallable()
339362
{
340363
$command = new \TestCommand();

0 commit comments

Comments
 (0)