Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable17] Document current bootstrap mechanism #2142

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions developer_manual/app/bootstrap.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
=============
Bootstrapping
=============

Every php process has a relatively short lifespan that lasts as long as the HTTP request or the invokation of the command
line program. At the beginning of this lifespan, Nextcloud initializes its services. At the same time, any additional apps
might want to register their services to Nextcloud as well. This event is called the *bootstrapping* and this chapter
shall shed some light on how to hook into this with an app.


.. _app-php:

app.php
-------

Nextcloud will ``require_once`` every installed and enabled app's ``appinfo/app.php`` file if it exists. The app can use
this file to run registrations of services, event listeners and similar.

To leverage the advantages of object-oriented programming, it's recommended to put the logic into an :ref:`application-php`
class and query an instance like

.. code-block:: php

<?php

declare(strict_types=1);

$app = \OC::$server->query(\OCA\MyApp\AppInfo\Application::class);
$app->registerHooks();


.. _application-php:

Application
-----------

An `Application` class shall serve as central initialization point of an app.

.. code-block:: php

<?php

declare(strict_types=1);

namespace OCA\MyApp\AppInfo;

use OCP\AppFramework\App;

class Application extends App {

public function registerHooks(): void {
\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\MyApp\Hooks\User', 'deleteUser');
}

}

.. note:: Nextcloud does not load this class for every request. You should query an instance inside your :ref:`app-php` to
load for every request, if desired.
35 changes: 0 additions & 35 deletions developer_manual/app/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,3 @@ purpose there are several events emitted that an app can act upon.
\OCP\Util::addStyle('myapp', 'style');
}
);

Best practice
-------------

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.


appinfo/app.php
^^^^^^^^^^^^^^^

.. code-block:: php

<?php

$app = new \OCA\MyApp\AppInfo\Application();
$app->registerHooks();


lib/AppInfo/Application.php
^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: php

<?php
namespace OCA\MyApp\AppInfo;

use OCP\AppFramework\App;

class Application extends App {

public function registerHooks() {
\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\MyApp\Hooks\User', 'deleteUser');
}

}