Skip to content

Commit

Permalink
Merge pull request #11 from PhpGt/10-dynamic-url
Browse files Browse the repository at this point in the history
10 dynamic url
  • Loading branch information
g105b authored Nov 28, 2021
2 parents a9ccd01 + d495ac3 commit 5bad687
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 52 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"require": {
"php": ">=8.0",
"phpgt/typesafegetter": "^1.2.2",
"phpgt/config": "^1.1.0",
"phpgt/http": "^1",
"phpgt/config": "^v1.1.0",
"phpgt/http": "^v1",
"phpgt/servicecontainer": "1.*",
"psr/http-message": "1.*",

Expand All @@ -16,7 +16,7 @@

"require-dev": {
"phpunit/phpunit": "~9.5",
"phpstan/phpstan": "~1.1"
"phpstan/phpstan": "~1.2"
},

"license": "MIT",
Expand Down
42 changes: 21 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 1 addition & 27 deletions example/01-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,12 @@
// Request 1: A page request, as if it is sent from a web browser.
$pageRequest = new Request(
"GET",
new Uri("/shop/phone/oneplus"),
new Uri("/project"),
new RequestHeaders([
// An example accept header from Firefox when requesting a normal link:
"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
])
);
// Request 2: An API request, as if it were sent via cURL.
$apiRequest = new Request(
"GET",
new Uri("/v1/shop/item/chair"),
new RequestHeaders([
"Accept" => "application/xml,application/json",
"Authorization: token 0123456789abcdef"
])
);
// Request 3: Another request but working with text/plain for example's sake.
$greetRequest = new Request(
"POST",
new Uri("/greet/Greg"),
new RequestHeaders([
"Accept" => "text/plain"
])
);

// redirects.csv will allow simple administration of redirects by adding oldUrl
// in first column, newUrl in second. The response code that will be sent can be
Expand Down Expand Up @@ -107,16 +90,7 @@
$router->route($pageRequest);
echo "Router::route() complete", PHP_EOL;

// TEMPORARY MANUAL TESTING:
// TODO: If there is no "namespace" declaration in the top of the file,
// import it into memory and add a namespace yourself (namespace according to
// the current request).
stream_wrapper_register("gt-logic-stream", LogicStreamWrapper::class);
//$logicFilePath = "page/shop/@category/@itemName.php";
//$logicCommonFilePath = "page/shop/_common.php";
//require("gt-logic-stream://$logicFilePath");
//require("gt-logic-stream://$logicCommonFilePath");
////////////////////////////

$logicAssembly = $router->getLogicAssembly();
$viewAssembly = $router->getViewAssembly();
Expand Down
4 changes: 4 additions & 0 deletions example/project/simple-site/page/project/@projectId/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function go() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Project's Module-A index</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function go() {

}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function go() {

}
4 changes: 4 additions & 0 deletions example/project/simple-site/page/project/_common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function go() {

}
1 change: 1 addition & 0 deletions example/project/simple-site/page/project/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Project index</h1>
4 changes: 4 additions & 0 deletions example/project/simple-site/page/project/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function go() {

}
24 changes: 24 additions & 0 deletions src/Path/DynamicPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,28 @@ public function get(string $key = null):?string {

return null;
}

public function getUrl(string $viewBasePath):string {
$path = "";

foreach($this->assemblyList as $assembly) {
foreach($assembly as $path) {
$fileName = pathinfo($path, PATHINFO_FILENAME);
$ext = pathinfo($path, PATHINFO_EXTENSION);
if($ext !== "html") {
continue(2);
}

if($fileName[0] === "_") {
continue;
}

break;
}
}

$url = "/" . trim(substr($path, strlen($viewBasePath)), "/");
$url = strtok($url, ".");
return $url;
}
}
15 changes: 14 additions & 1 deletion test/phpunit/Path/DynamicPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function testGet_noKey_shouldReturnDeepest():void {
$assembly->method("current")
->willReturnOnConsecutiveCalls(
"page/shop/_common.php",
"page/shop/@category/@itemName.php",
"page/shop/_common.php",
"page/shop/@category/@itemName.php",
);
Expand All @@ -58,4 +57,18 @@ public function testGet_noKey_shouldReturnDeepest():void {
$sut = new DynamicPath("/shop/OnePlus/6T", $assembly);
self::assertEquals("6T", $sut->get());
}

public function testGetUrl():void {
$assembly = self::createMock(Assembly::class);
$assembly->method("current")
->willReturnOnConsecutiveCalls(
"page/_header.html",
"page/_footer.html",
"page/shop/@category/@itemName.html",
);
$assembly->method("valid")
->willReturn(true);
$sut = new DynamicPath("/shop/OnePlus/6T", $assembly);
self::assertSame("/shop/@category/@itemName", $sut->getUrl("page/"));
}
}

0 comments on commit 5bad687

Please sign in to comment.