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:
parent
c39f302c04
commit
27a369d15a
3 changed files with 48 additions and 15 deletions
|
@ -1288,6 +1288,7 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorAuthUnlinkController' => 'applications/auth/controller/PhabricatorAuthUnlinkController.php',
|
'PhabricatorAuthUnlinkController' => 'applications/auth/controller/PhabricatorAuthUnlinkController.php',
|
||||||
'PhabricatorAuthValidateController' => 'applications/auth/controller/PhabricatorAuthValidateController.php',
|
'PhabricatorAuthValidateController' => 'applications/auth/controller/PhabricatorAuthValidateController.php',
|
||||||
'PhabricatorAuthenticationConfigOptions' => 'applications/config/option/PhabricatorAuthenticationConfigOptions.php',
|
'PhabricatorAuthenticationConfigOptions' => 'applications/config/option/PhabricatorAuthenticationConfigOptions.php',
|
||||||
|
'PhabricatorAutoEventListener' => 'infrastructure/events/PhabricatorAutoEventListener.php',
|
||||||
'PhabricatorBarePageExample' => 'applications/uiexample/examples/PhabricatorBarePageExample.php',
|
'PhabricatorBarePageExample' => 'applications/uiexample/examples/PhabricatorBarePageExample.php',
|
||||||
'PhabricatorBarePageView' => 'view/page/PhabricatorBarePageView.php',
|
'PhabricatorBarePageView' => 'view/page/PhabricatorBarePageView.php',
|
||||||
'PhabricatorBaseEnglishTranslation' => 'infrastructure/internationalization/translation/PhabricatorBaseEnglishTranslation.php',
|
'PhabricatorBaseEnglishTranslation' => 'infrastructure/internationalization/translation/PhabricatorBaseEnglishTranslation.php',
|
||||||
|
@ -4062,6 +4063,7 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorAuthUnlinkController' => 'PhabricatorAuthController',
|
'PhabricatorAuthUnlinkController' => 'PhabricatorAuthController',
|
||||||
'PhabricatorAuthValidateController' => 'PhabricatorAuthController',
|
'PhabricatorAuthValidateController' => 'PhabricatorAuthController',
|
||||||
'PhabricatorAuthenticationConfigOptions' => 'PhabricatorApplicationConfigOptions',
|
'PhabricatorAuthenticationConfigOptions' => 'PhabricatorApplicationConfigOptions',
|
||||||
|
'PhabricatorAutoEventListener' => 'PhabricatorEventListener',
|
||||||
'PhabricatorBarePageExample' => 'PhabricatorUIExample',
|
'PhabricatorBarePageExample' => 'PhabricatorUIExample',
|
||||||
'PhabricatorBarePageView' => 'AphrontPageView',
|
'PhabricatorBarePageView' => 'AphrontPageView',
|
||||||
'PhabricatorBaseEnglishTranslation' => 'PhabricatorTranslation',
|
'PhabricatorBaseEnglishTranslation' => 'PhabricatorTranslation',
|
||||||
|
|
17
src/infrastructure/events/PhabricatorAutoEventListener.php
Normal file
17
src/infrastructure/events/PhabricatorAutoEventListener.php
Normal 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 {
|
||||||
|
|
||||||
|
}
|
|
@ -1,36 +1,50 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
|
||||||
* @group events
|
|
||||||
*/
|
|
||||||
final class PhabricatorEventEngine {
|
final class PhabricatorEventEngine {
|
||||||
|
|
||||||
public static function initialize() {
|
public static function initialize() {
|
||||||
$listeners = PhabricatorEnv::getEnvConfig('events.listeners');
|
// NOTE: If any of this fails, we just log it and move on. It's important
|
||||||
foreach ($listeners as $listener) {
|
// 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 {
|
try {
|
||||||
id(new $listener())->register();
|
$listeners[] = newv($listener_class, array());
|
||||||
} catch (Exception $ex) {
|
} 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);
|
phlog($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the DarkConosole event logger.
|
// Add builtin listeners.
|
||||||
id(new DarkConsoleEventPluginAPI())->register();
|
$listeners[] = new DarkConsoleEventPluginAPI();
|
||||||
id(new ManiphestEdgeEventListener())->register();
|
$listeners[] = new ManiphestEdgeEventListener();
|
||||||
|
|
||||||
|
// Add application listeners.
|
||||||
$applications = PhabricatorApplication::getAllInstalledApplications();
|
$applications = PhabricatorApplication::getAllInstalledApplications();
|
||||||
foreach ($applications as $application) {
|
foreach ($applications as $application) {
|
||||||
$listeners = $application->getEventListeners();
|
$app_listeners = $application->getEventListeners();
|
||||||
foreach ($listeners as $listener) {
|
foreach ($app_listeners as $listener) {
|
||||||
$listener->setApplication($application);
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue