Skip to content

Commit

Permalink
seo
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Aug 22, 2024
1 parent c7fad65 commit 7b1b8db
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 26 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"phpstan/phpstan-phpunit": "^1.3"
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Elegantly\\Seo\\": "src/",
"Elegantly\\Seo\\Database\\Factories\\": "database/factories/"
Expand Down
8 changes: 6 additions & 2 deletions config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
// config for Elegantly/Seo
return [

'title' => env('APP_NAME'),
'title' => env('APP_NAME', 'Laravel'),

'robots' => null,
'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1',

'sitemap' => '/sitemap.xml',

'image' => null,

];
16 changes: 0 additions & 16 deletions src/Facades/Seo.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Facades/SeoManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Elegantly\Seo\Facades;

use Elegantly\Seo\Contracts\HasSeo;
use Elegantly\Seo\SeoData;
use Elegantly\Seo\Unified\SeoUnifiedData;
use Illuminate\Support\Facades\Facade;

/**
* @method static \Elegantly\Seo\SeoManager from(null|SeoData|SeoUnifiedData|\Elegantly\Seo\SeoManager|HasSeo $value = null)
*
* @see \Elegantly\Seo\Seo
*/
class SeoManager extends Facade
{
protected static function getFacadeAccessor(): string
{
return \Elegantly\Seo\SeoManager::class;
}
}
8 changes: 0 additions & 8 deletions src/Seo.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/SeoData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Elegantly\Seo;

use Elegantly\Seo\Schemas\Schema;
use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Unified\Image;
use Elegantly\Seo\Unified\SeoUnifiedData;
use Illuminate\Support\Facades\URL;

class SeoData extends SeoUnifiedData
{
public string $title;

public string $canonical;

/**
* @param null|Alternate[] $alternates
* @param null|Schema[] $schemas
*/
public function __construct(
?string $title = null,
?string $canonical = null,
public ?string $description = null,
public ?string $robots = null,
public ?string $sitemap = null,
public ?array $alternates = null,
public ?Image $image = null,
public ?string $locale = null,
public ?array $schemas = null,
public ?SeoTags $customTags = null,
) {
$this->title = $title ?? config('seo.title') ?? '';
$this->canonical = $canonical ?? URL::current();
}
}
25 changes: 25 additions & 0 deletions src/SeoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Elegantly\Seo;

use Elegantly\Seo\Contracts\HasSeo;
use Elegantly\Seo\Contracts\Taggable;
use Elegantly\Seo\OpenGraph\Verticals\Vertical;
use Elegantly\Seo\Schemas\Schema;
use Elegantly\Seo\Standard\StandardData;
use Elegantly\Seo\Twitter\Cards\Card;
use Elegantly\Seo\Unified\SeoUnifiedData;

class SeoManager implements Taggable
{
Expand All @@ -21,6 +23,29 @@ public function __construct(
public ?SeoTags $customTags = null,
) {}

public function from(
null|SeoData|SeoUnifiedData|SeoManager|HasSeo $value = null
): SeoManager {

if ($value instanceof SeoManager) {
return $value;
}

if ($value instanceof SeoData) {
return $value->toManager();
}

if ($value instanceof SeoUnifiedData) {
return $value->toManager();
}

if ($value instanceof HasSeo) {
return $this->from($value->getSeoData());
}

return $this->from(new SeoData);
}

public function toTags(): SeoTags
{
$tags = new SeoTags;
Expand Down
13 changes: 13 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Elegantly\Seo\Contracts\HasSeo;
use Elegantly\Seo\SeoData;
use Elegantly\Seo\SeoManager;
use Elegantly\Seo\Unified\SeoUnifiedData;

if (! function_exists('seo')) {
function seo(null|SeoData|SeoUnifiedData|SeoManager|HasSeo $value): SeoManager
{
return \Elegantly\Seo\Facades\SeoManager::from($value);
}
}
43 changes: 43 additions & 0 deletions tests/Features/SeoDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Elegantly\Seo\Facades\SeoManager;
use Elegantly\Seo\SeoData;
use Illuminate\Support\Facades\URL;

it('renders default seo from config', function () {

$data = new SeoData;

$url = URL::current();

expect(
$data->toTags()->toHtml()
)->toBe(implode("\n", [
'<title >Laravel</title>',
'<link rel="canonical" href="'.$url.'" />',
'<meta property="og:type" content="website" />',
'<meta property="og:title" content="Laravel" />',
'<meta property="og:url" content="'.$url.'" />',
'<meta name="twitter:card" content="summary" />',
'<meta name="twitter:title" content="Laravel" />',
]));
});

it('renders default seo from config using Facade', function () {

$data = SeoManager::from();

$url = URL::current();

expect(
$data->toTags()->toHtml()
)->toBe(implode("\n", [
'<title >Laravel</title>',
'<link rel="canonical" href="'.$url.'" />',
'<meta property="og:type" content="website" />',
'<meta property="og:title" content="Laravel" />',
'<meta property="og:url" content="'.$url.'" />',
'<meta name="twitter:card" content="summary" />',
'<meta name="twitter:title" content="Laravel" />',
]));
});

0 comments on commit 7b1b8db

Please sign in to comment.