From 27a369d15acc5403e59a02eb12bec62d36eb7c16 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 22 May 2014 15:19:28 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 2 + .../events/PhabricatorAutoEventListener.php | 17 +++++++ .../events/PhabricatorEventEngine.php | 44 ++++++++++++------- 3 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 src/infrastructure/events/PhabricatorAutoEventListener.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index deb32b0689..4af4552907 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/infrastructure/events/PhabricatorAutoEventListener.php b/src/infrastructure/events/PhabricatorAutoEventListener.php new file mode 100644 index 0000000000..1817e5b9c2 --- /dev/null +++ b/src/infrastructure/events/PhabricatorAutoEventListener.php @@ -0,0 +1,17 @@ +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); + } + } } }