summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-05-02 21:34:17 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-05-02 21:34:17 +0200
commite231ba2ec1908f0ac914bd8f1b73771d707719b8 (patch)
treed26d6eddda7ee5c26c345f84804edb2592f6e395
parent9c9625b0f6520f80996e17304805d8bae421f44c (diff)
add custom app.json config and parser
-rw-r--r--app/news.php56
-rw-r--r--appinfo/app.json4
-rw-r--r--appinfo/app.php36
-rw-r--r--config/appconfig.php75
-rw-r--r--db/mapperfactory.php8
-rw-r--r--tests/unit/config/AppConfigTest.php30
-rw-r--r--tests/unit/db/MapperFactoryTest.php21
7 files changed, 145 insertions, 85 deletions
diff --git a/app/news.php b/app/news.php
index 1214c1918..ecf1a33d0 100644
--- a/app/news.php
+++ b/app/news.php
@@ -16,6 +16,8 @@ namespace OCA\News\App;
use \OC\Files\View;
use \OCP\AppFramework\App;
+use \OCA\News\Config\AppConfig;
+
use \OCA\News\Core\Logger;
use \OCA\News\Core\Db;
@@ -214,7 +216,8 @@ class News extends App {
*/
$container->registerService('MapperFactory', function($c) {
return new MapperFactory(
- $c->query('CoreConfig'), $c->query('Db')
+ $c->query('DatabaseType'),
+ $c->query('Db')
);
});
@@ -236,11 +239,45 @@ class News extends App {
);
});
+
+ /**
+ * App config parser
+ */
+ $container->registerService('AppConfig', function($c) {
+ // not performant but well :/
+ $config = $c->query('ServerContainer')->getAppConfig();
+ $installedApps = $config->getApps();
+ $apps = array();
+ foreach($installedApps as $app) {
+ $apps[] = array(
+ $app => $config->getValue($app, 'installed_version', '0')
+ );
+ }
+
+ // order extensions in name => version
+ $loadedExtensions = get_loaded_extensions();
+ $extensions = array();
+ foreach ($loadedExtensions as $extension) {
+ $extensions[$extension] = phpversion($extension);
+ }
+
+ return new AppConfig(
+ $c->query('ServerContainer')->getNavigationManager(),
+ $c->query('L10N'),
+ $c->query('ServerContainer')->getURLGenerator(),
+ phpversion(),
+ implode('.', \OCP\Util::getVersion()),
+ $apps,
+ $extensions,
+ $c->query('DatabaseType')
+ );
+ });
+
/**
* Core
*/
$container->registerService('L10N', function($c) {
- return \OC_L10N::get($c['AppName']);
+ return $c->query('ServerContainer')->getL10N($c->query('AppName'));
});
$container->registerService('UserId', function($c) {
@@ -248,7 +285,7 @@ class News extends App {
});
$container->registerService('Logger', function($c) {
- return new Logger($c['AppName']);
+ return new Logger($c->query('AppName'));
});
$container->registerService('Db', function($c) {
@@ -259,6 +296,10 @@ class News extends App {
return $c->query('ServerContainer')->getConfig();
});
+ $container->registerService('DatabaseType', function($c) {
+ return $c->query('ServerContainer')->getConfig()->getSystemValue('dbtype');
+ });
+
/**
* Utility
@@ -398,5 +439,14 @@ class News extends App {
$container->registerMiddleWare('CORSMiddleware');
}
+
+ public function getAppConfig() {
+ return $this->getContainer()->query('AppConfig');
+ }
+
+
+ public function getLogger() {
+ return $this->getContainer()->query('Logger');
+ }
}
diff --git a/appinfo/app.json b/appinfo/app.json
index dfa0945b4..cfcf64111 100644
--- a/appinfo/app.json
+++ b/appinfo/app.json
@@ -24,11 +24,11 @@
"documentation": {
"developer": "https://github.com/owncloud/news/wiki"
},
- "cron": ["OCA\\News\\BackgroundJob\\Task"],
+ "jobs": ["OCA\\News\\BackgroundJob\\Task"],
"hooks": {
"OC_User::pre_deleteUser": "OCA\\News\\Hooks\\User::deleteUser"
},
- "databases": ["postgresql", "sqlite", "mysql"],
+ "databases": ["pgsql", "sqlite", "mysql"],
"categories": ["Multimedia"],
"dependencies": {
"php": ">=5.3",
diff --git a/appinfo/app.php b/appinfo/app.php
index 749f64601..e355fee38 100644
--- a/appinfo/app.php
+++ b/appinfo/app.php
@@ -11,30 +11,20 @@
* @copyright Bernhard Posselt 2012, 2014
*/
-namespace OCA\News;
+namespace OCA\News\App;
+$container = new News();
-\OCP\App::addNavigationEntry(array(
+$config = $container->getAppConfig();
+$config->loadConfig(__DIR__ . '/app.json');
+$config->registerNavigation();
+$config->registerBackgroundJobs();
+$config->registerHooks();
- // the string under which your app will be referenced in owncloud
- 'id' => 'news',
+// check for correct app dependencies
+try {
+ $config->testDependencies();
+} catch(\OCA\News\Config\DependencyException $e) {
+ $container->getLogger()->log($e->getMessage());
+}
- // sorting weight for the navigation. The higher the number, the higher
- // will it be listed in the navigation
- 'order' => 10,
-
- // the route that will be shown on startup
- 'href' => \OCP\Util::linkToRoute('news.page.index'),
-
- // the icon that will be shown in the navigation
- // this file needs to exist in img/example.png
- 'icon' => \OCP\Util::imagePath('news', 'app.svg'),
-
- // the title of your application. This will be used in the
- // navigation or on the settings page of your app
- 'name' => \OC_L10N::get('news')->t('News')
-
-));
-
-\OCP\Backgroundjob::addRegularTask('OCA\News\Backgroundjob\Task', 'run');
-\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\News\Hooks\User', 'deleteUser');
diff --git a/config/appconfig.php b/config/appconfig.php
index 0d4ea6537..1a2367c4b 100644
--- a/config/appconfig.php
+++ b/config/appconfig.php
@@ -24,8 +24,7 @@ use OCP\IURLGenerator;
// Used to parse app.json file, should be in core at some point
class AppConfig {
- public $config;
-
+ private $config = array();
private $navigationManager;
private $urlGenerator;
private $phpVersion;
@@ -60,10 +59,8 @@ class AppConfig {
/**
* @param string|array $data path to the config file or an array with the config
- * @throws \OCA\News\Config\DependencyException if a required lib or version
- * is not satisfied by the current installation
*/
- public function load($data) {
+ public function loadConfig($data) {
if(is_array($data)) {
$this->config = $data;
} else {
@@ -71,20 +68,9 @@ class AppConfig {
$this->config = json_decode($json, true);
}
- $this->testDependencies();
- $this->parseNavigation();
- $this->parseJobs();
- $this->parseHooks();
- }
-
-
- /**
- * Parses the navigation and creates a navigation entry if needed
- */
- private function parseNavigation() {
- // if key is missing, dont create a navigation
+ // fill config with default values if no navigation is added
if(array_key_exists('navigation', $this->config)) {
- $nav = $this->config['navigation'];
+ $nav =& $this->config['navigation'];
// add defaults
$defaults = array(
@@ -100,7 +86,30 @@ class AppConfig {
$nav[$key] = $value;
}
}
+ }
+ }
+
+ /**
+ * @param string $key if given returns the value of the config at index $key
+ * @return array|mixed the config
+ */
+ public function getConfig($key=null) {
+ // FIXME: is this function interface a good idea?
+ if($key !== null) {
+ return $this->config[$key];
+ } else {
+ return $this->config;
+ }
+ }
+
+ /**
+ * Parses the navigation and creates a navigation entry if needed
+ */
+ public function registerNavigation() {
+ // if key is missing, dont create a navigation
+ if(array_key_exists('navigation', $this->config)) {
+ $nav =& $this->config['navigation'];
$navConfig = array(
'id' => $nav['id'],
@@ -118,13 +127,32 @@ class AppConfig {
}
- private function parseJobs() {
-
+ /**
+ * Registers all jobs in the config
+ */
+ public function registerBackgroundJobs() {
+ // FIXME: this is temporarily static because core jobs are not public
+ // yet, therefore legacy code
+ foreach ($config['jobs'] as $job) {
+ \OCP\Backgroundjob::addRegularTask($job, 'run');
+ }
}
- private function parseHooks() {
-
+ /**
+ * Registers all hooks in the config
+ */
+ public function registerHooks() {
+ // FIXME: this is temporarily static because core emitters are not future
+ // proof, therefore legacy code in here
+ foreach ($config['hooks'] as $listen => $react) {
+ $listener = explode('::', $listen);
+ $reaction = explode('::', $react);
+
+ // config is written like HookNamespace::method => Class::method
+ \OCP\Util::connectHook($listener[0], $listener[1], $reaction[0],
+ $reaction[1]);
+ }
}
@@ -132,7 +160,7 @@ class AppConfig {
* Validates all dependencies that the app has
* @throws \OCA\News\DependencyException if one version is not satisfied
*/
- private function testDependencies() {
+ public function testDependencies() {
if(array_key_exists('dependencies', $this->config)) {
$deps = $this->config['dependencies'];
@@ -171,7 +199,6 @@ class AppConfig {
}
}
-
if($msg !== '') {
throw new DependencyException($msg);
}
diff --git a/db/mapperfactory.php b/db/mapperfactory.php
index af2640a33..ff14042d0 100644
--- a/db/mapperfactory.php
+++ b/db/mapperfactory.php
@@ -20,16 +20,16 @@ use \OCA\News\Core\Db;
class MapperFactory {
- private $settings;
+ private $dbType;
- public function __construct(IConfig $settings, Db $db) {
- $this->settings = $settings;
+ public function __construct($dbType, Db $db) {
+ $this->dbType = $dbType;
$this->db = $db;
}
public function getItemMapper() {
- switch($this->settings->getSystemValue('dbtype')) {
+ switch($this->dbType) {
case 'pgsql':
return new \OCA\News\Db\Postgres\ItemMapper($this->db);
break;
diff --git a/tests/unit/config/AppConfigTest.php b/tests/unit/config/AppConfigTest.php
index 99fba1fae..7ee533e8b 100644
--- a/tests/unit/config/AppConfigTest.php
+++ b/tests/unit/config/AppConfigTest.php
@@ -50,13 +50,13 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
}
public function testGetId() {
- $this->config->load(__DIR__ . '/../../../appinfo/app.json');
- $this->assertEquals('news', $this->config->config['id']);
+ $this->config->loadConfig(__DIR__ . '/../../../appinfo/app.json');
+ $this->assertEquals('news', $this->config->getConfig('id'));
}
public function testNoNavigation() {
- $this->config->load(array());
+ $this->config->loadConfig(array());
$this->nav->expects($this->never())
->method('add');
@@ -92,11 +92,12 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
->method('add')
->with($this->equalTo($expected));
- $this->config->load(array(
+ $this->config->loadConfig(array(
'id' => 'news',
'name' => 'News',
'navigation' => array()
));
+ $this->config->registerNavigation();
}
@@ -129,11 +130,12 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
->method('add')
->with($this->equalTo($expected));
- $this->config->load(array(
+ $this->config->loadConfig(array(
'id' => 'abc',
'name' => 'News',
'navigation' => $expected
));
+ $this->config->registerNavigation();
}
@@ -141,11 +143,12 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
* @expectedException \OCA\News\Config\DependencyException
*/
public function testPHPVersion() {
- $this->config->load(array(
+ $this->config->loadConfig(array(
'dependencies' => array(
'php' => '5.7'
)
));
+ $this->config->testDependencies();
}
@@ -153,11 +156,12 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
* @expectedException \OCA\News\Config\DependencyException
*/
public function testOwnCloudVersion() {
- $this->config->load(array(
+ $this->config->loadConfig(array(
'dependencies' => array(
'owncloud' => '>=4.5,<=6.0.2'
)
));
+ $this->config->testDependencies();
}
@@ -165,7 +169,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
* @expectedException \OCA\News\Config\DependencyException
*/
public function testAppVersion() {
- $this->config->load(array(
+ $this->config->loadConfig(array(
'dependencies' => array(
'apps' =>
array(
@@ -174,6 +178,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
)
)
));
+ $this->config->testDependencies();
}
@@ -181,7 +186,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
* @expectedException \OCA\News\Config\DependencyException
*/
public function testLibsVersion() {
- $this->config->load(array(
+ $this->config->loadConfig(array(
'dependencies' => array(
'libs' =>
array(
@@ -189,6 +194,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
)
)
));
+ $this->config->testDependencies();
}
@@ -196,7 +202,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
* @expectedException \OCA\News\Config\DependencyException
*/
public function testLibsExistence() {
- $this->config->load(array(
+ $this->config->loadConfig(array(
'dependencies' => array(
'libs' =>
array(
@@ -204,6 +210,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
)
)
));
+ $this->config->testDependencies();
}
@@ -211,7 +218,7 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
* @expectedException \OCA\News\Config\DependencyException
*/
public function testAppsExistence() {
- $this->config->load(array(
+ $this->config->loadConfig(array(
'dependencies' => array(
'apps' =>
array(
@@ -219,5 +226,6 @@ class AppConfigTest extends \PHPUnit_Framework_TestCase {
)
)
));
+ $this->config->testDependencies();
}
} \ No newline at end of file
diff --git a/tests/unit/db/MapperFactoryTest.php b/tests/unit/db/MapperFactoryTest.php
index a83eca8ec..2f0e86be4 100644
--- a/tests/unit/db/MapperFactoryTest.php
+++ b/tests/unit/db/MapperFactoryTest.php
@@ -23,9 +23,6 @@ class MapperFactoryTest extends \PHPUnit_Framework_TestCase {
private $settings;
public function setUp() {
- $this->settings = $this->getMockBuilder('\OCP\IConfig')
- ->disableOriginalConstructor()
- ->getMock();
$this->db = $this->getMockBuilder('\OCA\News\Core\Db')
->disableOriginalConstructor()
->getMock();
@@ -33,33 +30,21 @@ class MapperFactoryTest extends \PHPUnit_Framework_TestCase {
public function testGetItemMapperSqlite() {
- $this->settings->expects($this->once())
- ->method('getSystemValue')
- ->with($this->equalTo('dbtype'))
- ->will($this->returnValue('sqlite'));
- $factory = new MapperFactory($this->settings, $this->db);
+ $factory = new MapperFactory('sqlite', $this->db);
$this->assertTrue($factory->getItemMapper() instanceof ItemMapper);
}
public function testGetItemMapperMysql() {
- $this->settings->expects($this->once())
- ->method('getSystemValue')
- ->with($this->equalTo('dbtype'))
- ->will($this->returnValue('mysql'));
- $factory = new MapperFactory($this->settings, $this->db);
+ $factory = new MapperFactory('mysql', $this->db);
$this->assertTrue($factory->getItemMapper() instanceof ItemMapper);
}
public function testGetItemMapperPostgres() {
- $this->settings->expects($this->once())
- ->method('getSystemValue')
- ->with($this->equalTo('dbtype'))
- ->will($this->returnValue('pgsql'));
- $factory = new MapperFactory($this->settings, $this->db);
+ $factory = new MapperFactory('pgsql', $this->db);
$this->assertTrue($factory->getItemMapper() instanceof \OCA\News\Db\Postgres\ItemMapper);
}