Skip to content

Commit bcddca9

Browse files
Merge pull request #4905 from nextcloud/auto-di-for-apps-without-app.php
Allow automatic DI for apps that don't register the container in app.php
2 parents dd29e5a + c1cc819 commit bcddca9

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

lib/private/ServerContainer.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class ServerContainer extends SimpleContainer {
3737
/** @var DIContainer[] */
3838
protected $appContainers;
3939

40+
/** @var string[] */
41+
protected $hasNoAppContainer;
42+
4043
/** @var string[] */
4144
protected $namespaces;
4245

@@ -47,6 +50,7 @@ public function __construct() {
4750
parent::__construct();
4851
$this->appContainers = [];
4952
$this->namespaces = [];
53+
$this->hasNoAppContainer = [];
5054
}
5155

5256
/**
@@ -69,15 +73,27 @@ public function registerAppContainer($appName, DIContainer $container) {
6973

7074
/**
7175
* @param string $namespace
76+
* @param string $sensitiveNamespace
7277
* @return DIContainer
7378
* @throws QueryException
7479
*/
75-
protected function getAppContainer($namespace) {
80+
protected function getAppContainer($namespace, $sensitiveNamespace) {
7681
if (isset($this->appContainers[$namespace])) {
7782
return $this->appContainers[$namespace];
7883
}
7984

8085
if (isset($this->namespaces[$namespace])) {
86+
if (!isset($this->hasNoAppContainer[$namespace])) {
87+
$applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
88+
if (class_exists($applicationClassName)) {
89+
new $applicationClassName();
90+
if (isset($this->appContainers[$namespace])) {
91+
return $this->appContainers[$namespace];
92+
}
93+
}
94+
$this->hasNoAppContainer[$namespace] = true;
95+
}
96+
8197
return new DIContainer($this->namespaces[$namespace]);
8298
}
8399
throw new QueryException();
@@ -96,7 +112,7 @@ public function query($name) {
96112
if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
97113
$segments = explode('\\', $name);
98114
try {
99-
$appContainer = $this->getAppContainer(strtolower($segments[1]));
115+
$appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
100116
return $appContainer->queryNoFallback($name);
101117
} catch (QueryException $e) {
102118
// Didn't find the service or the respective app container,

0 commit comments

Comments
 (0)