{"action":"created","issue":{"url":"https://api.github.com/repos/owncloud/core/issues/15763","labels_url":"https://api.github.com/repos/owncloud/core/issues/15763/labels{/name}","comments_url":"https://api.github.com/repos/owncloud/core/issues/15763/comments","events_url":"https://api.github.com/repos/owncloud/core/issues/15763/events","html_url":"https://github.com/owncloud/core/issues/15763","id":69654355,"number":15763,"title":"Make it easy to reuse app containers in other apps","user":{"login":"Raydiation","id":195053,"avatar_url":"https://avatars.githubusercontent.com/u/195053?v=3","gravatar_id":"","url":"https://api.github.com/users/Raydiation","html_url":"https://github.com/Raydiation","followers_url":"https://api.github.com/users/Raydiation/followers","following_url":"https://api.github.com/users/Raydiation/following{/other_user}","gists_url":"https://api.github.com/users/Raydiation/gists{/gist_id}","starred_url":"https://api.github.com/users/Raydiation/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Raydiation/subscriptions","organizations_url":"https://api.github.com/users/Raydiation/orgs","repos_url":"https://api.github.com/users/Raydiation/repos","events_url":"https://api.github.com/users/Raydiation/events{/privacy}","received_events_url":"https://api.github.com/users/Raydiation/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/owncloud/core/labels/discussion","name":"discussion","color":"fbca04"},{"url":"https://api.github.com/repos/owncloud/core/labels/enhancement","name":"enhancement","color":"02e10c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-20T18:46:46Z","updated_at":"2015-04-20T18:54:12Z","closed_at":null,"body":"Sometimes you want to reuse classes from another app. If you do so, you also need to reuse the container because the author could suddenly decide to create a custom factory for a class. So in order to be able to react to that you cant simply resolve that class from your own container.\r\n\r\nI've written a small app that allows you to serve your news app feeds as rss, it also reuses the news app container: https://github.com/Raydiation/feedcentral/blob/master/appinfo/application.php#L28\r\n\r\nThe current way to do this that we need to resolve all deps that we want to use manually, e.g.\r\n\r\n```php\r\n$container->registerService('NewsContainer', function($c) {\r\n    $app = new News();\r\n    return $app->getContainer();\r\n});\r\n$container->registerService('OCA\\News\\Service\\ItemService', function($c) {\r\n    return $c->query('NewsContainer')->query('OCA\\News\\Service\\ItemService');\r\n});\r\n```\r\n\r\nWhat if we could just do the following:\r\n```php\r\n$container->registerAppContainer((new OCA\\News\\Application)->getContainer(), 'News');\r\n```\r\n\r\nAnd now all classes that start with **OCA\\\\News** will automatically be resolved from that app container.\r\n\r\nHere's a small patch to make it clearer how it would work:\r\n\r\n```diff\r\ndiff --git a/lib/private/appframework/utility/simplecontainer.php b/lib/private/appframework/utility/simplecontainer.php\r\nindex c7dff6f..790ab52 100644\r\n--- a/lib/private/appframework/utility/simplecontainer.php\r\n+++ b/lib/private/appframework/utility/simplecontainer.php\r\n@@ -34,6 +34,7 @@ use \\OCP\\AppFramework\\QueryException;\r\n  */\r\n class SimpleContainer extends \\Pimple\\Container implements \\OCP\\IContainer {\r\n \r\n+       private $appContainers = [];\r\n \r\n        /**\r\n         * @param \\ReflectionClass $class the class to instantiate\r\n@@ -94,7 +95,17 @@ class SimpleContainer extends \\Pimple\\Container implements \\OCP\\IContainer {\r\n                if ($this->offsetExists($name)) {\r\n                        return $this->offsetGet($name);\r\n                } else {\r\n-                       $object = $this->resolve($name);\r\n+                       $paths = implode('\\\\', $name, 3); // only the first two are important\r\n+                       $appNamespace = $paths[1];\r\n+\r\n+                       if (count($paths) >= 2 &&\r\n+                               isset($this->appContainers[$appNamespace])\r\n+                               strpos($paths[0], 'OCA') === 0 &&) {\r\n+                               $object = $this->appContainers[$appNamespace]->query($name);\r\n+                       } else {\r\n+                               $object = $this->resolve($name);\r\n+                       }\r\n+\r\n                        $this->registerService($name, function () use ($object) {\r\n                                return $object;\r\n                        });\r\n@@ -111,6 +122,21 @@ class SimpleContainer extends \\Pimple\\Container implements \\OCP\\IContainer {\r\n        }\r\n \r\n        /**\r\n+        * Allows you to register a separate container for a namespace. This makes\r\n+        * it possible to reuse a container of a different app and access all its\r\n+        * classes\r\n+        *\r\n+        * @param SimpleContainer $container the container that should be registered\r\n+        * for the namespace\r\n+        * @param string $appNamespace the app namespace that should resolve to a\r\n+        * different container, e.g. to register the container for OCA\\News\\Class\r\n+        * the string would be News\r\n+        */\r\n+       public function registerAppContainer(SimpleContainer $container, $appNamespace) {\r\n+               $this->appContainers[$appNamespace] = $container;\r\n+       }\r\n+\r\n+       /**\r\n         * The given closure is call the first time the given service is queried.\r\n         * The closure has to return the instance for the given service.\r\n```\r\n\r\n@oparoz @DeepDiver1975 @MorrisJobke @LukasReschke @PVince81 "},"comment":{"url":"https://api.github.com/repos/owncloud/core/issues/comments/94539466","html_url":"https://github.com/owncloud/core/issues/15763#issuecomment-94539466","issue_url":"https://api.github.com/repos/owncloud/core/issues/15763","id":94539466,"user":{"login":"oparoz","id":323220,"avatar_url":"https://avatars.githubusercontent.com/u/323220?v=3","gravatar_id":"","url":"https://api.github.com/users/oparoz","html_url":"https://github.com/oparoz","followers_url":"https://api.github.com/users/oparoz/followers","following_url":"https://api.github.com/users/oparoz/following{/other_user}","gists_url":"https://api.github.com/users/oparoz/gists{/gist_id}","starred_url":"https://api.github.com/users/oparoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oparoz/subscriptions","organizations_url":"https://api.github.com/users/oparoz/orgs","repos_url":"https://api.github.com/users/oparoz/repos","events_url":"https://api.github.com/users/oparoz/events{/privacy}","received_events_url":"https://api.github.com/users/oparoz/received_events","type":"User","site_admin":false},"created_at":"2015-04-20T18:54:12Z","updated_at":"2015-04-20T18:54:12Z","body":"I think it 's an elegant solution to the problems we're going to start facing when the first inter-dependant apps are going to be introduced."}}