{"action":"created","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/17374","labels_url":"https://api.github.com/repos/symfony/symfony/issues/17374/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/17374/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/17374/events","html_url":"https://github.com/symfony/symfony/pull/17374","id":126653803,"number":17374,"title":"[Console] Add Application::runCommand() to run sub-commands easily","user":{"login":"mnapoli","id":720328,"avatar_url":"https://avatars.githubusercontent.com/u/720328?v=3","gravatar_id":"","url":"https://api.github.com/users/mnapoli","html_url":"https://github.com/mnapoli","followers_url":"https://api.github.com/users/mnapoli/followers","following_url":"https://api.github.com/users/mnapoli/following{/other_user}","gists_url":"https://api.github.com/users/mnapoli/gists{/gist_id}","starred_url":"https://api.github.com/users/mnapoli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mnapoli/subscriptions","organizations_url":"https://api.github.com/users/mnapoli/orgs","repos_url":"https://api.github.com/users/mnapoli/repos","events_url":"https://api.github.com/users/mnapoli/events{/privacy}","received_events_url":"https://api.github.com/users/mnapoli/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/symfony/symfony/labels/Console","name":"Console","color":"DDDDDD"},{"url":"https://api.github.com/repos/symfony/symfony/labels/Status:%20Needs%20Review","name":"Status: Needs Review","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-14T13:22:43Z","updated_at":"2016-01-25T18:32:52Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/symfony/symfony/pulls/17374","html_url":"https://github.com/symfony/symfony/pull/17374","diff_url":"https://github.com/symfony/symfony/pull/17374.diff","patch_url":"https://github.com/symfony/symfony/pull/17374.patch"},"body":"| Q             | A\r\n| ------------- | ---\r\n| Bug fix?      | no\r\n| New feature?  | yes\r\n| BC breaks?    | no\r\n| Deprecations? | no\r\n| Tests pass?   | yes\r\n| Fixed tickets | -\r\n| License       | MIT\r\n| Doc PR        | -\r\n\r\nThis PR adds a new `Application::runCommand()` method that allows to easily run a sub-command from within a command.\r\n\r\n[How to run a sub-command today](http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command):\r\n\r\n```php\r\nprotected function execute(InputInterface $input, OutputInterface $output)\r\n{\r\n    $command = $this->getApplication()->find('demo:greet');\r\n\r\n    $arguments = array(\r\n        'command' => 'demo:greet',\r\n        'name'    => 'Fabien',\r\n        '--yell'  => true,\r\n    );\r\n\r\n    $greetInput = new ArrayInput($arguments);\r\n    $returnCode = $command->run($greetInput, $output);\r\n\r\n    // ...\r\n}\r\n```\r\n\r\nWith this change:\r\n\r\n```php\r\nprotected function execute(InputInterface $input, OutputInterface $output)\r\n{\r\n    $app = $this->getApplication();\r\n    $returnCode = $app->runCommand('demo:greet Fabien --yell', $output);\r\n\r\n    // ...\r\n}\r\n```\r\n\r\nI think that could be useful to avoid having to resort to use yet another tool to run grouped commands, for example Phing. Instead of running `bin/console db:drop --force && bin/console db:create && bin/console db:fixtures:load`, we could have a Symfony command that would run all that.\r\n\r\nAnd yes usually you are better off calling services directly instead of sub-commands, but in some applications it's usually not worth it, especially micro-applications. For example I [ran into this in Silly](https://github.com/mnapoli/silly/issues/17):\r\n\r\n```php\r\n$app->command('init', function ($input, $output) use ($app) {\r\n    $app->runCommand('db:drop --force', $output)\r\n    $app->runCommand('db:create', $output)\r\n    $app->runCommand('db:fixtures', $output)\r\n});\r\n```\r\n\r\nThat looks super simple and easy to maintain.\r\n\r\nFeedback welcome on the method and parameter names, I'm not 100% satisfied but it's better than nothing…"},"comment":{"url":"https://api.github.com/repos/symfony/symfony/issues/comments/174612960","html_url":"https://github.com/symfony/symfony/pull/17374#issuecomment-174612960","issue_url":"https://api.github.com/repos/symfony/symfony/issues/17374","id":174612960,"user":{"login":"fabpot","id":47313,"avatar_url":"https://avatars.githubusercontent.com/u/47313?v=3","gravatar_id":"","url":"https://api.github.com/users/fabpot","html_url":"https://github.com/fabpot","followers_url":"https://api.github.com/users/fabpot/followers","following_url":"https://api.github.com/users/fabpot/following{/other_user}","gists_url":"https://api.github.com/users/fabpot/gists{/gist_id}","starred_url":"https://api.github.com/users/fabpot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabpot/subscriptions","organizations_url":"https://api.github.com/users/fabpot/orgs","repos_url":"https://api.github.com/users/fabpot/repos","events_url":"https://api.github.com/users/fabpot/events{/privacy}","received_events_url":"https://api.github.com/users/fabpot/received_events","type":"User","site_admin":false},"created_at":"2016-01-25T18:32:52Z","updated_at":"2016-01-25T18:32:52Z","body":":+1: "}}