Skip to content

Commit 4152216

Browse files
Use PSR container interface and deprecate our own abstraction
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent b12d369 commit 4152216

File tree

10 files changed

+245
-141
lines changed

10 files changed

+245
-141
lines changed

3rdparty

lib/private/AppFramework/DependencyInjection/DIContainer.php

Lines changed: 94 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@
6969
use OCP\ISession;
7070
use OCP\IURLGenerator;
7171
use OCP\IUserSession;
72+
use Psr\Container\ContainerInterface;
7273

74+
/**
75+
* @deprecated 20.0.0
76+
*/
7377
class DIContainer extends SimpleContainer implements IAppContainer {
7478

7579
/**
@@ -116,58 +120,59 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
116120
return $this->getServer()->getUserFolder();
117121
});
118122

119-
$this->registerService(IAppData::class, function (SimpleContainer $c) {
120-
return $this->getServer()->getAppDataDir($c->query('AppName'));
123+
$this->registerService(IAppData::class, function (ContainerInterface $c) {
124+
return $this->getServer()->getAppDataDir($c->get('AppName'));
121125
});
122126

123-
$this->registerService(IL10N::class, function ($c) {
124-
return $this->getServer()->getL10N($c->query('AppName'));
127+
$this->registerService(IL10N::class, function (ContainerInterface $c) {
128+
return $this->getServer()->getL10N($c->get('AppName'));
125129
});
126130

127131
// Log wrapper
128-
$this->registerService(ILogger::class, function ($c) {
129-
return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName'));
132+
$this->registerService(ILogger::class, function (ContainerInterface $c) {
133+
return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName'));
130134
});
131135

132136
$this->registerService(IServerContainer::class, function () {
133137
return $this->getServer();
134138
});
135139
$this->registerAlias('ServerContainer', IServerContainer::class);
136140

137-
$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
138-
return $c->query(Manager::class);
141+
$this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) {
142+
return $c->get(Manager::class);
139143
});
140144

141-
$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
145+
$this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
142146
return $c;
143147
});
148+
$this->registerAlias(IAppContainer::class, ContainerInterface::class);
144149

145150
// commonly used attributes
146-
$this->registerService('UserId', function ($c) {
147-
return $c->query(IUserSession::class)->getSession()->get('user_id');
151+
$this->registerService('UserId', function (ContainerInterface $c) {
152+
return $c->get(IUserSession::class)->getSession()->get('user_id');
148153
});
149154

150-
$this->registerService('WebRoot', function ($c) {
151-
return $c->query('ServerContainer')->getWebRoot();
155+
$this->registerService('WebRoot', function (ContainerInterface $c) {
156+
return $c->get(IServerContainer::class)->getWebRoot();
152157
});
153158

154-
$this->registerService('OC_Defaults', function ($c) {
155-
return $c->getServer()->getThemingDefaults();
159+
$this->registerService('OC_Defaults', function (ContainerInterface $c) {
160+
return $c->get(IServerContainer::class)->getThemingDefaults();
156161
});
157162

158-
$this->registerService('Protocol', function ($c) {
163+
$this->registerService('Protocol', function (ContainerInterface $c) {
159164
/** @var \OC\Server $server */
160-
$server = $c->query('ServerContainer');
165+
$server = $c->get(IServerContainer::class);
161166
$protocol = $server->getRequest()->getHttpProtocol();
162167
return new Http($_SERVER, $protocol);
163168
});
164169

