From e9693f25f89e0833e2796447c3504102d006a5f6 Mon Sep 17 00:00:00 2001 From: Marek Sapota Date: Wed, 9 Nov 2011 17:23:33 -0800 Subject: [PATCH] Move event framework from Phabricator to libphutil Summary: Move event framework from Phabricator to libphutil so it can be used in other phutil projects, such as Arcanist. Test plan: Use along with path to libphutil, events should work as expected. Reviewers: epriestley Differential Revision: 1098 --- src/__phutil_library_map__.php | 7 +- .../plugin/event/DarkConsoleEventPlugin.php | 2 +- src/aphront/console/plugin/event/__init__.php | 2 +- .../event/api/DarkConsoleEventPluginAPI.php | 4 +- .../console/plugin/event/api/__init__.php | 3 +- ...ConduitAPI_maniphest_createtask_Method.php | 2 +- .../method/maniphest/createtask/__init__.php | 3 +- .../mail/base/DifferentialMail.php | 2 +- .../differential/mail/base/__init__.php | 2 +- .../taskedit/ManiphestTaskEditController.php | 2 +- .../controller/taskedit/__init__.php | 2 +- .../ManiphestTransactionSaveController.php | 2 +- .../controller/transactionsave/__init__.php | 2 +- .../replyhandler/ManiphestReplyHandler.php | 2 +- .../maniphest/replyhandler/__init__.php | 2 +- .../base/PhabricatorEventConstants.php | 21 ------ .../events/constant/base/__init__.php | 10 --- .../constant/type/PhabricatorEventType.php | 3 +- .../events/constant/type/__init__.php | 2 +- .../events/engine/PhabricatorEventEngine.php | 64 +------------------ src/infrastructure/events/engine/__init__.php | 1 - .../events/event/PhabricatorEvent.php | 31 +-------- src/infrastructure/events/event/__init__.php | 2 +- .../listener/PhabricatorEventListener.php | 53 --------------- .../events/listener/__init__.php | 12 ---- 25 files changed, 27 insertions(+), 211 deletions(-) delete mode 100644 src/infrastructure/events/constant/base/PhabricatorEventConstants.php delete mode 100644 src/infrastructure/events/constant/base/__init__.php delete mode 100644 src/infrastructure/events/listener/PhabricatorEventListener.php delete mode 100644 src/infrastructure/events/listener/__init__.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 601fa67e89..adfba5e543 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -423,9 +423,7 @@ phutil_register_library_map(array( 'PhabricatorEmailTokenController' => 'applications/auth/controller/emailtoken', 'PhabricatorEnv' => 'infrastructure/env', 'PhabricatorEvent' => 'infrastructure/events/event', - 'PhabricatorEventConstants' => 'infrastructure/events/constant/base', 'PhabricatorEventEngine' => 'infrastructure/events/engine', - 'PhabricatorEventListener' => 'infrastructure/events/listener', 'PhabricatorEventType' => 'infrastructure/events/constant/type', 'PhabricatorFeedConstants' => 'applications/feed/constants/base', 'PhabricatorFeedController' => 'applications/feed/controller/base', @@ -865,7 +863,7 @@ phutil_register_library_map(array( 'DarkConsoleController' => 'PhabricatorController', 'DarkConsoleErrorLogPlugin' => 'DarkConsolePlugin', 'DarkConsoleEventPlugin' => 'DarkConsolePlugin', - 'DarkConsoleEventPluginAPI' => 'PhabricatorEventListener', + 'DarkConsoleEventPluginAPI' => 'PhutilEventListener', 'DarkConsoleRequestPlugin' => 'DarkConsolePlugin', 'DarkConsoleServicesPlugin' => 'DarkConsolePlugin', 'DarkConsoleXHProfPlugin' => 'DarkConsolePlugin', @@ -1076,7 +1074,8 @@ phutil_register_library_map(array( 'PhabricatorDraftDAO' => 'PhabricatorLiskDAO', 'PhabricatorEmailLoginController' => 'PhabricatorAuthController', 'PhabricatorEmailTokenController' => 'PhabricatorAuthController', - 'PhabricatorEventType' => 'PhabricatorEventConstants', + 'PhabricatorEvent' => 'PhutilEvent', + 'PhabricatorEventType' => 'PhutilEventType', 'PhabricatorFeedController' => 'PhabricatorController', 'PhabricatorFeedDAO' => 'PhabricatorLiskDAO', 'PhabricatorFeedPublicStreamController' => 'PhabricatorFeedController', diff --git a/src/aphront/console/plugin/event/DarkConsoleEventPlugin.php b/src/aphront/console/plugin/event/DarkConsoleEventPlugin.php index 877f4c3fe4..2b7d92bbda 100644 --- a/src/aphront/console/plugin/event/DarkConsoleEventPlugin.php +++ b/src/aphront/console/plugin/event/DarkConsoleEventPlugin.php @@ -31,7 +31,7 @@ class DarkConsoleEventPlugin extends DarkConsolePlugin { public function generateData() { - $listeners = PhabricatorEventEngine::getInstance()->getAllListeners(); + $listeners = PhutilEventEngine::getInstance()->getAllListeners(); foreach ($listeners as $key => $listener) { $listeners[$key] = array( 'id' => $listener->getListenerID(), diff --git a/src/aphront/console/plugin/event/__init__.php b/src/aphront/console/plugin/event/__init__.php index 0106b475cb..fcb69861a5 100644 --- a/src/aphront/console/plugin/event/__init__.php +++ b/src/aphront/console/plugin/event/__init__.php @@ -8,9 +8,9 @@ phutil_require_module('phabricator', 'aphront/console/plugin/base'); phutil_require_module('phabricator', 'aphront/console/plugin/event/api'); -phutil_require_module('phabricator', 'infrastructure/events/engine'); phutil_require_module('phabricator', 'view/control/table'); +phutil_require_module('phutil', 'events/engine'); phutil_require_module('phutil', 'markup'); diff --git a/src/aphront/console/plugin/event/api/DarkConsoleEventPluginAPI.php b/src/aphront/console/plugin/event/api/DarkConsoleEventPluginAPI.php index 39e28380c1..46f7834ab3 100644 --- a/src/aphront/console/plugin/event/api/DarkConsoleEventPluginAPI.php +++ b/src/aphront/console/plugin/event/api/DarkConsoleEventPluginAPI.php @@ -19,7 +19,7 @@ /** * @group console */ -class DarkConsoleEventPluginAPI extends PhabricatorEventListener { +class DarkConsoleEventPluginAPI extends PhutilEventListener { private static $events = array(); private static $discardMode = false; @@ -36,7 +36,7 @@ class DarkConsoleEventPluginAPI extends PhabricatorEventListener { $this->listen(PhabricatorEventType::TYPE_ALL); } - public function handleEvent(PhabricatorEvent $event) { + public function handleEvent(PhutilEvent $event) { if (self::$discardMode) { return; } diff --git a/src/aphront/console/plugin/event/api/__init__.php b/src/aphront/console/plugin/event/api/__init__.php index 73140ad00e..29efa7cabd 100644 --- a/src/aphront/console/plugin/event/api/__init__.php +++ b/src/aphront/console/plugin/event/api/__init__.php @@ -7,7 +7,8 @@ phutil_require_module('phabricator', 'infrastructure/events/constant/type'); -phutil_require_module('phabricator', 'infrastructure/events/listener'); + +phutil_require_module('phutil', 'events/listener'); phutil_require_source('DarkConsoleEventPluginAPI.php'); diff --git a/src/applications/conduit/method/maniphest/createtask/ConduitAPI_maniphest_createtask_Method.php b/src/applications/conduit/method/maniphest/createtask/ConduitAPI_maniphest_createtask_Method.php index 5528b7a8dd..f19f635e08 100644 --- a/src/applications/conduit/method/maniphest/createtask/ConduitAPI_maniphest_createtask_Method.php +++ b/src/applications/conduit/method/maniphest/createtask/ConduitAPI_maniphest_createtask_Method.php @@ -112,7 +112,7 @@ final class ConduitAPI_maniphest_createtask_Method )); $event->setUser($request->getUser()); $event->setConduitRequest($request); - PhabricatorEventEngine::dispatchEvent($event); + PhutilEventEngine::dispatchEvent($event); $task = $event->getValue('task'); $transactions = $event->getValue('transactions'); diff --git a/src/applications/conduit/method/maniphest/createtask/__init__.php b/src/applications/conduit/method/maniphest/createtask/__init__.php index e49eab9a4b..ecd820aefb 100644 --- a/src/applications/conduit/method/maniphest/createtask/__init__.php +++ b/src/applications/conduit/method/maniphest/createtask/__init__.php @@ -16,8 +16,9 @@ phutil_require_module('phabricator', 'applications/maniphest/storage/transaction phutil_require_module('phabricator', 'applications/metamta/contentsource/source'); phutil_require_module('phabricator', 'applications/phid/constants'); phutil_require_module('phabricator', 'infrastructure/events/constant/type'); -phutil_require_module('phabricator', 'infrastructure/events/engine'); phutil_require_module('phabricator', 'infrastructure/events/event'); +phutil_require_module('phutil', 'events/engine'); + phutil_require_source('ConduitAPI_maniphest_createtask_Method.php'); diff --git a/src/applications/differential/mail/base/DifferentialMail.php b/src/applications/differential/mail/base/DifferentialMail.php index b02ee16377..70fcc3bfb4 100644 --- a/src/applications/differential/mail/base/DifferentialMail.php +++ b/src/applications/differential/mail/base/DifferentialMail.php @@ -120,7 +120,7 @@ abstract class DifferentialMail { 'mail' => $template, ) ); - PhabricatorEventEngine::dispatchEvent($event); + PhutilEventEngine::dispatchEvent($event); $template = $event->getValue('mail'); diff --git a/src/applications/differential/mail/base/__init__.php b/src/applications/differential/mail/base/__init__.php index c92e1dfce6..7c9230ba90 100644 --- a/src/applications/differential/mail/base/__init__.php +++ b/src/applications/differential/mail/base/__init__.php @@ -10,9 +10,9 @@ phutil_require_module('phabricator', 'applications/metamta/storage/mail'); phutil_require_module('phabricator', 'applications/phid/handle/data'); phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'infrastructure/events/constant/type'); -phutil_require_module('phabricator', 'infrastructure/events/engine'); phutil_require_module('phabricator', 'infrastructure/events/event'); +phutil_require_module('phutil', 'events/engine'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/maniphest/controller/taskedit/ManiphestTaskEditController.php b/src/applications/maniphest/controller/taskedit/ManiphestTaskEditController.php index 42225452d3..80680ef8f4 100644 --- a/src/applications/maniphest/controller/taskedit/ManiphestTaskEditController.php +++ b/src/applications/maniphest/controller/taskedit/ManiphestTaskEditController.php @@ -211,7 +211,7 @@ class ManiphestTaskEditController extends ManiphestController { )); $event->setUser($user); $event->setAphrontRequest($request); - PhabricatorEventEngine::dispatchEvent($event); + PhutilEventEngine::dispatchEvent($event); $task = $event->getValue('task'); $transactions = $event->getValue('transactions'); diff --git a/src/applications/maniphest/controller/taskedit/__init__.php b/src/applications/maniphest/controller/taskedit/__init__.php index f5eb5b88a6..b15d78f4ba 100644 --- a/src/applications/maniphest/controller/taskedit/__init__.php +++ b/src/applications/maniphest/controller/taskedit/__init__.php @@ -23,7 +23,6 @@ phutil_require_module('phabricator', 'applications/phid/handle/data'); phutil_require_module('phabricator', 'infrastructure/celerity/api'); phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'infrastructure/events/constant/type'); -phutil_require_module('phabricator', 'infrastructure/events/engine'); phutil_require_module('phabricator', 'infrastructure/events/event'); phutil_require_module('phabricator', 'infrastructure/javelin/api'); phutil_require_module('phabricator', 'infrastructure/javelin/markup'); @@ -38,6 +37,7 @@ phutil_require_module('phabricator', 'view/form/control/tokenizer'); phutil_require_module('phabricator', 'view/form/error'); phutil_require_module('phabricator', 'view/layout/panel'); +phutil_require_module('phutil', 'events/engine'); phutil_require_module('phutil', 'markup'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/maniphest/controller/transactionsave/ManiphestTransactionSaveController.php b/src/applications/maniphest/controller/transactionsave/ManiphestTransactionSaveController.php index eaf3537c53..fde6d26eb9 100644 --- a/src/applications/maniphest/controller/transactionsave/ManiphestTransactionSaveController.php +++ b/src/applications/maniphest/controller/transactionsave/ManiphestTransactionSaveController.php @@ -238,7 +238,7 @@ class ManiphestTransactionSaveController extends ManiphestController { )); $event->setUser($user); $event->setAphrontRequest($request); - PhabricatorEventEngine::dispatchEvent($event); + PhutilEventEngine::dispatchEvent($event); $task = $event->getValue('task'); $transactions = $event->getValue('transactions'); diff --git a/src/applications/maniphest/controller/transactionsave/__init__.php b/src/applications/maniphest/controller/transactionsave/__init__.php index f2a614e172..a949f45e12 100644 --- a/src/applications/maniphest/controller/transactionsave/__init__.php +++ b/src/applications/maniphest/controller/transactionsave/__init__.php @@ -20,9 +20,9 @@ phutil_require_module('phabricator', 'applications/markup/engine'); phutil_require_module('phabricator', 'applications/metamta/contentsource/source'); phutil_require_module('phabricator', 'applications/phid/constants'); phutil_require_module('phabricator', 'infrastructure/events/constant/type'); -phutil_require_module('phabricator', 'infrastructure/events/engine'); phutil_require_module('phabricator', 'infrastructure/events/event'); +phutil_require_module('phutil', 'events/engine'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/maniphest/replyhandler/ManiphestReplyHandler.php b/src/applications/maniphest/replyhandler/ManiphestReplyHandler.php index 3d1bee8554..c643266041 100644 --- a/src/applications/maniphest/replyhandler/ManiphestReplyHandler.php +++ b/src/applications/maniphest/replyhandler/ManiphestReplyHandler.php @@ -159,7 +159,7 @@ class ManiphestReplyHandler extends PhabricatorMailReplyHandler { 'transactions' => $xactions, )); $event->setUser($user); - PhabricatorEventEngine::dispatchEvent($event); + PhutilEventEngine::dispatchEvent($event); $task = $event->getValue('task'); $xactions = $event->getValue('transactions'); diff --git a/src/applications/maniphest/replyhandler/__init__.php b/src/applications/maniphest/replyhandler/__init__.php index 818dae18c4..fc9fe1dd74 100644 --- a/src/applications/maniphest/replyhandler/__init__.php +++ b/src/applications/maniphest/replyhandler/__init__.php @@ -15,9 +15,9 @@ phutil_require_module('phabricator', 'applications/metamta/replyhandler/base'); phutil_require_module('phabricator', 'applications/phid/constants'); phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'infrastructure/events/constant/type'); -phutil_require_module('phabricator', 'infrastructure/events/engine'); phutil_require_module('phabricator', 'infrastructure/events/event'); +phutil_require_module('phutil', 'events/engine'); phutil_require_module('phutil', 'utils'); diff --git a/src/infrastructure/events/constant/base/PhabricatorEventConstants.php b/src/infrastructure/events/constant/base/PhabricatorEventConstants.php deleted file mode 100644 index b8d5bd0791..0000000000 --- a/src/infrastructure/events/constant/base/PhabricatorEventConstants.php +++ /dev/null @@ -1,21 +0,0 @@ - - } - public static function initialize() { - self::$instance = new PhabricatorEventEngine(); - - // Register the DarkConosole event logger. - id(new DarkConsoleEventPluginAPI())->register(); - - // Instantiate and register custom event listeners so they can react to - // events. $listeners = PhabricatorEnv::getEnvConfig('events.listeners'); foreach ($listeners as $listener) { id(new $listener())->register(); } - } - public static function getInstance() { - if (!self::$instance) { - throw new Exception("Event engine has not been initialized!"); - } - return self::$instance; - } - - public function addListener( - PhabricatorEventListener $listener, - $type) { - $this->listeners[$type][] = $listener; - return $this; - } - - /** - * Get all the objects currently listening to any event. - */ - public function getAllListeners() { - $listeners = array_mergev($this->listeners); - $listeners = mpull($listeners, null, 'getListenerID'); - return $listeners; - } - - public static function dispatchEvent(PhabricatorEvent $event) { - $instance = self::getInstance(); - - $listeners = idx($instance->listeners, $event->getType(), array()); - $global_listeners = idx( - $instance->listeners, - PhabricatorEventType::TYPE_ALL, - array()); - - // Merge and deduplicate listeners (we want to send the event to each - // listener only once, even if it satisfies multiple criteria for the - // event). - $listeners = array_merge($listeners, $global_listeners); - $listeners = mpull($listeners, null, 'getListenerID'); - - foreach ($listeners as $listener) { - if ($event->isStopped()) { - // Do this first so if someone tries to dispatch a stopped event it - // doesn't go anywhere. Silly but less surprising. - break; - } - $listener->handleEvent($event); - } + // Register the DarkConosole event logger. + id(new DarkConsoleEventPluginAPI())->register(); } } diff --git a/src/infrastructure/events/engine/__init__.php b/src/infrastructure/events/engine/__init__.php index 14fda1ef3e..cc53e4e993 100644 --- a/src/infrastructure/events/engine/__init__.php +++ b/src/infrastructure/events/engine/__init__.php @@ -8,7 +8,6 @@ phutil_require_module('phabricator', 'aphront/console/plugin/event/api'); phutil_require_module('phabricator', 'infrastructure/env'); -phutil_require_module('phabricator', 'infrastructure/events/constant/type'); phutil_require_module('phutil', 'utils'); diff --git a/src/infrastructure/events/event/PhabricatorEvent.php b/src/infrastructure/events/event/PhabricatorEvent.php index d97ebeef84..197106e1a7 100644 --- a/src/infrastructure/events/event/PhabricatorEvent.php +++ b/src/infrastructure/events/event/PhabricatorEvent.php @@ -16,19 +16,14 @@ * limitations under the License. */ -final class PhabricatorEvent { +final class PhabricatorEvent extends PhutilEvent { private $user; private $aphrontRequest; private $conduitRequest; - private $type; - private $data; - private $stop = false; - public function __construct($type, array $data = array()) { - $this->type = $type; - $this->data = $data; + parent::__construct($type, $data); } public function setUser(PhabricatorUser $user) { @@ -58,28 +53,6 @@ final class PhabricatorEvent { return $this->conduitRequest; } - public function getType() { - return $this->type; - } - - public function getValue($key, $default = null) { - return idx($this->data, $key, $default); - } - - public function setValue($key, $value) { - $this->data[$key] = $value; - return $this; - } - - public function stop() { - $this->stop = true; - return $this; - } - - public function isStopped() { - return $this->stop; - } - } diff --git a/src/infrastructure/events/event/__init__.php b/src/infrastructure/events/event/__init__.php index 020d6b81eb..8e43b5a5bb 100644 --- a/src/infrastructure/events/event/__init__.php +++ b/src/infrastructure/events/event/__init__.php @@ -6,7 +6,7 @@ -phutil_require_module('phutil', 'utils'); +phutil_require_module('phutil', 'events/event'); phutil_require_source('PhabricatorEvent.php'); diff --git a/src/infrastructure/events/listener/PhabricatorEventListener.php b/src/infrastructure/events/listener/PhabricatorEventListener.php deleted file mode 100644 index fe69ed3d7b..0000000000 --- a/src/infrastructure/events/listener/PhabricatorEventListener.php +++ /dev/null @@ -1,53 +0,0 @@ - - } - - abstract public function register(); - abstract public function handleEvent(PhabricatorEvent $event); - - final public function listen($type) { - $engine = PhabricatorEventEngine::getInstance(); - $engine->addListener($this, $type); - } - - - /** - * Return a scalar ID unique to this listener. This is used to deduplicate - * listeners which match events on multiple rules, so they are invoked only - * once. - * - * @return int A scalar unique to this object instance. - */ - final public function getListenerID() { - if (!$this->listenerID) { - $this->listenerID = self::$nextListenerID; - self::$nextListenerID++; - } - return $this->listenerID; - } - - -} diff --git a/src/infrastructure/events/listener/__init__.php b/src/infrastructure/events/listener/__init__.php deleted file mode 100644 index b3e2ba606c..0000000000 --- a/src/infrastructure/events/listener/__init__.php +++ /dev/null @@ -1,12 +0,0 @@ -