Configuration

The config that allows the app to set global, app and user settings can be injected from the ServerContainer. All values are saved as strings and must be cast to the correct value.

  1. <?php
  2. namespace OCA\MyApp\AppInfo;
  3. use \OCP\AppFramework\App;
  4. use \OCA\MyApp\Service\AuthorService;
  5. class Application extends App {
  6. public function __construct(array $urlParams=array()){
  7. parent::__construct('myapp', $urlParams);
  8. $container = $this->getContainer();
  9. /**
  10. * Controllers
  11. */
  12. $container->registerService('AuthorService', function($c) {
  13. return new AuthorService(
  14. $c->query('Config'),
  15. $c->query('AppName')
  16. );
  17. });
  18. $container->registerService('Config', function($c) {
  19. return $c->query('ServerContainer')->getConfig();
  20. });
  21. }
  22. }

System values

System values are saved in the config/config.php and allow the app to modify and read the global configuration:

  1. <?php
  2. namespace OCA\MyApp\Service;
  3. use \OCP\IConfig;
  4. class AuthorService {
  5. private $config;
  6. private $appName;
  7. public function __construct(IConfig $config, $appName){
  8. $this->config = $config;
  9. $this->appName = $appName;
  10. }
  11. public function getSystemValue($key) {
  12. return $this->config->getSystemValue($key);
  13. }
  14. public function setSystemValue($key, $value) {
  15. $this->config->setSystemValue($key, $value);
  16. }
  17. }

App values

App values are saved in the database per app and are useful for setting global app settings:

  1. <?php
  2. namespace OCA\MyApp\Service;
  3. use \OCP\IConfig;
  4. class AuthorService {
  5. private $config;
  6. private $appName;
  7. public function __construct(IConfig $config, $appName){
  8. $this->config = $config;
  9. $this->appName = $appName;
  10. }
  11. public function getAppValue($key) {
  12. return $this->config->getAppValue($this->appName, $key);
  13. }
  14. public function setAppValue($key, $value) {
  15. $this->config->setAppValue($this->appName, $key, $value);
  16. }
  17. }

User values

User values are saved in the database per user and app and are good for saving user specific app settings:

  1. <?php
  2. namespace OCA\MyApp\Service;
  3. use \OCP\IConfig;
  4. class AuthorService {
  5. private $config;
  6. private $appName;
  7. public function __construct(IConfig $config, $appName){
  8. $this->config = $config;
  9. $this->appName = $appName;
  10. }
  11. public function getUserValue($key, $userId) {
  12. return $this->config->getUserValue($userId, $this->appName, $key);
  13. }
  14. public function setUserValue($key, $userId, $value) {
  15. $this->config->setUserValue($userId, $this->appName, $key, $value);
  16. }
  17. }