165-
$this->registerService('Dispatcher', function ($c) {
170+
$this->registerService('Dispatcher', function (ContainerInterface $c) {
166171
return new Dispatcher(
167-
$c['Protocol'],
168-
$c['MiddlewareDispatcher'],
169-
$c->query(IControllerMethodReflector::class),
170-
$c['Request']
172+
$c->get('Protocol'),
173+
$c->get(MiddlewareDispatcher::class),
174+
$c->get(IControllerMethodReflector::class),
175+
$c->get(IRequest::class)
171176
);
172177
});
173178

@@ -181,48 +186,49 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
181186
/**
182187
* Middleware
183188
*/
184-
$this->registerService('MiddlewareDispatcher', function (SimpleContainer $c) {
185-
$server = $this->getServer();
189+
$this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class);
190+
$this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) {
191+
$server = $this->getServer();
186192

187193
$dispatcher = new MiddlewareDispatcher();
188194

189195
$dispatcher->registerMiddleware(
190-
$c->query(OC\AppFramework\Middleware\CompressionMiddleware::class)
196+
$c->get(OC\AppFramework\Middleware\CompressionMiddleware::class)
191197
);
192198

193-
$dispatcher->registerMiddleware($c->query(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
199+
$dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
194200

195201
$dispatcher->registerMiddleware(
196-
$c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
202+
$c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
197203
);
198204

199205
$dispatcher->registerMiddleware(
200206
new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
201-
$c->query(IRequest::class),
202-
$c->query(IControllerMethodReflector::class)
207+
$c->get(IRequest::class),
208+
$c->get(IControllerMethodReflector::class)
203209
)
204210
);
205211
$dispatcher->registerMiddleware(
206212
new CORSMiddleware(
207-
$c->query(IRequest::class),
208-
$c->query(IControllerMethodReflector::class),
209-
$c->query(IUserSession::class),
210-
$c->query(OC\Security\Bruteforce\Throttler::class)
213+
$c->get(IRequest::class),
214+
$c->get(IControllerMethodReflector::class),
215+
$c->get(IUserSession::class),
216+
$c->get(OC\Security\Bruteforce\Throttler::class)
211217
)
212218
);
213219
$dispatcher->registerMiddleware(
214220
new OCSMiddleware(
215-
$c->query(IRequest::class)
221+
$c->get(IRequest::class)
216222
)
217223
);
218224

219225
$securityMiddleware = new SecurityMiddleware(
220-
$c->query(IRequest::class),
221-
$c->query(IControllerMethodReflector::class),
222-
$c->query(INavigationManager::class),
223-
$c->query(IURLGenerator::class),
224-
$server->getLogger(),
225-
$c['AppName'],
226+
$c->get(IRequest::class),
227+
$c->get(IControllerMethodReflector::class),
228+
$c->get(INavigationManager::class),
229+
$c->get(IURLGenerator::class),
230+
$server->query(ILogger::class),
231+
$c->get('AppName'),
226232
$server->getUserSession()->isLoggedIn(),
227233
$server->getGroupManager()->isAdmin($this->getUserId()),
228234
$server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()),
@@ -242,71 +248,71 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
242248
);
243249
$dispatcher->registerMiddleware(
244250
new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
245-
$c->query(IControllerMethodReflector::class),
246-
$c->query(ISession::class),
247-
$c->query(IUserSession::class),
248-
$c->query(ITimeFactory::class)
251+
$c->get(IControllerMethodReflector::class),
252+
$c->get(ISession::class),
253+
$c->get(IUserSession::class),
254+
$c->get(ITimeFactory::class)
249255
)
250256
);
251257
$dispatcher->registerMiddleware(
252258
new TwoFactorMiddleware(
253-
$c->query(OC\Authentication\TwoFactorAuth\Manager::class),
254-
$c->query(IUserSession::class),
255-
$c->query(ISession::class),
256-
$c->query(IURLGenerator::class),
257-
$c->query(IControllerMethodReflector::class),
258-
$c->query(IRequest::class)
259+
$c->get(OC\Authentication\TwoFactorAuth\Manager::class),
260+
$c->get(IUserSession::class),
261+
$c->get(ISession::class),
262+
$c->get(IURLGenerator::class),
263+
$c->get(IControllerMethodReflector::class),
264+
$c->get(IRequest::class)
259265
)
260266
);
261267
$dispatcher->registerMiddleware(
262268
new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
263-
$c->query(IControllerMethodReflector::class),
264-
$c->query(OC\Security\Bruteforce\Throttler::class),
265-
$c->query(IRequest::class)
269+
$c->get(IControllerMethodReflector::class),
270+
$c->get(OC\Security\Bruteforce\Throttler::class),
271+
$c->get(IRequest::class)
266272
)
267273
);
268274
$dispatcher->registerMiddleware(
269275
new RateLimitingMiddleware(
270-
$c->query(IRequest::class),
271-
$c->query(IUserSession::class),
272-
$c->query(IControllerMethodReflector::class),
273-
$c->query(OC\Security\RateLimiting\Limiter::class)
276+
$c->get(IRequest::class),
277+
$c->get(IUserSession::class),
278+
$c->get(IControllerMethodReflector::class),
279+
$c->get(OC\Security\RateLimiting\Limiter::class)
274280
)
275281
);
276282
$dispatcher->registerMiddleware(
277283
new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
278-
$c->query(IRequest::class),
279-
$c->query(ISession::class),
280-
$c->query(\OCP\IConfig::class)
284+
$c->get(IRequest::class),
285+
$c->get(ISession::class),
286+
$c->get(\OCP\IConfig::class)
281287
)
282288
);
283289
$dispatcher->registerMiddleware(
284-
$c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
290+
$c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
285291
);
286292

