{"action":"created","issue":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/6244","labels_url":"https://api.github.com/repos/cakephp/cakephp/issues/6244/labels{/name}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/6244/comments","events_url":"https://api.github.com/repos/cakephp/cakephp/issues/6244/events","html_url":"https://github.com/cakephp/cakephp/pull/6244","id":65630989,"number":6244,"title":"Sample facade/singleton solution for \"static classes\" based on TableRegistry","user":{"login":"robertpustulka","id":7437773,"avatar_url":"https://avatars.githubusercontent.com/u/7437773?v=3","gravatar_id":"","url":"https://api.github.com/users/robertpustulka","html_url":"https://github.com/robertpustulka","followers_url":"https://api.github.com/users/robertpustulka/followers","following_url":"https://api.github.com/users/robertpustulka/following{/other_user}","gists_url":"https://api.github.com/users/robertpustulka/gists{/gist_id}","starred_url":"https://api.github.com/users/robertpustulka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robertpustulka/subscriptions","organizations_url":"https://api.github.com/users/robertpustulka/orgs","repos_url":"https://api.github.com/users/robertpustulka/repos","events_url":"https://api.github.com/users/robertpustulka/events{/privacy}","received_events_url":"https://api.github.com/users/robertpustulka/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/31","html_url":"https://github.com/cakephp/cakephp/milestones/3.1.0","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/31/labels","id":919718,"number":31,"title":"3.1.0","description":"Backwards compatible with 3.0.x. Will require PHP 5.5\r\n\r\n### Specific issues\r\n\r\n* Adding the exception trace to the error view data. #6000 \r\n* Changed the translate behavior to support translation engines. #5958 \r\n* Database connection prefixes, or table prefixes.","creator":{"login":"lorenzo","id":37621,"avatar_url":"https://avatars.githubusercontent.com/u/37621?v=3","gravatar_id":"","url":"https://api.github.com/users/lorenzo","html_url":"https://github.com/lorenzo","followers_url":"https://api.github.com/users/lorenzo/followers","following_url":"https://api.github.com/users/lorenzo/following{/other_user}","gists_url":"https://api.github.com/users/lorenzo/gists{/gist_id}","starred_url":"https://api.github.com/users/lorenzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lorenzo/subscriptions","organizations_url":"https://api.github.com/users/lorenzo/orgs","repos_url":"https://api.github.com/users/lorenzo/repos","events_url":"https://api.github.com/users/lorenzo/events{/privacy}","received_events_url":"https://api.github.com/users/lorenzo/received_events","type":"User","site_admin":false},"open_issues":49,"closed_issues":28,"state":"open","created_at":"2015-01-02T15:38:07Z","updated_at":"2015-04-08T20:36:15Z","due_on":"2015-07-26T04:00:00Z","closed_at":null},"comments":14,"created_at":"2015-04-01T08:23:11Z","updated_at":"2015-04-09T07:01:56Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/6244","html_url":"https://github.com/cakephp/cakephp/pull/6244","diff_url":"https://github.com/cakephp/cakephp/pull/6244.diff","patch_url":"https://github.com/cakephp/cakephp/pull/6244.patch"},"body":"As discussed in #6227 - here is a sample implementation of a static proxy for TableRegistry.\r\n\r\n`TableRegistry` is now a proxy for *table locators* - classes which implement `LocatorInterface`.\r\nStatic calls work the same: `TableRegistry::get('Posts')`. But now you can swap locator instances:\r\n\r\n```php\r\nTableRegistry::get('Posts'); // makes a call on TableLocator instance\r\nTableRegistry::locator(new MyTableLocator);\r\nTableRegistry::get('Posts'); // now it gets a table from MyTableLocator instance\r\n```\r\n\r\nAnd it's now easy to mock registry in tests:\r\n```php\r\n$locator = $this->getMock('Cake\\ORM\\Locator\\LocatorInterface');\r\n$locator->expects($this->once())->method('get');\r\nTableRegistry::locator($locator);\r\nTableRegistry::get('Posts');\r\n```\r\n\r\n~~The problem is, there would be no method hints in IDEs, but we could make registries implement some `LocatorInterface` with usual `get()`, `config()` etc and place direct proxy static methods in TableRegistry.~~\r\n\r\nTo illustrate the benefits of this solution, I extracted table creation process into a wrapper method. Now I can extend base `TableLocator`, override `_create()` method with some DI magic and swap implementations in bootstrap. From now on all tables would be created by DI and no further changes would be required. Easy!\r\n\r\n\r\nIf nessessary we could do the same for `Log`, `Cache` and `Configure`, maybe more. But these classes are already using easily configurable engines, so there's no real need to do so.\r\n\r\n//edit:\r\n\r\n1. I've added RegistryInterface and made DefaultRegistry implement it.\r\n2. Updated examples to reflect recent changes."},"comment":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/comments/91131616","html_url":"https://github.com/cakephp/cakephp/pull/6244#issuecomment-91131616","issue_url":"https://api.github.com/repos/cakephp/cakephp/issues/6244","id":91131616,"user":{"login":"ADmad","id":142658,"avatar_url":"https://avatars.githubusercontent.com/u/142658?v=3","gravatar_id":"","url":"https://api.github.com/users/ADmad","html_url":"https://github.com/ADmad","followers_url":"https://api.github.com/users/ADmad/followers","following_url":"https://api.github.com/users/ADmad/following{/other_user}","gists_url":"https://api.github.com/users/ADmad/gists{/gist_id}","starred_url":"https://api.github.com/users/ADmad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ADmad/subscriptions","organizations_url":"https://api.github.com/users/ADmad/orgs","repos_url":"https://api.github.com/users/ADmad/repos","events_url":"https://api.github.com/users/ADmad/events{/privacy}","received_events_url":"https://api.github.com/users/ADmad/received_events","type":"User","site_admin":false},"created_at":"2015-04-09T07:01:56Z","updated_at":"2015-04-09T07:01:56Z","body":"@robertpustulka Builds on AppVeyour break often for one reason or another :stuck_out_tongue:  \r\n\r\nBy the way the PR is no longer mergeable so you need to rebase."}}