Skip to content

Commit f4013f3

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

File tree

4 files changed

+85
-82
lines changed

4 files changed

+85
-82
lines changed

lib/private/AppFramework/DependencyInjection/DIContainer.php

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

7374
/**
7475
* @deprecated 20.0.0
@@ -119,58 +120,59 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
119120
return $this->getServer()->getUserFolder();
120121
});
121122

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

126-
$this->registerService(IL10N::class, function ($c) {
127-
return $this->getServer()->getL10N($c->query('AppName'));
127+
$this->registerService(IL10N::class, function (ContainerInterface $c) {
128+
return $this->getServer()->getL10N($c->get('AppName'));
128129
});
129130

130131
// Log wrapper
131-
$this->registerService(ILogger::class, function ($c) {
132-
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'));
133134
});
134135

135136
$this->registerService(IServerContainer::class, function () {
136137
return $this->getServer();
137138
});
138139
$this->registerAlias('ServerContainer', IServerContainer::class);
139140

140-
$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
141-
return $c->query(Manager::class);
141+
$this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) {
142+
return $c->get(Manager::class);
142143
});
143144

144-
$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
145+
$this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
145146
return $c;
146147
});
148+
$this->registerAlias(IAppContainer::class, ContainerInterface::class);
147149

148150
// commonly used attributes
149-
$this->registerService('UserId', function ($c) {
150-
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');
151153
});
152154

153-
$this->registerService('WebRoot', function ($c) {
154-
return $c->query('ServerContainer')->getWebRoot();
155+
$this->registerService('WebRoot', function (ContainerInterface $c) {
156+
return $c->get(IServerContainer::class)->getWebRoot();
155157
});
156158

157-
$this->registerService('OC_Defaults', function ($c) {
158-
return $c->getServer()->getThemingDefaults();
159+
$this->registerService('OC_Defaults', function (ContainerInterface $c) {
160+
return $c->get(IServerContainer::class)->getThemingDefaults();
159161
});
160162

161-
$this->registerService('Protocol', function ($c) {
163+
$this->registerService('Protocol', function (ContainerInterface $c) {
162164
/** @var \OC\Server $server */
163-
$server = $c->query('ServerContainer');
165+
$server = $c->get(IServerContainer::class);
164166
$protocol = $server->getRequest()->getHttpProtocol();
165167
return new Http($_SERVER, $protocol);
166168
});
167169

