Example #1
0
 function __construct(Application $app, Request $request, SessionHandler $handler = NULL, $priority = 20)
 {
     parent::__construct($request, $handler);
     $middlewareOptions = ['priority' => $priority, 'uri' => $request['REQUEST_URI'], 'method' => $request['REQUEST_METHOD']];
     $self = $this;
     $app->after(function ($request, $response) use($self) {
         if ($self->shouldSetCookie()) {
             $self->close();
             list($name, $value, $options) = $self->getCookieElements();
             $response->setRawCookie($name, $value, $options);
             $cacheControlHeader = $self->generateCacheControlHeader();
             $response->setHeader('Cache-Control', $cacheControlHeader);
         }
     }, $middlewareOptions);
 }
Example #2
0
}
/**
 * Parse errors cannot be handled inside the same file where they originate.
 * For this reason we have to include the application file externally here
 * so that our shutdown function can handle E_PARSE.
 */
register_shutdown_function(function () {
    $fatals = [E_ERROR, E_PARSE, E_USER_ERROR, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING];
    $lastError = error_get_last();
    if ($lastError && in_array($lastError['type'], $fatals)) {
        if (headers_sent()) {
            return;
        }
        header_remove();
        header("HTTP/1.0 500 Internal Server Error");
        if (DEBUG) {
            extract($lastError);
            $msg = sprintf("Fatal error: %s in %s on line %d", $message, $file, $line);
        } else {
            $msg = "Oops! Something went terribly wrong :(";
        }
        $msg = "<pre style=\"color:red;\">{$msg}</pre>";
        echo "<html><body><h1>500 Internal Server Error</h1><hr/>{$msg}</body></html>";
    }
});
$injector = (require __DIR__ . '/bindings.php');
$app = new Application($injector);
foreach ($routes = (require __DIR__ . '/routes/routes.php') as list($method, $uri, $action)) {
    $app->route($method, $uri, $action);
}
$app->run();
Example #3
0
    if (empty($session['test'])) {
        $session['test'] = 0;
    }
    $session['test'] += 1;
    return sprintf("<html><body><h1>\$session['test']: %d</h1></body></html>", $session['test']);
}
function php_info()
{
    $info = ob_start();
    phpinfo();
    return ob_get_clean();
}
function fatalFunction()
{
    $obj->nonexistent();
}
function exceptionFunction()
{
    throw new \Exception('test');
}
function testComplexResponseFunctionTarget(Request $request)
{
    $response = new Response();
    return $response->setStatus(234)->setReasonPhrase('Custom Reason')->setHeader('X-My-Header', '1')->addHeader('X-My-Header', '2')->setBody('zanzibar!');
}
function printVars($request)
{
    return "<pre>" . print_r($request->all(), TRUE) . "</pre>";
}
$app = new Application();
$app->route('GET', '/', 'helloFunction')->route('GET', '/lambda', $lambda)->route('GET', '/static-method', 'StaticExampleClass::staticMethod')->route('GET', '/ctor-deps', 'CtorDependencies::myInstanceMethod')->route('GET', '/complex-response', 'complexResponse')->route('GET', '/phpinfo', 'php_info')->route('GET', '/args/{arg1}', 'argFunction')->route('GET', '/args/{arg1:\\d+}/{arg2:\\d+}', 'numericArgsFunction')->route('GET', '/output', 'output')->route('GET', '/session', 'session')->route('GET', '/complex', 'testComplexResponseFunctionTarget')->route('GET', '/print-vars', 'printVars')->route('GET', '/fatal', 'fatalFunction')->route('GET', '/exception', 'exceptionFunction')->run();