You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developer_manual/app_publishing_maintenance/upgrade-guide.rst
+53Lines changed: 53 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,59 @@ Once you've created and published the first version of your app, you will want t
6
6
7
7
This document will cover the most important changes in Nextcloud, as well as some guides on how to upgrade existing apps.
8
8
9
+
Upgrading to Nextcloud 20
10
+
-------------------------
11
+
12
+
.. note:: Critical changes were collected `on Github <https://github.com/nextcloud/server/issues/20953>`_. See the original ticket for links to the pull requests and tickets.
13
+
14
+
Back-end changes
15
+
^^^^^^^^^^^^^^^^
16
+
17
+
.. _upgrade-psr11:
18
+
19
+
PSR-11 integration
20
+
******************
21
+
22
+
Nextcloud 20 is the first major release of Nextcloud that brings full compatibility with :ref:`psr11`. From this point on it is highly recommended to use this interface mainly as the old ``\OCP\AppFramework\IAppContainer``, ``\OCP\IContainer`` and ``\OCP\IServerContainer`` got deprecated with this change.
23
+
24
+
If your app requires Nextcloud 20 or later, you can replace any of the old type hints with one of ``\Psr\Container\ContainerInterface`` and replace calls of ``query`` with ``get``, e.g. on the closures used when registering services:
25
+
26
+
.. code-block:: php
27
+
28
+
// old
29
+
$container->registerService('DecryptAll', function (IAppContainer $c) {
30
+
return new DecryptAll(
31
+
$c->query('Util'),
32
+
$c->query(KeyManager::class),
33
+
$c->query('Crypt'),
34
+
$c->query(ISession::class)
35
+
)
36
+
})
37
+
38
+
becomes
39
+
40
+
.. code-block:: php
41
+
42
+
// new
43
+
$container->registerService('DecryptAll', function (ContainerInterface $c) {
44
+
return new DecryptAll(
45
+
$c->get('Util'),
46
+
$c->get(KeyManage::class'),
47
+
$c->get('Crypt'),
48
+
$c->get(ISession::class)
49
+
)
50
+
})
51
+
52
+
.. note:: For a smoother transition, the old interfaces were changed so they are based on ``ContainerInterface``, hence you can use ``has`` and ``get`` on ``IContainer`` and sub types.
53
+
54
+
Deprecated APIs
55
+
***************
56
+
57
+
* ``\OCP\AppFramework\IAppContainer``: see :ref:`upgrade-psr11`
58
+
* ``\OCP\IContainer``: see :ref:`upgrade-psr11`
59
+
* ``\OCP\IServerContainer``: see :ref:`upgrade-psr11`
@@ -170,15 +178,15 @@ So basically the container is used as a giant factory to build all the classes t
170
178
Use automatic dependency assembly (recommended)
171
179
-----------------------------------------------
172
180
173
-
In Nextcloud it is possible to omit the **lib/AppInfo/Application.php** and use automatic dependency assembly instead.
181
+
In Nextcloud it is possible to build classes and their dependencies without having to explicitly register them on the container, as long as the container can `reflect <reflection>`_ the constructor and look up the parameters by their type. This concept is widely known as *auto-wiring*.
174
182
175
-
How does automatic assembly work
176
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183
+
How does auto-wiring work
184
+
^^^^^^^^^^^^^^^^^^^^^^^^^
177
185
178
-
Automatic assembly creates new instances of classes just by looking at the class name and its constructor parameters. For each constructor parameter the type or the variable name is used to query the container, e.g.:
186
+
Automatic assembly creates new instances of classes just by looking at the class name and its constructor parameters. For each constructor parameter the type or the argument name is used to query the container, e.g.:
179
187
180
-
* **SomeType $type** will use **$container->query('SomeType')**
181
-
* **$variable** will use **$container->query('variable')**
188
+
* **SomeType $type** will use **$container->get('SomeType')**
189
+
* **$variable** will use **$container->get('variable')**
182
190
183
191
If all constructor parameters are resolved, the class will be created, saved as a service and returned.
184
192
@@ -203,12 +211,12 @@ So basically the following is now possible:
.. note:: $AppName is resolved because the container registered a parameter under the key 'AppName' which will return the app id. The lookup is case sensitive so while $AppName will work correctly, using $appName as a constructor parameter will fail.
214
222
@@ -223,7 +231,7 @@ How does it affect the request lifecycle
223
231
224
232
* A request is matched for the route, e.g. with the name **page#index**
225
233
* The appropriate container is being queried for the entry PageController (to keep backwards compatibility)
226
-
* If the entry does not exist, the container is queried for OCA\\AppName\\Controller\\PageController and if no entry exists, the container tries to create the class by using reflection on its constructor parameters
234
+
* If the entry does not exist, the container is queried for OCA\\AppName\\Controller\\PageController and if no entry exists, the container tries to create the class by using `reflection`_ on its constructor parameters
227
235
228
236
How does this affect controllers
229
237
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -284,8 +292,8 @@ Interfaces and primitive types can not be instantiated, so the container can not
Copy file name to clipboardExpand all lines: developer_manual/digging_deeper/psr.rst
+11-1Lines changed: 11 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,4 +9,14 @@ On this page you find information about the implemented `php standards recommend
9
9
PSR-3: Logger Interface
10
10
-----------------------
11
11
12
-
As of Nextcloud 19, the dependency injection container can inject an instance of a ``\Psr\Log\LoggerInterface``. This is merely a wrapper of the existing (and strongly typed) ``\OCP\ILogger``. Apps may still use the Nextcloud logger, but the PSR-3 implementation shall easy the integration of 3rd party libraries that require the PSR-3 logger.
12
+
As of Nextcloud 19, the dependency injection container can inject an instance of a ``\Psr\Log\LoggerInterface``. This is merely a wrapper of the existing (and strongly typed) ``\OCP\ILogger``. Apps may still use the Nextcloud logger, but the `PSR-3`_ implementation shall easy the integration of 3rd party libraries that require the `PSR-3`_ logger.
13
+
14
+
.. _psr11:
15
+
16
+
PSR-11: Container Interface
17
+
---------------------------
18
+
19
+
As of Nextcloud 20, the dependency injection container follows the `PSR-11`_ container interface, so you may start type-hinting ``\Psr\Container\ContainerInterface`` whenever you want an instance of a container and use ``has($id)`` to check for existance and ``get($id)`` to retrieve an instance of a service. See the :ref:`dependency injection docs <dependency-injection>` for details.
0 commit comments