From f46964a2b95be50e74864671fbeee7df5257caf6 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 12 May 2014 16:14:58 +0200 Subject: [PATCH] more hooks documentation where it should go --- developer_manual/app/hooks.rst | 77 ++++++++++++++++++++++++++++++-- developer_manual/app/request.rst | 6 +-- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/developer_manual/app/hooks.rst b/developer_manual/app/hooks.rst index 0e9f50be4..b5b130ecd 100644 --- a/developer_manual/app/hooks.rst +++ b/developer_manual/app/hooks.rst @@ -4,7 +4,67 @@ Hooks .. sectionauthor:: Bernhard Posselt -Hooks are used to execute code before or after an event has occured. This is for instance useful to run cleanup code after users, groups or files have been deleted. +Hooks are used to execute code before or after an event has occured. This is for instance useful to run cleanup code after users, groups or files have been deleted. Hooks should be registered in the :doc:`app.php `: + +.. code-block:: php + + getContainer()->query('UserHooks')->register(); + +The hook logic should be in a seperate class that is being registered in the :doc:`container` + +.. code-block:: php + + getContainer(); + + /** + * Controllers + */ + $container->registerService('UserHooks', function($c) { + return new UserHooks( + $c->query('ServerContainer')->getUserManager() + ); + }); + } + } + +.. code-block:: php + + userManager = $userManager; + } + + public function register() { + $callback = function($user) { + // your code that executes before $user is deleted + }; + $userManager->listen('\OC\User', 'preDelete', $callback); + } + + } Available hooks =============== @@ -15,12 +75,21 @@ The scope is the first parameter that is passed to the **listen** method, the se listen('\OC\User', 'preDelete', function($user) { + $callback = function($user) { // your code that executes before $user is deleted - }); + }; + $userManager->listen('\OC\User', 'preDelete', $callback); -Hooks can also be removed by using the **removeListener** method on the object. +Hooks can also be removed by using the **removeListener** method on the object: + +.. code-block:: php + + removeListener(null, null, $callback); + The following hooks are available: diff --git a/developer_manual/app/request.rst b/developer_manual/app/request.rst index 33d980838..a53ecf1a0 100644 --- a/developer_manual/app/request.rst +++ b/developer_manual/app/request.rst @@ -21,12 +21,12 @@ In the beginning, all requests are sent to ownCloud's :file:`index.php` which in * Filesystem * Logging -The type of the app is determined by inspecting the app's :doc:`configuration file ` (:file:`appinfo/info.xml`). Loading apps means that the :doc:`main file
` (:file:`appinfo/app.php`) of each installed app is being loaded and executed. That means that if you want to execute code before a specific app is being run, you can place code in your app's :doc:`main` file. +The type of the app is determined by inspecting the app's :doc:`configuration file ` (:file:`appinfo/info.xml`). Loading apps means that the :doc:`main file ` (:file:`appinfo/app.php`) of each installed app is being loaded and executed. That means that if you want to execute code before a specific app is being run, you can place code in your app's :doc:`init` file. Afterwards the following steps are performed: -* Try to :doc:`authenticate the user ` -* Load and execute all the remaining apps' :doc:`main` files +* Try to authenticate the user +* Load and execute all the remaining apps' :doc:`init` files * Load and run all the routes in the apps' :file:`appinfo/routes.php` * Execute the router