Skip to content

Commit

Permalink
Merge pull request #115 from musimana/11.x
Browse files Browse the repository at this point in the history
v11.8.3
  • Loading branch information
musimana authored Jul 6, 2024
2 parents 639b4d9 + 3b45329 commit cfc68aa
Show file tree
Hide file tree
Showing 31 changed files with 348 additions and 281 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Public/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function show(Page $page): RedirectResponse|Response
return (new PublicViewRepository)
->getViewDetails(
$template->value,
(new PageContentResource)->getItem($page),
(new PageContentResource($page))->getItem(),
(new PageMetadataResource)->getItem($page)
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Public/PrivacyPolicyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __invoke(): Response

return (new PublicViewRepository)->getViewDetails(
$template,
(new PageContentResource)->getItem($page),
(new PageContentResource($page))->getItem(),
(new PageMetadataResource)->getItem($page)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ final class CreateEditSummaryResource implements PageItemInterface
/**
* Get the resource as an array.
*
* @return array{content: string, title: string, url: string}
* @return array{content: string, title: string, url: string|false}
*/
public function getItem(Page $page): array
{
return [
'content' => $page->getMetaDescription(),
'title' => $page->getTitle(),
'url' => $page->getUrlEdit(),
'url' => $page->getUrlAdminEdit(),
];
}
}
21 changes: 13 additions & 8 deletions app/Http/Resources/Views/Public/Content/PageContentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
namespace App\Http\Resources\Views\Public\Content;

use App\Http\Resources\Views\Public\Blocks\BlocksResource;
use App\Interfaces\Resources\Items\PageItemInterface;
use App\Interfaces\Resources\Items\ConstantItemInterface;
use App\Models\Page;

final class PageContentResource implements PageItemInterface
final class PageContentResource implements ConstantItemInterface
{
/** Instantiate the resource. */
public function __construct(
protected Page $page = new Page
) {}

/**
* Get the content array for the given page's full public resource.
* Get the full public item array for the resource's page.
*
* @return array{
* blocks: array<int, array{
Expand All @@ -21,13 +26,13 @@ final class PageContentResource implements PageItemInterface
* subheading: string,
* }
*/
public function getItem(Page $page): array
public function getItem(): array
{
return [
'blocks' => (new BlocksResource)->getItems($page->blocks),
'bodytext' => $page->getContent(),
'heading' => $page->getTitle(),
'subheading' => $page->getSubtitle(),
'blocks' => (new BlocksResource)->getItems($this->page->blocks),
'bodytext' => $this->page->getContent(),
'heading' => $this->page->getTitle(),
'subheading' => $this->page->getSubtitle(),
];
}
}
27 changes: 18 additions & 9 deletions app/Http/Resources/Views/Sitemaps/PageSitemapResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@

namespace App\Http\Resources\Views\Sitemaps;

use App\Interfaces\Resources\Items\PageItemInterface;
use App\Interfaces\Resources\Items\ConstantItemInterface;
use App\Models\Page;

final class PageSitemapResource implements PageItemInterface
final class PageSitemapResource implements ConstantItemInterface
{
/** Instantiate the resource. */
public function __construct(
protected Page $page = new Page,
protected string $changefreq = 'monthly',
protected string $priority = '0.7'
) {}

/**
* Get the content array for the given page's sitemap item.
*
* @return array{
* loc: string,
* lastmod: string,
* changefreq: string,
* priority: float
* priority: string
* }
*/
public function getItem(Page $page): array
public function getItem(): array
{
$updated_at = strtotime(strval($page->updated_at));
$lastmod = $updated_at ? date('Y-m-d', $updated_at) : strval(config('metadata.first_published_year')) . '-01-01';
$updated_at = strtotime(strval($this->page->updated_at));
$lastmod = $updated_at
? date('Y-m-d', $updated_at)
: strval(config('metadata.first_published_year')) . '-01-01';

return [
'loc' => $page->getUrl(),
'loc' => $this->page->getUrl(),
'lastmod' => $lastmod,
'changefreq' => 'weekly',
'priority' => 0.8,
'changefreq' => $this->changefreq,
'priority' => $this->priority,
];
}
}
8 changes: 4 additions & 4 deletions app/Http/Resources/Views/Sitemaps/PagesSitemapResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class PagesSitemapResource implements ConstantIndexInterface
* loc: string,
* lastmod: string,
* changefreq: string,
* priority: float
* priority: string
* }>
*/
public function getItems(): array
Expand All @@ -23,20 +23,20 @@ public function getItems(): array

