1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-19 12:00:55 +01:00

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
This commit is contained in:
Marek Sapota 2011-11-09 17:23:33 -08:00
parent 98c8e150b0
commit e9693f25f8
25 changed files with 27 additions and 211 deletions

View file

@ -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',

View file

@ -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(),

View file

@ -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');

View file

@ -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;
}

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -120,7 +120,7 @@ abstract class DifferentialMail {
'mail' => $template,
)
);
PhabricatorEventEngine::dispatchEvent($event);
PhutilEventEngine::dispatchEvent($event);
$template = $event->getValue('mail');

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -1,21 +0,0 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
abstract class PhabricatorEventConstants {
}

View file

@ -1,10 +0,0 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_source('PhabricatorEventConstants.php');

View file

@ -16,9 +16,8 @@
* limitations under the License.
*/
final class PhabricatorEventType extends PhabricatorEventConstants {
final class PhabricatorEventType extends PhutilEventType {
const TYPE_ALL = '*';
const TYPE_MANIPHEST_WILLEDITTASK = 'maniphest.willEditTask';
const TYPE_DIFFERENTIAL_WILLSENDMAIL = 'differential.willSendMail';

View file

@ -6,7 +6,7 @@
phutil_require_module('phabricator', 'infrastructure/events/constant/base');
phutil_require_module('phutil', 'events/constant/type');
phutil_require_source('PhabricatorEventType.php');

View file

@ -18,74 +18,14 @@
class PhabricatorEventEngine {
private static $instance;
private $listeners = array();
private function __construct() {
// <empty>
}
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();
}
}

View file

@ -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');

View file

@ -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;
}
}

View file

@ -6,7 +6,7 @@
phutil_require_module('phutil', 'utils');
phutil_require_module('phutil', 'events/event');
phutil_require_source('PhabricatorEvent.php');

View file

@ -1,53 +0,0 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
abstract class PhabricatorEventListener {
private $listenerID;
private static $nextListenerID = 1;
final public function __construct() {
// <empty>
}
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;
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_module('phabricator', 'infrastructure/events/engine');
phutil_require_source('PhabricatorEventListener.php');