Merge pull request #24386 from owncloud/psr4-for-apps

PSR-4 for apps
This commit is contained in:
Morris Jobke 2016-05-03 08:24:22 +02:00
commit a72e6a2dac
2 changed files with 25 additions and 3 deletions

View File

@ -103,6 +103,9 @@ class OC {
*/ */
public static $loader = null; public static $loader = null;
/** @var \Composer\Autoload\ClassLoader $composerAutoloader */
public static $composerAutoloader = null;
/** /**
* @var \OC\Server * @var \OC\Server
*/ */
@ -493,7 +496,7 @@ class OC {
self::$CLI = (php_sapi_name() == 'cli'); self::$CLI = (php_sapi_name() == 'cli');
// Add default composer PSR-4 autoloader // Add default composer PSR-4 autoloader
require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
try { try {
self::initPaths(); self::initPaths();

View File

@ -108,7 +108,7 @@ class OC_App {
foreach($apps as $app) { foreach($apps as $app) {
$path = self::getAppPath($app); $path = self::getAppPath($app);
if($path !== false) { if($path !== false) {
\OC::$loader->addValidRoot($path); self::registerAutoloading($app, $path);
} }
} }
@ -137,7 +137,10 @@ class OC_App {
if($appPath === false) { if($appPath === false) {
return; return;
} }
\OC::$loader->addValidRoot($appPath); // in case someone calls loadApp() directly
// in case someone calls loadApp() directly
self::registerAutoloading($app, $appPath);
if (is_file($appPath . '/appinfo/app.php')) { if (is_file($appPath . '/appinfo/app.php')) {
\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
if ($checkUpgrade and self::shouldUpgrade($app)) { if ($checkUpgrade and self::shouldUpgrade($app)) {
@ -155,6 +158,22 @@ class OC_App {
} }
} }
/**
* @param string $app
* @param string $path
*/
protected static function registerAutoloading($app, $path) {
// Register on PSR-4 composer autoloader
$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
if (defined('PHPUNIT_RUN')) {
\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
}
// Register on legacy autoloader
\OC::$loader->addValidRoot($path);
}
/** /**
* Load app.php from the given app * Load app.php from the given app
* *