Merge branch 'upgrade-sitemap-refresh' into 'develop'

Upgrade sitemap refresh

Closes #1

See merge request service-web-lab/boutique-cnc-sable!9
This commit is contained in:
Vincent BRIDIER 2022-11-04 17:46:26 +00:00
commit b1aa59d7d4
6 changed files with 183 additions and 439 deletions

View file

@ -1,37 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Jobs\RefreshSiteMap as JobsRefreshSiteMap;
use Illuminate\Console\Command;
class RefreshSiteMap extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sitemap:refresh';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run the RefreshSiteMapJob manually';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info('Running the Job, this could take some time.');
$job = new JobsRefreshSiteMap();
$job->console = $this;
$job->handle();
$this->info('Job completed');
}
}

View file

@ -24,7 +24,6 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('sitemap:refresh')->dailyAt('16:13');
$schedule->command('ldcweb:statuschecker')->everyFifteenMinutes();
$schedule->job(JobsXmlIncoReader::class)->name('xml-inco-reader')->onOneServer()->dailyAt('15:00');
}

View file

@ -1,400 +0,0 @@
<?php
namespace App\Jobs;
use App\Models\Range;
use App\Models\Product;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Routing\Route as RoutingRoute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Kwaadpepper\Enum\BaseEnum;
use ReflectionClass;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class RefreshSiteMap implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* Uri that start with something in
* this table will be ignored
*
* @var array
*/
protected $ignoreRoutes = [
'/login',
'/bo',
'/activer',
'/reinitialiser-mdp',
// Hack ignore laravel debug bar.
'/_debugbar',
// Ignore ignition route.
'_ignition/health-check',
// Ignore ignition route.
'_ignition/scripts/{script}',
// Ignore ignition route.
'_ignition/styles/{style}'
];
/**
* liste des routes à binder
*
* @var array
*/
protected $routesBinder = [
'range' => [
'rangeSlug' => [Range::class, 'slug']
],
'product' => [
'productSlug' => [Product::class, 'slug']
],
];
/**
* Execute the job.
*
* @return void
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
public function handle()
{
// @phpcs:enable
$this->assertRouteBinderIsCorrect();
$siteMap = [];
$routeCollection = Route::getRoutes();
/** @var \Illuminate\Routing\Route $route */
foreach ($routeCollection as $route) {
if ($this->routeShouldBeIgnored($route)) {
continue;
}
if (!$this->routeHandlesGetMethod($route)) {
continue;
}
/** @var array to be populated with models from DB to generate a route */
$rParams = [];
/** @var \array [lluminate\Routing\Route::signatureParameters] parameters signature from the controller */
$params = $route->signatureParameters();
/** @var \array [Illuminate\Routing\Route::parameterNames] parameters name from the registered route */
$pNames = $route->parameterNames();
$hasModels = false;
foreach ($params as $param) {
/** @var ReflectionClass|null $name */
$name = $param->getType() && !$param->getType()->isBuiltin()
? new ReflectionClass($param->getType()->getName())
: null;
/** @var string $pName $parameter name on route uri */
$pName = '';
// If the route has parameters and the controller accepts it.
if (count($pNames) and \array_key_exists($param->getPosition(), $pNames)) {
// Get the controller parameter name for the route.
$pName = $pNames[$param->getPosition()];
}
// If We have a custom binder for this route.
if ($this->hasRouteBinderParam($route, $pName)) {
$hasModels = true;
$this->processWithRouteBinderParam($rParams, $route, $pName);
continue;
}
// If This route takes a model as param.
if ($name and $name->isSubclassOf(Model::class)) {
$hasModels = true;
/** @var \string */
$modelClassName = $name->getName();
$this->setParamsForModel($rParams, $modelClassName, $pName);
continue;
}
}//end foreach
// If the route has no dynamic models as parameters.
if (!count($params) or !$hasModels) {
$siteMap[] = [
route($route->getName()),
Url::CHANGE_FREQUENCY_MONTHLY,
0.1
];
} else {
// If the route has dynamic models as parameters.
$this->genRoute($route, $siteMap, $rParams);
}
}//end foreach
$sm = Sitemap::create();
foreach ($siteMap as $smRules) {
$sm->add(Url::create($smRules[0])
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency($smRules[1])
->setPriority($smRules[2]));
}
$sm->writeToFile(\public_path('sitemap.xml'));
}
/**
* Process route with binderParam
*
* @param array $rParams
* @param RoutingRoute $route
* @param string $pName
* @return void
* @throws Exception When unhandled class.
*/
private function processWithRouteBinderParam(array &$rParams, RoutingRoute $route, string $pName)
{
$o = $this->getRouteBinderParam($route, $pName);
/** @var \string */
$modelClassName = $o[0];
/** @var \string */
$routeParamName = $o[1];
$rfLCls = (new ReflectionClass($modelClassName));
switch (true) {
case $rfLCls->isSubclassOf(Model::class):
$this->setParamsForModel($rParams, $modelClassName, $pName, $routeParamName);
break;
case $rfLCls->isSubclassOf(BaseEnum::class):
$this->setParamForEnum($rParams, $rfLCls->getName(), $pName);
break;
default:
throw new Exception(sprintf('Unhandled class type %s', $rfLCls->getName()));
}
}
/**
* Set route param Array for Enums
*
* @param array $rParams
* @param string $enumClassName
* @param string $pName
* @return void
*/
private function setParamForEnum(array &$rParams, string $enumClassName, string $pName)
{
foreach (forward_static_call(sprintf('%s::toArray', $enumClassName)) as $enum) {
if (!array_key_exists($pName, $rParams)) {
$rParams[$pName] = [];
}
$rParams[$pName][] = $enum->value;
}
}
/**
* Set route param Array for Models
*
* @param array $rParams
* @param string $modelClassName
* @param string $pName
* @param string $routeParamName
* @return void
*/
private function setParamsForModel(
array &$rParams,
string $modelClassName,
string $pName,
string $routeParamName = null
) {
$i = 0;
$limit = 10;
$modelTable = (new ReflectionClass($modelClassName))->newInstanceWithoutConstructor()->getTable();
$q = $modelClassName::query();
$q = Schema::hasColumn($modelTable, 'status') ? $q->where('status', true) : $q;
$q = Schema::hasColumn($modelTable, 'star_moment') ? $q->where('star_moment', true) : $q;
do {
$results = $q->offset($i * $limit)->limit($limit)->get();
$length = count($results);
$results->map(function (Model $model) use (&$rParams, $pName, $routeParamName) {
if (!array_key_exists($pName, $rParams)) {
$rParams[$pName] = [];
}
if ($routeParamName) {
$rParams[$pName][] = $model[$routeParamName];
} else {
$rParams[$pName][] = $model;
}
});
$i++;
} while ($length);
}
/**
* Recursively generate routes for all params
*
* @param RoutingRoute $route
* @param array $siteMap
* @param array $params
* @return void
*/
private function genRoute(RoutingRoute $route, array &$siteMap, array $params)
{
$filtered = false;
foreach ($params as $pName => $param) {
if (is_array($param)) {
collect($param)->map(function ($p) use ($route, &$siteMap, $params, $pName) {
$this->genRoute($route, $siteMap, array_merge($params, [$pName => $p]));
});
return;
}
$filtered = array_key_last($params) === $pName ? true : false;
}
if ($filtered) {
$siteMap[] = [
route($route->getName(), $params),
Url::CHANGE_FREQUENCY_DAILY,
0.1
];
}
}
/**
* Check if $route exists in routesBinder
*
* @param RoutingRoute $route
* @return boolean
*/
private function hasRouteBinder(RoutingRoute $route): bool
{
return array_key_exists($route->getName(), $this->routesBinder);
}
/**
* Check if $route exists in routesBinder and has $param
*
* @param RoutingRoute $route
* @param string $param
* @return boolean
*/
private function hasRouteBinderParam(RoutingRoute $route, string $param): bool
{
if ($this->hasRouteBinder($route)) {
return array_key_exists($param, $this->getRouteBinder($route));
}
return false;
}
/**
* Get the routeBinder
*
* @param RoutingRoute $route
* @return array
*/
private function getRouteBinder(RoutingRoute $route): array
{
return $this->routesBinder[$route->getName()];
}
/**
* Get the routeBinder param
*
* @param RoutingRoute $route
* @param string $param
* @return array
*/
private function getRouteBinderParam(RoutingRoute $route, string $param): array
{
return $this->routesBinder[$route->getName()][$param];
}
/**
* Assert that routeBinder is correct
*
* @throws Exception When route does not exist | incomplrte parameters | parameter is not a model class.
* @return void
*/
private function assertRouteBinderIsCorrect()
{
/** @var \Illuminate\Routing\RouteCollection */
$routeCollection = Route::getRoutes();
foreach ($this->routesBinder as $routeName => $params) {
if (!$routeCollection->hasNamedRoute($routeName)) {
throw new Exception(sprintf('Route %s does not exists', $routeName));
}
foreach ($params as $pName => $p) {
if (count($p) != 2) {
throw new Exception(sprintf(
'Route %s parameter %s does not have Model classname and parameter name',
$routeName,
$pName
));
}
$rfCls = (new ReflectionClass($p[0]))->newInstanceWithoutConstructor();
if (is_subclass_of($rfCls, BaseEnum::class)) {
return;
}
if (!is_subclass_of($rfCls, Model::class)) {
throw new Exception(sprintf(
'Route "%s" parameter "%s" first element must be a child of "%s"',
$routeName,
$pName,
Model::class
));
}
if (!Schema::hasColumn($rfCls->getTable(), $p[1])) {
throw new Exception(sprintf(
'"%s" parameter "%s" second element "%s" must refer to and existing database column of "%s"',
$routeName,
$pName,
$p[1],
$p[0]
));
}
}//end foreach
}//end foreach
}
/**
* Check if the route handles the HTTP GET method
*
* @param RoutingRoute $route
* @return boolean
*/
private function routeHandlesGetMethod(RoutingRoute $route): bool
{
return in_array('GET', $route->methods());
}
/**
* Check if the actual route uri start with
* a partial uri from the table
* Check also if it uses middleware auth
*
* @param RoutingRoute $route
* @return boolean
*/
private function routeShouldBeIgnored(RoutingRoute $route): bool
{
foreach ($this->ignoreRoutes as $iRoute) {
if (strpos($route->uri(), ltrim($iRoute, '/')) === 0) {
return true;
}
}
foreach ($route->middleware() as $middleware) {
if (strpos($middleware, 'auth') === 0) {
return true;
}
}
return false;
}
}

