{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/10908","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/10908/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/10908/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/10908/events","html_url":"https://github.com/yiisoft/yii2/issues/10908","id":134828041,"number":10908,"title":"Dependency Injection for Closure configuration","user":{"login":"SamMousa","id":547021,"avatar_url":"https://avatars.githubusercontent.com/u/547021?v=3","gravatar_id":"","url":"https://api.github.com/users/SamMousa","html_url":"https://github.com/SamMousa","followers_url":"https://api.github.com/users/SamMousa/followers","following_url":"https://api.github.com/users/SamMousa/following{/other_user}","gists_url":"https://api.github.com/users/SamMousa/gists{/gist_id}","starred_url":"https://api.github.com/users/SamMousa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SamMousa/subscriptions","organizations_url":"https://api.github.com/users/SamMousa/orgs","repos_url":"https://api.github.com/users/SamMousa/repos","events_url":"https://api.github.com/users/SamMousa/events{/privacy}","received_events_url":"https://api.github.com/users/SamMousa/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/yiisoft/yii2/labels/status:ready%20for%20adoption","name":"status:ready for adoption","color":"02e10c"},{"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:enhancement","name":"type:enhancement","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/yiisoft/yii2/milestones/16","html_url":"https://github.com/yiisoft/yii2/milestones/2.0.8","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/16/labels","id":1242959,"number":16,"title":"2.0.8","description":"","creator":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"open_issues":122,"closed_issues":24,"state":"open","created_at":"2015-08-05T22:18:21Z","updated_at":"2016-02-22T08:54:37Z","due_on":"2016-03-19T23:00:00Z","closed_at":null},"comments":5,"created_at":"2016-02-19T10:21:41Z","updated_at":"2016-02-22T15:59:30Z","closed_at":null,"body":"Currently an element in the `component` applciation configuration can be a closure:\r\n```php\r\n    'components' => [\r\n        'pheanstalk' => function() {\r\n            $result = new \\Pheanstalk\\Pheanstalk('localhost');\r\n            $result->watch('daily');\r\n            return $result;\r\n        },\r\n```\r\n\r\nThis is fine, but does not work when my object depends on other components and I want to use DI:\r\n```php\r\n    'components' => [\r\n        'pheanstalk' => function(yii\\web\\User $user) {\r\n            $result = new \\Pheanstalk\\Pheanstalk('localhost');\r\n            $result->watch($user->getId());\r\n\r\n            return $result;\r\n        },\r\n```\r\n\r\nSince we now have `Container::invoke` we could simply use that:\r\n```php\r\npublic static function createObject($type, array $params = [])\r\n    {\r\n        if (is_string($type)) {\r\n            return static::$container->get($type, $params);\r\n        } elseif (is_array($type) && isset($type['class'])) {\r\n            $class = $type['class'];\r\n            unset($type['class']);\r\n            return static::$container->get($class, $params, $type);\r\n        } elseif (is_callable($type, true)) {\r\n            return call_user_func($type, $params);\r\n        } elseif (is_array($type)) {\r\n            throw new InvalidConfigException('Object configuration must be an array containing a \"class\" element.');\r\n        } else {\r\n            throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type));\r\n        }\r\n    }\r\n```\r\n\r\nChanging `call_user_func($type, $params)` to `Yii::$container->invoke($type, [$params]);` would be backwards compatible.\r\n1. It adds an array around `$params`, making it the first positional argument.\r\n2. Invoke will fill any dependencies then pass the `$params` array (which is never set in this use case) as the first positional argument that is not explicitly type hinted for DI.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/187243864","html_url":"https://github.com/yiisoft/yii2/issues/10908#issuecomment-187243864","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/10908","id":187243864,"user":{"login":"samdark","id":47294,"avatar_url":"https://avatars.githubusercontent.com/u/47294?v=3","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false},"created_at":"2016-02-22T15:59:30Z","updated_at":"2016-02-22T15:59:30Z","body":"Yes."}}