Skip to content

[Console] Cannot call setCode with static closures #20845

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

Closed
araines opened this issue Dec 9, 2016 · 1 comment
Closed

[Console] Cannot call setCode with static closures #20845

araines opened this issue Dec 9, 2016 · 1 comment

Comments

@araines
Copy link

araines commented Dec 9, 2016

Following this commit:
symfony/console@9f7a193#diff-25a75e093c4a0d5564a36cebbb1384b3R290

I believe a BC break was introduced in Symfony 2.8 (and remains there) which means that it is no longer possible to use non-static closures with the console component.

Simplified example based on some of my code:

public static function register($console)
{
    $console
         ->register('acme:command')
         ->setCode(function (InputInterface $input, OutputInterface $output) {
             self::run($input, $output);
        });
}

public static function run(InputInterface $input, OutputInterface $output)
{
    // ...
}

Results in:

PHP Fatal error:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException'
with message 'Warning: Cannot bind an instance to a static closure' in 
/projects/api/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:286

This is caused because with the code introduced in 2.8 it attempts to bind an instance to any closure, which is not permitted for static closures.

I couldn't locate why this code change was introduced in the first place - simply removing the bind call appears to solve my problem, but without understanding why the change was introduced I don't know what else I might be breaking.

@araines
Copy link
Author

araines commented Dec 9, 2016

Managed to find the PR in question which introduced this issue: #14431

I'm going to look to prepare a patch

fabpot added a commit that referenced this issue Dec 13, 2016
This PR was squashed before being merged into the 2.8 branch (closes #20847).

Discussion
----------

[Console] fixed BC issue with static closures

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20845
| License       | MIT
| Doc PR        | n/a

Static closures were unable to be used in Command::setCode since #14431.  This change fixes the BC break and ensures static closures can still be used.

Edit: It seems the inability to bind static closures was considered a feature in PHP5 but was considered a bug by PHP7.  As such, the tests need to work around this fact - but the code can remain the same.  This code change can be tidied/removed once Symfony is PHP7+ only.

Discussion here:
https://bugs.php.net/bug.php?id=64761
https://bugs.php.net/bug.php?id=68792

Commits
-------

3247308 [Console] fixed BC issue with static closures
@fabpot fabpot closed this as completed Dec 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants