more hooks documentation where it should go

This commit is contained in:
Bernhard Posselt
2014-05-12 16:14:58 +02:00
parent 43f474d82e
commit f46964a2b9
2 changed files with 76 additions and 7 deletions

View File

@@ -4,7 +4,67 @@ Hooks
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
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 <init>`:
.. code-block:: php
<?php
namespace OCA\MyApp\AppInfo;
$app = new Application();
$app->getContainer()->query('UserHooks')->register();
The hook logic should be in a seperate class that is being registered in the :doc:`container`
.. code-block:: php
<?php
namespace OCA\MyApp\AppInfo;
use \OCP\AppFramework\App;
use \OCA\MyApp\Hooks\UserHooks;
class Application extends App {
public function __construct(array $urlParams=array()){
parent::__construct('myapp', $urlParams);
$container = $this->getContainer();
/**
* Controllers
*/
$container->registerService('UserHooks', function($c) {
return new UserHooks(
$c->query('ServerContainer')->getUserManager()
);
});
}
}
.. code-block:: php
<?php
namespace OCA\MyApp\Hooks;
class UserHooks {
private $userManager;
public function __construct($userManager){
$this->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
<?php
// listen on user predelete
$userManager->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
<?php
// delete previous callback
$userManager->removeListener(null, null, $callback);
The following hooks are available:

View File

@@ -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 <info>` (:file:`appinfo/info.xml`). Loading apps means that the :doc:`main file <main>` (: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 <info>` (:file:`appinfo/info.xml`). Loading apps means that the :doc:`main file <init>` (: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 <userbackend>`
* 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