if (!Page::where([['is_homepage', true], ['in_sitemap', true]])->exists()) {
$page = Page::factory()->homePage()->make(['updated_at' => Page::min('updated_at')]);
$static_pages[] = (new PageSitemapResource)->getItem($page);
$static_pages[] = (new PageSitemapResource($page, 'weekly', '1.0'))->getItem();
}

if (!Page::where([['slug', 'privacy'], ['in_sitemap', true]])->exists()) {
$page = Page::factory()->privacyPage()->make(['updated_at' => Page::min('updated_at')]);
$static_pages[] = (new PageSitemapResource)->getItem($page);
$static_pages[] = (new PageSitemapResource($page, 'yearly', '0.1'))->getItem();
}

return array_merge(
$static_pages,
Page::query()
->inSitemap()
->get()
->map(fn ($page) => (new PageSitemapResource)->getItem($page))
->map(fn ($page) => (new PageSitemapResource($page))->getItem())
->toArray(),
);
}
Expand Down
17 changes: 2 additions & 15 deletions app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Traits\Admin\IsAdminResource;
use App\Traits\HasPageView;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -10,7 +11,6 @@
use Illuminate\Database\Query\Builder as QueryBuilder;

/**
* @method Builder|static inSitemap()
* @method Builder|static isHomepage()
* @method Builder|static isNotHomepage()
* @method static Builder|static query()
Expand All @@ -19,6 +19,7 @@ final class Page extends Model
{
use HasFactory,
HasPageView,
IsAdminResource,
SoftDeletes;

/**
Expand Down Expand Up @@ -68,20 +69,6 @@ public function getUrl(): string
: route('page.show', $this->slug);
}

/** Get the admin URL for the page edit view. */
public function getUrlEdit(): string
{
return route('admin.page.edit', $this->slug);
}

/** Returns all Page models that should be in the sitemap (in_sitemap = 1). */
public function scopeInSitemap(Builder|QueryBuilder $query): Builder|QueryBuilder
{
return $query->where(function ($query) {
$query->where('in_sitemap', 1);
});
}

/** Return all Page models flagged as the homepage (is_homepage = 1). */
public function scopeIsHomepage(Builder|QueryBuilder $query): Builder|QueryBuilder
{
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/Views/SitemapViewRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SitemapViewRepository
* loc: string,
* lastmod: string,
* changefreq: string,
* priority: float
* priority: string
* }|string> $items = []
*/
public function getView(string $template, array $items = []): View
Expand Down
18 changes: 18 additions & 0 deletions app/Services/ReflectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Services;

use ReflectionClass;

final class ReflectionService
{
/**
* Get the class name for the given class.
*
* @param class-string $class
*/
public static function getClassName(string $class): ?string
{
return (new ReflectionClass($class))->getShortName();
}
}
18 changes: 18 additions & 0 deletions app/Traits/Admin/HasAdminEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Traits\Admin;

use App\Services\ReflectionService;

trait HasAdminEdit
{
/** Get the admin URL for the resource's edit view. */
public function getUrlAdminEdit(): string|false
{
$class_name = ReflectionService::getClassName(self::class);

return $class_name
? route('admin.' . strtolower($class_name) . '.edit', $this->id)
: false;
}
}
8 changes: 8 additions & 0 deletions app/Traits/Admin/IsAdminResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Traits\Admin;

trait IsAdminResource
{
use HasAdminEdit;
}
15 changes: 15 additions & 0 deletions app/Traits/HasPageView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;

/**
* @method Builder|static inSitemap()
* @method static Builder|static query()
*/
trait HasPageView
{
use HasContentBlocks;
Expand Down Expand Up @@ -35,4 +42,12 @@ public function isInSitemap(): bool
{
return $this->in_sitemap ?? false;
}

/** Returns all models that should be in the sitemap (in_sitemap = 1). */
public function scopeInSitemap(Builder|QueryBuilder $query): Builder|QueryBuilder
{
return $query->where(function ($query) {
$query->where('in_sitemap', 1);
});
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "8.2.*||8.3.*",
"guzzlehttp/guzzle": "^7.8.1",
"inertiajs/inertia-laravel": "^1.3",
"laravel/framework": "^11.11",
"laravel/framework": "^11.14",
"laravel/tinker": "^2.9.0",
"tightenco/ziggy": "^1.8.2"
},
Expand All @@ -25,7 +25,7 @@
"larastan/larastan": "^2.9",
"laravel/dusk": "^8.2",
"laravel/pint": "^1.16",
"laravel/sail": "^1.29",
"laravel/sail": "^1.30",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.1.1",
"pestphp/pest": "^2.34",
Expand Down
Loading

0 comments on commit cfc68aa

Please sign in to comment.