Skip to content

Commit

Permalink
Merge pull request #3977 from nextcloud/di_ng2
Browse files Browse the repository at this point in the history
Make DI work for all apps
  • Loading branch information
blizzz authored Mar 22, 2017
2 parents ec3f5aa + 9667ac2 commit 0b5e181
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 19 deletions.
7 changes: 4 additions & 3 deletions lib/private/AppFramework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ public static function buildAppNamespace($appId, $topNamespace='OCA\\') {

$appInfo = \OC_App::getAppInfo($appId);
if (isset($appInfo['namespace'])) {
return $topNamespace . trim($appInfo['namespace']);
self::$nameSpaceCache[$appId] = trim($appInfo['namespace']);
} else {
// if the tag is not found, fall back to uppercasing the first letter
self::$nameSpaceCache[$appId] = ucfirst($appId);
}

// if the tag is not found, fall back to uppercasing the first letter
self::$nameSpaceCache[$appId] = ucfirst($appId);
return $topNamespace . self::$nameSpaceCache[$appId];
}

Expand Down
31 changes: 24 additions & 7 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\AppFramework\Http\IOutput;
use OCP\AppFramework\IApi;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
use OCP\Files\Folder;
use OCP\Files\IAppData;
use OCP\IL10N;
Expand Down Expand Up @@ -373,24 +374,40 @@ public function registerCapability($serviceName) {
});
}

/**
* @param string $name
* @return mixed
* @throws QueryException if the query could not be resolved
*/
public function query($name) {
try {
return $this->queryNoFallback($name);
} catch (QueryException $e) {
return $this->getServer()->query($name);
}
}

/**
* @param string $name
* @return mixed
* @throws QueryException if the query could not be resolved
*/
public function queryNoFallback($name) {
$name = $this->sanitizeName($name);

if ($this->offsetExists($name)) {
return parent::query($name);
} else {
if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
$segments = explode('\\', $name);
if (strtolower($segments[1]) === strtolower($this['AppName'])) {
return parent::query($name);
}
} else if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
return parent::query($name);
} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
return parent::query($name);
} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
return parent::query($name);
}
}

return $this->getServer()->query($name);
throw new QueryException('Could not resolve ' . $name . '!' .
' Class can not be instantiated');
}
}
34 changes: 26 additions & 8 deletions lib/private/ServerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,26 @@ class ServerContainer extends SimpleContainer {
/** @var DIContainer[] */
protected $appContainers;

/** @var string[] */
protected $namespaces;

/**
* ServerContainer constructor.
*/
public function __construct() {
parent::__construct();
$this->appContainers = [];
$this->namespaces = [];
}

/**
* @param string $appName
* @param string $appNamespace
*/
public function registerNamespace($appName, $appNamespace) {
// Cut of OCA\ and lowercase
$appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
$this->namespaces[$appNamespace] = $appName;
}

/**
Expand All @@ -54,15 +68,19 @@ public function registerAppContainer($appName, DIContainer $container) {
}

/**
* @param string $appName
* @param string $namespace
* @return DIContainer
* @throws QueryException
*/
public function getAppContainer($appName) {
if (isset($this->appContainers[$appName])) {
return $this->appContainers[$appName];
protected function getAppContainer($namespace) {
if (isset($this->appContainers[$namespace])) {
return $this->appContainers[$namespace];
}

return new DIContainer($appName);
if (isset($this->namespaces[$namespace])) {
return new DIContainer($this->namespaces[$namespace]);
}
throw new QueryException();
}

/**
Expand All @@ -77,11 +95,11 @@ public function query($name) {
// the apps container first.
if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
$segments = explode('\\', $name);
$appContainer = $this->getAppContainer(strtolower($segments[1]));
try {
return $appContainer->query($name);
$appContainer = $this->getAppContainer(strtolower($segments[1]));
return $appContainer->queryNoFallback($name);
} catch (QueryException $e) {
// Didn't find the service in the respective app container,
// Didn't find the service or the respective app container,
// ignore it and fall back to the core container.
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/private/legacy/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public static function registerAutoloading($app, $path) {
self::$alreadyRegistered[$key] = true;
// Register on PSR-4 composer autoloader
$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
\OC::$server->registerNamespace($app, $appNamespace);
\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
Expand Down
1 change: 1 addition & 0 deletions lib/public/IContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function resolve($name);
*
* @param string $name
* @return mixed
* @throws QueryException if the query could not be resolved
* @since 6.0.0
*/
public function query($name);
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function setUp() {
parent::setUp();

$this->user = $this->createMock(IUser::class);
$this->appManager = $this->createMock('\OC\App\AppManager');
$this->appManager = $this->createMock(AppManager::class);
$this->session = $this->createMock(ISession::class);
$this->config = $this->createMock(IConfig::class);
$this->activityManager = $this->createMock(IManager::class);
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/InfoXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public function testClasses($app) {
$appPath = \OC_App::getAppPath($app);
\OC_App::registerAutoloading($app, $appPath);

//Add the appcontainer
$applicationClassName = \OCP\AppFramework\App::buildAppNamespace($app) . '\\AppInfo\\Application';
if (class_exists($applicationClassName)) {
$application = new $applicationClassName();
} else {
$application = new \OCP\AppFramework\App($app);
}

if (isset($appInfo['background-jobs'])) {
foreach ($appInfo['background-jobs'] as $job) {
$this->assertTrue(class_exists($job), 'Asserting background job "' . $job . '" exists');
Expand Down

0 comments on commit 0b5e181

Please sign in to comment.