Skip to content

Commit

Permalink
WIP support passing SEOData to TagManager directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphjsmit committed Sep 19, 2022
1 parent f112bf6 commit 9586a9d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
18 changes: 15 additions & 3 deletions src/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class TagManager implements Renderable
{
public Model $model;

public SEOData $SEOData;

public TagCollection $tags;

public function __construct()
Expand Down Expand Up @@ -68,16 +70,26 @@ public function fillSEOData(SEOData $SEOData = null): SEOData
);
}

public function for(Model $model): static
public function for(Model|SEOData $source): static
{
$this->model = $model;
if ( $source instanceof Model ) {
$this->model = $source;
unset($this->SEOData);
} elseif ( $source instanceof SEOData ) {
unset($this->model);
$this->SEOData = $source;
}

// The tags collection is already initialized when constructing the manager. Here, we'll
// initialize the collection again, but this time we pass the model to the initializer.
// The initializes will pass the generated SEOData to all underlying initializers, ensuring that
// the tags are always fully up-to-date and no remnants from previous initializations are present.
$SEOData = isset($this->model)
? $this->model->seo?->prepareForUsage()
: $this->SEOData;

$this->tags = TagCollection::initialize(
$this->fillSEOData($this->model->seo?->prepareForUsage() ?? new SEOData())
$this->fillSEOData($SEOData ?? new SEOData())
);

return $this;
Expand Down
7 changes: 4 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

use Illuminate\Database\Eloquent\Model;
use RalphJSmit\Laravel\SEO\Support\SEOData;
use RalphJSmit\Laravel\SEO\TagManager;

if ( ! function_exists('seo') ) {
function seo(Model $model = null): TagManager
function seo(Model|SEOData $source = null): TagManager
{
$tagManager = app(TagManager::class);

if ( $model ) {
$tagManager->for($model);
if ( $source ) {
$tagManager->for($source);
}

return $tagManager;
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/TagManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
['Custom homepage title', 'Custom homepage title'],
]);

test('can render the SEOData from an object that\'s directly passed in', function () {
$SEOData = new SEOData(
title: 'Awesome News - My Project',
);

$output = seo()->for($SEOData)->render();

expect($output)->toContain('Awesome News - My Project');
});

it('can pipe the SEOData through the transformer before putting it into the collection', function () {
config()->set('seo.title.infer_title_from_url', true);

Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use RalphJSmit\Laravel\SEO\Support\SEOData;
use RalphJSmit\Laravel\SEO\TagManager;
use RalphJSmit\Laravel\SEO\Tests\Fixtures\Page;

Expand All @@ -16,4 +17,18 @@
expect(seo()->for($page))->model->toBe($page);
expect(seo($page))->model->toBe($page);
expect(seo(null))->model->toBeNull();
});

it('can get the TagManager with a SEOData data object', function () {
expect(seo())->toBeInstanceOf(TagManager::class);

$SEOData = new SEOData(
title: 'Awesome News - My Project',
description: 'Lorem Ipsum',
);

expect(seo())->SEOData->toBeNull();
expect(seo()->for($SEOData))->SEOData->toBe($SEOData);
expect(seo($SEOData))->SEOData->toBe($SEOData);
expect(seo(null))->SEOData->toBeNull();
});

0 comments on commit 9586a9d

Please sign in to comment.