| Total Complexity | 40 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DIContainer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DIContainer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 81 | class DIContainer extends SimpleContainer implements IAppContainer { |
||
|
|
|||
| 82 | private string $appName; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private $middleWares = []; |
||
| 88 | |||
| 89 | /** @var ServerContainer */ |
||
| 90 | private $server; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Put your class dependencies in here |
||
| 94 | * @param string $appName the name of the app |
||
| 95 | * @param array $urlParams |
||
| 96 | * @param ServerContainer|null $server |
||
| 97 | */ |
||
| 98 | public function __construct(string $appName, array $urlParams = [], ServerContainer $server = null) { |
||
| 99 | parent::__construct(); |
||
| 100 | $this->appName = $appName; |
||
| 101 | $this['appName'] = $appName; |
||
| 102 | $this['urlParams'] = $urlParams; |
||
| 103 | |||
| 104 | $this->registerAlias('Request', IRequest::class); |
||
| 105 | |||
| 106 | /** @var \OC\ServerContainer $server */ |
||
| 107 | if ($server === null) { |
||
| 108 | $server = \OC::$server; |
||
| 109 | } |
||
| 110 | $this->server = $server; |
||
| 111 | $this->server->registerAppContainer($appName, $this); |
||
| 112 | |||
| 113 | // aliases |
||
| 114 | /** @deprecated inject $appName */ |
||
| 115 | $this->registerAlias('AppName', 'appName'); |
||
| 116 | /** @deprecated inject $webRoot*/ |
||
| 117 | $this->registerAlias('WebRoot', 'webRoot'); |
||
| 118 | /** @deprecated inject $userId */ |
||
| 119 | $this->registerAlias('UserId', 'userId'); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Core services |
||
| 123 | */ |
||
| 124 | $this->registerService(IOutput::class, function () { |
||
| 125 | return new Output($this->getServer()->getWebRoot()); |
||
| 126 | }); |
||
| 127 | |||
| 128 | $this->registerService(Folder::class, function () { |
||
| 129 | return $this->getServer()->getUserFolder(); |
||
| 130 | }); |
||
| 131 | |||
| 132 | $this->registerService(IAppData::class, function (ContainerInterface $c) { |
||
| 133 | return $this->getServer()->getAppDataDir($c->get('AppName')); |
||
| 134 | }); |
||
| 135 | |||
| 136 | $this->registerService(IL10N::class, function (ContainerInterface $c) { |
||
| 137 | return $this->getServer()->getL10N($c->get('AppName')); |
||
| 138 | }); |
||
| 139 | |||
| 140 | // Log wrappers |
||
| 141 | $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
||
| 142 | return new ScopedPsrLogger( |
||
| 143 | $c->get(PsrLoggerAdapter::class), |
||
| 144 | $c->get('AppName') |
||
| 145 | ); |
||
| 146 | }); |
||
| 147 | $this->registerService(ILogger::class, function (ContainerInterface $c) { |
||
| 148 | return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName')); |
||
| 149 | }); |
||
| 150 | |||
| 151 | $this->registerService(IServerContainer::class, function () { |
||
| 152 | return $this->getServer(); |
||
| 153 | }); |
||
| 154 | $this->registerAlias('ServerContainer', IServerContainer::class); |
||
| 155 | |||
| 156 | $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { |
||
| 157 | return $c->get(Manager::class); |
||
| 158 | }); |
||
| 159 | |||
| 160 | $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
||
| 161 | return $c; |
||
| 162 | }); |
||
| 163 | $this->registerAlias(IAppContainer::class, ContainerInterface::class); |
||
| 164 | |||
| 165 | // commonly used attributes |
||
| 166 | $this->registerService('userId', function (ContainerInterface $c) { |
||
| 167 | return $c->get(IUserSession::class)->getSession()->get('user_id'); |
||
| 168 | }); |
||
| 169 | |||
| 170 | $this->registerService('webRoot', function (ContainerInterface $c) { |
||
| 171 | return $c->get(IServerContainer::class)->getWebRoot(); |
||
| 172 | }); |
||
| 173 | |||
| 174 | $this->registerService('OC_Defaults', function (ContainerInterface $c) { |
||
| 175 | return $c->get(IServerContainer::class)->getThemingDefaults(); |
||
| 176 | }); |
||
| 177 | |||
| 178 | $this->registerService('Protocol', function (ContainerInterface $c) { |
||
| 179 | /** @var \OC\Server $server */ |
||
| 180 | $server = $c->get(IServerContainer::class); |
||
| 181 | $protocol = $server->getRequest()->getHttpProtocol(); |
||
| 182 | return new Http($_SERVER, $protocol); |
||
| 183 | }); |
||
| 184 | |||
| 185 | $this->registerService('Dispatcher', function (ContainerInterface $c) { |
||
| 186 | return new Dispatcher( |
||
| 187 | $c->get('Protocol'), |
||
| 188 | $c->get(MiddlewareDispatcher::class), |
||
| 189 | $c->get(IControllerMethodReflector::class), |
||
| 190 | $c->get(IRequest::class), |
||
| 191 | $c->get(IConfig::class), |
||
| 192 | $c->get(IDBConnection::class), |
||
| 193 | $c->get(LoggerInterface::class), |
||
| 194 | $c->get(EventLogger::class), |
||
| 195 | $c, |
||
| 196 | ); |
||
| 197 | }); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * App Framework default arguments |
||
| 201 | */ |
||
| 202 | $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
||
| 203 | $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
||
| 204 | $this->registerParameter('corsMaxAge', 1728000); |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Middleware |
||
| 208 | */ |
||
| 209 | $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
||
| 210 | $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
||
| 211 | $server = $this->getServer(); |
||
| 212 | |||
| 213 | $dispatcher = new MiddlewareDispatcher(); |
||
| 214 | |||
| 215 | $dispatcher->registerMiddleware( |
||
| 216 | $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class) |
||
| 217 | ); |
||
| 218 | |||
| 219 | $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
||
| 220 | |||
| 221 | $dispatcher->registerMiddleware( |
||
| 222 | $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
||
| 223 | ); |
||
| 224 | |||
| 225 | $dispatcher->registerMiddleware( |
||
| 226 | new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
||
| 227 | $c->get(IRequest::class), |
||
| 228 | $c->get(IControllerMethodReflector::class) |
||
| 229 | ) |
||
| 230 | ); |
||
| 231 | $dispatcher->registerMiddleware( |
||
| 232 | new CORSMiddleware( |
||
| 233 | $c->get(IRequest::class), |
||
| 234 | $c->get(IControllerMethodReflector::class), |
||
| 235 | $c->get(IUserSession::class), |
||
| 236 | $c->get(OC\Security\Bruteforce\Throttler::class) |
||
| 237 | ) |
||
| 238 | ); |
||
| 239 | $dispatcher->registerMiddleware( |
||
| 240 | new OCSMiddleware( |
||
| 241 | $c->get(IRequest::class) |
||
| 242 | ) |
||
| 243 | ); |
||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | $securityMiddleware = new SecurityMiddleware( |
||
| 248 | $c->get(IRequest::class), |
||
| 249 | $c->get(IControllerMethodReflector::class), |
||
| 250 | $c->get(INavigationManager::class), |
||
| 251 | $c->get(IURLGenerator::class), |
||
| 252 | $server->get(LoggerInterface::class), |
||
| 253 | $c->get('AppName'), |
||
| 254 | $server->getUserSession()->isLoggedIn(), |
||
| 255 | $this->getUserId() !== null && $server->getGroupManager()->isAdmin($this->getUserId()), |
||
| 256 | $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
||
| 257 | $server->getAppManager(), |
||
| 258 | $server->getL10N('lib'), |
||
| 259 | $c->get(AuthorizedGroupMapper::class), |
||
| 260 | $server->get(IUserSession::class) |
||
| 261 | ); |
||
| 262 | $dispatcher->registerMiddleware($securityMiddleware); |
||
| 263 | $dispatcher->registerMiddleware( |
||
| 264 | new OC\AppFramework\Middleware\Security\CSPMiddleware( |
||
| 265 | $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
||
| 266 | $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
||
| 267 | $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
||
| 268 | ) |
||
| 269 | ); |
||
| 270 | $dispatcher->registerMiddleware( |
||
| 271 | $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
||
| 272 | ); |
||
| 273 | $dispatcher->registerMiddleware( |
||
| 274 | new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
||
| 275 | $c->get(IControllerMethodReflector::class), |
||
| 276 | $c->get(ISession::class), |
||
| 277 | $c->get(IUserSession::class), |
||
| 278 | $c->get(ITimeFactory::class) |
||
| 279 | ) |
||
| 280 | ); |
||
| 281 | $dispatcher->registerMiddleware( |
||
| 282 | new TwoFactorMiddleware( |
||
| 283 | $c->get(OC\Authentication\TwoFactorAuth\Manager::class), |
||
| 284 | $c->get(IUserSession::class), |
||
| 285 | $c->get(ISession::class), |
||
| 286 | $c->get(IURLGenerator::class), |
||
| 287 | $c->get(IControllerMethodReflector::class), |
||
| 288 | $c->get(IRequest::class) |
||
| 289 | ) |
||
| 290 | ); |
||
| 291 | $dispatcher->registerMiddleware( |
||
| 292 | new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
||
| 293 | $c->get(IControllerMethodReflector::class), |
||
| 294 | $c->get(OC\Security\Bruteforce\Throttler::class), |
||
| 295 | $c->get(IRequest::class) |
||
| 296 | ) |
||
| 297 | ); |
||
| 298 | $dispatcher->registerMiddleware( |
||
| 299 | new RateLimitingMiddleware( |
||
| 300 | $c->get(IRequest::class), |
||
| 301 | $c->get(IUserSession::class), |
||
| 302 | $c->get(IControllerMethodReflector::class), |
||
| 303 | $c->get(OC\Security\RateLimiting\Limiter::class) |
||
| 304 | ) |
||
| 305 | ); |
||
| 306 | $dispatcher->registerMiddleware( |
||
| 307 | new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
||
| 308 | $c->get(IRequest::class), |
||
| 309 | $c->get(ISession::class), |
||
| 310 | $c->get(\OCP\IConfig::class), |
||
| 311 | $c->get(OC\Security\Bruteforce\Throttler::class) |
||
| 312 | ) |
||
| 313 | ); |
||
| 314 | $dispatcher->registerMiddleware( |
||
| 315 | $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
||
| 316 | ); |
||
| 317 | |||
| 318 | /** @var \OC\AppFramework\Bootstrap\Coordinator $coordinator */ |
||
| 319 | $coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class); |
||
| 320 | $registrationContext = $coordinator->getRegistrationContext(); |
||
| 321 | if ($registrationContext !== null) { |
||
| 322 | $appId = $this->getAppName(); |
||
| 323 | foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) { |
||
| 324 | if ($middlewareRegistration->getAppId() === $appId) { |
||
| 325 | $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService())); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | } |
||
| 329 | foreach ($this->middleWares as $middleWare) { |
||
| 330 | $dispatcher->registerMiddleware($c->get($middleWare)); |
||
| 331 | } |
||
| 332 | |||
| 333 | $dispatcher->registerMiddleware( |
||
| 334 | new SessionMiddleware( |
||
| 335 | $c->get(IControllerMethodReflector::class), |
||
| 336 | $c->get(ISession::class) |
||
| 337 | ) |
||
| 338 | ); |
||
| 339 | return $dispatcher; |
||
| 340 | }); |
||
| 341 | |||
| 342 | $this->registerService(IAppConfig::class, function (ContainerInterface $c) { |
||
| 343 | return new OC\AppFramework\Services\AppConfig( |
||
| 344 | $c->get(IConfig::class), |
||
| 345 | $c->get('AppName') |
||
| 346 | ); |
||
| 347 | }); |
||
| 348 | $this->registerService(IInitialState::class, function (ContainerInterface $c) { |
||
| 349 | return new OC\AppFramework\Services\InitialState( |
||
| 350 | $c->get(IInitialStateService::class), |
||
| 351 | $c->get('AppName') |
||
| 352 | ); |
||
| 353 | }); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return \OCP\IServerContainer |
||
| 358 | */ |
||
| 359 | public function getServer() { |
||
| 360 | return $this->server; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $middleWare |
||
| 365 | * @return boolean|null |
||
| 366 | */ |
||
| 367 | public function registerMiddleWare($middleWare) { |
||
| 368 | if (in_array($middleWare, $this->middleWares, true) !== false) { |
||
| 369 | return false; |
||
| 370 | } |
||
| 371 | $this->middleWares[] = $middleWare; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * used to return the appname of the set application |
||
| 376 | * @return string the name of your application |
||
| 377 | */ |
||
| 378 | public function getAppName() { |
||
| 379 | return $this->query('AppName'); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @deprecated use IUserSession->isLoggedIn() |
||
| 384 | * @return boolean |
||
| 385 | */ |
||
| 386 | public function isLoggedIn() { |
||
| 387 | return \OC::$server->getUserSession()->isLoggedIn(); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @deprecated use IGroupManager->isAdmin($userId) |
||
| 392 | * @return boolean |
||
| 393 | */ |
||
| 394 | public function isAdminUser() { |
||
| 395 | $uid = $this->getUserId(); |
||
| 396 | return \OC_User::isAdminUser($uid); |
||
| 397 | } |
||
| 398 | |||
| 399 | private function getUserId() { |
||
| 400 | return $this->getServer()->getSession()->get('user_id'); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @deprecated use the ILogger instead |
||
| 405 | * @param string $message |
||
| 406 | * @param string $level |
||
| 407 | * @return mixed |
||
| 408 | */ |
||
| 409 | public function log($message, $level) { |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Register a capability |
||
| 432 | * |
||
| 433 | * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
||
| 434 | */ |
||
| 435 | public function registerCapability($serviceName) { |
||
| 436 | $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
||
| 437 | return $this->query($serviceName); |
||
| 438 | }); |
||
| 439 | } |
||
| 440 | |||
| 441 | public function has($id): bool { |
||
| 442 | if (parent::has($id)) { |
||
| 443 | return true; |
||
| 444 | } |
||
| 445 | |||
| 446 | if ($this->server->has($id, true)) { |
||
| 447 | return true; |
||
| 448 | } |
||
| 449 | |||
| 450 | return false; |
||
| 451 | } |
||
| 452 | |||
| 453 | public function query(string $name, bool $autoload = true) { |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $name |
||
| 479 | * @return mixed |
||
| 480 | * @throws QueryException if the query could not be resolved |
||
| 481 | */ |
||
| 482 | public function queryNoFallback($name) { |
||
| 497 | } |
||
| 498 | } |
||
| 499 |
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.