168-
$this->registerService('Dispatcher', function ($c) {
170+
$this->registerService('Dispatcher', function (ContainerInterface $c) {
169171
return new Dispatcher(
170-
$c['Protocol'],
171-
$c['MiddlewareDispatcher'],
172-
$c->query(IControllerMethodReflector::class),
173-
$c['Request']
172+
$c->get('Protocol'),
173+
$c->get(MiddlewareDispatcher::class),
174+
$c->get(IControllerMethodReflector::class),
175+
$c->get(IRequest::class)
174176
);
175177
});
176178

@@ -184,48 +186,48 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
184186
/**
185187
* Middleware
186188
*/
187-
$this->registerService('MiddlewareDispatcher', function (SimpleContainer $c) {
188-
$server = $this->getServer();
189+
$this->registerService('MiddlewareDispatcher', function (ContainerInterface $c) {
190+
$server = $this->getServer();
189191

190192
$dispatcher = new MiddlewareDispatcher();
191193

192194
$dispatcher->registerMiddleware(
193-
$c->query(OC\AppFramework\Middleware\CompressionMiddleware::class)
195+
$c->get(OC\AppFramework\Middleware\CompressionMiddleware::class)
194196
);
195197

196-
$dispatcher->registerMiddleware($c->query(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
198+
$dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
197199

198200
$dispatcher->registerMiddleware(
199-
$c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
201+
$c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
200202
);
201203

202204
$dispatcher->registerMiddleware(
203205
new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
204-
$c->query(IRequest::class),
205-
$c->query(IControllerMethodReflector::class)
206+
$c->get(IRequest::class),
207+
$c->get(IControllerMethodReflector::class)
206208
)
207209
);
208210
$dispatcher->registerMiddleware(
209211
new CORSMiddleware(
210-
$c->query(IRequest::class),
211-
$c->query(IControllerMethodReflector::class),
212-
$c->query(IUserSession::class),
213-
$c->query(OC\Security\Bruteforce\Throttler::class)
212+
$c->get(IRequest::class),
213+
$c->get(IControllerMethodReflector::class),
214+
$c->get(IUserSession::class),
215+
$c->get(OC\Security\Bruteforce\Throttler::class)
214216
)
215217
);
216218
$dispatcher->registerMiddleware(
217219
new OCSMiddleware(
218-
$c->query(IRequest::class)
220+
$c->get(IRequest::class)
219221
)
220222
);
221223

222224
$securityMiddleware = new SecurityMiddleware(
223-
$c->query(IRequest::class),
224-
$c->query(IControllerMethodReflector::class),
225-
$c->query(INavigationManager::class),
226-
$c->query(IURLGenerator::class),
227-
$server->getLogger(),
228-
$c['AppName'],
225+
$c->get(IRequest::class),
226+
$c->get(IControllerMethodReflector::class),
227+
$c->get(INavigationManager::class),
228+
$c->get(IURLGenerator::class),
229+
$server->query(ILogger::class),
230+
$c->get('AppName'),
229231
$server->getUserSession()->isLoggedIn(),
230232
$server->getGroupManager()->isAdmin($this->getUserId()),
231233
$server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()),
@@ -245,71 +247,71 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
245247
);
246248
$dispatcher->registerMiddleware(
247249
new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
248-
$c->query(IControllerMethodReflector::class),
249-
$c->query(ISession::class),
250-
$c->query(IUserSession::class),
251-
$c->query(ITimeFactory::class)
250+
$c->get(IControllerMethodReflector::class),
251+
$c->get(ISession::class),
252+
$c->get(IUserSession::class),
253+
$c->get(ITimeFactory::class)
252254
)
253255
);
254256
$dispatcher->registerMiddleware(
255257
new TwoFactorMiddleware(
256-
$c->query(OC\Authentication\TwoFactorAuth\Manager::class),
257-
$c->query(IUserSession::class),
258-
$c->query(ISession::class),
259-
$c->query(IURLGenerator::class),
260-
$c->query(IControllerMethodReflector::class),
261-
$c->query(IRequest::class)
258+
$c->get(OC\Authentication\TwoFactorAuth\Manager::class),
259+
$c->get(IUserSession::class),
260+
$c->get(ISession::class),
261+
$c->get(IURLGenerator::class),
262+
$c->get(IControllerMethodReflector::class),
263+
$c->get(IRequest::class)
262264
)
263265
);
264266
$dispatcher->registerMiddleware(
265267
new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
266-
$c->query(IControllerMethodReflector::class),
267-
$c->query(OC\Security\Bruteforce\Throttler::class),
268-
$c->query(IRequest::class)
268+
$c->get(IControllerMethodReflector::class),
269+
$c->get(OC\Security\Bruteforce\Throttler::class),
270+
$c->get(IRequest::class)
269271
)
270272
);
271273
$dispatcher->registerMiddleware(
272274
new RateLimitingMiddleware(
273-
$c->query(IRequest::class),
274-
$c->query(IUserSession::class),
275-
$c->query(IControllerMethodReflector::class),
276-
$c->query(OC\Security\RateLimiting\Limiter::class)
275+
$c->get(IRequest::class),
276+
$c->get(IUserSession::class),
277+
$c->get(IControllerMethodReflector::class),
278+
$c->get(OC\Security\RateLimiting\Limiter::class)
277279
)
278280
);
279281
$dispatcher->registerMiddleware(
280282
new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
281-
$c->query(IRequest::class),
282-
$c->query(ISession::class),
283-
$c->query(\OCP\IConfig::class)
283+
$c->get(IRequest::class),
284+
$c->get(ISession::class),
285+
$c->get(\OCP\IConfig::class)
284286
)
285287
);
286288
$dispatcher->registerMiddleware(
287-
$c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
289+
$c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
288290
);
289291

290292
foreach ($this->middleWares as $middleWare) {
291-
$dispatcher->registerMiddleware($c->query($middleWare));
293+
$dispatcher->registerMiddleware($c->get($middleWare));
292294
}
293295

294296
$dispatcher->registerMiddleware(
295297
new SessionMiddleware(
296-
$c->query(IControllerMethodReflector::class),
297-
$c->query(ISession::class)
298+
$c->get(IControllerMethodReflector::class),
299+
$c->get(ISession::class)
298300
)
299301
);
300302
return $dispatcher;
301303
});
302304

303-
$this->registerService(IAppConfig::class, function (SimpleContainer $c) {
305+
$this->registerService(IAppConfig::class, function (ContainerInterface $c) {
304306
return new OC\AppFramework\Services\AppConfig(
305-
$c->query(IConfig::class),
306-
$c->query('AppName')
307+
$c->get(IConfig::class),
308+
$c->get('AppName')
307309
);
308310
});
309-
$this->registerService(IInitialState::class, function (SimpleContainer $c) {
311+
$this->registerService(IInitialState::class, function (ContainerInterface $c) {
310312
return new OC\AppFramework\Services\InitialState(
311-
$c->query(IInitialStateService::class),
312-
$c->query('AppName')
313+
$c->get(IInitialStateService::class),
314+
$c->get('AppName')
313315
);
314316
});
315317
}

lib/private/AppFramework/Utility/SimpleContainer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
use ArrayAccess;
3434
use Closure;
35-
use Exception;
3635
use OCP\AppFramework\QueryException;
3736
use OCP\IContainer;
3837
use Pimple\Container;
@@ -153,7 +152,7 @@ public function registerParameter($name, $value) {
153152
* @param bool $shared
154153
*/
155154
public function registerService($name, Closure $closure, $shared = true) {
156-
$wrapped = function() use ($closure) {
155+
$wrapped = function () use ($closure) {
157156
return $closure($this);
158157
};
159158
$name = $this->sanitizeName($name);
@@ -175,7 +174,7 @@ public function registerService($name, Closure $closure, $shared = true) {
175174
* @param string $target the target that should be resolved instead
176175
*/
177176
public function registerAlias($alias, $target) {
178-
$this->registerService($alias, function (IContainer $container) use ($target) {
177+
$this->registerService($alias, function (ContainerInterface $container) use ($target) {
179178
return $container->get($target);
180179
}, false);
181180
}

lib/private/Server.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@
139139
use OCA\Theming\Util;
140140
use OCP\Accounts\IAccountManager;
141141
use OCP\App\IAppManager;
142-
use OCP\AppFramework\QueryException;
143142
use OCP\Authentication\LoginCredentials\IStore;
144143
use OCP\BackgroundJob\IJobList;
145144
use OCP\Collaboration\AutoComplete\IManager;
@@ -178,7 +177,6 @@
178177
use OCP\IAvatarManager;
179178
use OCP\ICache;
180179
use OCP\ICacheFactory;
181-
use OCP\IContainer;
182180
use OCP\IDateTimeFormatter;
183181
use OCP\IDateTimeZone;
184182
use OCP\IDBConnection;
@@ -226,6 +224,8 @@
226224
use OCP\User\Events\UserLoggedInEvent;
227225
use OCP\User\Events\UserLoggedInWithCookieEvent;
228226
use OCP\User\Events\UserLoggedOutEvent;
227+
use Psr\Container\ContainerExceptionInterface;
228+
use Psr\Container\ContainerInterface;
229229
use Psr\Log\LoggerInterface;
230230
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
231231
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -257,7 +257,10 @@ public function __construct($webRoot, \OC\Config $config) {
257257
// To find out if we are running from CLI or not
258258
$this->registerParameter('isCLI', \OC::$CLI);
259259

260-
$this->registerService(\OCP\IServerContainer::class, function (IServerContainer $c) {
260+
$this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
261+
return $c;
262+
});
263+
$this->registerService(\OCP\IServerContainer::class, function (ContainerInterface $c) {
261264
return $c;
262265
});
263266

@@ -2233,16 +2236,16 @@ public function getGeneratorHelper() {
22332236
}
22342237

22352238
private function registerDeprecatedAlias(string $alias, string $target) {
2236-
$this->registerService($alias, function (IContainer $container) use ($target, $alias) {
2239+
$this->registerService($alias, function (ContainerInterface $container) use ($target, $alias) {
22372240
try {
22382241
/** @var ILogger $logger */
2239-
$logger = $container->query(ILogger::class);
2242+
$logger = $container->get(ILogger::class);
22402243
$logger->debug('The requested alias "' . $alias . '" is depreacted. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', ['app' => 'serverDI']);
2241-
} catch (QueryException $e) {
2244+
} catch (ContainerExceptionInterface $e) {
22422245
// Could not get logger. Continue
22432246
}
22442247

2245-
return $container->query($target);
2248+
return $container->get($target);
22462249
}, false);
22472250
}
22482251
}

lib/private/ServerContainer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use OC\AppFramework\DependencyInjection\DIContainer;
3232
use OC\AppFramework\Utility\SimpleContainer;
3333
use OCP\AppFramework\QueryException;
34-
use OCP\IContainer;
3534
use function explode;
3635
use function strtolower;
3736

@@ -155,7 +154,7 @@ public function query(string $name, bool $autoload = true) {
155154
}
156155

157156
private function getAppContainerForService(string $id): ?DIContainer {
158-
if(strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
157+
if (strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
159158
return null;
160159
}
161160

0 commit comments

Comments
 (0)