1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-18 18:51:12 +01:00

Make it much easier to add one-off event listeners

Summary:
Ref T4657. Right now, you have to muck with `events.listeners` to install listeners. Instead, automatically install all subclasses of AutoEventListener.

Primarily, this makes it easier to resolve requests with "drop this file in `src/extensions/`, no warranty", which seems to have worked well so far in resolving things like custom remarkup rules, etc.

Test Plan:
  - Added such a listener, had it autoregister.
  - Clicked around and saw the effects of normal listeners.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T4657

Differential Revision: https://secure.phabricator.com/D9262
This commit is contained in:
epriestley 2014-05-22 15:19:28 -07:00
parent c39f302c04
commit 27a369d15a
3 changed files with 48 additions and 15 deletions

View file

@ -1288,6 +1288,7 @@ phutil_register_library_map(array(
'PhabricatorAuthUnlinkController' => 'applications/auth/controller/PhabricatorAuthUnlinkController.php',
'PhabricatorAuthValidateController' => 'applications/auth/controller/PhabricatorAuthValidateController.php',
'PhabricatorAuthenticationConfigOptions' => 'applications/config/option/PhabricatorAuthenticationConfigOptions.php',
'PhabricatorAutoEventListener' => 'infrastructure/events/PhabricatorAutoEventListener.php',
'PhabricatorBarePageExample' => 'applications/uiexample/examples/PhabricatorBarePageExample.php',
'PhabricatorBarePageView' => 'view/page/PhabricatorBarePageView.php',
'PhabricatorBaseEnglishTranslation' => 'infrastructure/internationalization/translation/PhabricatorBaseEnglishTranslation.php',
@ -4062,6 +4063,7 @@ phutil_register_library_map(array(
'PhabricatorAuthUnlinkController' => 'PhabricatorAuthController',
'PhabricatorAuthValidateController' => 'PhabricatorAuthController',
'PhabricatorAuthenticationConfigOptions' => 'PhabricatorApplicationConfigOptions',
'PhabricatorAutoEventListener' => 'PhabricatorEventListener',
'PhabricatorBarePageExample' => 'PhabricatorUIExample',
'PhabricatorBarePageView' => 'AphrontPageView',
'PhabricatorBaseEnglishTranslation' => 'PhabricatorTranslation',

View file

@ -0,0 +1,17 @@
<?php
/**
* Event listener which is registered automatically, without requiring
* configuration.
*
* Normally, event listeners must be registered via applications. This is
* appropriate for structured listeners in libraries, but it adds a lot of
* overhead and is cumbersome for one-off listeners.
*
* All concrete subclasses of this class are automatically registered at
* startup. This allows it to be used with custom one-offs that can be dropped
* into `phabricator/src/extensions/`.
*/
abstract class PhabricatorAutoEventListener extends PhabricatorEventListener {
}

View file

@ -1,36 +1,50 @@
<?php
/**
* @group events
*/
final class PhabricatorEventEngine {
public static function initialize() {
$listeners = PhabricatorEnv::getEnvConfig('events.listeners');
foreach ($listeners as $listener) {
// NOTE: If any of this fails, we just log it and move on. It's important
// to try to make it through here because users may have difficulty fixing
// fix the errors if we don't: for example, if we fatal here a user may not
// be able to run `bin/config` in order to remove an invalid listener.
// Load automatic listeners.
$listeners = id(new PhutilSymbolLoader())
->setAncestorClass('PhabricatorAutoEventListener')
->loadObjects();
// Load configured listeners.
$config_listeners = PhabricatorEnv::getEnvConfig('events.listeners');
foreach ($config_listeners as $listener_class) {
try {
id(new $listener())->register();
$listeners[] = newv($listener_class, array());
} catch (Exception $ex) {
// If the listener does not exist, or throws when registering, just
// log it and continue. In particular, this is important to let you
// run `bin/config` in order to remove an invalid listener.
phlog($ex);
}
}
// Register the DarkConosole event logger.
id(new DarkConsoleEventPluginAPI())->register();
id(new ManiphestEdgeEventListener())->register();
// Add builtin listeners.
$listeners[] = new DarkConsoleEventPluginAPI();
$listeners[] = new ManiphestEdgeEventListener();
// Add application listeners.
$applications = PhabricatorApplication::getAllInstalledApplications();
foreach ($applications as $application) {
$listeners = $application->getEventListeners();
foreach ($listeners as $listener) {
$app_listeners = $application->getEventListeners();
foreach ($app_listeners as $listener) {
$listener->setApplication($application);
$listener->register();
$listeners[] = $listener;
}
}
// Now, register all of the listeners.
foreach ($listeners as $listener) {
try {
$listener->register();
} catch (Exception $ex) {
phlog($ex);
}
}
}
}