From 973cd6687026ef2a4d9e1139cc99f229772c614e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 3 Mar 2023 17:43:17 +0100 Subject: [PATCH] Should fix #1 and fix #2 also allows optionnal parameters --- src/Lib/SitemapGenerator.php | 76 +++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/Lib/SitemapGenerator.php b/src/Lib/SitemapGenerator.php index aa3efdf..5f47ece 100644 --- a/src/Lib/SitemapGenerator.php +++ b/src/Lib/SitemapGenerator.php @@ -121,53 +121,39 @@ final class SitemapGenerator */ private function handleRoute(\Illuminate\Routing\Route $route, array &$generatedUris): void { - /** @var array to be populated with models from DB to generate a route */ - $rParams = []; - /** @var array<\ReflectionParameter> parameters signature from the controller */ $params = $route->signatureParameters(); /** @var array parameters name from the registered route */ $pNames = $route->parameterNames(); - $hasModels = false; - - $rParams = $this->handleParams($params, $pNames, $route, $hasModels); - - // If the route has no dynamic models as parameters. - if (!count($params) or !$hasModels) { - $rName = $route->getName(); - $generatedUris[] = [ - route($route->getName()), - self::getRouteFrequency($rName), - self::getRoutePriority($rName) - ]; - } else { - // If the route has dynamic models as parameters. - $this->genRoute($route, $generatedUris, $rParams); - } + $this->handleParams($params, $pNames, $route, $generatedUris); } /** * Handle all route params * - * @param array<\ReflectionParameter> $params - * @param array|null $pNames - * @param \Illuminate\Routing\Route $route - * @param boolean $hasModels - * phpcs:ignore Generic.Files.LineLength.TooLong - * @return array> List of parameters resolved. + * @param array<\ReflectionParameter> $params + * @param array|null $pNames + * @param \Illuminate\Routing\Route $route + * @param array $generatedUris + * @return void * @throws SitemapException If a class on a route is not handled. */ private function handleParams( array $params, array $pNames = null, \Illuminate\Routing\Route $route, - bool &$hasModels - ): array { + array &$generatedUris + ): void { + $hasModels = false; + $hasNullable = false; /** @var array> List of parameters resolved. */ $rParams = []; + /** @var array> List of parameters resolved. */ + $rParamsNullable = []; foreach ($params as $param) { - $name = null; + $name = null; + $hasNullable = $hasNullable ?: $param->allowsNull(); if ($paramType = $param->getType() and !$paramType->isBuiltin()) { $name = new \ReflectionClass($paramType->getName()); } @@ -183,20 +169,23 @@ final class SitemapGenerator if (config('app.debug')) { dump(\trans( - 'Route name : :routeName, Param [type: :typeName, name: :paramName, binder: :hasBinder]', + // phpcs:ignore Generic.Files.LineLength.TooLong + 'Route name : :routeName, Param [type: :typeName, name: :paramName, binder: :hasBinder, nullable: :nullable]', [ 'routeName' => $route->getName(), 'typeName' => $name ? $name->getName() : '', 'paramName' => $pName, - 'hasBinder' => $this->hasRouteBinderParam($route, $pName) ? 'true' : 'false' + 'hasBinder' => $this->hasRouteBinderParam($route, $pName) ? 'Yes' : 'No', + 'nullable' => $param->allowsNull() ? 'Yes' : 'No' ] )); } // If We have a custom binder for this route. if ($this->hasRouteBinderParam($route, $pName)) { - $hasModels = true; - $rParams += $this->processWithRouteBinderParam($route, $pName); + $hasModels = true; + $rParams += $this->processWithRouteBinderParam($route, $pName); + $rParamsNullable += $param->allowsNull() ? [$pName => null] : $this->processWithRouteBinderParam($route, $pName); continue; } @@ -204,12 +193,29 @@ final class SitemapGenerator if ($name and $name->isSubclassOf(Model::class)) { $hasModels = true; /** @var \string */ - $modelClassName = $name->getName(); - $rParams += $this->setParamsForModel($modelClassName, $pName); + $modelClassName = $name->getName(); + $rParams += $this->setParamsForModel($modelClassName, $pName); + $rParamsNullable += $param->allowsNull() ? [$pName => null] : $this->setParamsForModel($modelClassName, $pName); continue; } } //end foreach - return $rParams; + + + // If the route has no dynamic models as parameters. + if (!count($params) or !$hasModels) { + $rName = $route->getName(); + $generatedUris[] = [ + route($route->getName()), + self::getRouteFrequency($rName), + self::getRoutePriority($rName) + ]; + return; + } + // If the route has dynamic models as parameters. + $this->genRoute($route, $generatedUris, $rParams); + if ($hasNullable) { + $this->genRoute($route, $generatedUris, $rParamsNullable); + } } /**