diff --git a/composer.lock b/composer.lock index 55a4cfd..68a055b 100644 --- a/composer.lock +++ b/composer.lock @@ -246,12 +246,12 @@ "source": { "type": "git", "url": "https://github.com/PhpGt/ServiceContainer.git", - "reference": "7c78bbc84b7fb299078bd1715b4102f13b28beab" + "reference": "5024aa98e9700cdde2d6595237ef74fa660c83c8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PhpGt/ServiceContainer/zipball/7c78bbc84b7fb299078bd1715b4102f13b28beab", - "reference": "7c78bbc84b7fb299078bd1715b4102f13b28beab", + "url": "https://api.github.com/repos/PhpGt/ServiceContainer/zipball/5024aa98e9700cdde2d6595237ef74fa660c83c8", + "reference": "5024aa98e9700cdde2d6595237ef74fa660c83c8", "shasum": "" }, "require": { @@ -287,7 +287,7 @@ "type": "github" } ], - "time": "2021-06-02T14:34:04+00:00" + "time": "2021-06-14T15:17:29+00:00" }, { "name": "phpgt/typesafegetter", @@ -1023,16 +1023,16 @@ }, { "name": "phpstan/phpstan", - "version": "0.12.88", + "version": "0.12.89", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "464d1a81af49409c41074aa6640ed0c4cbd9bb68" + "reference": "54c0f5a6c30511b77128d58b6369f718df250542" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/464d1a81af49409c41074aa6640ed0c4cbd9bb68", - "reference": "464d1a81af49409c41074aa6640ed0c4cbd9bb68", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/54c0f5a6c30511b77128d58b6369f718df250542", + "reference": "54c0f5a6c30511b77128d58b6369f718df250542", "shasum": "" }, "require": { @@ -1063,13 +1063,17 @@ "description": "PHPStan - PHP Static Analysis Tool", "support": { "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/0.12.88" + "source": "https://github.com/phpstan/phpstan/tree/0.12.89" }, "funding": [ { "url": "https://github.com/ondrejmirtes", "type": "github" }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, { "url": "https://www.patreon.com/phpstan", "type": "patreon" @@ -1079,7 +1083,7 @@ "type": "tidelift" } ], - "time": "2021-05-17T12:24:49+00:00" + "time": "2021-06-09T20:23:49+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1401,16 +1405,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.4", + "version": "9.5.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "c73c6737305e779771147af66c96ca6a7ed8a741" + "reference": "89ff45ea9d70e35522fb6654a2ebc221158de276" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c73c6737305e779771147af66c96ca6a7ed8a741", - "reference": "c73c6737305e779771147af66c96ca6a7ed8a741", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/89ff45ea9d70e35522fb6654a2ebc221158de276", + "reference": "89ff45ea9d70e35522fb6654a2ebc221158de276", "shasum": "" }, "require": { @@ -1440,7 +1444,7 @@ "sebastian/global-state": "^5.0.1", "sebastian/object-enumerator": "^4.0.3", "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^2.3", + "sebastian/type": "^2.3.2", "sebastian/version": "^3.0.2" }, "require-dev": { @@ -1488,7 +1492,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.4" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.5" }, "funding": [ { @@ -1500,7 +1504,7 @@ "type": "github" } ], - "time": "2021-03-23T07:16:29+00:00" + "time": "2021-06-05T04:49:07+00:00" }, { "name": "sebastian/cli-parser", @@ -2008,16 +2012,16 @@ }, { "name": "sebastian/global-state", - "version": "5.0.2", + "version": "5.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "a90ccbddffa067b51f574dea6eb25d5680839455" + "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/a90ccbddffa067b51f574dea6eb25d5680839455", - "reference": "a90ccbddffa067b51f574dea6eb25d5680839455", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/23bd5951f7ff26f12d4e3242864df3e08dec4e49", + "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49", "shasum": "" }, "require": { @@ -2060,7 +2064,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.2" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.3" }, "funding": [ { @@ -2068,7 +2072,7 @@ "type": "github" } ], - "time": "2020-10-26T15:55:19+00:00" + "time": "2021-06-11T13:31:12+00:00" }, { "name": "sebastian/lines-of-code", @@ -2359,16 +2363,16 @@ }, { "name": "sebastian/type", - "version": "2.3.1", + "version": "2.3.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/type.git", - "reference": "81cd61ab7bbf2de744aba0ea61fae32f721df3d2" + "reference": "0d1c587401514d17e8f9258a27e23527cb1b06c1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/81cd61ab7bbf2de744aba0ea61fae32f721df3d2", - "reference": "81cd61ab7bbf2de744aba0ea61fae32f721df3d2", + "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/0d1c587401514d17e8f9258a27e23527cb1b06c1", + "reference": "0d1c587401514d17e8f9258a27e23527cb1b06c1", "shasum": "" }, "require": { @@ -2403,7 +2407,7 @@ "homepage": "https://github.com/sebastianbergmann/type", "support": { "issues": "https://github.com/sebastianbergmann/type/issues", - "source": "https://github.com/sebastianbergmann/type/tree/2.3.1" + "source": "https://github.com/sebastianbergmann/type/tree/2.3.2" }, "funding": [ { @@ -2411,7 +2415,7 @@ "type": "github" } ], - "time": "2020-10-26T13:18:59+00:00" + "time": "2021-06-04T13:02:07+00:00" }, { "name": "sebastian/version", diff --git a/example/01-test.php b/example/01-test.php index 67be37b..543d4d6 100644 --- a/example/01-test.php +++ b/example/01-test.php @@ -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"); @@ -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); @@ -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 .= "\\"; @@ -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; diff --git a/example/project/simple-site/app-router.php b/example/project/simple-site/app-router.php index b27ce70..4329180 100644 --- a/example/project/simple-site/app-router.php +++ b/example/project/simple-site/app-router.php @@ -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); } } @@ -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); } } diff --git a/example/project/simple-site/page/shop/@category/@itemName.html b/example/project/simple-site/page/shop/@category/@itemName.html new file mode 100644 index 0000000..b747fc0 --- /dev/null +++ b/example/project/simple-site/page/shop/@category/@itemName.html @@ -0,0 +1 @@ +

This is an item. Its name is ITEM NAME

diff --git a/example/project/simple-site/page/shop/@category/@itemName.php b/example/project/simple-site/page/shop/@category/@itemName.php index c379f4d..cf19b3d 100644 --- a/example/project/simple-site/page/shop/@category/@itemName.php +++ b/example/project/simple-site/page/shop/@category/@itemName.php @@ -1,6 +1,7 @@ get("itemName"), PHP_EOL; } diff --git a/example/project/simple-site/page/shop/@category/index.php b/example/project/simple-site/page/shop/@category/index.php new file mode 100644 index 0000000..f9a052c --- /dev/null +++ b/example/project/simple-site/page/shop/@category/index.php @@ -0,0 +1,5 @@ +