From 15f1aa2419d6e3e254568777a01b7c24a38e8136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 18:01:03 +0200 Subject: [PATCH 01/60] Drop unused $response for RunController::index --- src/Controller/RunController.php | 2 +- src/ServiceProvider/RouteProvider.php | 2 +- tests/Controller/RunTest.php | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index 1a7803a7..6ff44479 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -28,7 +28,7 @@ public function __construct(App $app, SearcherInterface $searcher) $this->searcher = $searcher; } - public function index(Request $request, Response $response): void + public function index(Request $request): void { $search = []; $keys = ['date_start', 'date_end', 'server_name', 'url']; diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index 42ca34c7..9340d8d4 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -48,7 +48,7 @@ private function registerRoutes(Container $di, App $app): void // https://github.com/perftools/xhgui/issues/261 $response->headers->set('Cache-Control', 'public, max-age=0'); - $controller->index($request, $response); + $controller->index($request); })->setName('home'); $app->get('/run/view', static function () use ($di, $app): void { diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 868dcc2c..6d273255 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -20,7 +20,7 @@ public function setUp(): void public function testIndexEmpty(): void { - $this->runs->index($this->app->request(), $this->app->response()); + $this->runs->index($this->app->request()); $result = $this->view->all(); $this->assertEquals('Recent runs', $result['title']); @@ -42,7 +42,7 @@ public function testIndexSortedWallTime(): void 'QUERY_STRING' => 'sort=wt', ]); - $this->runs->index($this->app->request(), $this->app->response()); + $this->runs->index($this->app->request()); $result = $this->view->all(); $this->assertEquals('Longest wall time', $result['title']); $this->assertEquals('wt', $result['paging']['sort']); @@ -56,7 +56,7 @@ public function testIndexSortedCpu(): void 'QUERY_STRING' => 'sort=cpu&direction=desc', ]); - $this->runs->index($this->app->request(), $this->app->response()); + $this->runs->index($this->app->request()); $result = $this->view->all(); $this->assertEquals('Most CPU time', $result['title']); $this->assertEquals('cpu', $result['paging']['sort']); @@ -71,7 +71,7 @@ public function testIndexWithSearch(): void 'QUERY_STRING' => 'sort=mu&direction=asc&url=index.php', ]); - $this->runs->index($this->app->request(), $this->app->response()); + $this->runs->index($this->app->request()); $result = $this->view->all(); $this->assertEquals('Highest memory use', $result['title']); $this->assertEquals('mu', $result['paging']['sort']); From fb40403bbbf07552a72f01d32243f4f075974b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 18:01:34 +0200 Subject: [PATCH 02/60] Drop unused $response for RunController::view --- src/Controller/RunController.php | 2 +- src/ServiceProvider/RouteProvider.php | 2 +- tests/Controller/RunTest.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index 6ff44479..1ab7d52b 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -75,7 +75,7 @@ public function index(Request $request): void ]); } - public function view(Request $request, Response $response): void + public function view(Request $request): void { $detailCount = $this->config('detail.count'); $result = $this->searcher->get($request->get('id')); diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index 9340d8d4..ed5b4486 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -66,7 +66,7 @@ private function registerRoutes(Container $di, App $app): void /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $controller->view($request, $response); + $controller->view($request); })->setName('run.view'); $app->get('/run/delete', static function () use ($di, $app): void { diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 6d273255..520efe36 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -232,7 +232,7 @@ public function testFilterCustomMethods(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*,strpos()', ]); - $this->runs->view($this->app->request(), $this->app->response()); + $this->runs->view($this->app->request()); $result = $this->view->all(); $this->assertCount(1, $result['profile']); @@ -249,7 +249,7 @@ public function testFilterCustomMethod(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*', ]); - $this->runs->view($this->app->request(), $this->app->response()); + $this->runs->view($this->app->request()); $result = $this->view->all(); $this->assertCount(2, $result['profile']); @@ -266,7 +266,7 @@ public function testFilterMethods(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=true', ]); - $this->runs->view($this->app->request(), $this->app->response()); + $this->runs->view($this->app->request()); $result = $this->view->all(); $this->assertCount(2, $result['profile']); From ada72285202a59eae6b4d91eabf89ac72381bcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 17:52:13 +0200 Subject: [PATCH 03/60] Update CustomController::query not to rely on request/response details --- src/Controller/CustomController.php | 21 +++++++-------------- src/ServiceProvider/RouteProvider.php | 8 +++++++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Controller/CustomController.php b/src/Controller/CustomController.php index d5540ada..4e11ab54 100644 --- a/src/Controller/CustomController.php +++ b/src/Controller/CustomController.php @@ -3,7 +3,6 @@ namespace XHGui\Controller; use Slim\Http\Request; -use Slim\Http\Response; use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Searcher\SearcherInterface; @@ -38,31 +37,25 @@ public function help(Request $request): void ]); } - public function query(Request $request, Response $response) + public function query($query, $retrieve) { - $response['Content-Type'] = 'application/json'; - - $query = json_decode($request->post('query'), true); $error = []; - if (null === $query) { + $conditions = json_decode($query, true); + if (null === $conditions) { $error['query'] = json_last_error(); } - $retrieve = json_decode($request->post('retrieve'), true); - if (null === $retrieve) { + $fields = json_decode($retrieve, true); + if (null === $fields) { $error['retrieve'] = json_last_error(); } if (count($error) > 0) { - $json = json_encode(['error' => $error]); - - return $response->body($json); + return ['error' => $error]; } $perPage = $this->config('page.limit'); - $res = $this->searcher->query($query, $perPage, $retrieve); - - return $response->body(json_encode($res)); + return $this->searcher->query($conditions, $perPage, $fields); } } diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index ed5b4486..50e6b3a1 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -201,7 +201,13 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); - $controller->query($request, $response); + $query = $request->post('query'); + $retrieve = $request->post('retrieve'); + + $result = $controller->query($query, $retrieve); + + $response->body(json_encode($result)); + $response['Content-Type'] = 'application/json'; })->setName('custom.query'); // Waterfall routes From 4450f78517eb7334fc9edc9d39247342c52525bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 17:54:38 +0200 Subject: [PATCH 04/60] Update ImportController::import not to rely on response details --- src/Controller/ImportController.php | 5 ++--- src/ServiceProvider/RouteProvider.php | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Controller/ImportController.php b/src/Controller/ImportController.php index c11c4d81..6c817c7b 100644 --- a/src/Controller/ImportController.php +++ b/src/Controller/ImportController.php @@ -27,7 +27,7 @@ public function __construct(App $app, SaverInterface $saver, $token) $this->token = $token; } - public function import(Request $request, Response $response): void + public function import(Request $request, Response $response) { try { $id = $this->runImport($request); @@ -40,8 +40,7 @@ public function import(Request $request, Response $response): void $response->setStatus(500); } - $response['Content-Type'] = 'application/json'; - $response->body(json_encode($result)); + return $result; } private function runImport(Request $request): string diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index 50e6b3a1..caf25f75 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -162,7 +162,10 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); - $controller->import($request, $response); + $result = $controller->import($request, $response); + + $response['Content-Type'] = 'application/json'; + $response->body(json_encode($result)); })->setName('run.import'); // Watch function routes. From a806e1159e9783f66fad7bb996fea5932c6121ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 17:56:07 +0200 Subject: [PATCH 05/60] Update MetricsController::metrics not to rely on response details --- src/Controller/MetricsController.php | 6 ++---- src/ServiceProvider/RouteProvider.php | 5 ++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Controller/MetricsController.php b/src/Controller/MetricsController.php index 2d15d2a1..eff2412b 100644 --- a/src/Controller/MetricsController.php +++ b/src/Controller/MetricsController.php @@ -2,7 +2,6 @@ namespace XHGui\Controller; -use Slim\Http\Response; use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Searcher\SearcherInterface; @@ -20,7 +19,7 @@ public function __construct(App $app, SearcherInterface $searcher) $this->searcher = $searcher; } - public function metrics(Response $response): void + public function metrics() { $stats = $this->searcher->stats(); @@ -36,7 +35,6 @@ public function metrics(Response $response): void $body .= "# TYPE xhgui_latest_profile_seconds gauge\n"; $body .= sprintf("xhgui_latest_profile_seconds %0.1F\n", $stats['latest']); - $response->body($body); - $response['Content-Type'] = 'text/plain; version=0.0.4'; + return $body; } } diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index caf25f75..88508a5f 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -235,7 +235,10 @@ private function registerRoutes(Container $di, App $app): void $controller = $di[Controller\MetricsController::class]; $response = $app->response(); - $controller->metrics($response); + $body = $controller->metrics(); + + $response->body($body); + $response['Content-Type'] = 'text/plain; version=0.0.4'; })->setName('metrics'); } From 8a8b9b0de8c54a2f86a32003a35781988b062a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 17:57:17 +0200 Subject: [PATCH 06/60] Update RunController::callgraphData not to rely on response details --- src/Controller/RunController.php | 7 ++----- src/ServiceProvider/RouteProvider.php | 5 ++++- tests/Controller/RunTest.php | 10 ++++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index 1ab7d52b..8795c88b 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -331,16 +331,13 @@ public function callgraph(Request $request): void ]); } - public function callgraphData(Request $request, Response $response) + public function callgraphData(Request $request) { $profile = $this->searcher->get($request->get('id')); $metric = $request->get('metric') ?: 'wt'; $threshold = (float)$request->get('threshold') ?: 0.01; - $callgraph = $profile->getCallgraph($metric, $threshold); - $response['Content-Type'] = 'application/json'; - - return $response->body(json_encode($callgraph)); + return $profile->getCallgraph($metric, $threshold); } public function callgraphDataDot(Request $request, Response $response) diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index 88508a5f..56455394 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -143,7 +143,10 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); - $controller->callgraphData($request, $response); + $callgraph = $controller->callgraphData($request, $response); + + $response['Content-Type'] = 'application/json'; + $response->body(json_encode($callgraph)); })->setName('run.callgraph.data'); $app->get('/run/callgraph/dot', static function () use ($di, $app): void { diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 520efe36..ad8a7931 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -156,11 +156,13 @@ public function testCallgraphData(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaaa', ]); - $this->runs->callgraphData($this->app->request(), $this->app->response()); - $response = $this->app->response(); + $result = $this->runs->callgraphData($this->app->request()); - $this->assertEquals('application/json', $response['Content-Type']); - $this->assertStringStartsWith('{"', $response->body()); + $this->assertInternalType('array', $result); + $this->assertArrayHasKey('metric', $result); + $this->assertArrayHasKey('total', $result); + $this->assertArrayHasKey('nodes', $result); + $this->assertArrayHasKey('links', $result); } public function testDeleteSubmit(): void From aa9d522e34513f4628dca3f62e166da8faed87b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 17:58:18 +0200 Subject: [PATCH 07/60] Update RunController::callgraphDataDot not to rely on response details --- src/Controller/RunController.php | 8 ++------ src/ServiceProvider/RouteProvider.php | 5 ++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index 8795c88b..edecbf86 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -4,7 +4,6 @@ use Exception; use Slim\Http\Request; -use Slim\Http\Response; use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Options\SearchOptions; @@ -340,15 +339,12 @@ public function callgraphData(Request $request) return $profile->getCallgraph($metric, $threshold); } - public function callgraphDataDot(Request $request, Response $response) + public function callgraphDataDot(Request $request) { $profile = $this->searcher->get($request->get('id')); $metric = $request->get('metric') ?: 'wt'; $threshold = (float)$request->get('threshold') ?: 0.01; - $callgraph = $profile->getCallgraphNodes($metric, $threshold); - $response['Content-Type'] = 'application/json'; - - return $response->body(json_encode($callgraph)); + return $profile->getCallgraphNodes($metric, $threshold); } } diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index 56455394..f0a03c4e 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -155,7 +155,10 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); - $controller->callgraphDataDot($request, $response); + $callgraph = $controller->callgraphDataDot($request); + + $response['Content-Type'] = 'application/json'; + $response->body(json_encode($callgraph)); })->setName('run.callgraph.dot'); // Import route From 996883bb48ebdb76105d4c76e0484404707a9813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 17:59:43 +0200 Subject: [PATCH 08/60] Update WaterfallController::query not to rely on response details --- src/Controller/WaterfallController.php | 11 +++++------ src/ServiceProvider/RouteProvider.php | 5 ++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Controller/WaterfallController.php b/src/Controller/WaterfallController.php index ac990330..9d809648 100644 --- a/src/Controller/WaterfallController.php +++ b/src/Controller/WaterfallController.php @@ -3,7 +3,6 @@ namespace XHGui\Controller; use Slim\Http\Request; -use Slim\Http\Response; use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Options\SearchOptions; @@ -55,7 +54,7 @@ public function index(): void ]); } - public function query(Request $request, Response $response): void + public function query(Request $request) { $search = []; $keys = ['remote_addr', 'request_start', 'request_end']; @@ -68,20 +67,20 @@ public function query(Request $request, Response $response): void 'conditions' => $search, 'projection' => true, ])); - $datas = []; + $data = []; /** @var Profile $r */ foreach ($result['results'] as $r) { $duration = $r->get('main()', 'wt'); $start = $r->getMeta('SERVER.REQUEST_TIME_FLOAT'); $title = $r->getMeta('url'); - $datas[] = [ + $data[] = [ 'id' => $r->getId(), 'title' => $title, 'start' => $start * 1000, 'duration' => $duration / 1000, // Convert to correct scale ]; } - $response->body(json_encode($datas)); - $response['Content-Type'] = 'application/json'; + + return $data; } } diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index f0a03c4e..bcbf5f68 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -232,7 +232,10 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); - $controller->query($request, $response); + $data = $controller->query($request); + + $response->body(json_encode($data)); + $response['Content-Type'] = 'application/json'; })->setName('waterfall.data'); // Metrics From eb8cd45c2652a9e9606ba894028dc99f31fafe7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 18:03:29 +0200 Subject: [PATCH 09/60] Update ImportController::import not to rely on response details --- src/Controller/ImportController.php | 10 +++++----- src/ServiceProvider/RouteProvider.php | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Controller/ImportController.php b/src/Controller/ImportController.php index 6c817c7b..000af53a 100644 --- a/src/Controller/ImportController.php +++ b/src/Controller/ImportController.php @@ -5,7 +5,6 @@ use Exception; use InvalidArgumentException; use Slim\Http\Request; -use Slim\Http\Response; use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Saver\SaverInterface; @@ -27,20 +26,21 @@ public function __construct(App $app, SaverInterface $saver, $token) $this->token = $token; } - public function import(Request $request, Response $response) + public function import(Request $request) { + $status = 200; try { $id = $this->runImport($request); $result = ['ok' => true, 'id' => $id, 'size' => $request->getContentLength()]; } catch (InvalidArgumentException $e) { $result = ['error' => true, 'message' => $e->getMessage()]; - $response->setStatus(401); + $status = 401; } catch (Exception $e) { $result = ['error' => true, 'message' => $e->getMessage()]; - $response->setStatus(500); + $status = 500; } - return $result; + return [$status, $result]; } private function runImport(Request $request): string diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index bcbf5f68..b14579c1 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -168,9 +168,10 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); - $result = $controller->import($request, $response); + [$status, $result] = $controller->import($request); $response['Content-Type'] = 'application/json'; + $response->setStatus($status); $response->body(json_encode($result)); })->setName('run.import'); From 51724b6f9ce3e2b1bbd96296b23aecae59a4fece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 18:09:59 +0200 Subject: [PATCH 10/60] Drop unused $response for RunController::url --- tests/Controller/RunTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index ad8a7931..067c38b0 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -90,7 +90,7 @@ public function testUrl(): void 'QUERY_STRING' => 'url=%2Ftasks', ]); - $this->runs->url($this->app->request(), $this->app->response()); + $this->runs->url($this->app->request()); $result = $this->view->all(); $this->assertEquals('url.view', $result['base_url']); From 6146c2a53f74f0192777d8d82c778fa35521cb18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 21:01:57 +0200 Subject: [PATCH 11/60] Tests: Add request/response lazy properties --- tests/LazyContainerProperties.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index 29a1f415..3140289d 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -4,6 +4,8 @@ use LazyProperty\LazyPropertiesTrait; use MongoDB; +use Slim\Http\Request; +use Slim\Http\Response; use Slim\Slim as App; use Slim\View; use XHGui\Application; @@ -25,6 +27,10 @@ trait LazyContainerProperties protected $mongo; /** @var MongoDB */ protected $mongodb; + /** @var Request */ + protected $request; + /** @var Response */ + protected $response; /** @var Controller\RunController */ protected $runs; /** @var App */ @@ -49,6 +55,8 @@ protected function setupProperties(): void 'import', 'mongo', 'mongodb', + 'request', + 'response', 'runs', 'saver', 'searcher', @@ -109,6 +117,16 @@ protected function getMongoDb() return $this->di[MongoDB::class]; } + protected function getRequest(): Request + { + return $this->di['app']->request(); + } + + protected function getResponse(): Response + { + return $this->di['app']->response(); + } + protected function getSearcher() { return $this->di['searcher']; From a48b1f2a9c70e25021f0b2a1dd19c5e602066779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 21:02:35 +0200 Subject: [PATCH 12/60] Use request/response properties for test --- tests/Controller/ImportTest.php | 2 +- tests/Controller/RunTest.php | 22 +++++++++++----------- tests/Controller/WatchTest.php | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/Controller/ImportTest.php b/tests/Controller/ImportTest.php index 9de83ef8..c073c9a7 100644 --- a/tests/Controller/ImportTest.php +++ b/tests/Controller/ImportTest.php @@ -50,7 +50,7 @@ public function testImportSuccess(): void $before = $searcher->getForUrl('/things', []); $this->assertEmpty($before['results']); - $this->import->import($this->app->request(), $this->app->response()); + $this->import->import($this->request); $after = $searcher->getForUrl('/things', []); $this->assertNotEmpty($after['results']); diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 067c38b0..2aa06448 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -20,7 +20,7 @@ public function setUp(): void public function testIndexEmpty(): void { - $this->runs->index($this->app->request()); + $this->runs->index($this->request); $result = $this->view->all(); $this->assertEquals('Recent runs', $result['title']); @@ -42,7 +42,7 @@ public function testIndexSortedWallTime(): void 'QUERY_STRING' => 'sort=wt', ]); - $this->runs->index($this->app->request()); + $this->runs->index($this->request); $result = $this->view->all(); $this->assertEquals('Longest wall time', $result['title']); $this->assertEquals('wt', $result['paging']['sort']); @@ -56,7 +56,7 @@ public function testIndexSortedCpu(): void 'QUERY_STRING' => 'sort=cpu&direction=desc', ]); - $this->runs->index($this->app->request()); + $this->runs->index($this->request); $result = $this->view->all(); $this->assertEquals('Most CPU time', $result['title']); $this->assertEquals('cpu', $result['paging']['sort']); @@ -71,7 +71,7 @@ public function testIndexWithSearch(): void 'QUERY_STRING' => 'sort=mu&direction=asc&url=index.php', ]); - $this->runs->index($this->app->request()); + $this->runs->index($this->request); $result = $this->view->all(); $this->assertEquals('Highest memory use', $result['title']); $this->assertEquals('mu', $result['paging']['sort']); @@ -90,7 +90,7 @@ public function testUrl(): void 'QUERY_STRING' => 'url=%2Ftasks', ]); - $this->runs->url($this->app->request()); + $this->runs->url($this->request); $result = $this->view->all(); $this->assertEquals('url.view', $result['base_url']); @@ -139,7 +139,7 @@ public function testCallgraph(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaaa', ]); - $this->runs->callgraph($this->app->request()); + $this->runs->callgraph($this->request); $result = $this->view->all(); $this->assertArrayHasKey('profile', $result); $this->assertArrayHasKey('date_format', $result); @@ -156,7 +156,7 @@ public function testCallgraphData(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaaa', ]); - $result = $this->runs->callgraphData($this->app->request()); + $result = $this->runs->callgraphData($this->request); $this->assertInternalType('array', $result); $this->assertArrayHasKey('metric', $result); @@ -190,7 +190,7 @@ public function testDeleteSubmit(): void $result = $searcher->getAll(new SearchOptions()); $count = count($result['results']); - $this->runs->deleteSubmit($this->app->request()); + $this->runs->deleteSubmit($this->request); $result = $searcher->getAll(new SearchOptions()); $this->assertCount($count - 1, $result['results']); @@ -234,7 +234,7 @@ public function testFilterCustomMethods(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*,strpos()', ]); - $this->runs->view($this->app->request()); + $this->runs->view($this->request); $result = $this->view->all(); $this->assertCount(1, $result['profile']); @@ -251,7 +251,7 @@ public function testFilterCustomMethod(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*', ]); - $this->runs->view($this->app->request()); + $this->runs->view($this->request); $result = $this->view->all(); $this->assertCount(2, $result['profile']); @@ -268,7 +268,7 @@ public function testFilterMethods(): void 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=true', ]); - $this->runs->view($this->app->request()); + $this->runs->view($this->request); $result = $this->view->all(); $this->assertCount(2, $result['profile']); diff --git a/tests/Controller/WatchTest.php b/tests/Controller/WatchTest.php index d5de4c05..f1587507 100644 --- a/tests/Controller/WatchTest.php +++ b/tests/Controller/WatchTest.php @@ -42,7 +42,7 @@ public function testPostAdd(): void $this->app->expects($this->once()) ->method('redirect'); - $this->watches->post($this->app->request()); + $this->watches->post($this->request); $result = $this->searcher->getAllWatches(); $this->assertCount(2, $result); @@ -61,7 +61,7 @@ public function testPostModify(): void ['name' => 'strpos', '_id' => $saved[0]['_id']], ], ]; - $this->watches->post($this->app->request()); + $this->watches->post($this->request); $result = $searcher->getAllWatches(); $this->assertCount(1, $result); @@ -79,7 +79,7 @@ public function testPostDelete(): void ['removed' => 1, 'name' => 'strpos', '_id' => $saved[0]['_id']], ], ]; - $this->watches->post($this->app->request()); + $this->watches->post($this->request); $result = $this->searcher->getAllWatches(); $this->assertCount(0, $result); From 8fb634f5d1614335ab7a43cd42ccd1e916fc9bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 19:45:09 +0200 Subject: [PATCH 13/60] Take request from method argument --- src/Controller/RunController.php | 5 ++--- src/Controller/WaterfallController.php | 3 +-- src/ServiceProvider/RouteProvider.php | 6 ++++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index edecbf86..e570c702 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -97,7 +97,7 @@ public function view(Request $request): void } if (false !== $request->get(self::FILTER_ARGUMENT_NAME, false)) { - $profile = $result->sort('ewt', $result->filter($result->getProfile(), $this->getFilters())); + $profile = $result->sort('ewt', $result->filter($result->getProfile(), $this->getFilters($request))); } else { $profile = $result->sort('ewt', $result->getProfile()); } @@ -114,9 +114,8 @@ public function view(Request $request): void /** * @return array */ - protected function getFilters() + private function getFilters($request) { - $request = $this->app->request(); $filterString = $request->get(self::FILTER_ARGUMENT_NAME); if (strlen($filterString) > 1 && $filterString !== 'true') { $filters = array_map('trim', explode(',', $filterString)); diff --git a/src/Controller/WaterfallController.php b/src/Controller/WaterfallController.php index 9d809648..4d578b02 100644 --- a/src/Controller/WaterfallController.php +++ b/src/Controller/WaterfallController.php @@ -22,9 +22,8 @@ public function __construct(App $app, SearcherInterface $searcher) $this->searcher = $searcher; } - public function index(): void + public function index($request): void { - $request = $this->app->request(); $search = []; $keys = ['remote_addr', 'request_start', 'request_end']; foreach ($keys as $key) { diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index b14579c1..9a671d68 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -221,10 +221,12 @@ private function registerRoutes(Container $di, App $app): void })->setName('custom.query'); // Waterfall routes - $app->get('/waterfall', static function () use ($di): void { + $app->get('/waterfall', static function () use ($di, $app): void { /** @var Controller\WaterfallController $controller */ $controller = $di[Controller\WaterfallController::class]; - $controller->index(); + $request = $app->request(); + + $controller->index($request); })->setName('waterfall.list'); $app->get('/waterfall/data', static function () use ($di, $app): void { From 749493484e39a281c18865150a85e2530cba1d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 21:03:25 +0200 Subject: [PATCH 14/60] Add flashSuccess to AbstractController --- src/AbstractController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/AbstractController.php b/src/AbstractController.php index 3ff0c12a..ef9cb557 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -30,6 +30,11 @@ protected function render(string $template, array $data = []): void $response->write($body); } + protected function flashSuccess(string $message): void + { + $this->app->flash('success', $message); + } + protected function config(string $key) { return $this->app->config($key); From fb4a32a11a8894fd82184a1e0ba85cd84c669046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 21:04:12 +0200 Subject: [PATCH 15/60] Use flashSuccess helper --- src/Controller/RunController.php | 4 ++-- src/Controller/WatchController.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index e570c702..4e86bb81 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -156,7 +156,7 @@ public function deleteSubmit(Request $request): void // Delete the profile run. $this->searcher->delete($id); - $this->app->flash('success', 'Deleted profile ' . $id); + $this->flashSuccess('Deleted profile ' . $id); $this->app->redirect($this->app->urlFor('home')); } @@ -171,7 +171,7 @@ public function deleteAllSubmit(): void // Delete all profile runs. $this->searcher->truncate(); - $this->app->flash('success', 'Deleted all profiles'); + $this->flashSuccess('Deleted all profiles'); $this->app->redirect($this->app->urlFor('home')); } diff --git a/src/Controller/WatchController.php b/src/Controller/WatchController.php index 25dfc27f..cb9dd8b7 100644 --- a/src/Controller/WatchController.php +++ b/src/Controller/WatchController.php @@ -35,7 +35,7 @@ public function post(Request $request): void $this->searcher->saveWatch($data); } if ($saved) { - $this->app->flash('success', 'Watch functions updated.'); + $this->flashSuccess('Watch functions updated.'); } $this->app->redirect($this->app->urlFor('watch.list')); } From 4777c3f91f0acc5b86922402059c93151d55a798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 22:02:51 +0200 Subject: [PATCH 16/60] Add redirect helper to AbstractController --- src/AbstractController.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/AbstractController.php b/src/AbstractController.php index ef9cb557..3b9aafe6 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -30,6 +30,17 @@ protected function render(string $template, array $data = []): void $response->write($body); } + /** + * Redirect to the URL of a named route + * + * @param string $name The route name + * @param array $params Associative array of URL parameters and replacement values + */ + protected function redirectTo(string $name, array $params = []): void + { + $this->app->redirectTo($name, $params); + } + protected function flashSuccess(string $message): void { $this->app->flash('success', $message); From 59b01f8894b5ea5eda12099090bb541fe71cf735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 22:05:25 +0200 Subject: [PATCH 17/60] Use redirectTo() helper --- src/Controller/RunController.php | 6 ++---- src/Controller/WatchController.php | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index 4e86bb81..5b889efe 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -157,8 +157,7 @@ public function deleteSubmit(Request $request): void $this->searcher->delete($id); $this->flashSuccess('Deleted profile ' . $id); - - $this->app->redirect($this->app->urlFor('home')); + $this->redirectTo('home'); } public function deleteAllForm(): void @@ -172,8 +171,7 @@ public function deleteAllSubmit(): void $this->searcher->truncate(); $this->flashSuccess('Deleted all profiles'); - - $this->app->redirect($this->app->urlFor('home')); + $this->redirectTo('home'); } public function url(Request $request): void diff --git a/src/Controller/WatchController.php b/src/Controller/WatchController.php index cb9dd8b7..d29a5df3 100644 --- a/src/Controller/WatchController.php +++ b/src/Controller/WatchController.php @@ -37,6 +37,7 @@ public function post(Request $request): void if ($saved) { $this->flashSuccess('Watch functions updated.'); } - $this->app->redirect($this->app->urlFor('watch.list')); + + $this->redirectTo('watch.list'); } } From cababa97cb9b061290e1997d4b8588690d287e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 23:44:27 +0200 Subject: [PATCH 18/60] Cleanup no longer called mocks --- tests/Controller/RunTest.php | 14 -------------- tests/Controller/WatchTest.php | 6 ------ 2 files changed, 20 deletions(-) diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 2aa06448..2a6a9587 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -180,13 +180,6 @@ public function testDeleteSubmit(): void ], ]); - $this->app->expects($this->once()) - ->method('urlFor') - ->with('home'); - - $this->app->expects($this->once()) - ->method('redirect'); - $result = $searcher->getAll(new SearchOptions()); $count = count($result['results']); @@ -207,13 +200,6 @@ public function testDeleteAllSubmit(): void 'PATH_INFO' => '/run/delete_all', ]); - $this->app->expects($this->once()) - ->method('urlFor') - ->with('home'); - - $this->app->expects($this->once()) - ->method('redirect'); - $result = $this->searcher->getAll(new SearchOptions()); $this->assertGreaterThan(0, count($result['results'])); diff --git a/tests/Controller/WatchTest.php b/tests/Controller/WatchTest.php index f1587507..7586fcaa 100644 --- a/tests/Controller/WatchTest.php +++ b/tests/Controller/WatchTest.php @@ -35,12 +35,6 @@ public function testPostAdd(): void ['name' => 'strpos'], ], ]; - $this->app->expects($this->once()) - ->method('urlFor') - ->with('watch.list'); - - $this->app->expects($this->once()) - ->method('redirect'); $this->watches->post($this->request); $result = $this->searcher->getAllWatches(); From 8345fdd9b6ffd5ad3a0821f291a10ff530cfeddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 27 Aug 2020 10:04:18 +0300 Subject: [PATCH 19/60] Update Slim (2.6.3 => 3.12.3) --- composer.json | 2 +- composer.lock | 158 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 141 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index f64ecde0..95ab64fd 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "ext-json": "*", "alcaeus/mongo-php-adapter": "^1.1", "pimple/pimple": "^3.0", - "slim/slim": "^2.6.3", + "slim/slim": "^3.12", "slim/views": "^0.1.0", "symfony/options-resolver": "^3.3", "twig/twig": "^1.26" diff --git a/composer.lock b/composer.lock index f1c7b6f8..3313759d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e730d73a305484062563824871b88d51", + "content-hash": "50c9a1adf75c57f72870c27574ef4e32", "packages": [ { "name": "alcaeus/mongo-php-adapter", @@ -130,6 +130,52 @@ ], "time": "2017-10-27T19:42:57+00:00" }, + { + "name": "nikic/fast-route", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/nikic/FastRoute.git", + "reference": "181d480e08d9476e61381e04a71b34dc0432e812" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nikic/FastRoute/zipball/181d480e08d9476e61381e04a71b34dc0432e812", + "reference": "181d480e08d9476e61381e04a71b34dc0432e812", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35|~5.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "FastRoute\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov", + "email": "nikic@php.net" + } + ], + "description": "Fast request router for PHP", + "keywords": [ + "router", + "routing" + ], + "time": "2018-02-13T20:26:39+00:00" + }, { "name": "pimple/pimple", "version": "v3.2.3", @@ -229,31 +275,91 @@ ], "time": "2017-02-14T16:28:37+00:00" }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, { "name": "slim/slim", - "version": "2.6.3", + "version": "3.12.3", "source": { "type": "git", "url": "https://github.com/slimphp/Slim.git", - "reference": "9224ed81ac1c412881e8d762755e3d76ebf580c0" + "reference": "1c9318a84ffb890900901136d620b4f03a59da38" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/slimphp/Slim/zipball/9224ed81ac1c412881e8d762755e3d76ebf580c0", - "reference": "9224ed81ac1c412881e8d762755e3d76ebf580c0", + "url": "https://api.github.com/repos/slimphp/Slim/zipball/1c9318a84ffb890900901136d620b4f03a59da38", + "reference": "1c9318a84ffb890900901136d620b4f03a59da38", "shasum": "" }, "require": { - "php": ">=5.3.0" + "ext-json": "*", + "ext-libxml": "*", + "ext-simplexml": "*", + "nikic/fast-route": "^1.0", + "php": ">=5.5.0", + "pimple/pimple": "^3.0", + "psr/container": "^1.0", + "psr/http-message": "^1.0" }, - "suggest": { - "ext-mcrypt": "Required for HTTP cookie encryption", - "phpseclib/mcrypt_compat": "Polyfil for mcrypt extension" + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0", + "squizlabs/php_codesniffer": "^2.5" }, "type": "library", "autoload": { - "psr-0": { - "Slim": "." + "psr-4": { + "Slim\\": "Slim" } }, "notification-url": "https://packagist.org/downloads/", @@ -263,18 +369,34 @@ "authors": [ { "name": "Josh Lockhart", - "email": "info@joshlockhart.com", - "homepage": "http://www.joshlockhart.com/" + "email": "hello@joshlockhart.com", + "homepage": "https://joshlockhart.com" + }, + { + "name": "Andrew Smith", + "email": "a.smith@silentworks.co.uk", + "homepage": "http://silentworks.co.uk" + }, + { + "name": "Rob Allen", + "email": "rob@akrabat.com", + "homepage": "http://akrabat.com" + }, + { + "name": "Gabriel Manricks", + "email": "gmanricks@me.com", + "homepage": "http://gabrielmanricks.com" } ], - "description": "Slim Framework, a PHP micro framework", - "homepage": "http://github.com/codeguy/Slim", + "description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs", + "homepage": "https://slimframework.com", "keywords": [ - "microframework", - "rest", + "api", + "framework", + "micro", "router" ], - "time": "2017-01-07T12:21:41+00:00" + "time": "2019-11-28T17:40:33+00:00" }, { "name": "slim/views", From 7c9ca51706b4236dff1fe35f7c8d14465727e493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 21 Dec 2020 20:23:29 +0200 Subject: [PATCH 20/60] Add slim/twig-view (2.5.1) dependency --- composer.json | 1 + composer.lock | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 95ab64fd..7eb64417 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "alcaeus/mongo-php-adapter": "^1.1", "pimple/pimple": "^3.0", "slim/slim": "^3.12", + "slim/twig-view": "^2.5", "slim/views": "^0.1.0", "symfony/options-resolver": "^3.3", "twig/twig": "^1.26" diff --git a/composer.lock b/composer.lock index 3313759d..6acf3205 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "50c9a1adf75c57f72870c27574ef4e32", + "content-hash": "a38cd6521654dec69f72e7c4ca101a00", "packages": [ { "name": "alcaeus/mongo-php-adapter", @@ -398,6 +398,57 @@ ], "time": "2019-11-28T17:40:33+00:00" }, + { + "name": "slim/twig-view", + "version": "2.5.1", + "source": { + "type": "git", + "url": "https://github.com/slimphp/Twig-View.git", + "reference": "47bd5cc1cbbdf5196d0873ece0ee97c6c7b352e9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slimphp/Twig-View/zipball/47bd5cc1cbbdf5196d0873ece0ee97c6c7b352e9", + "reference": "47bd5cc1cbbdf5196d0873ece0ee97c6c7b352e9", + "shasum": "" + }, + "require": { + "php": ">=5.5.0", + "psr/http-message": "^1.0", + "twig/twig": "^1.38|^2.7|^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8|^5.7", + "slim/slim": "^3.10" + }, + "type": "library", + "autoload": { + "psr-4": { + "Slim\\Views\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Josh Lockhart", + "email": "hello@joshlockhart.com", + "homepage": "http://joshlockhart.com" + } + ], + "description": "Slim Framework 3 view helper built on top of the Twig 2 templating component", + "homepage": "http://slimframework.com", + "keywords": [ + "framework", + "slim", + "template", + "twig", + "view" + ], + "time": "2019-11-28T18:03:50+00:00" + }, { "name": "slim/views", "version": "0.1.3", From b101762d08feae69fa00137b2f70d1c3863ca320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 20:53:41 +0200 Subject: [PATCH 21/60] Add slim/flash (0.4.0) dependency --- composer.json | 1 + composer.lock | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7eb64417..eb1b2b44 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "ext-json": "*", "alcaeus/mongo-php-adapter": "^1.1", "pimple/pimple": "^3.0", + "slim/flash": "^0.4.0", "slim/slim": "^3.12", "slim/twig-view": "^2.5", "slim/views": "^0.1.0", diff --git a/composer.lock b/composer.lock index 6acf3205..3baaa678 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a38cd6521654dec69f72e7c4ca101a00", + "content-hash": "33e281923c96ce554da51d0303951080", "packages": [ { "name": "alcaeus/mongo-php-adapter", @@ -325,6 +325,54 @@ ], "time": "2016-08-06T14:39:51+00:00" }, + { + "name": "slim/flash", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/slimphp/Slim-Flash.git", + "reference": "9aaff5fded3b54f4e519ec3d4ac74d3d1f2cbbbc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slimphp/Slim-Flash/zipball/9aaff5fded3b54f4e519ec3d4ac74d3d1f2cbbbc", + "reference": "9aaff5fded3b54f4e519ec3d4ac74d3d1f2cbbbc", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Slim\\Flash\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Josh Lockhart", + "email": "hello@joshlockhart.com", + "homepage": "http://joshlockhart.com" + } + ], + "description": "Slim Framework Flash message service provider", + "homepage": "http://slimframework.com", + "keywords": [ + "flash", + "framework", + "message", + "provider", + "slim" + ], + "time": "2017-10-22T10:35:05+00:00" + }, { "name": "slim/slim", "version": "3.12.3", From 5cd6d55ea24ccf0b0e23f93b28f1cdd890585e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 27 Aug 2020 10:13:22 +0300 Subject: [PATCH 22/60] Replace Slim\Slim -> Slim\App --- src/Application.php | 2 +- src/Controller/CustomController.php | 2 +- src/Controller/ImportController.php | 2 +- src/Controller/MetricsController.php | 2 +- src/Controller/RunController.php | 2 +- src/Controller/WatchController.php | 2 +- src/Controller/WaterfallController.php | 2 +- src/ServiceProvider/RouteProvider.php | 2 +- src/ServiceProvider/SlimProvider.php | 2 +- src/Twig/TwigExtension.php | 2 +- tests/LazyContainerProperties.php | 2 +- tests/Twig/ExtensionTest.php | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Application.php b/src/Application.php index b24229aa..d8355620 100644 --- a/src/Application.php +++ b/src/Application.php @@ -3,7 +3,7 @@ namespace XHGui; use Pimple\Container; -use Slim\Slim as App; +use Slim\App; use XHGui\Saver\SaverInterface; class Application extends Container diff --git a/src/Controller/CustomController.php b/src/Controller/CustomController.php index 4e11ab54..b639b59a 100644 --- a/src/Controller/CustomController.php +++ b/src/Controller/CustomController.php @@ -2,8 +2,8 @@ namespace XHGui\Controller; +use Slim\App; use Slim\Http\Request; -use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Searcher\SearcherInterface; diff --git a/src/Controller/ImportController.php b/src/Controller/ImportController.php index 000af53a..b02f1b6f 100644 --- a/src/Controller/ImportController.php +++ b/src/Controller/ImportController.php @@ -4,8 +4,8 @@ use Exception; use InvalidArgumentException; +use Slim\App; use Slim\Http\Request; -use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Saver\SaverInterface; diff --git a/src/Controller/MetricsController.php b/src/Controller/MetricsController.php index eff2412b..81d37609 100644 --- a/src/Controller/MetricsController.php +++ b/src/Controller/MetricsController.php @@ -2,7 +2,7 @@ namespace XHGui\Controller; -use Slim\Slim as App; +use Slim\App; use XHGui\AbstractController; use XHGui\Searcher\SearcherInterface; diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index 5b889efe..e40fe085 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -3,8 +3,8 @@ namespace XHGui\Controller; use Exception; +use Slim\App; use Slim\Http\Request; -use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Options\SearchOptions; use XHGui\Searcher\SearcherInterface; diff --git a/src/Controller/WatchController.php b/src/Controller/WatchController.php index d29a5df3..b9a64748 100644 --- a/src/Controller/WatchController.php +++ b/src/Controller/WatchController.php @@ -2,8 +2,8 @@ namespace XHGui\Controller; +use Slim\App; use Slim\Http\Request; -use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Searcher\SearcherInterface; diff --git a/src/Controller/WaterfallController.php b/src/Controller/WaterfallController.php index 4d578b02..6b894da0 100644 --- a/src/Controller/WaterfallController.php +++ b/src/Controller/WaterfallController.php @@ -2,8 +2,8 @@ namespace XHGui\Controller; +use Slim\App; use Slim\Http\Request; -use Slim\Slim as App; use XHGui\AbstractController; use XHGui\Options\SearchOptions; use XHGui\Profile; diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index 9a671d68..db1c2c11 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -5,7 +5,7 @@ use Exception; use Pimple\Container; use Pimple\ServiceProviderInterface; -use Slim\Slim as App; +use Slim\App; use Slim\Views\Twig; use XHGui\Controller; use XHGui\Twig\TwigExtension; diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index 82fcef96..a44ac308 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -4,8 +4,8 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; +use Slim\App; use Slim\Middleware\SessionCookie; -use Slim\Slim as App; use Slim\Views\Twig; use XHGui\Twig\TwigExtension; diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index bf6a630b..897b7c82 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -2,8 +2,8 @@ namespace XHGui\Twig; +use Slim\App; use Slim\Router; -use Slim\Slim as App; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index 3140289d..95f0e292 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -4,9 +4,9 @@ use LazyProperty\LazyPropertiesTrait; use MongoDB; +use Slim\App; use Slim\Http\Request; use Slim\Http\Response; -use Slim\Slim as App; use Slim\View; use XHGui\Application; use XHGui\Controller; diff --git a/tests/Twig/ExtensionTest.php b/tests/Twig/ExtensionTest.php index d9d0e97f..5978f201 100755 --- a/tests/Twig/ExtensionTest.php +++ b/tests/Twig/ExtensionTest.php @@ -2,8 +2,8 @@ namespace XHGui\Test\Twig; +use Slim\App; use Slim\Environment; -use Slim\Slim as App; use XHGui\Test\TestCase; use XHGui\Twig\TwigExtension; From 9605dc159f0e01b50a09c162c60100095f69fea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 9 Oct 2020 08:25:56 +0300 Subject: [PATCH 23/60] Replace Slim\Environment -> Slim\Http\Environment --- tests/Controller/ImportTest.php | 2 +- tests/Controller/RunTest.php | 2 +- tests/Controller/WatchTest.php | 2 +- tests/Twig/ExtensionTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Controller/ImportTest.php b/tests/Controller/ImportTest.php index c073c9a7..92cc848c 100644 --- a/tests/Controller/ImportTest.php +++ b/tests/Controller/ImportTest.php @@ -2,7 +2,7 @@ namespace XHGui\Test\Controller; -use Slim\Environment; +use Slim\Http\Environment; use XHGui\Profile; use XHGui\Test\TestCase; diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 2a6a9587..32371dfe 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -2,7 +2,7 @@ namespace XHGui\Test\Controller; -use Slim\Environment; +use Slim\Http\Environment; use XHGui\Options\SearchOptions; use XHGui\Test\TestCase; diff --git a/tests/Controller/WatchTest.php b/tests/Controller/WatchTest.php index 7586fcaa..fbb75dd1 100644 --- a/tests/Controller/WatchTest.php +++ b/tests/Controller/WatchTest.php @@ -2,7 +2,7 @@ namespace XHGui\Test\Controller; -use Slim\Environment; +use Slim\Http\Environment; use XHGui\Test\TestCase; class WatchTest extends TestCase diff --git a/tests/Twig/ExtensionTest.php b/tests/Twig/ExtensionTest.php index 5978f201..f06591d2 100755 --- a/tests/Twig/ExtensionTest.php +++ b/tests/Twig/ExtensionTest.php @@ -3,7 +3,7 @@ namespace XHGui\Test\Twig; use Slim\App; -use Slim\Environment; +use Slim\Http\Environment; use XHGui\Test\TestCase; use XHGui\Twig\TwigExtension; From 04e6accff4b9c00c2bb95d96e72d5162f8d1605e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Dec 2020 23:12:43 +0200 Subject: [PATCH 24/60] AbstractController: Update config() to Slim v3 --- src/AbstractController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AbstractController.php b/src/AbstractController.php index 3b9aafe6..99db6025 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -48,6 +48,6 @@ protected function flashSuccess(string $message): void protected function config(string $key) { - return $this->app->config($key); + return $this->app->getContainer()->get($key); } } From 8f416ea7450cf1bc7c7fcda59ffcd153212069a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Dec 2020 23:41:42 +0200 Subject: [PATCH 25/60] AbstractController: Update render() method for Slim v3 --- src/AbstractController.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/AbstractController.php b/src/AbstractController.php index 99db6025..5e8fce54 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -2,8 +2,8 @@ namespace XHGui; -use Slim\Http\Response; -use Slim\Slim as App; +use Psr\Http\Message\ResponseInterface; +use Slim\App; use Slim\Views\Twig; abstract class AbstractController @@ -20,14 +20,13 @@ public function __construct(App $app) protected function render(string $template, array $data = []): void { - /** @var Response $response */ - $response = $this->app->response; + $container = $this->app->getContainer(); + /** @var ResponseInterface $response */ + $response = $container->get('response'); /** @var Twig $renderer */ - $renderer = $this->app->view; + $renderer = $container->get('view'); - $renderer->appendData($data); - $body = $renderer->fetch($template); - $response->write($body); + $renderer->render($response, $template, $data); } /** From 14d2ffd6eded680fc16719e04101689123e86f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 21:03:25 +0200 Subject: [PATCH 26/60] AbstractController: Update flashSuccess() to Slim v3 --- src/AbstractController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/AbstractController.php b/src/AbstractController.php index 5e8fce54..a4551503 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -4,6 +4,7 @@ use Psr\Http\Message\ResponseInterface; use Slim\App; +use Slim\Flash; use Slim\Views\Twig; abstract class AbstractController @@ -42,7 +43,9 @@ protected function redirectTo(string $name, array $params = []): void protected function flashSuccess(string $message): void { - $this->app->flash('success', $message); + /** @var Flash\Messages $flash */ + $flash = $this->app->getContainer()->get('flash'); + $flash->addMessage('success', $message); } protected function config(string $key) From 496e16f50b5caf9c1bde204eea8c0ea7f0ace36b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Dec 2020 23:29:42 +0200 Subject: [PATCH 27/60] Update TwigExtension to Slim v3 --- src/Twig/TwigExtension.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 897b7c82..58f3e36a 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -2,7 +2,7 @@ namespace XHGui\Twig; -use Slim\App; +use Slim\Http\Request; use Slim\Router; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -10,18 +10,18 @@ class TwigExtension extends AbstractExtension { - /** @var App */ - protected $_app; /** @var Router */ private $router; - /** @var string */ + /** @var string|null */ private $pathPrefix; + /** @var Request */ + private $request; - public function __construct(App $app) + public function __construct(Router $router, Request $request, ?string $pathPrefix) { - $this->_app = $app; - $this->router = $app->router(); - $this->pathPrefix = $app->config('path.prefix'); + $this->router = $router; + $this->request = $request; + $this->pathPrefix = $pathPrefix; } public function getFunctions(): array @@ -75,8 +75,8 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - // this is copy of \Slim\Slim::urlFor() to mix path prefix in - // \Slim\Slim::urlFor + // this is copy of \Slim\Slim::urlFor() + // to mix path prefix in \Slim\Slim::urlFor return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query; } @@ -136,8 +136,7 @@ private function pathPrefix(): string return $this->pathPrefix; } - $request = $this->_app->request(); - $rootUri = $request->getRootUri(); + $rootUri = $this->request->getUri()->getBasePath(); // Get URL part prepending index.php $indexPos = strpos($rootUri, 'index.php'); From 2a7772343819709173d9343813280a892de91960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 21 Dec 2020 20:41:19 +0200 Subject: [PATCH 28/60] Replace slim/views to use slim/twig-view --- src/ServiceProvider/SlimProvider.php | 68 +++++++++++++++------------- tests/LazyContainerProperties.php | 3 +- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index a44ac308..aaf3c55f 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -4,8 +4,10 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; +use Psr\Container\ContainerInterface; use Slim\App; -use Slim\Middleware\SessionCookie; +use Slim\Container as SlimContainer; +use Slim\Http\Uri; use Slim\Views\Twig; use XHGui\Twig\TwigExtension; @@ -16,48 +18,52 @@ class SlimProvider implements ServiceProviderInterface */ public function register(Container $c): void { - $c['view'] = static function ($c) { - // Configure Twig view for slim - $view = new Twig(); - - $view->twigTemplateDirs = [ - $c['app.template_dir'], - ]; - $view->parserOptions = [ - 'charset' => 'utf-8', - 'cache' => $c['app.cache_dir'], - 'auto_reload' => true, - 'strict_variables' => false, - 'autoescape' => 'html', - ]; - - // set global variables to templates - $view->appendData([ - 'date_format' => $c['config']['date.format'], - ]); - - return $view; - }; - - $c['app'] = static function ($c) { + $c['app'] = function ($c) { if ($c['config']['timezone']) { date_default_timezone_set($c['config']['timezone']); } $app = new App($c['config']); + $this->registerSlimContainer($app->getContainer()); +/* // Enable cookie based sessions $app->add(new SessionCookie([ 'httponly' => true, ])); - - $view = $c['view']; - $view->parserExtensions = [ - new TwigExtension($app), - ]; - $app->view($view); +*/ return $app; }; } + + private function registerSlimContainer(ContainerInterface $container): void + { + $container['view'] = static function (SlimContainer $container) { + $view = new Twig($container['template_dir'], [ + 'cache' => $container['cache_dir'], + ]); + + $view->addExtension($container[TwigExtension::class]); + + // set global variables to templates + $view['date_format'] = $container['date.format']; + + return $view; + }; + + $container[TwigExtension::class] = static function (SlimContainer $container) { + $router = $container->get('router'); + $request = $container->get('request'); + $pathPrefix = $container->get('path.prefix'); + + return new TwigExtension($router, $request, $pathPrefix); + }; + + $container[Uri::class] = static function (SlimContainer $container) { + $env = $container->get('environment'); + + return Uri::createFromEnvironment($env); + }; + } } diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index 95f0e292..e51b73d2 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -8,6 +8,7 @@ use Slim\Http\Request; use Slim\Http\Response; use Slim\View; +use Slim\Views\Twig; use XHGui\Application; use XHGui\Controller; use XHGui\Saver\SaverInterface; @@ -41,7 +42,7 @@ trait LazyContainerProperties protected $searcher; /** @var SaverInterface */ protected $saver; - /** @var View */ + /** @var Twig */ protected $view; /** @var Controller\WatchController */ protected $watches; From 5122efec0aacf8a028b414df38d57efd1b7c26db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 21 Dec 2020 20:42:51 +0200 Subject: [PATCH 29/60] Remove slim/views dependency --- composer.json | 1 - composer.lock | 55 +-------------------------------------------------- 2 files changed, 1 insertion(+), 55 deletions(-) diff --git a/composer.json b/composer.json index eb1b2b44..d2636add 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,6 @@ "slim/flash": "^0.4.0", "slim/slim": "^3.12", "slim/twig-view": "^2.5", - "slim/views": "^0.1.0", "symfony/options-resolver": "^3.3", "twig/twig": "^1.26" }, diff --git a/composer.lock b/composer.lock index 3baaa678..f969b39d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "33e281923c96ce554da51d0303951080", + "content-hash": "129ea9eeefd256ce64df58c35f868657", "packages": [ { "name": "alcaeus/mongo-php-adapter", @@ -497,59 +497,6 @@ ], "time": "2019-11-28T18:03:50+00:00" }, - { - "name": "slim/views", - "version": "0.1.3", - "source": { - "type": "git", - "url": "https://github.com/slimphp/Slim-Views.git", - "reference": "8561c785e55a39df6cb6f95c3aba3281a60ed5b0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/slimphp/Slim-Views/zipball/8561c785e55a39df6cb6f95c3aba3281a60ed5b0", - "reference": "8561c785e55a39df6cb6f95c3aba3281a60ed5b0", - "shasum": "" - }, - "require": { - "php": ">=5.3.0", - "slim/slim": ">=2.4.0" - }, - "suggest": { - "smarty/smarty": "Smarty templating system", - "twig/twig": "Twig templating system" - }, - "type": "library", - "autoload": { - "psr-4": { - "Slim\\Views\\": "./" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Josh Lockhart", - "email": "info@joshlockhart.com", - "homepage": "http://www.joshlockhart.com/" - }, - { - "name": "Andrew Smith", - "email": "a.smith@silentworks.co.uk", - "homepage": "http://thoughts.silentworks.co.uk/" - } - ], - "description": "Smarty and Twig View Parser package for the Slim Framework", - "homepage": "http://github.com/codeguy/Slim-Views", - "keywords": [ - "extensions", - "slimphp", - "templating" - ], - "time": "2014-12-09T23:48:51+00:00" - }, { "name": "symfony/options-resolver", "version": "v3.4.47", From fa94aceb0691d1b9785b1f13f359cf4177fc8488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Sep 2020 06:12:43 +0300 Subject: [PATCH 30/60] Add RequestProxy class for easy api for parameters --- src/RequestProxy.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/RequestProxy.php diff --git a/src/RequestProxy.php b/src/RequestProxy.php new file mode 100644 index 00000000..9601a7ca --- /dev/null +++ b/src/RequestProxy.php @@ -0,0 +1,34 @@ +request = $request; + } + + public function get(string $key, $default = null) + { + return $this->request->getQueryParam($key, $default); + } + + public function post(string $key, $default = null) + { + return $this->request->getParsedBodyParam($key, $default); + } + + public function getBody() + { + return $this->request->getBody(); + } +} From 57ccba27e2d88b70c7a478073548c62b5b878dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 26 Dec 2020 13:46:54 +0200 Subject: [PATCH 31/60] Add RequestProxy to slim container --- src/ServiceProvider/SlimProvider.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index aaf3c55f..5f1f8e1d 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -9,6 +9,7 @@ use Slim\Container as SlimContainer; use Slim\Http\Uri; use Slim\Views\Twig; +use XHGui\RequestProxy; use XHGui\Twig\TwigExtension; class SlimProvider implements ServiceProviderInterface @@ -65,5 +66,9 @@ private function registerSlimContainer(ContainerInterface $container): void return Uri::createFromEnvironment($env); }; + + $container['request.proxy'] = static function (SlimContainer $container) { + return new RequestProxy($container['request']); + }; } } From d79e2d5cc3a88ff39a87a2bc909e174459ccbecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 20:55:20 +0200 Subject: [PATCH 32/60] Register flash to container --- src/ServiceProvider/SlimProvider.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index 5f1f8e1d..78d488ee 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -7,6 +7,7 @@ use Psr\Container\ContainerInterface; use Slim\App; use Slim\Container as SlimContainer; +use Slim\Flash; use Slim\Http\Uri; use Slim\Views\Twig; use XHGui\RequestProxy; @@ -27,13 +28,6 @@ public function register(Container $c): void $app = new App($c['config']); $this->registerSlimContainer($app->getContainer()); -/* - // Enable cookie based sessions - $app->add(new SessionCookie([ - 'httponly' => true, - ])); -*/ - return $app; }; } @@ -53,6 +47,10 @@ private function registerSlimContainer(ContainerInterface $container): void return $view; }; + $container['flash'] = static function () { + return new Flash\Messages(); + }; + $container[TwigExtension::class] = static function (SlimContainer $container) { $router = $container->get('router'); $request = $container->get('request'); From b596720d22260b26a0b5b45713f533f8f6c086e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 22:11:22 +0200 Subject: [PATCH 33/60] Add ResponseProxy class --- src/ResponseProxy.php | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/ResponseProxy.php diff --git a/src/ResponseProxy.php b/src/ResponseProxy.php new file mode 100644 index 00000000..88c703fb --- /dev/null +++ b/src/ResponseProxy.php @@ -0,0 +1,59 @@ +response = $response ?: new Response(); + } + + public function setHeader(string $name, string $value): self + { + $this->response = $this->response->withHeader($name, $value); + + return $this; + } + + public function setStatus($code): self + { + $this->response = $this->response->withStatus($code); + + return $this; + } + + public function redirect(string $url): self + { + $this->response = $this->response->withRedirect($url); + + return $this; + } + + public function getResponse(): Response + { + return $this->response; + } + + public function write($data): self + { + $this->response->write($data); + + return $this; + } + + public function writeJson($data): self + { + $this->response->write(json_encode($data)); + + return $this; + } +} From 62e9f7761a466a0757aa78d618ebfa8eb79587f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 22:29:30 +0200 Subject: [PATCH 34/60] Add ResponseProxy to SlimProvider --- src/ServiceProvider/SlimProvider.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index 78d488ee..55fe478f 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -11,6 +11,7 @@ use Slim\Http\Uri; use Slim\Views\Twig; use XHGui\RequestProxy; +use XHGui\ResponseProxy; use XHGui\Twig\TwigExtension; class SlimProvider implements ServiceProviderInterface @@ -68,5 +69,9 @@ private function registerSlimContainer(ContainerInterface $container): void $container['request.proxy'] = static function (SlimContainer $container) { return new RequestProxy($container['request']); }; + + $container['response.proxy'] = static function (SlimContainer $container) { + return new ResponseProxy($container['response']); + }; } } From 5558910f1d594909c757da9d34dc7791b00ffc7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 22:05:25 +0200 Subject: [PATCH 35/60] Update redirectTo() to Slim v3 --- src/AbstractController.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/AbstractController.php b/src/AbstractController.php index a4551503..c4dd8bfd 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -5,6 +5,7 @@ use Psr\Http\Message\ResponseInterface; use Slim\App; use Slim\Flash; +use Slim\Router; use Slim\Views\Twig; abstract class AbstractController @@ -38,7 +39,14 @@ protected function render(string $template, array $data = []): void */ protected function redirectTo(string $name, array $params = []): void { - $this->app->redirectTo($name, $params); + $container = $this->app->getContainer(); + /** @var ResponseProxy $response */ + $response = $container->get('response.proxy'); + /** @var Router $router */ + $router = $container->get('router'); + + $url = $router->pathFor($name, $params); + $response->redirect($url); } protected function flashSuccess(string $message): void From d0185ea1583b99e7c6faffe2ca379b531d0998f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 22:42:42 +0200 Subject: [PATCH 36/60] Add response.final to SlimProvider --- src/ServiceProvider/SlimProvider.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index 55fe478f..002aeb3c 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -73,5 +73,12 @@ private function registerSlimContainer(ContainerInterface $container): void $container['response.proxy'] = static function (SlimContainer $container) { return new ResponseProxy($container['response']); }; + + $container['response.final'] = static function (SlimContainer $container) { + /** @var ResponseProxy $response */ + $response = $container['response.proxy']; + + return $response->getResponse(); + }; } } From ea83f10f501464651bbdfdb9c626c86628ae71eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 8 Oct 2020 20:32:32 +0300 Subject: [PATCH 37/60] Update routes to Slim v3 Remove static routes as it confuses Slim 3.x: > Cannot bind an instance to a static closure in vendor/slim/slim/Slim/App.php:227 --- src/ServiceProvider/RouteProvider.php | 184 ++++++++++++-------------- tests/Twig/ExtensionTest.php | 2 +- 2 files changed, 85 insertions(+), 101 deletions(-) diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index db1c2c11..d969e6ea 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -6,9 +6,9 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; use Slim\App; -use Slim\Views\Twig; use XHGui\Controller; -use XHGui\Twig\TwigExtension; +use XHGui\RequestProxy as Request; +use XHGui\ResponseProxy as Response; class RouteProvider implements ServiceProviderInterface { @@ -35,26 +35,36 @@ private function registerRoutes(Container $di, App $app): void ]); }); + /** + * Wrap Request/Response with RequestProxuy/RequestWrapper + */ + $wrap = static function ($handler) use ($di, $app) { + return function () use ($handler, $di, $app) { + $container = $app->getContainer(); + $request = $container->get('request.proxy'); + $response = $container->get('response.proxy'); + + $handler($di, $request, $response); + + return $container->get('response.final'); + }; + }; + // Profile Runs routes - $app->get('/', static function () use ($di, $app): void { + $app->get('/', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $response = $app->response(); // The list changes whenever new profiles are recorded. // Generally avoid caching, but allow re-use in browser's bfcache // and by cache proxies for concurrent requests. // https://github.com/perftools/xhgui/issues/261 - $response->headers->set('Cache-Control', 'public, max-age=0'); + $response->setHeader('Cache-Control', 'public, max-age=0'); $controller->index($request); - })->setName('home'); - - $app->get('/run/view', static function () use ($di, $app): void { - $request = $app->request(); - $response = $app->response(); + }))->setName('home'); + $app->get('/run/view', $wrap(function ($di, Request $request, Response $response): void { // Permalink views to a specific run are meant to be public and immutable. // But limit the cache to only a short period of time (enough to allow // handling of abuse or other stampedes). This way we don't have to @@ -62,196 +72,170 @@ private function registerRoutes(Container $di, App $app): void // or for after XHGui itself is upgraded and static assets may be // incompatible etc. // https://github.com/perftools/xhgui/issues/261 - $response->headers->set('Cache-Control', 'public, max-age=60, must-revalidate'); + $response->setHeader('Cache-Control', 'public, max-age=0'); /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; $controller->view($request); - })->setName('run.view'); + }))->setName('run.view'); - $app->get('/run/delete', static function () use ($di, $app): void { + $app->get('/run/delete', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->deleteForm($request); - })->setName('run.delete.form'); + }))->setName('run.delete.form'); - $app->post('/run/delete', static function () use ($di, $app): void { + $app->post('/run/delete', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->deleteSubmit($request); - })->setName('run.delete.submit'); + }))->setName('run.delete.submit'); - $app->get('/run/delete_all', static function () use ($di): void { + $app->get('/run/delete_all', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; $controller->deleteAllForm(); - })->setName('run.deleteAll.form'); + }))->setName('run.deleteAll.form'); - $app->post('/run/delete_all', static function () use ($di): void { + $app->post('/run/delete_all', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; $controller->deleteAllSubmit(); - })->setName('run.deleteAll.submit'); + }))->setName('run.deleteAll.submit'); - $app->get('/url/view', static function () use ($di, $app): void { + $app->get('/url/view', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->url($request); - })->setName('url.view'); + }))->setName('url.view'); - $app->get('/run/compare', static function () use ($di, $app): void { + $app->get('/run/compare', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->compare($request); - })->setName('run.compare'); + }))->setName('run.compare'); - $app->get('/run/symbol', static function () use ($di, $app): void { + $app->get('/run/symbol', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->symbol($request); - })->setName('run.symbol'); + }))->setName('run.symbol'); - $app->get('/run/symbol/short', static function () use ($di, $app): void { + $app->get('/run/symbol/short', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->symbolShort($request); - })->setName('run.symbol-short'); + }))->setName('run.symbol-short'); - $app->get('/run/callgraph', static function () use ($di, $app): void { + $app->get('/run/callgraph', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $controller->callgraph($request); - })->setName('run.callgraph'); + }))->setName('run.callgraph'); - $app->get('/run/callgraph/data', static function () use ($di, $app): void { + $app->get('/run/callgraph/data', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $response = $app->response(); - $callgraph = $controller->callgraphData($request, $response); + $callgraph = $controller->callgraphData($request); - $response['Content-Type'] = 'application/json'; - $response->body(json_encode($callgraph)); - })->setName('run.callgraph.data'); + $response + ->setHeader('Content-Type', 'application/json') + ->writeJson($callgraph); + }))->setName('run.callgraph.data'); - $app->get('/run/callgraph/dot', static function () use ($di, $app): void { + $app->get('/run/callgraph/dot', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\RunController $controller */ $controller = $di[Controller\RunController::class]; - $request = $app->request(); - $response = $app->response(); $callgraph = $controller->callgraphDataDot($request); - $response['Content-Type'] = 'application/json'; - $response->body(json_encode($callgraph)); - })->setName('run.callgraph.dot'); + $response + ->setHeader('Content-Type', 'application/json') + ->writeJson($callgraph); + }))->setName('run.callgraph.dot'); // Import route - $app->post('/run/import', static function () use ($di, $app): void { + $app->post('/run/import', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\ImportController $controller */ $controller = $di[Controller\ImportController::class]; - $request = $app->request(); - $response = $app->response(); [$status, $result] = $controller->import($request); - $response['Content-Type'] = 'application/json'; - $response->setStatus($status); - $response->body(json_encode($result)); - })->setName('run.import'); + $response + ->setHeader('Content-Type', 'application/json') + ->setStatus($status) + ->writeJson($result); + }))->setName('run.import'); // Watch function routes. - $app->get('/watch', static function () use ($di): void { + $app->get('/watch', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\WatchController $controller */ $controller = $di[Controller\WatchController::class]; $controller->get(); - })->setName('watch.list'); + }))->setName('watch.list'); - $app->post('/watch', static function () use ($di, $app): void { + $app->post('/watch', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\WatchController $controller */ $controller = $di[Controller\WatchController::class]; - $request = $app->request(); - $controller->post($request); - })->setName('watch.save'); + }))->setName('watch.save'); // Custom report routes. - $app->get('/custom', static function () use ($di): void { + $app->get('/custom', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\CustomController $controller */ $controller = $di[Controller\CustomController::class]; $controller->get(); - })->setName('custom.view'); + }))->setName('custom.view'); - $app->get('/custom/help', static function () use ($di, $app): void { + $app->get('/custom/help', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\CustomController $controller */ $controller = $di[Controller\CustomController::class]; - $request = $app->request(); - $controller->help($request); - })->setName('custom.help'); + }))->setName('custom.help'); - $app->post('/custom/query', static function () use ($di, $app): void { + $app->post('/custom/query', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\CustomController $controller */ $controller = $di[Controller\CustomController::class]; - $request = $app->request(); - $response = $app->response(); - $query = $request->post('query'); $retrieve = $request->post('retrieve'); $result = $controller->query($query, $retrieve); - $response->body(json_encode($result)); - $response['Content-Type'] = 'application/json'; - })->setName('custom.query'); + $response + ->setHeader('Content-Type', 'application/json') + ->writeJson($result); + }))->setName('custom.query'); // Waterfall routes - $app->get('/waterfall', static function () use ($di, $app): void { + $app->get('/waterfall', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\WaterfallController $controller */ $controller = $di[Controller\WaterfallController::class]; - $request = $app->request(); - $controller->index($request); - })->setName('waterfall.list'); + }))->setName('waterfall.list'); - $app->get('/waterfall/data', static function () use ($di, $app): void { + $app->get('/waterfall/data', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\WaterfallController $controller */ $controller = $di[Controller\WaterfallController::class]; - $request = $app->request(); - $response = $app->response(); - $data = $controller->query($request); + $data = $controller->query($di['response.proxy']); - $response->body(json_encode($data)); - $response['Content-Type'] = 'application/json'; - })->setName('waterfall.data'); + $response + ->setHeader('Content-Type', 'application/json') + ->writeJson($data); + }))->setName('waterfall.data'); // Metrics - $app->get('/metrics', static function () use ($di, $app): void { + $app->get('/metrics', $wrap(function ($di, Request $request, Response $response): void { /** @var Controller\MetricsController $controller */ $controller = $di[Controller\MetricsController::class]; - $response = $app->response(); $body = $controller->metrics(); - $response->body($body); - $response['Content-Type'] = 'text/plain; version=0.0.4'; - })->setName('metrics'); + $response + ->setHeader('Content-Type', 'text/plain; version=0.0.4') + ->write($body); + }))->setName('metrics'); } private function registerControllers(Container $app): void diff --git a/tests/Twig/ExtensionTest.php b/tests/Twig/ExtensionTest.php index f06591d2..1135d12a 100755 --- a/tests/Twig/ExtensionTest.php +++ b/tests/Twig/ExtensionTest.php @@ -16,7 +16,7 @@ public function setUp(): void { parent::setUp(); $app = new App(); - $app->get('/test', static function (): void { + $app->get('/test', function (): void { })->setName('test'); $this->ext = new TwigExtension($app); } From 959c4bf34778c8e5dec74e6c0dec3b9335420720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 26 Dec 2020 12:17:25 +0200 Subject: [PATCH 38/60] No custom error handler --- src/ServiceProvider/RouteProvider.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index d969e6ea..8cadabe4 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -2,7 +2,6 @@ namespace XHGui\ServiceProvider; -use Exception; use Pimple\Container; use Pimple\ServiceProviderInterface; use Slim\App; @@ -20,8 +19,9 @@ public function register(Container $di): void private function registerRoutes(Container $di, App $app): void { + /* $app->error(static function (Exception $e) use ($di, $app): void { - /** @var Twig $view */ + // @var Twig $view $view = $di['view']; $view->parserOptions['cache'] = false; $view->parserExtensions = [ @@ -34,6 +34,7 @@ private function registerRoutes(Container $di, App $app): void 'stack_trace' => $e->getTraceAsString(), ]); }); + */ /** * Wrap Request/Response with RequestProxuy/RequestWrapper From e6ca6aa5587a2f03a0cd11802929724ed1536d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 26 Dec 2020 13:27:41 +0200 Subject: [PATCH 39/60] Add app.template_dir, app.cache_dir to Slim container --- src/ServiceProvider/ConfigProvider.php | 8 +++++++- src/ServiceProvider/ServiceProvider.php | 4 ---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ServiceProvider/ConfigProvider.php b/src/ServiceProvider/ConfigProvider.php index 90bc4d6d..a6363bba 100644 --- a/src/ServiceProvider/ConfigProvider.php +++ b/src/ServiceProvider/ConfigProvider.php @@ -23,7 +23,13 @@ public function register(Container $app): void Config::load($app['app.config_dir'] . '/config.php'); } - return Config::all(); + $config = Config::all(); + $config += [ + 'template_dir' => $app['app.dir'] . '/templates', + 'cache_dir' => $app['app.dir'] . '/cache', + ]; + + return $config; }; } } diff --git a/src/ServiceProvider/ServiceProvider.php b/src/ServiceProvider/ServiceProvider.php index 429decc9..3ef37124 100644 --- a/src/ServiceProvider/ServiceProvider.php +++ b/src/ServiceProvider/ServiceProvider.php @@ -17,11 +17,7 @@ public function register(Container $app): void private function setupPaths(Container $app): void { $app['app.dir'] = dirname(__DIR__, 2); - $app['app.template_dir'] = $app['app.dir'] . '/templates'; $app['app.config_dir'] = $app['app.dir'] . '/config'; - $app['app.cache_dir'] = static function ($app) { - return $app['config']['cache'] ?? $app['app.dir'] . '/cache'; - }; } /** From ff0f8f2b1e565a17a34170ebb342ee4428a1aa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 16:42:15 +0200 Subject: [PATCH 40/60] Update to use RequestProxy in Controllers --- src/Controller/CustomController.php | 2 +- src/Controller/ImportController.php | 2 +- src/Controller/RunController.php | 2 +- src/Controller/WatchController.php | 2 +- src/Controller/WaterfallController.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Controller/CustomController.php b/src/Controller/CustomController.php index b639b59a..012c4cab 100644 --- a/src/Controller/CustomController.php +++ b/src/Controller/CustomController.php @@ -3,8 +3,8 @@ namespace XHGui\Controller; use Slim\App; -use Slim\Http\Request; use XHGui\AbstractController; +use XHGui\RequestProxy as Request; use XHGui\Searcher\SearcherInterface; class CustomController extends AbstractController diff --git a/src/Controller/ImportController.php b/src/Controller/ImportController.php index b02f1b6f..6c57ef93 100644 --- a/src/Controller/ImportController.php +++ b/src/Controller/ImportController.php @@ -5,8 +5,8 @@ use Exception; use InvalidArgumentException; use Slim\App; -use Slim\Http\Request; use XHGui\AbstractController; +use XHGui\RequestProxy as Request; use XHGui\Saver\SaverInterface; class ImportController extends AbstractController diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index e40fe085..c7db7a24 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -4,9 +4,9 @@ use Exception; use Slim\App; -use Slim\Http\Request; use XHGui\AbstractController; use XHGui\Options\SearchOptions; +use XHGui\RequestProxy as Request; use XHGui\Searcher\SearcherInterface; class RunController extends AbstractController diff --git a/src/Controller/WatchController.php b/src/Controller/WatchController.php index b9a64748..de3036c8 100644 --- a/src/Controller/WatchController.php +++ b/src/Controller/WatchController.php @@ -3,8 +3,8 @@ namespace XHGui\Controller; use Slim\App; -use Slim\Http\Request; use XHGui\AbstractController; +use XHGui\RequestProxy as Request; use XHGui\Searcher\SearcherInterface; class WatchController extends AbstractController diff --git a/src/Controller/WaterfallController.php b/src/Controller/WaterfallController.php index 6b894da0..c5d0e66e 100644 --- a/src/Controller/WaterfallController.php +++ b/src/Controller/WaterfallController.php @@ -3,10 +3,10 @@ namespace XHGui\Controller; use Slim\App; -use Slim\Http\Request; use XHGui\AbstractController; use XHGui\Options\SearchOptions; use XHGui\Profile; +use XHGui\RequestProxy as Request; use XHGui\Searcher\SearcherInterface; class WaterfallController extends AbstractController From 7a6aeef066c3aa622daf821a9b91b3238a0569f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 20:53:52 +0200 Subject: [PATCH 41/60] Tests: Add TwigView to provide all() method for tests --- tests/TwigView.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/TwigView.php diff --git a/tests/TwigView.php b/tests/TwigView.php new file mode 100644 index 00000000..223f51a8 --- /dev/null +++ b/tests/TwigView.php @@ -0,0 +1,24 @@ +data = $data; + + return parent::render($response, $template, $data); + } + + public function all(): array + { + return $this->data + $this->defaultVariables; + } +} From f8934a8905b6dc42861d9a833b985a188e7b84c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 20:55:50 +0200 Subject: [PATCH 42/60] Override view.class for tests --- src/ServiceProvider/SlimProvider.php | 3 ++- tests/LazyContainerProperties.php | 30 ++++++++++++---------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index 002aeb3c..235e7fe8 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -35,8 +35,9 @@ public function register(Container $c): void private function registerSlimContainer(ContainerInterface $container): void { + $container['view.class'] = Twig::class; $container['view'] = static function (SlimContainer $container) { - $view = new Twig($container['template_dir'], [ + $view = new $container['view.class']($container['template_dir'], [ 'cache' => $container['cache_dir'], ]); diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index e51b73d2..40a69792 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -4,6 +4,8 @@ use LazyProperty\LazyPropertiesTrait; use MongoDB; +use Pimple\Container; +use Pimple\ServiceProviderInterface; use Slim\App; use Slim\Http\Request; use Slim\Http\Response; @@ -14,7 +16,6 @@ use XHGui\Saver\SaverInterface; use XHGui\Searcher\MongoSearcher; use XHGui\Searcher\SearcherInterface; -use XHGui\Twig\TwigExtension; trait LazyContainerProperties { @@ -42,7 +43,7 @@ trait LazyContainerProperties protected $searcher; /** @var SaverInterface */ protected $saver; - /** @var Twig */ + /** @var TwigView */ protected $view; /** @var Controller\WatchController */ protected $watches; @@ -69,25 +70,20 @@ protected function setupProperties(): void protected function getDi() { $di = new Application(); - $config = $di['config']; // Use a test databases // TODO: do the same for PDO. currently PDO uses DSN syntax and has too many variations $di['mongodb.database'] = 'test_xhgui'; - /** @var App $app */ - $app = $this->getMockBuilder(App::class) - ->setMethods(['redirect', 'render', 'urlFor']) - ->setConstructorArgs([$config]) - ->getMock(); - $di['app'] = $app; + /** @var \Slim\Container $container */ + $container = $di['app']->getContainer(); + $container->register(new class() implements ServiceProviderInterface { + public function register(Container $container): void + { + $container['view.class'] = TwigView::class; + } + }); - $view = $di['view']; - $view->parserExtensions = [ - new TwigExtension($app), - ]; - - $app->view($view); $di->boot(); return $di; @@ -143,9 +139,9 @@ protected function getSaver() return $this->di['saver']; } - protected function getView(): View + protected function getView(): Twig { - return $this->di['view']; + return $this->di['app']->getContainer()->get('view'); } protected function getWatches() From 64ec14d6064bac93c809f3c30c018d5ca33ac6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 20:57:51 +0200 Subject: [PATCH 43/60] Tests: Add env lazy property --- tests/LazyContainerProperties.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index 40a69792..7337a9ea 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -7,9 +7,9 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; use Slim\App; +use Slim\Http\Environment; use Slim\Http\Request; use Slim\Http\Response; -use Slim\View; use Slim\Views\Twig; use XHGui\Application; use XHGui\Controller; @@ -39,6 +39,8 @@ trait LazyContainerProperties protected $app; /** @var array */ protected $config; + /** @var Environment */ + protected $env; /** @var SearcherInterface */ protected $searcher; /** @var SaverInterface */ @@ -54,6 +56,7 @@ protected function setupProperties(): void 'di', 'app', 'config', + 'env', 'import', 'mongo', 'mongodb', @@ -99,6 +102,11 @@ protected function getConfig() return $this->di['config']; } + protected function getEnv() + { + return Environment::mock(); + } + protected function getImport() { return $this->di[Controller\ImportController::class]; From 1acfda46c7080df0234d2ceb7220be1976f32489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 21:01:57 +0200 Subject: [PATCH 44/60] Add request/response lazy properties --- tests/LazyContainerProperties.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index 7337a9ea..a5fa1d37 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -13,6 +13,7 @@ use Slim\Views\Twig; use XHGui\Application; use XHGui\Controller; +use XHGui\RequestProxy; use XHGui\Saver\SaverInterface; use XHGui\Searcher\MongoSearcher; use XHGui\Searcher\SearcherInterface; @@ -29,7 +30,7 @@ trait LazyContainerProperties protected $mongo; /** @var MongoDB */ protected $mongodb; - /** @var Request */ + /** @var RequestProxy */ protected $request; /** @var Response */ protected $response; @@ -122,14 +123,14 @@ protected function getMongoDb() return $this->di[MongoDB::class]; } - protected function getRequest(): Request + protected function getRequest(): RequestProxy { - return $this->di['app']->request(); + return new RequestProxy(Request::createFromEnvironment($this->env)); } protected function getResponse(): Response { - return $this->di['app']->response(); + return $this->di['app']->getContainer()->get('response'); } protected function getSearcher() From 7cf9e56631e2eb1b05e39395d5176b9046fe036a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 21:02:35 +0200 Subject: [PATCH 45/60] Fill $this->env request mocking --- tests/Controller/ImportTest.php | 4 ++-- tests/Controller/RunTest.php | 25 ++++++++++++------------- tests/Controller/WatchTest.php | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/tests/Controller/ImportTest.php b/tests/Controller/ImportTest.php index 92cc848c..0c9832e8 100644 --- a/tests/Controller/ImportTest.php +++ b/tests/Controller/ImportTest.php @@ -12,7 +12,7 @@ public function setUp(): void { parent::setUp(); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', ]); @@ -40,7 +40,7 @@ public function testImportSuccess(): void ], ], ]; - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', 'slim.input' => json_encode($data), diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 32371dfe..6f43846b 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -12,7 +12,7 @@ public function setUp(): void { parent::setUp(); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', ]); @@ -36,7 +36,7 @@ public function testIndexEmpty(): void public function testIndexSortedWallTime(): void { - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', 'QUERY_STRING' => 'sort=wt', @@ -50,7 +50,7 @@ public function testIndexSortedWallTime(): void public function testIndexSortedCpu(): void { - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', 'QUERY_STRING' => 'sort=cpu&direction=desc', @@ -65,7 +65,7 @@ public function testIndexSortedCpu(): void public function testIndexWithSearch(): void { - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', 'QUERY_STRING' => 'sort=mu&direction=asc&url=index.php', @@ -84,7 +84,7 @@ public function testUrl(): void { $this->skipIfPdo('getForUrl is not implemented'); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/url/view', 'QUERY_STRING' => 'url=%2Ftasks', @@ -133,7 +133,7 @@ public function testCallgraph(): void { $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaaa', @@ -150,14 +150,13 @@ public function testCallgraphData(): void { $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaaa', ]); $result = $this->runs->callgraphData($this->request); - $this->assertInternalType('array', $result); $this->assertArrayHasKey('metric', $result); $this->assertArrayHasKey('total', $result); @@ -171,7 +170,7 @@ public function testDeleteSubmit(): void $searcher = $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'REQUEST_METHOD' => 'POST', 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/run/delete', @@ -195,7 +194,7 @@ public function testDeleteAllSubmit(): void $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/run/delete_all', ]); @@ -214,7 +213,7 @@ public function testFilterCustomMethods(): void $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/run/view', 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*,strpos()', @@ -231,7 +230,7 @@ public function testFilterCustomMethod(): void $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/run/view', 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*', @@ -248,7 +247,7 @@ public function testFilterMethods(): void $this->searcher->truncate(); $this->importFixture($this->saver); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/run/view', 'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=true', diff --git a/tests/Controller/WatchTest.php b/tests/Controller/WatchTest.php index fbb75dd1..5a8e5a86 100644 --- a/tests/Controller/WatchTest.php +++ b/tests/Controller/WatchTest.php @@ -12,7 +12,7 @@ public function setUp(): void parent::setUp(); $this->skipIfPdo('Watchers not implemented'); - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/watch', ]); From 162053e8caa76345300b85299666424f41a92358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 21:35:41 +0200 Subject: [PATCH 46/60] Add TwigExtension to lazy properties --- tests/LazyContainerProperties.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index a5fa1d37..c83f5fad 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -17,6 +17,7 @@ use XHGui\Saver\SaverInterface; use XHGui\Searcher\MongoSearcher; use XHGui\Searcher\SearcherInterface; +use XHGui\Twig\TwigExtension; trait LazyContainerProperties { @@ -42,6 +43,8 @@ trait LazyContainerProperties protected $config; /** @var Environment */ protected $env; + /** @var TwigExtension */ + protected $ext; /** @var SearcherInterface */ protected $searcher; /** @var SaverInterface */ @@ -58,6 +61,7 @@ protected function setupProperties(): void 'app', 'config', 'env', + 'ext', 'import', 'mongo', 'mongodb', @@ -81,10 +85,20 @@ protected function getDi() /** @var \Slim\Container $container */ $container = $di['app']->getContainer(); - $container->register(new class() implements ServiceProviderInterface { + $container->register(new class($this) implements ServiceProviderInterface { + private $ctx; + + public function __construct($ctx) + { + $this->ctx = $ctx; + } + public function register(Container $container): void { $container['view.class'] = TwigView::class; + $container['environment'] = function () { + return $this->ctx->getEnv(); + }; } }); @@ -103,9 +117,14 @@ protected function getConfig() return $this->di['config']; } - protected function getEnv() + public function getEnv() + { + return $this->env ?? $this->env = Environment::mock(); + } + + protected function getExt() { - return Environment::mock(); + return $this->app->getContainer()[TwigExtension::class]; } protected function getImport() From d7472136d63aef006a71da3fb4f3d7d1023ad93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 27 Dec 2020 21:36:03 +0200 Subject: [PATCH 47/60] Use twig extension via lazy properties --- tests/Twig/ExtensionTest.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/Twig/ExtensionTest.php b/tests/Twig/ExtensionTest.php index 1135d12a..0ce8beb2 100755 --- a/tests/Twig/ExtensionTest.php +++ b/tests/Twig/ExtensionTest.php @@ -2,23 +2,16 @@ namespace XHGui\Test\Twig; -use Slim\App; use Slim\Http\Environment; use XHGui\Test\TestCase; -use XHGui\Twig\TwigExtension; class ExtensionTest extends TestCase { - /** @var TwigExtension */ - private $ext; - public function setUp(): void { parent::setUp(); - $app = new App(); - $app->get('/test', function (): void { + $this->app->get('/test', function (): void { })->setName('test'); - $this->ext = new TwigExtension($app); } public function testFormatBytes(): void @@ -101,7 +94,7 @@ public function testUrl($url, $query, $expected): void public function testStaticUrlNoIndexPhp(): void { - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => '/index.php', 'PHP_SELF' => '/index.php', 'REQUEST_URI' => '/', @@ -112,7 +105,7 @@ public function testStaticUrlNoIndexPhp(): void public function testStaticUrlWithIndexPhp(): void { - Environment::mock([ + $this->env = Environment::mock([ 'SCRIPT_NAME' => '/xhgui/webroot/index.php', 'PHP_SELF' => '/xhgui/webroot/index.php/', 'REQUEST_URI' => '/xhgui/webroot/index.php/', From 465edce4eba580751c0d5c60afc34bf8b419898f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 00:14:38 +0200 Subject: [PATCH 48/60] Add createPostRequest, createRequest helpers method to TestCase --- tests/TestCase.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 8a62ee3e..6cba14d3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,8 @@ namespace XHGui\Test; +use Slim\Http\Request; +use XHGui\RequestProxy; use XHGui\Saver\SaverInterface; abstract class TestCase extends \PHPUnit\Framework\TestCase @@ -34,6 +36,22 @@ protected function loadFixture(string $fileName): array return $data; } + protected function createRequest(array $query): RequestProxy + { + $request = Request::createFromEnvironment($this->env); + $request = $request->withQueryParams($query); + + return new RequestProxy($request); + } + + protected function createPostRequest(array $post): RequestProxy + { + $request = Request::createFromEnvironment($this->env); + $request = $request->withParsedBody($post); + + return new RequestProxy($request); + } + protected function skipIfPdo($details = null): void { $saveHandler = $this->config['save.handler']; From 6679883e467dfaebaf4de6ac3b266757b6e6fe5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 00:01:57 +0200 Subject: [PATCH 49/60] Add flash.storage to able to override it for test --- src/ServiceProvider/SlimProvider.php | 8 ++++++-- tests/LazyContainerProperties.php | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ServiceProvider/SlimProvider.php b/src/ServiceProvider/SlimProvider.php index 235e7fe8..2ded4abf 100644 --- a/src/ServiceProvider/SlimProvider.php +++ b/src/ServiceProvider/SlimProvider.php @@ -49,8 +49,12 @@ private function registerSlimContainer(ContainerInterface $container): void return $view; }; - $container['flash'] = static function () { - return new Flash\Messages(); + // Having "null" here will make use of $_SESSION + $container['flash.storage'] = null; + $container['flash'] = static function ($container) { + $storage = $container['flash.storage']; + + return new Flash\Messages($storage); }; $container[TwigExtension::class] = static function (SlimContainer $container) { diff --git a/tests/LazyContainerProperties.php b/tests/LazyContainerProperties.php index c83f5fad..ad0286de 100644 --- a/tests/LazyContainerProperties.php +++ b/tests/LazyContainerProperties.php @@ -96,6 +96,7 @@ public function __construct($ctx) public function register(Container $container): void { $container['view.class'] = TwigView::class; + $container['flash.storage'] = []; $container['environment'] = function () { return $this->ctx->getEnv(); }; From 98c56f0a74f313790bd74bdced5983480395bde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 00:24:23 +0200 Subject: [PATCH 50/60] Add getContentLength method to RequestProxy --- src/RequestProxy.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/RequestProxy.php b/src/RequestProxy.php index 9601a7ca..6092982b 100644 --- a/src/RequestProxy.php +++ b/src/RequestProxy.php @@ -31,4 +31,9 @@ public function getBody() { return $this->request->getBody(); } + + public function getContentLength(): ?int + { + return $this->request->getContentLength(); + } } From e887c8cd473624aa8b7df4dbbc68d469ce25e647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 00:25:29 +0200 Subject: [PATCH 51/60] Add createJsonPostRequest helper to TestCase --- tests/TestCase.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 6cba14d3..91f090cf 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -52,6 +52,16 @@ protected function createPostRequest(array $post): RequestProxy return new RequestProxy($request); } + protected function createJsonPostRequest(array $data): RequestProxy + { + $request = Request::createFromEnvironment($this->env); + $stream = $request->getBody(); + $stream->write(json_encode($data)); + $stream->rewind(); + + return new RequestProxy($request); + } + protected function skipIfPdo($details = null): void { $saveHandler = $this->config['save.handler']; From c630642e211f8a07892203a21b2e00fd77ccaf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 23:55:48 +0200 Subject: [PATCH 52/60] Fix tests with post request --- tests/Controller/ImportTest.php | 5 +++-- tests/Controller/RunTest.php | 7 +++---- tests/Controller/WatchTest.php | 19 ++++++++++--------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/Controller/ImportTest.php b/tests/Controller/ImportTest.php index 0c9832e8..e84b3ce9 100644 --- a/tests/Controller/ImportTest.php +++ b/tests/Controller/ImportTest.php @@ -43,14 +43,15 @@ public function testImportSuccess(): void $this->env = Environment::mock([ 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/', - 'slim.input' => json_encode($data), ]); + $request = $this->createJsonPostRequest($data); + $searcher = $this->searcher->truncate(); $before = $searcher->getForUrl('/things', []); $this->assertEmpty($before['results']); - $this->import->import($this->request); + $this->import->import($request); $after = $searcher->getForUrl('/things', []); $this->assertNotEmpty($after['results']); diff --git a/tests/Controller/RunTest.php b/tests/Controller/RunTest.php index 6f43846b..b9ec93d3 100644 --- a/tests/Controller/RunTest.php +++ b/tests/Controller/RunTest.php @@ -174,15 +174,14 @@ public function testDeleteSubmit(): void 'REQUEST_METHOD' => 'POST', 'SCRIPT_NAME' => 'index.php', 'PATH_INFO' => '/run/delete', - 'slim.request.form_hash' => [ - 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaa', - ], ]); + $request = $this->createPostRequest(['id' => 'aaaaaaaaaaaaaaaaaaaaaaaa']); + $result = $searcher->getAll(new SearchOptions()); $count = count($result['results']); - $this->runs->deleteSubmit($this->request); + $this->runs->deleteSubmit($request); $result = $searcher->getAll(new SearchOptions()); $this->assertCount($count - 1, $result['results']); diff --git a/tests/Controller/WatchTest.php b/tests/Controller/WatchTest.php index 5a8e5a86..8c2b9e78 100644 --- a/tests/Controller/WatchTest.php +++ b/tests/Controller/WatchTest.php @@ -29,14 +29,14 @@ public function testGet(): void public function testPostAdd(): void { $this->searcher->truncateWatches(); - $_POST = [ + $request = $this->createPostRequest([ 'watch' => [ ['name' => 'strlen'], ['name' => 'strpos'], ], - ]; + ]); - $this->watches->post($this->request); + $this->watches->post($request); $result = $this->searcher->getAllWatches(); $this->assertCount(2, $result); @@ -50,12 +50,12 @@ public function testPostModify(): void $searcher->saveWatch(['name' => 'strlen']); $saved = $searcher->getAllWatches(); - $_POST = [ + $request = $this->createPostRequest([ 'watch' => [ ['name' => 'strpos', '_id' => $saved[0]['_id']], ], - ]; - $this->watches->post($this->request); + ]); + $this->watches->post($request); $result = $searcher->getAllWatches(); $this->assertCount(1, $result); @@ -68,12 +68,13 @@ public function testPostDelete(): void $this->searcher->saveWatch(['name' => 'strlen']); $saved = $this->searcher->getAllWatches(); - $_POST = [ + $request = $this->createPostRequest([ 'watch' => [ ['removed' => 1, 'name' => 'strpos', '_id' => $saved[0]['_id']], ], - ]; - $this->watches->post($this->request); + ]); + + $this->watches->post($request); $result = $this->searcher->getAllWatches(); $this->assertCount(0, $result); From 2d9a6be25a8fafd72e36f9d90186c77c167ba4cb Mon Sep 17 00:00:00 2001 From: luzip665 Date: Mon, 17 Jan 2022 10:48:27 +0100 Subject: [PATCH 53/60] Show flash messages at top of page --- src/AbstractController.php | 7 +++++++ templates/layout/base.twig | 4 +++- webroot/index.php | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/AbstractController.php b/src/AbstractController.php index c4dd8bfd..06209efc 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -28,6 +28,13 @@ protected function render(string $template, array $data = []): void /** @var Twig $renderer */ $renderer = $container->get('view'); + if (!isset($data['flash'])) { + /** @var Flash\Messages $flash */ + $flash = $this->app->getContainer()->get('flash'); + $messages = $flash->getMessages(); + $data['flash'] = $messages; + } + $renderer->render($response, $template, $data); } diff --git a/templates/layout/base.twig b/templates/layout/base.twig index eb0f49d1..8742efad 100644 --- a/templates/layout/base.twig +++ b/templates/layout/base.twig @@ -36,7 +36,9 @@
{% if flash.success %}
- {{ flash.success }} + {% for msg in flash.success %} +
  • {{ msg }}
  • + {% endfor %}
    {% endif %} diff --git a/webroot/index.php b/webroot/index.php index 8f921389..5ad094af 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -4,5 +4,7 @@ require dirname(__DIR__) . '/vendor/autoload.php'; +session_start(); + $app = new Application(); $app->run(); From cc61ef5fdd7c76c379822c87e139b48aef01b583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:13:46 +0200 Subject: [PATCH 54/60] TwigExtension: Extract getBasePath method --- src/Twig/TwigExtension.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 58f3e36a..9ffaaef1 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -136,7 +136,7 @@ private function pathPrefix(): string return $this->pathPrefix; } - $rootUri = $this->request->getUri()->getBasePath(); + $rootUri = $this->getBasePath(); // Get URL part prepending index.php $indexPos = strpos($rootUri, 'index.php'); @@ -146,4 +146,9 @@ private function pathPrefix(): string return $rootUri; } + + private function getBasePath(): string + { + return $this->request->getUri()->getBasePath(); + } } From d344354f21768b8b47b383cb109a093173134940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:15:33 +0200 Subject: [PATCH 55/60] TwigExtension: Rename pathPrefix to getPathPrefix --- src/Twig/TwigExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 9ffaaef1..76560bd4 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -78,7 +78,7 @@ public function url(string $name, $queryargs = []): string // this is copy of \Slim\Slim::urlFor() // to mix path prefix in \Slim\Slim::urlFor - return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query; + return rtrim($this->getPathPrefix(), '/') . $this->router->urlFor($name) . $query; } /** @@ -89,7 +89,7 @@ public function url(string $name, $queryargs = []): string */ public function staticUrl(string $path): string { - $rootUri = $this->pathPrefix(); + $rootUri = $this->getPathPrefix(); return rtrim($rootUri, '/') . '/' . $path; } @@ -130,7 +130,7 @@ public function formatPercent($value): string return number_format((float)$value * 100, 0) . ' %'; } - private function pathPrefix(): string + private function getPathPrefix(): string { if ($this->pathPrefix !== null) { return $this->pathPrefix; From 7e31007e32feb3446724500bc067ac6ed2b1ea2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:18:49 +0200 Subject: [PATCH 56/60] TwigExtension: Make pathPrefix a function --- src/Twig/TwigExtension.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 76560bd4..5d63d47e 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -75,10 +75,7 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - // this is copy of \Slim\Slim::urlFor() - // to mix path prefix in \Slim\Slim::urlFor - - return rtrim($this->getPathPrefix(), '/') . $this->router->urlFor($name) . $query; + return $this->pathPrefix($this->router->urlFor($name) . $query); } /** @@ -89,9 +86,7 @@ public function url(string $name, $queryargs = []): string */ public function staticUrl(string $path): string { - $rootUri = $this->getPathPrefix(); - - return rtrim($rootUri, '/') . '/' . $path; + return $this->pathPrefix($path); } public function formatBytes($value): string @@ -130,6 +125,11 @@ public function formatPercent($value): string return number_format((float)$value * 100, 0) . ' %'; } + private function pathPrefix($path): string + { + return rtrim($this->getPathPrefix(), '/') . '/'. $path; + } + private function getPathPrefix(): string { if ($this->pathPrefix !== null) { From bddab1717126cec43a9362ed85f85260277ed75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:19:33 +0200 Subject: [PATCH 57/60] TwigExtension: Strip basePath from urlFor result --- src/Twig/TwigExtension.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 5d63d47e..d0041915 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -75,7 +75,15 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - return $this->pathPrefix($this->router->urlFor($name) . $query); + $basePath = $this->getBasePath(); + $url = $this->router->urlFor($name); + + // Remove basePath from url + if (strpos($url, $basePath) === 0) { + $url = ltrim(substr($url, strlen($basePath)), '/'); + } + + return $this->pathPrefix($url . $query); } /** From 63b5646be77f4f47fd324c04c55ee613fc9abd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:25:51 +0200 Subject: [PATCH 58/60] TwigExtension: Pre-compute basePath and pathPrefix --- src/Twig/TwigExtension.php | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index d0041915..4cbcb325 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -12,16 +12,16 @@ class TwigExtension extends AbstractExtension { /** @var Router */ private $router; - /** @var string|null */ + /** @var string */ + private $basePath; + /** @var string */ private $pathPrefix; - /** @var Request */ - private $request; public function __construct(Router $router, Request $request, ?string $pathPrefix) { $this->router = $router; - $this->request = $request; - $this->pathPrefix = $pathPrefix; + $this->basePath = $request->getUri()->getBasePath(); + $this->pathPrefix = $this->buildPathPrefix($this->basePath, $pathPrefix); } public function getFunctions(): array @@ -75,12 +75,11 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - $basePath = $this->getBasePath(); $url = $this->router->urlFor($name); // Remove basePath from url - if (strpos($url, $basePath) === 0) { - $url = ltrim(substr($url, strlen($basePath)), '/'); + if (strpos($url, $this->basePath) === 0) { + $url = ltrim(substr($url, strlen($this->basePath)), '/'); } return $this->pathPrefix($url . $query); @@ -135,17 +134,15 @@ public function formatPercent($value): string private function pathPrefix($path): string { - return rtrim($this->getPathPrefix(), '/') . '/'. $path; + return rtrim($this->pathPrefix, '/') . '/'. $path; } - private function getPathPrefix(): string + private function buildPathPrefix(string $rootUri, ?string $pathPrefix): string { - if ($this->pathPrefix !== null) { - return $this->pathPrefix; + if ($pathPrefix !== null) { + return $pathPrefix; } - $rootUri = $this->getBasePath(); - // Get URL part prepending index.php $indexPos = strpos($rootUri, 'index.php'); if ($indexPos > 0) { @@ -154,9 +151,4 @@ private function getPathPrefix(): string return $rootUri; } - - private function getBasePath(): string - { - return $this->request->getUri()->getBasePath(); - } } From c56b7c044e1d477337e8023d9e26dde7a57b08db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:30:12 +0200 Subject: [PATCH 59/60] TwigExtension: Move rtrim to buildPathPrefix rather pathPrefix --- src/Twig/TwigExtension.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 4cbcb325..d6621ef6 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -21,7 +21,7 @@ public function __construct(Router $router, Request $request, ?string $pathPrefi { $this->router = $router; $this->basePath = $request->getUri()->getBasePath(); - $this->pathPrefix = $this->buildPathPrefix($this->basePath, $pathPrefix); + $this->pathPrefix = rtrim($this->buildPathPrefix($this->basePath, $pathPrefix), '/'); } public function getFunctions(): array @@ -134,13 +134,13 @@ public function formatPercent($value): string private function pathPrefix($path): string { - return rtrim($this->pathPrefix, '/') . '/'. $path; + return $this->pathPrefix . '/' . $path; } private function buildPathPrefix(string $rootUri, ?string $pathPrefix): string { if ($pathPrefix !== null) { - return $pathPrefix; + return rtrim($pathPrefix, '/'); } // Get URL part prepending index.php @@ -149,6 +149,6 @@ private function buildPathPrefix(string $rootUri, ?string $pathPrefix): string return substr($rootUri, 0, $indexPos); } - return $rootUri; + return rtrim($rootUri, '/'); } } From 9faee081243b6d9d50b03c46217b29c4c5e74ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:36:33 +0200 Subject: [PATCH 60/60] TwigExtension: Avoid strpos(): Empty needle error --- src/Twig/TwigExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index d6621ef6..d104d1c1 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -78,11 +78,11 @@ public function url(string $name, $queryargs = []): string $url = $this->router->urlFor($name); // Remove basePath from url - if (strpos($url, $this->basePath) === 0) { - $url = ltrim(substr($url, strlen($this->basePath)), '/'); + if ($this->basePath && strpos($url, $this->basePath) === 0) { + $url = substr($url, strlen($this->basePath)); } - return $this->pathPrefix($url . $query); + return $this->pathPrefix(ltrim($url, '/') . $query); } /**