Skip to content

Commit 26d8475

Browse files
committed
feature: non-html views
1 parent a0d6ab7 commit 26d8475

7 files changed

Lines changed: 29 additions & 22 deletions

File tree

router.default.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
use Gt\Http\Request;
55
use GT\Routing\BaseRouter;
66
use GT\Routing\Method\Any;
7-
use GT\Routing\Method\Get;
8-
use GT\Routing\Method\Post;
97
use GT\Routing\Path\FileMatch\BasicFileMatch;
108
use GT\Routing\Path\FileMatch\MagicFileMatch;
119
use GT\Routing\Path\PathMatcher;
12-
use GT\Routing\Path\DynamicPath;
13-
use GT\WebEngine\View\BaseView;
1410
use GT\WebEngine\View\HTMLView;
15-
use GT\WebEngine\View\NullView;
11+
use GT\WebEngine\View\JSONView;
1612

1713
class DefaultRouter extends BaseRouter {
1814
#[Any(name: "page-route", accept: "text/html,application/xhtml+xml")]
@@ -118,7 +114,7 @@ public function api(
118114
):void {
119115
$pathMatcher = new PathMatcher("api");
120116
$this->pathMatcherFilter($pathMatcher);
121-
$this->setViewClass(NullView::class);
117+
$this->setViewClass(JSONView::class);
122118
$sortNestLevelCallback = fn(string $a, string $b) =>
123119
substr_count($a, "/") > substr_count($b, "/")
124120
? 1

src/Dispatch/Dispatcher.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Gt\Http\Stream;
2424
use GT\Input\Input;
2525
use GT\Input\InputData\InputData;
26+
use Gt\Json\Schema\JsonDocument;
2627
use GT\Routing\Assembly;
2728
use Gt\Routing\BaseRouter;
2829
use GT\Routing\Path\DynamicPath;
@@ -58,8 +59,8 @@ class Dispatcher {
5859

5960
private Injector $injector;
6061
private NullView|JSONView|HTMLView $view;
61-
private HTMLDocument/*|NullViewModel*/ $viewModel;
62-
private ViewModelProcessor $viewModelProcessor;
62+
private HTMLDocument|JsonDocument $viewModel;
63+
private ?ViewModelProcessor $viewModelProcessor;
6364
private BaseRouter $router;
6465
private ?SessionInit $sessionInit = null;
6566
private Assembly $logicAssembly;
@@ -186,7 +187,8 @@ public function __construct(
186187
$this->config->getString("view.partial_directory"),
187188
);
188189
$this->viewModelProcessor = $viewModelInit->getViewModelProcessor();
189-
$this->viewModelInitCallback = fn() => $viewModelInit->initHTMLDocument(
190+
$this->viewModelInitCallback = $this->viewModel instanceof HTMLDocument
191+
? fn() => $viewModelInit->initHTMLDocument(
190192
$this->serviceContainer->get(Binder::class),
191193
$this->serviceContainer->get(HTMLAttributeBinder::class),
192194
$this->serviceContainer->get(ListBinder::class),
@@ -196,7 +198,8 @@ public function __construct(
196198
$this->serviceContainer->get(HTMLAttributeCollection::class),
197199
$this->serviceContainer->get(ListElementCollection::class),
198200
$this->serviceContainer->get(BindableCache::class),
199-
);
201+
)
202+
: fn() => null;
200203

201204
$this->logicExecutor = $logicExecutor ?? new LogicExecutor(
202205
$appNamespace,
@@ -369,12 +372,12 @@ public function processResponse(
369372
):void {
370373
$dynamicPath = $this->serviceContainer->get(DynamicPath::class);
371374

372-
$this->viewModelProcessor->processDynamicPath(
375+
$this->viewModelProcessor?->processDynamicPath(
373376
$this->viewModel,
374377
$dynamicPath,
375378
);
376379

377-
$logicAssemblyComponentList = $this->viewModelProcessor->processPartialContent(
380+
$logicAssemblyComponentList = $this->viewModelProcessor?->processPartialContent(
378381
$this->viewModel,
379382
);
380383

@@ -384,7 +387,7 @@ public function processResponse(
384387
$this->bindErrorDetails($errorThrowable);
385388
}
386389

387-
foreach($logicAssemblyComponentList as $logicAssemblyComponent) {
390+
foreach($logicAssemblyComponentList ?? [] as $logicAssemblyComponent) {
388391
$assembly = $logicAssemblyComponent->assembly;
389392
$componentElement = $logicAssemblyComponent->component;
390393
$this->serviceContainer->set($componentElement);

src/Init/RouterInit.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Gt\Dom\HTMLDocument;
55
use GT\Http\Request;
66
use GT\Http\Response;
7+
use Gt\Json\Schema\JsonDocument;
78
use Gt\Routing\Assembly;
89
use Gt\Routing\BaseRouter;
910
use Gt\Routing\Path\DynamicPath;
@@ -17,7 +18,7 @@ class RouterInit {
1718
private BaseRouter $baseRouter;
1819
private NullView|HTMLView|JSONView $view;
1920
// TODO: What other viewModels are there?
20-
private HTMLDocument $viewModel;
21+
private HTMLDocument|JsonDocument $viewModel;
2122
private DynamicPath $dynamicPath;
2223
private Assembly $viewAssembly;
2324
private Assembly $logicAssembly;
@@ -79,7 +80,7 @@ public function getView():NullView|HTMLView|JSONView {
7980
return $this->view;
8081
}
8182

82-
public function getViewModel():HTMLDocument {
83+
public function getViewModel():HTMLDocument|JsonDocument {
8384
return $this->viewModel;
8485
}
8586

src/Init/ViewModelInit.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Gt\DomTemplate\ListElementCollection;
1515
use Gt\DomTemplate\PlaceholderBinder;
1616
use Gt\DomTemplate\TableBinder;
17+
use Gt\Json\Schema\JsonDocument;
1718
use GT\WebEngine\Logic\HTMLDocumentProcessor;
1819
use GT\WebEngine\Logic\ViewModelProcessor;
1920

@@ -22,7 +23,7 @@ class ViewModelInit {
2223
private bool $initialised = false;
2324

2425
public function __construct(
25-
HTMLDocument $model,
26+
HTMLDocument|JsonDocument $model,
2627
string $componentDirectory,
2728
string $partialDirectory,
2829
) {
@@ -87,7 +88,7 @@ public function initHTMLDocument(
8788
);
8889
}
8990

90-
public function getViewModelProcessor():ViewModelProcessor {
91-
return $this->viewModelProcessor;
91+
public function getViewModelProcessor():?ViewModelProcessor {
92+
return $this->viewModelProcessor ?? null;
9293
}
9394
}

src/Service/DefaultServiceLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function __construct(
3232
$this->uniqid = uniqid(more_entropy: true);
3333
}
3434

35+
public function loadConfig():Config {
36+
return $this->config;
37+
}
38+
3539
public function loadResponseHeaders():ResponseHeaders {
3640
$response = $this->container->get(Response::class);
3741
return $response->headers;

src/View/JSONView.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22
namespace GT\WebEngine\View;
33

4+
use Gt\Json\Schema\JsonDocument;
5+
46
/**
57
* This file is just stubbed out for now, and does not need unit tests writing
68
* for it until the structured object document feature is planned out properly.
79
*/
810
class JSONView extends BaseView {
9-
public function createViewModel():JSONDocument {
10-
// TODO: Implement createViewModel() method.
11+
public function createViewModel():JsonDocument {
12+
return new JsonDocument();
1113
}
1214
}

src/View/NullView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* for it until the structured object document feature is planned out properly.
77
*/
88
class NullView extends BaseView {
9-
public function createViewModel():mixed {
9+
public function createViewModel():null {
1010
// TODO: Implement createViewModel() method.
11-
return "";
11+
return null;
1212
}
1313
}

0 commit comments

Comments
 (0)