Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions build.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,5 @@
"command": "vendor/bin/sync",
"arguments": ["asset/", "www/asset", "--delete", "--symlink"]
}
},

"data/**/*": {
"require": {
"vendor/bin/sync": "*"
},
"execute": {
"command": "vendor/bin/sync",
"arguments": ["data/", "www/data", "--delete", "--symlink"]
}
}
}
1 change: 1 addition & 0 deletions src/Dispatch/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ public function processResponse(

$componentList = $this->viewModelProcessor?->processPartialContent(
$this->viewModel,
$this->viewAssembly,
);

// TODO: CSRF handling - needs to be done on any POST request.
Expand Down
60 changes: 60 additions & 0 deletions src/Logic/HTMLDocumentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GT\DomTemplate\PartialExpander;
use GT\Routing\Assembly;
use GT\Routing\Path\DynamicPath;
use GT\WebEngine\View\HeaderFooterPartialConflictException;

class HTMLDocumentProcessor extends ViewModelProcessor {
function processDynamicPath(
Expand All @@ -30,7 +31,16 @@ function processDynamicPath(

function processPartialContent(
HTMLDocument $model,
?Assembly $viewAssembly = null,
):LogicAssemblyComponentList {
if($viewAssembly
&& $this->containsPartialExtends($model)
&& $this->containsHeaderOrFooterView($viewAssembly)) {
throw new HeaderFooterPartialConflictException(
"Header/footer view files cannot be combined with partial views."
);
}

$componentList = new LogicAssemblyComponentList();

try {
Expand Down Expand Up @@ -81,4 +91,54 @@ function processPartialContent(

return $componentList;
}

private function containsHeaderOrFooterView(Assembly $viewAssembly):bool {
foreach($viewAssembly as $viewFile) {
$fileName = pathinfo($viewFile, PATHINFO_FILENAME);
if($fileName === "_header" || $fileName === "_footer") {
return true;
}
}

return false;
}

private function containsPartialExtends(HTMLDocument $model):bool {
return $this->containsPartialExtendsInNode($model->documentElement);
}

/** @return ?array<string, array<string, string>|string> */
private function parseCommentIni(string $data):?array {
set_error_handler(
static fn() => true
);

try {
$parsed = parse_ini_string($data, true);
}
finally {
restore_error_handler();
}

return is_array($parsed)
? $parsed
: null;
}

private function containsPartialExtendsInNode(\DOMNode $node):bool {
if($node->nodeType === XML_COMMENT_NODE) {
$parsed = $this->parseCommentIni(trim($node->textContent));
if(isset($parsed["extends"])) {
return true;
}
}

foreach($node->childNodes as $childNode) {
if($this->containsPartialExtendsInNode($childNode)) {
return true;
}
}

return false;
}
}
3 changes: 2 additions & 1 deletion src/Logic/ViewModelProcessor.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace GT\WebEngine\Logic;

use Generator;
use GT\Dom\HTMLDocument;
use GT\Routing\Assembly;
use GT\Routing\Path\DynamicPath;

abstract class ViewModelProcessor {
Expand All @@ -18,5 +18,6 @@ abstract function processDynamicPath(

abstract function processPartialContent(
HTMLDocument $model,
?Assembly $viewAssembly = null,
):LogicAssemblyComponentList;
}
6 changes: 6 additions & 0 deletions src/View/HeaderFooterPartialConflictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace GT\WebEngine\View;

use GT\WebEngine\WebEngineException;

class HeaderFooterPartialConflictException extends WebEngineException {}
141 changes: 141 additions & 0 deletions test/phpunit/DefaultRouterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
namespace GT\WebEngine\Test;

use GT\Dom\HTMLDocument;
use GT\DomTemplate\ComponentExpander;
use GT\DomTemplate\PartialContent;
use GT\DomTemplate\PartialContentDirectoryNotFoundException;
use GT\DomTemplate\PartialExpander;
use Gt\Http\Request;
use Gt\Http\Stream;
use Gt\Http\Uri;
use GT\WebEngine\Logic\HTMLDocumentProcessor;
use GT\Routing\RouterConfig;
use GT\WebEngine\DefaultRouter;
use Gt\ServiceContainer\Container;
use GT\WebEngine\View\HeaderFooterPartialConflictException;
use GT\WebEngine\View\HTMLView;
use PHPUnit\Framework\TestCase;

require_once dirname(__DIR__, 2) . "/router.default.php";

class DefaultRouterTest extends TestCase {
private string $tmpDir;
private string $cwd;

protected function setUp():void {
parent::setUp();
$this->cwd = getcwd();
$this->tmpDir = sys_get_temp_dir() . "/phpgt-webengine-test--DefaultRouter-" . uniqid();
mkdir($this->tmpDir . "/page/admin", recursive: true);
}

protected function tearDown():void {
chdir($this->cwd);
$this->removeDirectory($this->tmpDir);
parent::tearDown();
}

public function testRoute_pageRequest_includesHeadersAndFootersInNestedOrder():void {
file_put_contents($this->tmpDir . "/page/_header.html", "<html><body><header>site</header>");
file_put_contents($this->tmpDir . "/page/admin/_header.html", "<nav>admin</nav>");
file_put_contents($this->tmpDir . "/page/admin/users.html", "<main>users</main>");
file_put_contents($this->tmpDir . "/page/admin/_footer.html", "<footer>admin</footer>");
file_put_contents($this->tmpDir . "/page/_footer.html", "<footer>site</footer></body></html>");

chdir($this->tmpDir);

$request = self::createMock(Request::class);
$request->method("getMethod")->willReturn("GET");
$request->method("getHeaderLine")
->with("accept")
->willReturn("text/html");
$request->method("getUri")->willReturn(new Uri("https://example.test/admin/users"));

$sut = new DefaultRouter(new RouterConfig(307, "text/html"));
$container = new Container();
$container->set($request);
$sut->setContainer($container);
$sut->route($request);

self::assertSame(
[
"page/_header.html",
"page/admin/_header.html",
"page/admin/users.html",
"page/admin/_footer.html",
"page/_footer.html",
],
iterator_to_array($sut->getViewAssembly()),
);
}

public function testRoute_pageRequest_withHeadersFootersAndPartials_throwsLogicException():void {
class_exists(HTMLDocument::class);
class_exists(ComponentExpander::class);
class_exists(PartialContent::class);
class_exists(PartialContentDirectoryNotFoundException::class);
class_exists(PartialExpander::class);

file_put_contents($this->tmpDir . "/page/_header.html", "<html><body><header>site</header>");
file_put_contents($this->tmpDir . "/page/admin/_header.html", "<nav>admin</nav>");
file_put_contents($this->tmpDir . "/page/admin/users.html", "<!-- extends=layout --><main>users</main>");
file_put_contents($this->tmpDir . "/page/admin/_footer.html", "<footer>admin</footer>");
file_put_contents($this->tmpDir . "/page/_footer.html", "<footer>site</footer></body></html>");
mkdir($this->tmpDir . "/page/_partial", recursive: true);
file_put_contents(
$this->tmpDir . "/page/_partial/layout.html",
"<!doctype html><html><body><section data-partial></section></body></html>",
);

chdir($this->tmpDir);

$request = self::createMock(Request::class);
$request->method("getMethod")->willReturn("GET");
$request->method("getHeaderLine")
->with("accept")
->willReturn("text/html");
$request->method("getUri")->willReturn(new Uri("https://example.test/admin/users"));

$sut = new DefaultRouter(new RouterConfig(307, "text/html"));
$container = new Container();
$container->set($request);
$sut->setContainer($container);
$sut->route($request);

$view = new HTMLView(new Stream());
foreach($sut->getViewAssembly() as $viewFile) {
$view->addViewFile($viewFile);
}
$viewModel = $view->createViewModel();

$processor = new HTMLDocumentProcessor("components", "page/_partial");
$this->expectException(HeaderFooterPartialConflictException::class);
$this->expectExceptionMessage(
"Header/footer view files cannot be combined with partial views."
);
$processor->processPartialContent($viewModel, $sut->getViewAssembly());
}

private function removeDirectory(string $dir):void {
if(!is_dir($dir)) {
return;
}

foreach(scandir($dir) ?: [] as $file) {
if($file === "." || $file === "..") {
continue;
}

$path = $dir . DIRECTORY_SEPARATOR . $file;
if(is_dir($path)) {
$this->removeDirectory($path);
continue;
}

unlink($path);
}

rmdir($dir);
}
}
Loading