static function run() { App::init(); App::route(); try { $obj = new ReflectionClass(App::$_controller . 'Controller'); $instance = $obj->newInstanceArgs(); $obj->getmethod(App::$_action . 'Action')->invoke($instance); } catch (Exception $e) { APP_DEBUG ? exit($e->getMessage()) : APP::log($e->getMessage()); } }
/** * Add routes to an App from this class by inferring details from PHPDoc @url and @method properties * @param App $app */ public function load(App $app) { $class = get_called_class(); $methods = get_class_methods($class); foreach ($methods as $class_method) { $method_props = []; $rm = new \ReflectionMethod($this, $class_method); $phpdoc = preg_split('/\\r\\n|\\n|\\r/', preg_replace('%^\\s*/?\\*+((?<=\\*)/|[ \\t]*)%m', '', $rm->getDocComment())); foreach ($phpdoc as $docline) { if (preg_match('#^@(url|method)\\s+(\\S+)$#', $docline, $matches)) { $method_props[$matches[1]] = $matches[2]; } } if (isset($method_props['url'])) { $route = $app->route($class . '::' . $class_method, $method_props['url'], $rm->getClosure($this)); if (isset($method_props['method'])) { $route->via($method_props['method']); } } } }
$app->route('GET *', function () use($app) { try { // Get the route. $app->initRoute(); $document = (new DocumentLocator($app->get('app.dir.pages')))->fromRoute($app->get('app.route.path')); // Work out if the document has expired and should not redirect. if ($document->hasExpired() && !$document->shouldRedirect()) { throw new Exceptions\DocumentExpiredException($app->get('app.route')); } // If the document should redirect, then redirect it. if ($document->shouldRedirect()) { $app->redirect($document->redirect); } // Render the document. $document->render(); // Render the document into the template, and display it. $output = $app->view()->render($document->template, ['page' => ['title' => $document->header['title'], 'content' => $document->output, 'header' => $document->header]]); $app->halt(200, $output); } catch (Exceptions\NoContentException $e) { if ($app->get('app.settings.debug')) { throw $e; } else { $app->notFound(); } } catch (\Exception $e) { if ($app->get('app.settings.debug')) { ldd($e); } else { $app->error($e); } } });
<?php namespace Microsite; include 'microsite.phar'; include 'postmark.php'; $app = new App(); Config::load(__DIR__ . '/config.php'); $app->template_dirs = [__DIR__ . '/views']; $app->route('all', '/', function () { echo 'Greetings from IF.'; }); $app->route('invite_get', '/invite', function (Response $response) { return $response->render('invite.php'); })->get(); $app->share('postmark', function () { return new \PostMark(Config::get('postmark_key'), Config::get('from_address')); }); $app->route('invite_post', '/invite', function (Response $response, App $app) { $email = $_POST['email']; $response['shipname'] = 'Nostromo'; $plain = $response->render('plain_invite.php'); $html = $response->render('html_invite.php'); $result = $app->postmark()->to($email)->subject('Congratulations, Captain!')->plain_message($plain)->html_message($html)->send(); echo '<p>The invitation was sent ' . ($result ? 'successfully' : 'unsuccessfully') . '.</p>'; })->post(); $app->route('inbound_email', '/inbound/mail', function (App $app) { $data = json_decode(file_get_contents('php://input')); $output = print_r($data, 1) . "\r\n----------\r\n"; file_put_contents(__DIR__ . '/inbound.log', $output, FILE_APPEND); $from = $data->From;