Skip to content

Commit

Permalink
Merge pull request #399 from perftools/render-method
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Dec 23, 2020
2 parents dafc4e2 + 59a78f0 commit 2c6db92
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 115 deletions.
45 changes: 11 additions & 34 deletions src/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

namespace XHGui;

use Slim\Http\Response;
use Slim\Slim as App;
use Slim\Views\Twig;

abstract class AbstractController
{
/**
* @var array
*/
protected $_templateVars = [];

/**
* @var string|null
*/
protected $_template = null;

/**
* @var App
*/
Expand All @@ -26,31 +18,16 @@ public function __construct(App $app)
$this->app = $app;
}

public function set($vars)
protected function render(string $template, array $data = [])
{
$this->_templateVars = array_merge($this->_templateVars, $vars);
}

public function templateVars()
{
return $this->_templateVars;
}

public function render()
{
// We want to render the specified Twig template to the output buffer.
// The simplest way to do that is Slim::render, but that is not allowed
// in middleware, because it uses Slim\View::display which prints
// directly to the native PHP output buffer.
// Doing that is problematic, because the HTTP headers set via $app->response()
// must be output first, which won't happen until after the middleware
// is completed. Output of headers and body is done by the Slim::run entry point.

// The below is copied from Slim::render (slim/slim@2.6.3).
// Modified to use View::fetch + Response::write, instead of View::display.
$this->app->view->appendData($this->_templateVars);
$body = $this->app->view->fetch($this->_template);
$this->app->response->write($body);
/** @var Response $response */
$response = $this->app->response;
/** @var Twig $renderer */
$renderer = $this->app->view;

$renderer->appendData($data);
$body = $renderer->fetch($template);
$response->write($body);
}

protected function config(string $key)
Expand Down
5 changes: 2 additions & 3 deletions src/Controller/CustomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(App $app, SearcherInterface $searcher)

public function get()
{
$this->_template = 'custom/create.twig';
$this->render('custom/create.twig');
}

public function help(Request $request)
Expand All @@ -33,8 +33,7 @@ public function help(Request $request)
} else {
$res = $this->searcher->latest();
}
$this->_template = 'custom/help.twig';
$this->set([
$this->render('custom/help.twig', [
'data' => print_r($res->toArray(), 1),
]);
}
Expand Down
28 changes: 10 additions & 18 deletions src/Controller/RunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ public function index(Request $request, Response $response)
'direction' => $result['direction'],
];

$this->_template = 'runs/list.twig';
$this->set([
$this->render('runs/list.twig', [
'paging' => $paging,
'base_url' => 'home',
'runs' => $result['results'],
'date_format' => $this->config('date.format'),
'search' => $search,
'has_search' => strlen(implode('', $search)) > 0,
'has_search' => implode('', $search) !== '',
'title' => $title,
]);
}
Expand Down Expand Up @@ -120,8 +119,7 @@ public function view(Request $request, Response $response)
$profile = $result->sort('ewt', $result->getProfile());
}

