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
17 changes: 14 additions & 3 deletions src/Logic/LogicProjectNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ public function __toString():string {
$str = str_replace("/", "\\", $this->path);
$str = $this->namespacePrefix . "\\" . $str;
$str = strtok($str, ".");
$str = str_replace(["-", "@"], " ", $str);
$namespace = "";
foreach(explode("\\", $str) as $part) {
$part = ucwords($part);
$namespace .= "\\";
$namespace .= str_replace(" ", "", $part);
$namespace .= $this->pathPartToClassPart($part);
}
$namespace = trim($namespace, "\\");
$namespace .= "Page";
return $namespace;
}

private function pathPartToClassPart(string $part):string {
$dynamicPrefix = "";
while(str_starts_with($part, "@")) {
$dynamicPrefix .= "_";
$part = substr($part, 1);
}

$part = str_replace("-", " ", $part);
$part = ucwords($part);
$part = str_replace(" ", "", $part);
return $dynamicPrefix . $part;
}
}
26 changes: 26 additions & 0 deletions test/phpunit/Logic/LogicExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ public function testInvoke_executesClassMethodFromProjectNamespace():void {
self::assertSame(["$relativePath::go()"], $invoked);
}

public function testLogicProjectNamespace_preservesDynamicMarkers():void {
self::assertSame(
"Example\\App\\Page\\Shop\\_Category\\_ItemPage",
(string)new LogicProjectNamespace(
"page/shop/@category/@item.php",
"Example\\App",
),
);

self::assertSame(
"Example\\App\\Page\\Shop\\_Category\\ItemPage",
(string)new LogicProjectNamespace(
"page/shop/@category/item.php",
"Example\\App",
),
);

self::assertSame(
"Example\\App\\Page\\Shop\\Category\\ItemPage",
(string)new LogicProjectNamespace(
"page/shop/category/item.php",
"Example\\App",
),
);
}

public function testInvoke_executesProjectNamespacedFunctionWhenNoClassExists():void {
$relativePath = $this->createLogicFile("FunctionPage.php", "<?php\n");
$projectNamespace = (string)(new LogicProjectNamespace($relativePath, "Example\\App"));
Expand Down
Loading