refactor sitemap generation and navigation handling; remove old GenerateSitemap command, update to use tasks for better organization, and enhance navigation data retrieval
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use Spatie\Sitemap\Sitemap;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
|
||||
class AddUrlsToSitemapTask
|
||||
{
|
||||
public function run(Sitemap $sitemap, $items, callable $callback)
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$urlData = $callback($item);
|
||||
$url = route($urlData['route'], $urlData['params']);
|
||||
$sitemap->add(Url::create($url)
|
||||
->setLastModificationDate($urlData['lastModificationDate'])
|
||||
->setPriority($urlData['priority']));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FindPageByPathTask
|
||||
{
|
||||
public function run(string $path): ?Page
|
||||
{
|
||||
$cacheKey = 'page_' . md5($path);
|
||||
|
||||
return Cache::remember($cacheKey, now()->addHours(48), function () use ($path) {
|
||||
return Page::where('path', '=', $path)
|
||||
->with('section.pages.section', 'section.mainSection')
|
||||
->first();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use App\Containers\AppStructure\Tasks\FindPageByPathTask;
|
||||
use App\Containers\AppStructure\Tasks\GeneratePathTask;
|
||||
use App\Containers\AppStructure\Tasks\GetIndexRouteNameTask;
|
||||
use App\Ship\Resources\Breadcrumb\ClientBreadcrumbPage;
|
||||
use App\Ship\Resources\Breadcrumb\ClientBreadcrumbSection;
|
||||
use App\Ship\Resources\Breadcrumb\ClientBreadcrumbSubSection;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class GenerateBreadcrumbsTask
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GeneratePathTask $generatePathTask,
|
||||
private readonly GetIndexRouteNameTask $getIndexRouteNameTask,
|
||||
private readonly FindPageByPathTask $findPageByPathTask
|
||||
){}
|
||||
|
||||
public function run(?string $routeN = null): ?array
|
||||
{
|
||||
$routeName = $routeN ?? Route::currentRouteName();
|
||||
|
||||
$indexRouteName = $this->getIndexRouteNameTask->run($routeName);
|
||||
|
||||
$finalRouteName = Route::has($indexRouteName) ? $indexRouteName : $routeName;
|
||||
|
||||
$path = $this->generatePathTask->run($finalRouteName);
|
||||
|
||||
if ($path === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$page = $this->findPageByPathTask->run($path);
|
||||
|
||||
if (!$page?->section) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'mainSection' => new ClientBreadcrumbSection($page->section->mainSection),
|
||||
'subSection' => new ClientBreadcrumbSubSection($page->section),
|
||||
'page' => new ClientBreadcrumbPage($page),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class GeneratePathTask
|
||||
{
|
||||
public function run(?string $routeName): string|null
|
||||
{
|
||||
if ($routeName === 'page.view') {
|
||||
return request()->path();
|
||||
}
|
||||
|
||||
try {
|
||||
$route = Route::getRoutes()->getByName($routeName);
|
||||
|
||||
// Если у маршрута есть обязательные параметры без значений по умолчанию, возвращаем null
|
||||
if ($route && count($route->parameterNames()) > 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$routeUrl = route($routeName);
|
||||
return ltrim(parse_url($routeUrl, PHP_URL_PATH), '/');
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducation;
|
||||
|
||||
class GetAdditionalEducationsForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return AdditionalEducation::query()
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\MainSection;
|
||||
use App\Containers\AppStructure\UI\API\Transformers\NavigationResource;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetCachedNavigationDataTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Cache::remember('navigation', now()->addHours(1), function () {
|
||||
return NavigationResource::collection(
|
||||
MainSection::with('subSections.pages.section')
|
||||
->orderBy('sort', 'asc')
|
||||
->get()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Department;
|
||||
|
||||
class GetDepartmentsForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Department::query()
|
||||
->where('is_active', true)
|
||||
->with('faculty')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Division;
|
||||
|
||||
class GetDivisionsForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Division::query()
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\Education\Models\EducationalProgram;
|
||||
use App\Ship\Enums\Education\EducationalProgramStatus;
|
||||
|
||||
class GetEducationalProgramsForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return EducationalProgram::query()
|
||||
->where('status', EducationalProgramStatus::PUBLISHED)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\Event\Models\Event;
|
||||
|
||||
class GetEventsForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$now = now()->toDateString();
|
||||
|
||||
return Event::query()
|
||||
->where('event_date_end', '>=', $now)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
|
||||
class GetFacultiesForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Faculty::query()
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
class GetIndexRouteNameTask
|
||||
{
|
||||
public function run(string $routeName = null): ?string
|
||||
{
|
||||
if ($routeName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parts = explode('.', $routeName);
|
||||
|
||||
// Если в маршруте нет точек или он уже заканчивается на index
|
||||
if (count($parts) <= 1 || end($parts) === 'index') {
|
||||
return $routeName;
|
||||
}
|
||||
|
||||
// Заменяем последнюю часть на index
|
||||
$parts[count($parts) - 1] = 'index';
|
||||
|
||||
return implode('.', $parts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\MainSection;
|
||||
|
||||
class GetNavigationDataTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return MainSection::with('subSections.pages')
|
||||
->orderBy('sort', 'asc')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
|
||||
class GetPagesForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Page::query()
|
||||
->where('is_visible', true)
|
||||
->where('code', 200)
|
||||
->where('path', '!=', null)
|
||||
->where('is_url', false)
|
||||
->where('title', '!=', null)
|
||||
->where('searchable', true)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\Article\Enums\PostStatus;
|
||||
use App\Containers\Article\Models\Post;
|
||||
|
||||
class GetPostsForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Post::query()
|
||||
->where('status', PostStatus::PUBLISHED)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\User\Models\User;
|
||||
|
||||
class GetUsersForSitemapTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return User::query()
|
||||
->whereHas('userDetail', function ($q) {
|
||||
$q->where('is_only_worker', false);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\AppStructure\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class RegisterApplicationRoutesTask
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$routes = Route::getRoutes();
|
||||
|
||||
foreach ($routes as $route) {
|
||||
if (!Page::where('path', '=', $route->uri)->where('is_registered', '=', true)->exists()) {
|
||||
Page::create([
|
||||
'path' => $route->uri,
|
||||
'is_registered' => true,
|
||||
'is_url' => false,
|
||||
'searchable' => false,
|
||||
'code' => 200,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user