From aa06d760b375c0cc9bbc693bf4bd412e9fda8f50 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 07:15:52 -0500 Subject: Index and Action use Exceptions Main Web entry point accepts exceptions, and main code in Action throws them. --- index.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 387b642e2..075ee9676 100644 --- a/index.php +++ b/index.php @@ -47,7 +47,11 @@ if (!$user && common_config('site', 'private') && $actionfile = INSTALLDIR."/actions/$action.php"; -if (file_exists($actionfile)) { +if (!file_exists($actionfile)) { + $cac = new ClientErrorAction(); + $cac->handle(array('code' => 404, + 'message' => _('Unknown action'))); +} else { include_once $actionfile; @@ -66,9 +70,22 @@ if (file_exists($actionfile)) { } $config['db']['database'] = $mirror; } - if (call_user_func(array($action_obj, 'prepare'), $_REQUEST)) { - call_user_func(array($action_obj, 'handle'), $_REQUEST); + + try { + if ($action_obj->prepare($_REQUEST)) { + $action_obj->handle($_REQUEST); + } + } catch (ClientException cex) { + $cac = new ClientErrorAction(); + $cac->handle(array('code' => $cex->code, + 'message' => $cex->message)); + } catch (ServerException sex) { // snort snort guffaw + $sac = new ServerErrorAction(); + $sac->handle(array('code' => $sex->code, + 'message' => $sex->message)); + } catch (Exception ex) { + $sac = new ServerErrorAction(); + $sac->handle(array('code' => 500, + 'message' => $ex->message)); } -} else { - common_user_error(_('Unknown action')); -} \ No newline at end of file +} -- cgit v1.2.3-54-g00ecf From 5466f6a6d0950331a4cb54e09b44ea4524751fb4 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 07:25:35 -0500 Subject: Better exception handling in index Some better exception handling in Web entry point. --- index.php | 26 +++++++++++--------------- lib/common.php | 5 +++++ 2 files changed, 16 insertions(+), 15 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 075ee9676..dac5a8071 100644 --- a/index.php +++ b/index.php @@ -48,9 +48,8 @@ if (!$user && common_config('site', 'private') && $actionfile = INSTALLDIR."/actions/$action.php"; if (!file_exists($actionfile)) { - $cac = new ClientErrorAction(); - $cac->handle(array('code' => 404, - 'message' => _('Unknown action'))); + $cac = new ClientErrorAction(_('Unknown action'), 404); + $cac->showPage(); } else { include_once $actionfile; @@ -75,17 +74,14 @@ if (!file_exists($actionfile)) { if ($action_obj->prepare($_REQUEST)) { $action_obj->handle($_REQUEST); } - } catch (ClientException cex) { - $cac = new ClientErrorAction(); - $cac->handle(array('code' => $cex->code, - 'message' => $cex->message)); - } catch (ServerException sex) { // snort snort guffaw - $sac = new ServerErrorAction(); - $sac->handle(array('code' => $sex->code, - 'message' => $sex->message)); - } catch (Exception ex) { - $sac = new ServerErrorAction(); - $sac->handle(array('code' => 500, - 'message' => $ex->message)); + } catch (ClientException $cex) { + $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode()); + $cac->showPage(); + } catch (ServerException $sex) { // snort snort guffaw + $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode()); + $sac->showPage(); + } catch (Exception $ex) { + $sac = new ServerErrorAction($ex->getMessage()); + $sac->showPage(); } } diff --git a/lib/common.php b/lib/common.php index 7d3ec108c..64c7f2410 100644 --- a/lib/common.php +++ b/lib/common.php @@ -182,6 +182,8 @@ foreach ($_config_files as $_config_file) { } } +// XXX: how many of these could be auto-loaded on use? + require_once('Validate.php'); require_once('markdown.php'); @@ -193,6 +195,9 @@ require_once(INSTALLDIR.'/lib/subs.php'); require_once(INSTALLDIR.'/lib/Shorturl_api.php'); require_once(INSTALLDIR.'/lib/twitter.php'); +require_once(INSTALLDIR.'/lib/clientexception.php'); +require_once(INSTALLDIR.'/lib/serverexception.php'); + // XXX: other formats here define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER); -- cgit v1.2.3-54-g00ecf From f4e8cc6d9f88e78876baa54d0ffba77694c56a1b Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:44:30 -0500 Subject: Add InitializePlugin and CleanupPlugin events We add two events to allow plugins to initialize and cleanup. --- EVENTS.txt | 5 +++++ index.php | 5 +++++ lib/common.php | 4 ++++ 3 files changed, 14 insertions(+) (limited to 'index.php') diff --git a/EVENTS.txt b/EVENTS.txt index 68e25fa3b..4b8260b3c 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -1,3 +1,8 @@ +InitializePlugin: a chance to initialize a plugin in a complete + environment + +CleanupPlugin: a chance to cleanup a plugin at the end of a program + StartPrimaryNav: Showing the primary nav menu - $action: the current action diff --git a/index.php b/index.php index dac5a8071..0a79b9731 100644 --- a/index.php +++ b/index.php @@ -85,3 +85,8 @@ if (!file_exists($actionfile)) { $sac->showPage(); } } + +// XXX: cleanup exit() calls or add an exit handler so +// this always gets called + +Event::handle('CleanupPlugin'); diff --git a/lib/common.php b/lib/common.php index 64c7f2410..2f85eb7c5 100644 --- a/lib/common.php +++ b/lib/common.php @@ -212,3 +212,7 @@ function __autoload($class) require_once(INSTALLDIR.'/lib/' . strtolower($class) . '.php'); } } + +// Give plugins a chance to initialize in a fully-prepared environment + +Event::handle('InitializePlugin'); -- cgit v1.2.3-54-g00ecf From 9e23b5c5d706f4573261d6530688a44a8b80bcf4 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 11:46:26 -0500 Subject: Change action autoloading to allow actions in plugins Since plugins may define custom actions, we shouldn't require that there be a file in our actions/ subdir for every action. So, I changed the (admittedly hackish) auto-loading code in index.php so it instead checks whether a class exists with the expected name. This, in turn, uses the increasingly hacking __autoload() function, which I changed to auto-load stuff named "BlahblahAction" from the actions subdir if available. --- index.php | 13 ++++++------- lib/common.php | 3 +++ 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 0a79b9731..e62d9469a 100644 --- a/index.php +++ b/index.php @@ -22,6 +22,8 @@ define('LACONICA', true); require_once INSTALLDIR . '/lib/common.php'; +// XXX: we need a little more structure in this script + // get and cache current user $user = common_current_user(); @@ -45,19 +47,16 @@ if (!$user && common_config('site', 'private') && common_redirect(common_local_url('login')); } -$actionfile = INSTALLDIR."/actions/$action.php"; +$action_class = ucfirst($action).'Action'; -if (!file_exists($actionfile)) { +if (!class_exists($action_class)) { $cac = new ClientErrorAction(_('Unknown action'), 404); $cac->showPage(); } else { - - include_once $actionfile; - - $action_class = ucfirst($action).'Action'; - $action_obj = new $action_class(); + // XXX: find somewhere for this little block to live + if ($config['db']['mirror'] && $action_obj->isReadOnly()) { if (is_array($config['db']['mirror'])) { // "load balancing", ha ha diff --git a/lib/common.php b/lib/common.php index 041459cf3..7bfd14c42 100644 --- a/lib/common.php +++ b/lib/common.php @@ -211,6 +211,9 @@ function __autoload($class) require_once(INSTALLDIR.'/classes/' . $class . '.php'); } else if (file_exists(INSTALLDIR.'/lib/' . strtolower($class) . '.php')) { require_once(INSTALLDIR.'/lib/' . strtolower($class) . '.php'); + } else if (mb_substr($class, -6) == 'Action' && + file_exists(INSTALLDIR.'/actions/' . strtolower(mb_substr($class, 0, -6)) . '.php')) { + require_once(INSTALLDIR.'/actions/' . strtolower(mb_substr($class, 0, -6)) . '.php'); } } -- cgit v1.2.3-54-g00ecf