Merge pull request #14548 from nextcloud/bugfix/noid/avoid-duplicate-container-creation

Avoid duplicate App container creation
This commit is contained in:
Morris Jobke 2019-03-08 10:31:10 +01:00 committed by GitHub
commit ec5f4200ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 24 deletions

View File

@ -77,7 +77,7 @@ class Application extends App {
}
/**
* @param IManager $contactsManager
* @param IContactsManager $contactsManager
*/
public function setupSystemContactsProvider(IContactsManager $contactsManager) {
/** @var ContactsManager $cm */

View File

@ -26,7 +26,6 @@ namespace OCA\DAV\Tests\unit\AppInfo;
use OCA\DAV\AppInfo\Application;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager;
use OCP\Contacts\IManager;
use Test\TestCase;
/**
@ -43,24 +42,8 @@ class ApplicationTest extends TestCase {
// assert service instances in the container are properly setup
$s = $c->query(ContactsManager::class);
$this->assertInstanceOf('OCA\DAV\CardDAV\ContactsManager', $s);
$this->assertInstanceOf(ContactsManager::class, $s);
$s = $c->query(CardDavBackend::class);
$this->assertInstanceOf('OCA\DAV\CardDAV\CardDavBackend', $s);
}
public function testContactsManagerSetup() {
$app = new Application();
$c = $app->getContainer();
$c->registerService(CardDavBackend::class, function() {
$service = $this->createMock(CardDavBackend::class);
$service->method('getAddressBooksForUser')->willReturn([]);
return $service;
});
// assert setupContactsProvider() is proper
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject $cm */
$cm = $this->createMock(IManager::class);
$app->setupContactsProvider($cm, 'xxx');
$this->addToAssertionCount(1);
$this->assertInstanceOf(CardDavBackend::class, $s);
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -57,7 +58,7 @@ class ServerContainer extends SimpleContainer {
* @param string $appName
* @param string $appNamespace
*/
public function registerNamespace($appName, $appNamespace) {
public function registerNamespace(string $appName, string $appNamespace): void {
// Cut of OCA\ and lowercase
$appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
$this->namespaces[$appNamespace] = $appName;
@ -67,17 +68,30 @@ class ServerContainer extends SimpleContainer {
* @param string $appName
* @param DIContainer $container
*/
public function registerAppContainer($appName, DIContainer $container) {
public function registerAppContainer(string $appName, DIContainer $container): void {
$this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
}
/**
* @param string $appName
* @return DIContainer
* @throws QueryException
*/
public function getRegisteredAppContainer(string $appName): DIContainer {
if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) {
return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))];
}
throw new QueryException();
}
/**
* @param string $namespace
* @param string $sensitiveNamespace
* @return DIContainer
* @throws QueryException
*/
protected function getAppContainer($namespace, $sensitiveNamespace) {
protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
if (isset($this->appContainers[$namespace])) {
return $this->appContainers[$namespace];
}

View File

@ -66,11 +66,16 @@ class App {
/**
* @param string $appName
* @param array $urlParams an array with variables extracted from the routes
* @since 6.0.0
*/
public function __construct(string $appName, array $urlParams = []) {
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
try {
$this->container = \OC::$server->getRegisteredAppContainer($appName);
} catch (QueryException $e) {
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
}
}
/**