View file

@ -16,6 +16,7 @@
"guzzlehttp/guzzle": "^7.0.1",
"kwaadpepper/enum": "^2.0.0",
"kwaadpepper/exception-handler": "^0.1.0",
"kwaadpepper/refresh-sitemap": "^1.0",
"laravel/framework": "^8.12",
"laravel/tinker": "^2.5",
"ldc/ldccrmv2client": "^3.0",

61
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "895507e8a27256f31399e18f2655aece",
"content-hash": "8592d7aa5a9448824833f6626cf48c54",
"packages": [
{
"name": "asm89/stack-cors",
@ -1392,6 +1392,65 @@
},
"time": "2022-08-22T15:14:23+00:00"
},
{
"name": "kwaadpepper/refresh-sitemap",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/Kwaadpepper/refresh-sitemap.git",
"reference": "003b116bff9f9531d8ca69cd1fc529878b8b54a6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Kwaadpepper/refresh-sitemap/zipball/003b116bff9f9531d8ca69cd1fc529878b8b54a6",
"reference": "003b116bff9f9531d8ca69cd1fc529878b8b54a6",
"shasum": ""
},
"require": {
"illuminate/support": "~8",
"kwaadpepper/enum": "~2",
"laravel/framework": "~8",
"spatie/laravel-sitemap": "^5.8"
},
"require-dev": {
"slevomat/coding-standard": "^7.2"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Kwaadpepper\\RefreshSitemap\\RefreshSitemapServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Kwaadpepper\\RefreshSitemap\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jérémy Munsch",
"email": "github@jeremydev.ovh",
"homepage": "https://jeremydev.ovh"
}
],
"description": "Create sitemap.xml for all routes",
"homepage": "https://github.com/kwaadpepper/refresh-sitemap",
"keywords": [
"Sitemap",
"laravel"
],
"support": {
"issues": "https://github.com/Kwaadpepper/refresh-sitemap/issues",
"source": "https://github.com/Kwaadpepper/refresh-sitemap/tree/1.0.0"
},
"time": "2022-06-10T17:46:53+00:00"
},
{
"name": "laravel/framework",
"version": "v8.83.23",

122
config/refresh-sitemap.php Normal file
View file

@ -0,0 +1,122 @@
<?php
use App\Models\Product;
use Spatie\Sitemap\Tags\Url;
return [
/**
* A list of url to ignore
* These are hard coded
* Each url does not have to be exhaustive
*
* For example, if you want to ignore all routes
* of the backoffice group
* just add '/backoffice' into this table
*/
'ignoreRoutes' => [
'/login',
'/bo',
'/activer',
'/reinitialiser-mdp',
'/gammes-de-produits',
'/gamme-de-produits',
// Hack ignore laravel debug bar.
'/_debugbar',
// Ignore ignition route.
'_ignition/health-check',
// Ignore ignition route.
'_ignition/scripts/{script}',
// Ignore ignition route.
'_ignition/styles/{style}'
],
/**
* This table is needed to resolve models matching
* routes
*
* it has to be filled like following
*
* 'products.meat-type' => [
* 'slugMeatType' => [MeatTypes::class, 'slug']
* ],
* 'kitchen-side.health-nutrition.balanced-menu.menu' => [
* 'type' => [BalancedMenuType::class, 'type']
* ]
*
* The index is the complete route name, the array content is an array
* within index is the argument name with its model class.
* This will allow the generator to generate a route for each of the models
* in this table
*/
'routesBinder' => [
'product' => [
'productSlug' => [Product::class, 'slug']
],
],
/**
* You can put route conditions here
* These will be used to decide whether the route should appear
* in the sitemap or not
*
* These are called in Eloquent where
*
* example
*
* 'queryConditions' => [
* 'status' => true,
* 'ocbStatus' => EleveurOcbStatus::yes()->value,
* 'star_moment' => true
* ]
*
*/
'queryConditions' => [],
/**
* The default route frequency to use with sitemap generator
*/
'routeDefaultFrequency' => Url::CHANGE_FREQUENCY_DAILY,
/**
* The default route priority to use with sitemap generator
*
* priority should be a number between 0 and 1
* https://www.sitemaps.org/protocol.html
*/
'routeDefaultPriority' => 0.5,
/**
* Frequencies for routes
* use the full or partial route name as index
* and a frequency within \Spatie\Sitemap\Tags\Url constants
*/
'routeFrequencies' => [],
/**
* Priorities for routes
* use the full or partial route name as index
* and a priority number between 0 and 1
*/
'routePriorities' => [],
/**
* Enable or disable schedule for the job
* If set to true the sitemap generator will be run
* using the 'cron' instruction
*/
'schedule' => true,
/**
* If you want to set specific
* instruction for when to schedule job
* you can set this with a cron instruction
* You can generate one using https://crontab.guru/
* https://laravel.com/docs/8.x/scheduling#schedule-frequency-options
*
* schedule must be set to true
*
* Default is every day at 15:45
*/
'cron' => '45 15 * * *'
];