1
0
Fork 0
mirror of https://github.com/archtechx/laravel-seo.git synced 2025-12-13 18:24:04 +00:00

Add support Laravel 10.x & remove unused package (#26)

* feat: support Laravel 10.x & remove unneccessary package

* Update

* Update

* Update
This commit is contained in:
Dinh Quoc Han 2023-02-15 18:06:47 +07:00 committed by GitHub
parent a737879baf
commit 0992083ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 47 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ArchTech\SEO;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
/**
@ -323,6 +324,38 @@ class SEOManager
return $this->get($key);
}
/** Render blade directive. */
public function render(...$args): array|string|null
{
// Flipp and Previewify support more arguments
if (in_array($args[0], ['flipp', 'previewify'], true)) {
$method = array_shift($args);
return $this->{$method}(...$args);
}
// Two arguments indicate that we're setting a value, e.g. `@seo('title', 'foo')
if (count($args) === 2) {
return $this->set($args[0], $args[1]);
}
// An array means we don't return anything, e.g. `@seo(['title' => 'foo'])
if (is_array($args[0])) {
foreach ($args[0] as $type => $value) {
if (in_array($type, ['flipp', 'previewify'], true)) {
$this->{$type}(...Arr::wrap($value));
} else {
$this->set($type, $value);
}
}
return null;
}
// A single value means we fetch a value, e.g. `@seo('title')
return $this->get($args[0]);
}
/** Handle magic get. */
public function __get(string $key): string|null
{

View file

@ -5,17 +5,14 @@ declare(strict_types=1);
namespace ArchTech\SEO;
use ArchTech\SEO\Commands\GenerateFaviconsCommand;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use ImLiam\BladeHelper\BladeHelperServiceProvider;
use ImLiam\BladeHelper\Facades\BladeHelper;
class SEOServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton('seo', SEOManager::class);
$this->app->register(BladeHelperServiceProvider::class);
}
public function boot(): void
@ -32,34 +29,8 @@ class SEOServiceProvider extends ServiceProvider
__DIR__ . '/../assets/views' => resource_path('views/vendor/seo'),
], 'seo-views');
BladeHelper::directive('seo', function (...$args) {
// Flipp and Previewify support more arguments
if (in_array($args[0], ['flipp', 'previewify'], true)) {
$method = array_shift($args);
return seo()->{$method}(...$args);
}
// Two arguments indicate that we're setting a value, e.g. `@seo('title', 'foo')
if (count($args) === 2) {
return seo()->set($args[0], $args[1]);
}
// An array means we don't return anything, e.g. `@seo(['title' => 'foo'])
if (is_array($args[0])) {
foreach ($args[0] as $type => $value) {
if (in_array($type, ['flipp', 'previewify'], true)) {
seo()->{$type}(...Arr::wrap($value));
} else {
seo()->set($type, $value);
}
}
return null;
}
// A single value means we fetch a value, e.g. `@seo('title')
return seo()->get($args[0]);
Blade::directive('seo', function ($expression) {
return "<?php echo seo()->render($expression); ?>";
});
}
}