szurubooru/public_html/dispatch.php

96 lines
2.2 KiB
PHP
Raw Normal View History

2013-10-05 12:55:03 +02:00
<?php
require_once '../src/core.php';
2013-10-05 12:55:03 +02:00
2014-05-15 21:38:22 +02:00
if (isset($_SERVER['REDIRECT_URL']))
$query = rtrim($_SERVER['REDIRECT_URL'], '/');
else
$query = rtrim($_SERVER['REQUEST_URI'], '/');
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
$context->query = $query;
function renderView()
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
\Chibi\View::render($context->layoutName, $context);
}
$context->simpleControllerName = null;
$context->simpleActionName = null;
\Chibi\Router::setObserver(function($route, $args)
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
$context->route = $route;
list ($className, $methodName) = $route->destination;
$context->simpleControllerName = TextCaseConverter::convert(
str_replace('Controller', '', $className),
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE);
$context->simpleActionName = TextCaseConverter::convert(
Introducing API Right now there's a lot of messy code in controllers. Furthermore, there is no way to interact with szurubooru via vanilla HTTP, since API is next to non-existent. So, basing upon my experiences from another project, I plan to: - Create actual API. It is going to consist of well-defined "jobs" that do things currently done by controllers. Benefits of such approach are as follows: - defining them in their own classes allows to clean up code a lot, - it allows to abstract from input method (POST data, part of URL, whatever), and leave processing of these to controllers, - it allows to make proxy controller, whose purpose would be to let users interact with API (jobs) directly in well-documented and consistent way. - Make controllers responsible only for mediating between views and API. Behavior of these may remain inconsistent, since views they're talking to are also messy to begin with. Such controllers might be removed altogether in the future in favor of making views talk to API directly through previously mentioned ApiController. - Organize all sorts of privilege checking and possibly other stuff into methods within jobs. - Actually distinguish POST from GET requests. - Leave POST-only controller methods as Actions, but rename GET-only methods to Views. Example: editAction for editing comments, but listView for showing comment list. The choice of these suffixes might be subject to changes in future. - Get rid of ?json and $context->transport. They now look like disease to me. This commit introduces job system and converts CommentController to use the new API.
2014-05-01 16:25:10 +02:00
preg_replace('/Action|View/', '', $methodName),
2014-04-29 21:35:29 +02:00
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE);
$context->viewName = sprintf(
'%s-%s',
$context->simpleControllerName,
$context->simpleActionName);
});
2014-05-15 10:32:53 +02:00
Assets::setTitle(Core::getConfig()->main->title);
2014-04-29 21:35:29 +02:00
$context->handleExceptions = false;
2014-05-15 23:11:53 +02:00
$context->layoutName
= isset($_SERVER['HTTP_X_AJAX'])
2014-04-29 21:35:29 +02:00
? 'layout-json'
: 'layout-normal';
$context->viewName = '';
$context->transport = new StdClass;
session_start();
2014-05-01 16:12:37 +02:00
if (!Auth::isLoggedIn())
Auth::tryAutoLogin();
2014-04-29 21:35:29 +02:00
register_shutdown_function(function()
{
$error = error_get_last();
if ($error !== null)
\Chibi\Util\Headers::setCode(400);
});
2014-04-29 21:35:29 +02:00
try
{
try
{
\Chibi\Router::run($query);
renderView();
AuthController::observeWorkFinish();
}
catch (\Chibi\UnhandledRouteException $e)
{
throw new SimpleNotFoundException($query . ' not found.');
}
}
catch (SimpleException $e)
{
if ($e instanceof SimpleNotFoundException)
\Chibi\Util\Headers::setCode(404);
else
\Chibi\Util\Headers::setCode(400);
Messenger::message($e->getMessage(), false);
2014-04-29 21:35:29 +02:00
if (!$context->handleExceptions)
$context->viewName = 'message';
renderView();
}
catch (Exception $e)
{
\Chibi\Util\Headers::setCode(400);
2014-05-15 23:11:53 +02:00
Messenger::message($e->getMessage(), false);
2014-04-29 21:35:29 +02:00
$context->transport->exception = $e;
$context->transport->queries = \Chibi\Database::getLogs();
$context->viewName = 'error-exception';
renderView();
}