$this->_template = 'runs/view.twig';
$this->set([
$this->render('runs/view.twig', [
'profile' => $profile,
'result' => $result,
'wall_time' => $timeChart,
Expand Down Expand Up @@ -157,8 +155,7 @@ public function deleteForm(Request $request)
// Get details
$result = $this->searcher->get($id);

$this->_template = 'runs/delete-form.twig';
$this->set([
$this->render('runs/delete-form.twig', [
'run_id' => $id,
'result' => $result,
]);
Expand All @@ -185,7 +182,7 @@ public function deleteSubmit(Request $request)

public function deleteAllForm()
{
$this->_template = 'runs/delete-all-form.twig';
$this->render('runs/delete-all-form.twig');
}

public function deleteAllSubmit()
Expand Down Expand Up @@ -239,8 +236,7 @@ public function url(Request $request)
'direction' => $runs['direction'],
];

$this->_template = 'runs/url.twig';
$this->set([
$this->render('runs/url.twig', [
'paging' => $paging,
'base_url' => 'url.view',
'runs' => $runs['results'],
Expand Down Expand Up @@ -288,8 +284,7 @@ public function compare(Request $request)
$comparison = $baseRun->compare($headRun);
}

$this->_template = 'runs/compare.twig';
$this->set([
$this->render('runs/compare.twig', [
'base_url' => 'run.compare',
'base_run' => $baseRun,
'head_run' => $headRun,
Expand All @@ -314,8 +309,7 @@ public function symbol(Request $request)
$profile->calculateSelf();
list($parents, $current, $children) = $profile->getRelatives($symbol);

$this->_template = 'runs/symbol.twig';
$this->set([
$this->render('runs/symbol.twig', [
'symbol' => $symbol,
'id' => $id,
'main' => $profile->get('main()'),
Expand All @@ -336,8 +330,7 @@ public function symbolShort(Request $request)
$profile->calculateSelf();
list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);

$this->_template = 'runs/symbol-short.twig';
$this->set([
$this->render('runs/symbol-short.twig', [
'symbol' => $symbol,
'id' => $id,
'main' => $profile->get('main()'),
Expand All @@ -351,8 +344,7 @@ public function callgraph(Request $request)
{
$profile = $this->searcher->get($request->get('id'));

$this->_template = 'runs/callgraph.twig';
$this->set([
$this->render('runs/callgraph.twig', [
'profile' => $profile,
'date_format' => $this->config('date.format'),
]);
Expand Down
3 changes: 1 addition & 2 deletions src/Controller/WatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function get()
{
$watched = $this->searcher->getAllWatches();

$this->_template = 'watch/list.twig';
$this->set(['watched' => $watched]);
$this->render('watch/list.twig', ['watched' => $watched]);
}

public function post(Request $request)
Expand Down
3 changes: 1 addition & 2 deletions src/Controller/WaterfallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public function index()
'direction' => $result['direction'],
];

$this->_template = 'waterfall/list.twig';
$this->set([
$this->render('waterfall/list.twig', [
'runs' => $result['results'],
'search' => $search,
'paging' => $paging,
Expand Down
21 changes: 0 additions & 21 deletions src/Middleware/RenderMiddleware.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Slim\Slim as App;
use Slim\Views\Twig;
use XHGui\Db\PdoRepository;
use XHGui\Middleware\RenderMiddleware;
use XHGui\Saver\NormalizingSaver;
use XHGui\Searcher\MongoSearcher;
use XHGui\Searcher\PdoSearcher;
Expand Down Expand Up @@ -99,9 +98,6 @@ protected function _slimApp()
'httponly' => true,
]));

// Add renderer.
$app->add(new RenderMiddleware());

$view = $c['view'];
$view->parserExtensions = [
new TwigExtension($app),
Expand Down
37 changes: 17 additions & 20 deletions src/ServiceProvider/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ private function registerRoutes(Container $di, App $app)
new TwigExtension($app),
];

// Remove the controller so we don't render it.
unset($app->controller);

$app->view($view);
$app->render('error/view.twig', [
'message' => $e->getMessage(),
Expand All @@ -40,7 +37,7 @@ private function registerRoutes(Container $di, App $app)
// Profile Runs routes
$app->get('/', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();
$response = $app->response();

Expand All @@ -49,7 +46,7 @@ private function registerRoutes(Container $di, App $app)

$app->get('/run/view', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();
$response = $app->response();

Expand All @@ -58,7 +55,7 @@ private function registerRoutes(Container $di, App $app)

$app->get('/run/delete', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();

$controller->deleteForm($request);
Expand All @@ -72,9 +69,9 @@ private function registerRoutes(Container $di, App $app)
$controller->deleteSubmit($request);
})->setName('run.delete.submit');

$app->get('/run/delete_all', static function () use ($di, $app) {
$app->get('/run/delete_all', static function () use ($di) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$controller->deleteAllForm();
})->setName('run.deleteAll.form');

Expand All @@ -86,39 +83,39 @@ private function registerRoutes(Container $di, App $app)

$app->get('/url/view', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();

$controller->url($request);
})->setName('url.view');

$app->get('/run/compare', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();

$controller->compare($request);
})->setName('run.compare');

$app->get('/run/symbol', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();

$controller->symbol($request);
})->setName('run.symbol');

$app->get('/run/symbol/short', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();

$controller->symbolShort($request);
})->setName('run.symbol-short');

$app->get('/run/callgraph', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller = $di['runController'];
$request = $app->request();

$controller->callgraph($request);
Expand Down Expand Up @@ -153,9 +150,9 @@ private function registerRoutes(Container $di, App $app)
})->setName('run.import');

// Watch function routes.
$app->get('/watch', static function () use ($di, $app) {
$app->get('/watch', static function () use ($di) {
/** @var Controller\WatchController $controller */
$controller = $app->controller = $di['watchController'];
$controller = $di['watchController'];
$controller->get();
})->setName('watch.list');

Expand All @@ -168,15 +165,15 @@ private function registerRoutes(Container $di, App $app)
})->setName('watch.save');

// Custom report routes.
$app->get('/custom', static function () use ($di, $app) {
$app->get('/custom', static function () use ($di) {
/** @var Controller\CustomController $controller */
$controller = $app->controller = $di['customController'];
$controller = $di['customController'];
$controller->get();
})->setName('custom.view');

$app->get('/custom/help', static function () use ($di, $app) {
/** @var Controller\CustomController $controller */
$controller = $app->controller = $di['customController'];
$controller = $di['customController'];
$request = $app->request();

$controller->help($request);
Expand All @@ -192,9 +189,9 @@ private function registerRoutes(Container $di, App $app)
})->setName('custom.query');

// Waterfall routes
$app->get('/waterfall', static function () use ($di, $app) {
$app->get('/waterfall', static function () use ($di) {
/** @var Controller\WaterfallController $controller */
$controller = $app->controller = $di['waterfallController'];
$controller = $di['waterfallController'];
$controller->index();
})->setName('waterfall.list');

Expand Down
Loading

0 comments on commit 2c6db92

Please sign in to comment.