Skip to content

Commit 072a2c9

Browse files
Document current bootstrap mechanism
Signed-off-by: Christoph Wurst <[email protected]>
1 parent a595b19 commit 072a2c9

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

developer_manual/app/bootstrap.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=============
2+
Bootstrapping
3+
=============
4+
5+
Every php process has a relatively short lifespan that lasts as long as the HTTP request or the invokation of the command
6+
line program. At the beginning of this lifespan, Nextcloud initializes its services. At the same time, any additional apps
7+
might want to register their services to Nextcloud as well. This event is called the *bootstrapping* and this chapter
8+
shall shed some light on how to hook into this with an app.
9+
10+
11+
.. _app-php:
12+
13+
app.php
14+
-------
15+
16+
Nextcloud will ``require_once`` every installed and enabled app's ``appinfo/app.php`` file if it exists. The app can use
17+
this file to run registrations of services, event listeners and similar.
18+
19+
To leverage the advantages of object-oriented programming, it's recommended to put the logic into an :ref:`application-php`
20+
class and query an instance like
21+
22+
.. code-block:: php
23+
24+
<?php
25+
26+
declare(strict_types=1);
27+
28+
$app = \OC::$server->query(\OCA\MyApp\AppInfo\Application::class);
29+
$app->registerHooks();
30+
31+
32+
.. _application-php:
33+
34+
Application
35+
-----------
36+
37+
An `Application` class shall serve as central initialization point of an app.
38+
39+
.. code-block:: php
40+
41+
<?php
42+
43+
declare(strict_types=1);
44+
45+
namespace OCA\MyApp\AppInfo;
46+
47+
use OCP\AppFramework\App;
48+
49+
class Application extends App {
50+
51+
public function registerHooks(): void {
52+
\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\MyApp\Hooks\User', 'deleteUser');
53+
}
54+
55+
}
56+
57+
.. note:: Nextcloud does not load this class for every request. You should query an instance inside your :ref:`app-php` to
58+
load for every request, if desired.

developer_manual/app/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ App development
88
intro
99
tutorial
1010
upgrade-guide
11+
bootstrap
1112
npm
1213
javascript-apis
1314
requests/index

developer_manual/app/init.rst

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,7 @@ purpose there are several events emitted that an app can act upon.
6161
}
6262
);
6363
64-
Best practice
65-
-------------
66-
67-
A common way to have a cleaner code structure is to create a class Application in :file:`lib/AppInfo/Application.php` that will then execute your setup of hooks or background tasks. You can then just call it in your :file:`appinfo/app.php`. That way you can also make use of Nextclouds dependency injection feature and properly test those methods.
68-
69-
70-
appinfo/app.php
71-
^^^^^^^^^^^^^^^
72-
73-
.. code-block:: php
74-
75-
<?php
76-
77-
$app = new \OCA\MyApp\AppInfo\Application();
78-
$app->registerHooks();
79-
8064
8165
lib/AppInfo/Application.php
8266
^^^^^^^^^^^^^^^^^^^^^^^^^^^
8367

84-
.. code-block:: php
85-
86-
<?php
87-
namespace OCA\MyApp\AppInfo;
88-
89-
use OCP\AppFramework\App;
90-
91-
class Application extends App {
92-
93-
public function registerHooks() {
94-
\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\MyApp\Hooks\User', 'deleteUser');
95-
}
96-
97-
}

0 commit comments

Comments
 (0)