Skip to content

Commit

Permalink
docs: basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jun 14, 2021
1 parent 495aa20 commit cfd4af5
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 59 deletions.
62 changes: 33 additions & 29 deletions composer.lock

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

90 changes: 67 additions & 23 deletions example/01-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
use Gt\Http\Request;
use Gt\Http\Uri;
use Gt\Routing\BaseRouter;
use Gt\Routing\LogicStreamWrapper;
use Gt\Routing\LogicStream\LogicStreamNamespace;
use Gt\Routing\LogicStream\LogicStreamWrapper;
use Gt\Routing\Path\DynamicPath;
use Gt\Routing\Path\FileMatch\BasicFileMatch;
use Gt\Routing\Path\FileMatch\MagicFileMatch;
use Gt\Routing\Path\PathMatcher;
use Gt\Routing\Redirects;
use Gt\ServiceContainer\Container;

require(__DIR__ . "/../vendor/autoload.php");

Expand Down Expand Up @@ -57,12 +63,29 @@
// Set the current directory to the base directory of simple-site project.
chdir(__DIR__ . "/project/simple-site");

$container = new \Gt\ServiceContainer\Container();
$container = new Container();
$container->set($pageRequest);
$dynamicPath = new DynamicPath();
$container->set($dynamicPath);
$baseAssemblyDirectory = "page";

$pathMatcher = new PathMatcher($baseAssemblyDirectory);
$pathMatcher->addFilter(function(string $filePath, string $uriPath, string $baseDir):bool {
// There are three types of matching files: Basic, Magic and Dynamic.
// Basic is where a URI matches directly to a file on disk.
// Magic is where a URI matches a PHP.Gt-specific file, like _common or _header.
// Dynamic is where a URI matches a file/directory marked as dynamic with "@".
$basicFileMatch = new BasicFileMatch($filePath, $baseDir);
if($basicFileMatch->matches($uriPath)) {
return true;
}

$magicFileMatch = new MagicFileMatch($filePath, $baseDir);
if($magicFileMatch->matches($uriPath)) {
return true;
}

$pathMatcher = new \Gt\Routing\Path\PathMatcher();
$pathMatcher->addFilter(function(string $filePath, string $uriPath):bool {
var_dump($uriPath);
return false;
});
$container->set($pathMatcher);
$injector = new \Gt\ServiceContainer\Injector($container);
Expand Down Expand Up @@ -97,9 +120,14 @@
//require("gt-logic-stream://$logicCommonFilePath");
////////////////////////////

foreach($logicAssembly = $router->getLogicAssembly() as $logicPathname) {
foreach($router->getLogicAssembly() as $logicPathname) {
echo "Loading logic class: $logicPathname", PHP_EOL;
require($logicPathname);
require("gt-logic-stream://$logicPathname");

// TODO: Build $className: it's either going to be path-matched with the application
// namespace, or it's going to be automatically generated from the gt-logic-stream.
// Need to look for either classes, load it, reference the "go", etc.

$className = $config->getSection("app")->getString("namespace");
$className .= "\\";

Expand Down Expand Up @@ -127,26 +155,42 @@
}

$className = str_replace("@", "_", $className);
$logicStreamNamespace = new LogicStreamNamespace(
$logicPathname,
LogicStreamWrapper::NAMESPACE_PREFIX
);

$class = new $className();
$goFunctionName = $logicAssembly->getFunctionName() ?? "go";
$refFunction = new ReflectionMethod($class, $goFunctionName);
$data = $logicAssembly->getData();
$injectionParameters = [];
foreach($refFunction->getParameters() as $param) {
$paramName = $param->getName();
$paramType = $param->getType()->getName();
if(!isset($data[$paramName])) {
continue;
}
if(gettype($data[$paramName]) !== $paramType
&& get_class($data[$paramName]) !== $paramType) {
continue;
$class = null;
$goFunctionName = "go";
/** @var ReflectionMethod|ReflectionFunction|null $refFunction */
$refFunction = null;

if(class_exists($className)) {
$class = new $className();
$refFunction = new ReflectionMethod($class, $goFunctionName);
}
else {
$refFunctionFQN = "$logicStreamNamespace\\$goFunctionName";

if(function_exists($refFunctionFQN)) {
$refFunction = new ReflectionFunction($refFunctionFQN);
}
array_push($injectionParameters, $data[$paramName]);
}

call_user_func([$class, $goFunctionName], ...$injectionParameters);
if(is_null($refFunction)) {
die("ERROR! Can't load go function!");
}

if($class) {
/** @var ReflectionMethod $refFunction */
$closure = $refFunction->getClosure($class);
}
else {
/** @var ReflectionFunction $refFunction */
$closure = $refFunction->getClosure();
}

$injector->invoke($class, $closure);
}
foreach($router->getViewAssembly() as $viewPathname) {
echo "Loading view part: $viewPathname", PHP_EOL;
Expand Down
25 changes: 20 additions & 5 deletions example/project/simple-site/app-router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public function api(Request $request, PathMatcher $pathMatcher):void {
echo "API ROUTE CALLBACK", PHP_EOL;
foreach($pathMatcher->findForUriPath(
$request->getUri()->getPath(),
"api",
"api/v1",
"php"
) as $logicName => $path) {
$this->addToLogicAssembly($path, $logicName);
$this->addToLogicAssembly($path);
}
}

Expand All @@ -28,12 +28,27 @@ public function page(PathMatcher $pathMatcher, Request $request):void {
// TODO: add logic and view assembly in the api directory
// (configured from $this->routerConfig)

foreach($pathMatcher->findForUriPath(
$sortNestLevelCallback = fn(string $a, string $b) =>
substr_count($a, "/") > substr_count($b, "/");

$matchingLogics = $pathMatcher->findForUriPath(
$request->getUri()->getPath(),
"page",
"php"
) as $logicName => $path) {
$this->addToLogicAssembly($path, $logicName);
);
usort($matchingLogics, $sortNestLevelCallback);
foreach($matchingLogics as $path) {
$this->addToLogicAssembly($path);
}

$matchingViews = $pathMatcher->findForUriPath(
$request->getUri()->getPath(),
"page",
"html"
);
usort($matchingViews, $sortNestLevelCallback);
foreach($matchingViews as $path) {
$this->addToViewAssembly($path);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>This is an item. Its name is <span data-bind:text="name">ITEM NAME</span></h1>
5 changes: 3 additions & 2 deletions example/project/simple-site/page/shop/@category/@itemName.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
use Gt\Http\Request;
use Gt\Routing\Path\DynamicPath;

function go(Request $request):void {
echo "SHOP ITEM!", PHP_EOL;
function go(Request $request, DynamicPath $path):void {
echo "SHOP ITEM: " . $path->get("itemName"), PHP_EOL;
}
5 changes: 5 additions & 0 deletions example/project/simple-site/page/shop/@category/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
function go():void {
echo "Category index GO!", PHP_EOL;
echo "THIS SHOULD NOT EXECUTE WHEN THERE'S A NAMED CATEGORY!", PHP_EOL;
}

0 comments on commit cfd4af5

Please sign in to comment.