From 0874d3e6b5d7c1dc4dc2ef4ec98c6a540d4defcf Mon Sep 17 00:00:00 2001 From: F4ilji Date: Fri, 18 Jul 2025 13:07:51 +0500 Subject: [PATCH] refactor sitemap generation and navigation handling; remove old GenerateSitemap command, update to use tasks for better organization, and enhance navigation data retrieval --- .gitignore | 4 +- app/Console/Commands/GenerateSitemap.php | 223 ------------------ .../AppStructure/Actions/RenderPageAction.php | 44 ++++ .../Tasks/AddUrlsToSitemapTask.php | 20 ++ .../AppStructure/Tasks/FindPageByPathTask.php | 20 ++ .../Tasks/GenerateBreadcrumbsTask.php | 48 ++++ .../AppStructure/Tasks/GeneratePathTask.php | 29 +++ .../GetAdditionalEducationsForSitemapTask.php | 15 ++ .../Tasks/GetCachedNavigationDataTask.php | 21 ++ .../Tasks/GetDepartmentsForSitemapTask.php | 16 ++ .../Tasks/GetDivisionsForSitemapTask.php | 15 ++ .../GetEducationalProgramsForSitemapTask.php | 16 ++ .../Tasks/GetEventsForSitemapTask.php | 17 ++ .../Tasks/GetFacultiesForSitemapTask.php | 15 ++ .../Tasks/GetIndexRouteNameTask.php | 25 ++ .../Tasks/GetNavigationDataTask.php | 15 ++ .../Tasks/GetPagesForSitemapTask.php | 20 ++ .../Tasks/GetPostsForSitemapTask.php | 16 ++ .../Tasks/GetUsersForSitemapTask.php | 17 ++ .../Tasks/RegisterApplicationRoutesTask.php | 26 ++ .../UI/API/Controllers/NavigateController.php | 6 +- .../UI/CLI/Commands/GenerateSitemap.php | 165 +++---------- .../UI/CLI/Commands/RegisterRoutes.php | 44 +--- .../UI/WEB/Controllers/PageController.php | 45 +--- app/Containers/Widget/Models/CustomForm.php | 3 +- app/Http/Middleware/HandleInertiaRequests.php | 30 +-- app/Ship/Middleware/HandleInertiaRequests.php | 20 +- public/.gitignore | 1 + 28 files changed, 461 insertions(+), 475 deletions(-) delete mode 100644 app/Console/Commands/GenerateSitemap.php create mode 100644 app/Containers/AppStructure/Actions/RenderPageAction.php create mode 100644 app/Containers/AppStructure/Tasks/AddUrlsToSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/FindPageByPathTask.php create mode 100644 app/Containers/AppStructure/Tasks/GenerateBreadcrumbsTask.php create mode 100644 app/Containers/AppStructure/Tasks/GeneratePathTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetAdditionalEducationsForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetCachedNavigationDataTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetDepartmentsForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetDivisionsForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetEducationalProgramsForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetEventsForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetFacultiesForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetIndexRouteNameTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetNavigationDataTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetPagesForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetPostsForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/GetUsersForSitemapTask.php create mode 100644 app/Containers/AppStructure/Tasks/RegisterApplicationRoutesTask.php diff --git a/.gitignore b/.gitignore index 3542715..613fd50 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ _deploy /public/abitur /public/sveden dump.sql -.idea \ No newline at end of file +.idea +.DS_Store +**/.DS_Store diff --git a/app/Console/Commands/GenerateSitemap.php b/app/Console/Commands/GenerateSitemap.php deleted file mode 100644 index af71b7a..0000000 --- a/app/Console/Commands/GenerateSitemap.php +++ /dev/null @@ -1,223 +0,0 @@ -generatePages($sitemap); - $this->generatePosts($sitemap); - $this->generateDivisions($sitemap); - $this->generateEducationPrograms($sitemap); // Добавляем генерацию для EducationProgram - $this->generateEvents($sitemap); // Добавляем генерацию для Event - $this->generateAdditionalEducations($sitemap); // Добавляем генерацию для AdditionalEducation - $this->generateFaculties($sitemap); // Добавляем генерацию для Faculty - $this->generateDepartments($sitemap); // Добавляем генерацию для Department - $this->generateUsers($sitemap); // Добавляем генерацию для User - - // Сохраняем карту сайта в файл - $sitemap->writeToFile(public_path('sitemap.xml')); - } - - protected function generatePages(Sitemap $sitemap) - { - $pages = Page::query() - ->where('is_visible', true) - ->where('code', 200) - ->where('path', '!=', null) - ->where('is_url', false) - ->where('title', '!=', null) - ->where('searchable', true) - ->get(); - - $this->addUrlsToSitemap($sitemap, $pages, function($page) { - return [ - 'route' => 'page.view', - 'params' => ['path' => $page->path], - 'lastModificationDate' => $page->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generatePosts(Sitemap $sitemap) - { - $posts = Post::query() - ->where('status', PostStatus::PUBLISHED) - ->get(); - - $this->addUrlsToSitemap($sitemap, $posts, function($post) { - return [ - 'route' => 'client.post.show', - 'params' => ['slug' => $post->slug], - 'lastModificationDate' => $post->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateDivisions(Sitemap $sitemap) - { - $divisions = Division::query() - ->where('is_active', true) - ->get(); - - $this->addUrlsToSitemap($sitemap, $divisions, function($division) { - return [ - 'route' => 'client.division.show', - 'params' => ['slug' => $division->slug], - 'lastModificationDate' => $division->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateEducationPrograms(Sitemap $sitemap) - { - $programs = EducationalProgram::query() - ->where('status', EducationalProgramStatus::PUBLISHED) // Пример условия для активных программ - ->get(); - - $this->addUrlsToSitemap($sitemap, $programs, function($program) { - return [ - 'route' => 'client.program.show', - 'params' => ['slug' => $program->slug], - 'lastModificationDate' => $program->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateEvents(Sitemap $sitemap) - { - $now = now()->toDateString(); // Текущая дата - - $events = Event::query() - ->where('event_date_end', '>=', $now) // Только актуальные события - ->get(); - - $this->addUrlsToSitemap($sitemap, $events, function($event) { - return [ - 'route' => 'client.event.show', - 'params' => ['slug' => $event->slug], - 'lastModificationDate' => $event->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateAdditionalEducations(Sitemap $sitemap) - { - $educations = AdditionalEducation::query() - ->where('is_active', true) // Пример условия для активных программ - ->get(); - - $this->addUrlsToSitemap($sitemap, $educations, function($education) { - return [ - 'route' => 'client.additionalEducation.show', - 'params' => ['slug' => $education->slug], - 'lastModificationDate' => $education->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateFaculties(Sitemap $sitemap) - { - $faculties = Faculty::query() - ->where('is_active', true) // Пример условия для активных факультетов - ->get(); - - $this->addUrlsToSitemap($sitemap, $faculties, function($faculty) { - return [ - 'route' => 'client.faculty.show', - 'params' => ['slug' => $faculty->slug], - 'lastModificationDate' => $faculty->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateDepartments(Sitemap $sitemap) - { - $departments = Department::query() - ->where('is_active', true) // Пример условия для активных кафедр - ->get(); - - $this->addUrlsToSitemap($sitemap, $departments, function($department) { - return [ - 'route' => 'client.department.show', - 'params' => ['facultySlug' => $department->faculty->slug, 'departmentSlug' => $department->slug], - 'lastModificationDate' => $department->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function generateUsers(Sitemap $sitemap) - { - $users = User::query() - ->whereHas('userDetail', function ($q) { - $q->where('is_only_worker', false); - }) - ->get(); - - $this->addUrlsToSitemap($sitemap, $users, function($user) { - return [ - 'route' => 'client.person.show', - 'params' => ['slug' => $user->slug], - 'lastModificationDate' => $user->updated_at, - 'priority' => 0.5, - ]; - }); - } - - protected function addUrlsToSitemap(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'])); - } - } -} diff --git a/app/Containers/AppStructure/Actions/RenderPageAction.php b/app/Containers/AppStructure/Actions/RenderPageAction.php new file mode 100644 index 0000000..3c8e578 --- /dev/null +++ b/app/Containers/AppStructure/Actions/RenderPageAction.php @@ -0,0 +1,44 @@ +findPageByPathTask->run($path); + $this->handleCheckNotFoundStatusCode($page); + $subSectionPages = $page->section ? PageResource::collection($page->section->pages) : null; + $seo = $this->seoPageProvider->getSeoForModel($page); + $pageResource = new PageResource($page); + $this->handlePageStatusCode($pageResource); + + return inertia()->render('Page', [ + 'page' => $pageResource, + 'subSectionPages' => $subSectionPages, + 'seo' => $seo, + ]); + } + + private function handlePageStatusCode(PageResource $pageResource): void + { + if ($pageResource->code != 200) { + abort($pageResource->code); + } + } + + private function handleCheckNotFoundStatusCode($page): void + { + if ($page === null) { + abort(404); + } + } +} diff --git a/app/Containers/AppStructure/Tasks/AddUrlsToSitemapTask.php b/app/Containers/AppStructure/Tasks/AddUrlsToSitemapTask.php new file mode 100644 index 0000000..6834e4b --- /dev/null +++ b/app/Containers/AppStructure/Tasks/AddUrlsToSitemapTask.php @@ -0,0 +1,20 @@ +add(Url::create($url) + ->setLastModificationDate($urlData['lastModificationDate']) + ->setPriority($urlData['priority'])); + } + } +} diff --git a/app/Containers/AppStructure/Tasks/FindPageByPathTask.php b/app/Containers/AppStructure/Tasks/FindPageByPathTask.php new file mode 100644 index 0000000..9464da9 --- /dev/null +++ b/app/Containers/AppStructure/Tasks/FindPageByPathTask.php @@ -0,0 +1,20 @@ +addHours(48), function () use ($path) { + return Page::where('path', '=', $path) + ->with('section.pages.section', 'section.mainSection') + ->first(); + }); + } +} diff --git a/app/Containers/AppStructure/Tasks/GenerateBreadcrumbsTask.php b/app/Containers/AppStructure/Tasks/GenerateBreadcrumbsTask.php new file mode 100644 index 0000000..80b3c0d --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GenerateBreadcrumbsTask.php @@ -0,0 +1,48 @@ +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), + ]; + } +} diff --git a/app/Containers/AppStructure/Tasks/GeneratePathTask.php b/app/Containers/AppStructure/Tasks/GeneratePathTask.php new file mode 100644 index 0000000..5358cfd --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GeneratePathTask.php @@ -0,0 +1,29 @@ +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; + } + } +} diff --git a/app/Containers/AppStructure/Tasks/GetAdditionalEducationsForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetAdditionalEducationsForSitemapTask.php new file mode 100644 index 0000000..134d128 --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetAdditionalEducationsForSitemapTask.php @@ -0,0 +1,15 @@ +where('is_active', true) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetCachedNavigationDataTask.php b/app/Containers/AppStructure/Tasks/GetCachedNavigationDataTask.php new file mode 100644 index 0000000..49a7bd9 --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetCachedNavigationDataTask.php @@ -0,0 +1,21 @@ +addHours(1), function () { + return NavigationResource::collection( + MainSection::with('subSections.pages.section') + ->orderBy('sort', 'asc') + ->get() + ); + }); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetDepartmentsForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetDepartmentsForSitemapTask.php new file mode 100644 index 0000000..d05de0d --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetDepartmentsForSitemapTask.php @@ -0,0 +1,16 @@ +where('is_active', true) + ->with('faculty') + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetDivisionsForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetDivisionsForSitemapTask.php new file mode 100644 index 0000000..b6bcee4 --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetDivisionsForSitemapTask.php @@ -0,0 +1,15 @@ +where('is_active', true) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetEducationalProgramsForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetEducationalProgramsForSitemapTask.php new file mode 100644 index 0000000..ccd159f --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetEducationalProgramsForSitemapTask.php @@ -0,0 +1,16 @@ +where('status', EducationalProgramStatus::PUBLISHED) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetEventsForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetEventsForSitemapTask.php new file mode 100644 index 0000000..be0b67a --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetEventsForSitemapTask.php @@ -0,0 +1,17 @@ +toDateString(); + + return Event::query() + ->where('event_date_end', '>=', $now) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetFacultiesForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetFacultiesForSitemapTask.php new file mode 100644 index 0000000..1b57b6e --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetFacultiesForSitemapTask.php @@ -0,0 +1,15 @@ +where('is_active', true) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetIndexRouteNameTask.php b/app/Containers/AppStructure/Tasks/GetIndexRouteNameTask.php new file mode 100644 index 0000000..2442b08 --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetIndexRouteNameTask.php @@ -0,0 +1,25 @@ +orderBy('sort', 'asc') + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetPagesForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetPagesForSitemapTask.php new file mode 100644 index 0000000..8416ecc --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetPagesForSitemapTask.php @@ -0,0 +1,20 @@ +where('is_visible', true) + ->where('code', 200) + ->where('path', '!=', null) + ->where('is_url', false) + ->where('title', '!=', null) + ->where('searchable', true) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetPostsForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetPostsForSitemapTask.php new file mode 100644 index 0000000..3a8bdfe --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetPostsForSitemapTask.php @@ -0,0 +1,16 @@ +where('status', PostStatus::PUBLISHED) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/GetUsersForSitemapTask.php b/app/Containers/AppStructure/Tasks/GetUsersForSitemapTask.php new file mode 100644 index 0000000..81b2f6e --- /dev/null +++ b/app/Containers/AppStructure/Tasks/GetUsersForSitemapTask.php @@ -0,0 +1,17 @@ +whereHas('userDetail', function ($q) { + $q->where('is_only_worker', false); + }) + ->get(); + } +} diff --git a/app/Containers/AppStructure/Tasks/RegisterApplicationRoutesTask.php b/app/Containers/AppStructure/Tasks/RegisterApplicationRoutesTask.php new file mode 100644 index 0000000..c5da2eb --- /dev/null +++ b/app/Containers/AppStructure/Tasks/RegisterApplicationRoutesTask.php @@ -0,0 +1,26 @@ +uri)->where('is_registered', '=', true)->exists()) { + Page::create([ + 'path' => $route->uri, + 'is_registered' => true, + 'is_url' => false, + 'searchable' => false, + 'code' => 200, + ]); + } + } + } +} diff --git a/app/Containers/AppStructure/UI/API/Controllers/NavigateController.php b/app/Containers/AppStructure/UI/API/Controllers/NavigateController.php index 33c8591..8b75eb2 100644 --- a/app/Containers/AppStructure/UI/API/Controllers/NavigateController.php +++ b/app/Containers/AppStructure/UI/API/Controllers/NavigateController.php @@ -2,14 +2,16 @@ namespace App\Containers\AppStructure\UI\API\Controllers; -use App\Containers\AppStructure\Models\MainSection; +use App\Containers\AppStructure\Tasks\GetNavigationDataTask; use App\Containers\AppStructure\UI\API\Transformers\NavigationResource; use App\Ship\Controllers\Controller; class NavigateController extends Controller { + public function __construct(private readonly GetNavigationDataTask $getNavigationDataTask){} + public function index() { - return NavigationResource::collection(MainSection::with('subSections.pages')->orderBy('sort', 'asc')->get()); + return NavigationResource::collection($this->getNavigationDataTask->run()); } } diff --git a/app/Containers/AppStructure/UI/CLI/Commands/GenerateSitemap.php b/app/Containers/AppStructure/UI/CLI/Commands/GenerateSitemap.php index e4a0641..e7f1954 100644 --- a/app/Containers/AppStructure/UI/CLI/Commands/GenerateSitemap.php +++ b/app/Containers/AppStructure/UI/CLI/Commands/GenerateSitemap.php @@ -3,72 +3,42 @@ namespace App\Containers\AppStructure\UI\CLI\Commands; use App\Ship\Abstracts\Commands\ConsoleCommand as AbstractConsoleCommand; -use App\Containers\AdditionalEducation\Models\AdditionalEducation; -use App\Containers\AppStructure\Models\Page; -use App\Containers\Article\Enums\PostStatus; -use App\Containers\Article\Models\Post; -use App\Containers\Education\Models\EducationalProgram; -use App\Containers\Event\Models\Event; -use App\Containers\InstituteStructure\Models\Department; -use App\Containers\InstituteStructure\Models\Division; -use App\Containers\InstituteStructure\Models\Faculty; -use App\Containers\User\Models\User; -use App\Ship\Enums\Education\EducationalProgramStatus; +use App\Containers\AppStructure\Tasks\GetPagesForSitemapTask; +use App\Containers\AppStructure\Tasks\GetPostsForSitemapTask; +use App\Containers\AppStructure\Tasks\GetDivisionsForSitemapTask; +use App\Containers\AppStructure\Tasks\GetEducationalProgramsForSitemapTask; +use App\Containers\AppStructure\Tasks\GetEventsForSitemapTask; +use App\Containers\AppStructure\Tasks\GetAdditionalEducationsForSitemapTask; +use App\Containers\AppStructure\Tasks\GetFacultiesForSitemapTask; +use App\Containers\AppStructure\Tasks\GetDepartmentsForSitemapTask; +use App\Containers\AppStructure\Tasks\GetUsersForSitemapTask; +use App\Containers\AppStructure\Tasks\AddUrlsToSitemapTask; use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; class GenerateSitemap extends AbstractConsoleCommand { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'sitemap:generate'; - - /** - * The console command description. - * - * @var string - */ protected $description = 'Генерирует карту сайта'; - /** - * Execute the console command. - * - * @return int - */ + public function __construct( + private readonly GetPagesForSitemapTask $getPagesForSitemapTask, + private readonly GetPostsForSitemapTask $getPostsForSitemapTask, + private readonly GetDivisionsForSitemapTask $getDivisionsForSitemapTask, + private readonly GetEducationalProgramsForSitemapTask $getEducationalProgramsForSitemapTask, + private readonly GetEventsForSitemapTask $getEventsForSitemapTask, + private readonly GetAdditionalEducationsForSitemapTask $getAdditionalEducationsForSitemapTask, + private readonly GetFacultiesForSitemapTask $getFacultiesForSitemapTask, + private readonly GetDepartmentsForSitemapTask $getDepartmentsForSitemapTask, + private readonly GetUsersForSitemapTask $getUsersForSitemapTask, + private readonly AddUrlsToSitemapTask $addUrlsToSitemapTask + ) {parent::__construct();} + public function handle() { $sitemap = Sitemap::create(); - // Генерация карты сайта для всех моделей - $this->generatePages($sitemap); - $this->generatePosts($sitemap); - $this->generateDivisions($sitemap); - $this->generateEducationPrograms($sitemap); // Добавляем генерацию для EducationProgram - $this->generateEvents($sitemap); // Добавляем генерацию для Event - $this->generateAdditionalEducations($sitemap); // Добавляем генерацию для AdditionalEducation - $this->generateFaculties($sitemap); // Добавляем генерацию для Faculty - $this->generateDepartments($sitemap); // Добавляем генерацию для Department - $this->generateUsers($sitemap); // Добавляем генерацию для User - - // Сохраняем карту сайта в файл - $sitemap->writeToFile(public_path('sitemap.xml')); - } - - protected function generatePages(Sitemap $sitemap) - { - $pages = Page::query() - ->where('is_visible', true) - ->where('code', 200) - ->where('path', '!=', null) - ->where('is_url', false) - ->where('title', '!=', null) - ->where('searchable', true) - ->get(); - - $this->addUrlsToSitemap($sitemap, $pages, function($page) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getPagesForSitemapTask->run(), function($page) { return [ 'route' => 'page.view', 'params' => ['path' => $page->path], @@ -76,15 +46,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generatePosts(Sitemap $sitemap) - { - $posts = Post::query() - ->where('status', PostStatus::PUBLISHED) - ->get(); - - $this->addUrlsToSitemap($sitemap, $posts, function($post) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getPostsForSitemapTask->run(), function($post) { return [ 'route' => 'client.post.show', 'params' => ['slug' => $post->slug], @@ -92,15 +55,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateDivisions(Sitemap $sitemap) - { - $divisions = Division::query() - ->where('is_active', true) - ->get(); - - $this->addUrlsToSitemap($sitemap, $divisions, function($division) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getDivisionsForSitemapTask->run(), function($division) { return [ 'route' => 'client.division.show', 'params' => ['slug' => $division->slug], @@ -108,15 +64,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateEducationPrograms(Sitemap $sitemap) - { - $programs = EducationalProgram::query() - ->where('status', EducationalProgramStatus::PUBLISHED) // Пример условия для активных программ - ->get(); - - $this->addUrlsToSitemap($sitemap, $programs, function($program) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getEducationalProgramsForSitemapTask->run(), function($program) { return [ 'route' => 'client.program.show', 'params' => ['slug' => $program->slug], @@ -124,17 +73,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateEvents(Sitemap $sitemap) - { - $now = now()->toDateString(); // Текущая дата - - $events = Event::query() - ->where('event_date_end', '>=', $now) // Только актуальные события - ->get(); - - $this->addUrlsToSitemap($sitemap, $events, function($event) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getEventsForSitemapTask->run(), function($event) { return [ 'route' => 'client.event.show', 'params' => ['slug' => $event->slug], @@ -142,15 +82,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateAdditionalEducations(Sitemap $sitemap) - { - $educations = AdditionalEducation::query() - ->where('is_active', true) // Пример условия для активных программ - ->get(); - - $this->addUrlsToSitemap($sitemap, $educations, function($education) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getAdditionalEducationsForSitemapTask->run(), function($education) { return [ 'route' => 'client.additionalEducation.show', 'params' => ['slug' => $education->slug], @@ -158,15 +91,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateFaculties(Sitemap $sitemap) - { - $faculties = Faculty::query() - ->where('is_active', true) // Пример условия для активных факультетов - ->get(); - - $this->addUrlsToSitemap($sitemap, $faculties, function($faculty) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getFacultiesForSitemapTask->run(), function($faculty) { return [ 'route' => 'client.faculty.show', 'params' => ['slug' => $faculty->slug], @@ -174,15 +100,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateDepartments(Sitemap $sitemap) - { - $departments = Department::query() - ->where('is_active', true) // Пример условия для активных кафедр - ->get(); - - $this->addUrlsToSitemap($sitemap, $departments, function($department) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getDepartmentsForSitemapTask->run(), function($department) { return [ 'route' => 'client.department.show', 'params' => ['facultySlug' => $department->faculty->slug, 'departmentSlug' => $department->slug], @@ -190,17 +109,8 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function generateUsers(Sitemap $sitemap) - { - $users = User::query() - ->whereHas('userDetail', function ($q) { - $q->where('is_only_worker', false); - }) - ->get(); - - $this->addUrlsToSitemap($sitemap, $users, function($user) { + $this->addUrlsToSitemapTask->run($sitemap, $this->getUsersForSitemapTask->run(), function($user) { return [ 'route' => 'client.person.show', 'params' => ['slug' => $user->slug], @@ -208,16 +118,7 @@ class GenerateSitemap extends AbstractConsoleCommand 'priority' => 0.5, ]; }); - } - protected function addUrlsToSitemap(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'])); - } + $sitemap->writeToFile(public_path('sitemap.xml')); } } diff --git a/app/Containers/AppStructure/UI/CLI/Commands/RegisterRoutes.php b/app/Containers/AppStructure/UI/CLI/Commands/RegisterRoutes.php index 8061ec6..33bd5cd 100644 --- a/app/Containers/AppStructure/UI/CLI/Commands/RegisterRoutes.php +++ b/app/Containers/AppStructure/UI/CLI/Commands/RegisterRoutes.php @@ -3,51 +3,19 @@ namespace App\Containers\AppStructure\UI\CLI\Commands; use App\Ship\Abstracts\Commands\ConsoleCommand as AbstractConsoleCommand; -use App\Containers\AppStructure\Models\Page; -use Illuminate\Support\Facades\Route; +use App\Containers\AppStructure\Tasks\RegisterApplicationRoutesTask; + class RegisterRoutes extends AbstractConsoleCommand { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'routes:register'; - - /** - * The console command description. - * - * @var string - */ protected $description = 'Register application routes in the database'; - /** - * Execute the console command. - * - * @return int - */ + public function __construct(private readonly RegisterApplicationRoutesTask $registerApplicationRoutesTask) + {parent::__construct();} + public function handle() { - $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, - ]); - - $this->info("Маршрут зарегистрирован: " . $route->uri); - } else { - $this->info("Маршрут уже существует: " . $route->uri); - } - } - + $this->registerApplicationRoutesTask->run(); $this->info('Все маршруты успешно проверены.'); } } diff --git a/app/Containers/AppStructure/UI/WEB/Controllers/PageController.php b/app/Containers/AppStructure/UI/WEB/Controllers/PageController.php index 89d7762..7f22eeb 100644 --- a/app/Containers/AppStructure/UI/WEB/Controllers/PageController.php +++ b/app/Containers/AppStructure/UI/WEB/Controllers/PageController.php @@ -2,55 +2,16 @@ namespace App\Containers\AppStructure\UI\WEB\Controllers; -use App\Containers\AppStructure\Models\Page; -use App\Containers\AppStructure\UI\WEB\Transformers\PageResource; -use App\Ship\Contracts\SeoServiceInterface; +use App\Containers\AppStructure\Actions\RenderPageAction; use App\Ship\Controllers\Controller; -use Illuminate\Support\Facades\Cache; use Inertia\Response; class PageController extends Controller { - public function __construct(readonly SeoServiceInterface $seoPageProvider){} + public function __construct(private readonly RenderPageAction $renderPageAction){} public function render(string $path): Response { - $page = $this->getPageByPath($path); - $this->handleCheckNotFoundStatusCode($page); - $subSectionPages = $page->section ? PageResource::collection($page->section->pages) : null; - $seo = $this->seoPageProvider->getSeoForModel($page); - $pageResource = new PageResource($page); - $this->handlePageStatusCode($pageResource); - - return inertia()->render('Page', [ - 'page' => $pageResource, - 'subSectionPages' => $subSectionPages, - 'seo' => $seo, - ]); - } - - private function handlePageStatusCode(PageResource $pageResource): void - { - if ($pageResource->code != 200) { - abort($pageResource->code); - } - } - - private function handleCheckNotFoundStatusCode($page): void - { - if ($page === null) { - abort(404); - } - } - - public function getPageByPath(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(); - }); + return $this->renderPageAction->run($path); } } diff --git a/app/Containers/Widget/Models/CustomForm.php b/app/Containers/Widget/Models/CustomForm.php index 38d5161..53a58ab 100644 --- a/app/Containers/Widget/Models/CustomForm.php +++ b/app/Containers/Widget/Models/CustomForm.php @@ -5,6 +5,7 @@ namespace App\Containers\Widget\Models; use App\Containers\Widget\Enums\CustomFormStatus; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; class CustomForm extends Model { @@ -19,7 +20,7 @@ class CustomForm extends Model 'settings' => 'array', ]; - public function responses() + public function responses(): HasMany { return $this->hasMany(CustomFormResponse::class, 'custom_form_id', 'id'); } diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index 5923e8b..c3274a8 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -2,11 +2,9 @@ namespace App\Http\Middleware; -use App\Containers\AppStructure\Models\MainSection; -use App\Containers\AppStructure\UI\API\Transformers\NavigationResource; -use App\Services\App\Breadcrumb\BreadcrumbService; +use App\Containers\AppStructure\Tasks\GetCachedNavigationDataTask; +use App\Containers\AppStructure\Tasks\GenerateBreadcrumbsTask; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Cache; use Inertia\Middleware; use Tightenco\Ziggy\Ziggy; @@ -14,6 +12,8 @@ class HandleInertiaRequests extends Middleware { protected $rootView = 'app'; + public function __construct(private readonly GenerateBreadcrumbsTask $generateBreadcrumbsTask){} + public function version(Request $request): ?string { return parent::version($request); @@ -31,8 +31,8 @@ class HandleInertiaRequests extends Middleware 'location' => url()->current(), ]); }, - 'navigation' => $this->getNavigation(), - 'breadcrumbs' => $this->getBreadcrumbs(), + 'navigation' => app(GetCachedNavigationDataTask::class)->run(), + 'breadcrumbs' => $this->generateBreadcrumbsTask->run(), 'urlPrev' => function () { if (url()->previous() !== url()->current()) { return url()->previous(); @@ -41,20 +41,4 @@ class HandleInertiaRequests extends Middleware }, ]; } - - private function getNavigation() - { - return Cache::remember('navigation', now()->addHours(1), function () { - return NavigationResource::collection( - MainSection::with('subSections.pages.section') - ->orderBy('sort', 'asc') - ->get() - ); - }); - } - - private function getBreadcrumbs() - { - return app(BreadcrumbService::class)->generateBreadcrumbs(); - } -} \ No newline at end of file +} diff --git a/app/Ship/Middleware/HandleInertiaRequests.php b/app/Ship/Middleware/HandleInertiaRequests.php index 7588d8e..88fd8f7 100644 --- a/app/Ship/Middleware/HandleInertiaRequests.php +++ b/app/Ship/Middleware/HandleInertiaRequests.php @@ -2,11 +2,9 @@ namespace App\Ship\Middleware; -use App\Containers\AppStructure\Models\MainSection; -use App\Containers\AppStructure\UI\API\Transformers\NavigationResource; -use App\Services\App\Breadcrumb\BreadcrumbService; +use App\Containers\AppStructure\Tasks\GetCachedNavigationDataTask; +use App\Containers\AppStructure\Tasks\GenerateBreadcrumbsTask; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Cache; use Inertia\Middleware; use Tightenco\Ziggy\Ziggy; @@ -14,6 +12,8 @@ class HandleInertiaRequests extends Middleware { protected $rootView = 'app'; + public function __construct(private readonly GenerateBreadcrumbsTask $generateBreadcrumbsTask){} + public function version(Request $request): ?string { return parent::version($request); @@ -22,16 +22,10 @@ class HandleInertiaRequests extends Middleware public function share(Request $request): array { // Навигация (кешированная) - $navigation = Cache::remember('navigation', now()->addHours(1), function () { - return NavigationResource::collection( - MainSection::with('subSections.pages.section') - ->orderBy('sort', 'asc') - ->get() - ); - }); + $navigation = app(GetCachedNavigationDataTask::class)->run(); // Хлебные крошки (автоматически по текущему URL) - $breadcrumbs = app(BreadcrumbService::class)->generateBreadcrumbs(); + $breadcrumbs = $this->generateBreadcrumbsTask->run(); return [ ...parent::share($request), @@ -52,4 +46,4 @@ class HandleInertiaRequests extends Middleware }, ]; } -} \ No newline at end of file +} diff --git a/public/.gitignore b/public/.gitignore index 153343e..49e973d 100644 --- a/public/.gitignore +++ b/public/.gitignore @@ -11,3 +11,4 @@ site.webmanifest robots.txt ./sveden ./abitur +sitemap.xml