Welcome to mirror list, hosted at ThFree Co, Russian Federation.

configuration.rst « storage « basics « developer_manual - github.com/nextcloud/documentation.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: afb26e8565c35c8824c37fd50ecadad82b689a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
=============
Configuration
=============

.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>

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.


.. code-block:: php

    <?php
    namespace OCA\MyApp\AppInfo;

    use OCP\AppFramework\App;
    use OCP\IConfig;
    use OCP\IServerContainer;
    use OCA\MyApp\Service\AuthorService;

    class Application extends App {

        public function __construct(array $urlParams=array()){
            parent::__construct('myapp', $urlParams);

            $container = $this->getContainer();

            /**
             * Controllers
             */
            $container->registerService('AuthorService', function(IServerContainer $c): AuthorService {
                return new AuthorService(
                    $c->get(IConfig::class),
                    $c->get('appName')
                );
            });
        }
    }

System values
-------------

System values are saved in the :file:`config/config.php` and allow the app to modify and read the global configuration. Please note that ``setSystemValue`` might throw a ``OCP\HintException`` when the config file is read-only.

.. code-block:: php

    <?php
    namespace OCA\MyApp\Service;

    use OCP\HintException;
    use OCP\IConfig;

    class AuthorService {
        private IConfig $config;
        private string $appName;

        public function __construct(IConfig $config, string $appName){
            $this->config = $config;
            $this->appName = $appName;
        }

        public function getSystemValue(string $key) {
            return $this->config->getSystemValue($key);
        }

        public function setSystemValue(string $key, $value): void {
            try {
                $this->config->setSystemValue($key, $value);
            } catch (HintException $e) {
                // Handle exception, e.g. when config file is read-only
            }
        }
    }

.. note:: It's also possible to use ``getSystemValueBool``, ``getSystemValueString``, ``getSystemValueInt`` to get type hinted return values.

App values
----------

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

.. code-block:: php

    <?php
    namespace OCA\MyApp\Service;

    use OCP\IConfig;

    class AuthorService {
        private IConfig $config;
        private string $appName;

        public function __construct(IConfig $config, string $appName){
            $this->config = $config;
            $this->appName = $appName;
        }

        public function getAppValue(string $key): string {
            return $this->config->getAppValue($this->appName, $key);
        }

        public function setAppValue(string $key, string $value): void {
            $this->config->setAppValue($this->appName, $key, $value);
        }
    }

User values
-----------

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

.. code-block:: php

    <?php
    namespace OCA\MyApp\Service;

    use OCP\IConfig;

    class AuthorService {
        private IConfig $config;
        private string $appName;

        public function __construct(IConfig $config, string $appName){
            $this->config = $config;
            $this->appName = $appName;
        }

        public function getUserValue(string $key, string $userId): string {
            return $this->config->getUserValue($userId, $this->appName, $key);
        }

        public function setUserValue(string $key, string $userId, string $value): void {
            $this->config->setUserValue($userId, $this->appName, $key, $value);
        }
    }