287293
foreach ($this->middleWares as $middleWare) {
288-
$dispatcher->registerMiddleware($c->query($middleWare));
294+
$dispatcher->registerMiddleware($c->get($middleWare));
289295
}
290296

291297
$dispatcher->registerMiddleware(
292298
new SessionMiddleware(
293-
$c->query(IControllerMethodReflector::class),
294-
$c->query(ISession::class)
299+
$c->get(IControllerMethodReflector::class),
300+
$c->get(ISession::class)
295301
)
296302
);
297303
return $dispatcher;
298304
});
299305

300-
$this->registerService(IAppConfig::class, function (SimpleContainer $c) {
306+
$this->registerService(IAppConfig::class, function (ContainerInterface $c) {
301307
return new OC\AppFramework\Services\AppConfig(
302-
$c->query(IConfig::class),
303-
$c->query('AppName')
308+
$c->get(IConfig::class),
309+
$c->get('AppName')
304310
);
305311
});
306-
$this->registerService(IInitialState::class, function (SimpleContainer $c) {
312+
$this->registerService(IInitialState::class, function (ContainerInterface $c) {
307313
return new OC\AppFramework\Services\InitialState(
308-
$c->query(IInitialStateService::class),
309-
$c->query('AppName')
314+
$c->get(IInitialStateService::class),
315+
$c->get('AppName')
310316
);
311317
});
312318
}
@@ -396,6 +402,18 @@ public function registerCapability($serviceName) {
396402
});
397403
}
398404

405+
public function has($id): bool {
406+
if (parent::has($id)) {
407+
return true;
408+
}
409+
410+
if ($this->server->has($id, true)) {
411+
return true;
412+
}
413+
414+
return false;
415+
}
416+
399417
public function query(string $name, bool $autoload = true) {
400418
try {
401419
return $this->queryNoFallback($name);
@@ -421,14 +439,12 @@ public function queryNoFallback($name) {
421439

422440
if ($this->offsetExists($name)) {
423441
return parent::query($name);
424-
} else {
425-
if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
426-
return parent::query($name);
427-
} elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
428-
return parent::query($name);
429-
} elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
430-
return parent::query($name);
431-
}
442+
} elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
443+
return parent::query($name);
444+
} elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
445+
return parent::query($name);
446+
} elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
447+
return parent::query($name);
432448
}
433449

434450
throw new QueryException('Could not resolve ' . $name . '!' .

0 commit comments

Comments
 (0)