Skip to content

Commit

Permalink
Add @seo Blade directive (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Jun 24, 2024
1 parent 1faa51d commit ab454e7
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/Data/DefaultsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public function __construct(
public string $handle,
public ?string $locale = null,
public ?Collection $sites = null,
) {
}
) {}

public function id(): string
{
Expand Down
39 changes: 19 additions & 20 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Aerni\AdvancedSeo;

use Aerni\AdvancedSeo\Actions\ShouldProcessViewCascade;
use Aerni\AdvancedSeo\Data\SeoVariables;
use Aerni\AdvancedSeo\GraphQL\Fields\SeoField;
use Aerni\AdvancedSeo\GraphQL\Queries\SeoDefaultsQuery;
Expand All @@ -27,8 +26,9 @@
use Aerni\AdvancedSeo\GraphQL\Types\SocialMediaDefaultsType;
use Aerni\AdvancedSeo\Models\Defaults;
use Aerni\AdvancedSeo\Stache\SeoStore;
use Aerni\AdvancedSeo\View\ViewCascade;
use Aerni\AdvancedSeo\View\CascadeComposer;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
use Statamic\Facades\CP\Nav;
use Statamic\Facades\Git;
Expand All @@ -41,7 +41,6 @@
use Statamic\Providers\AddonServiceProvider;
use Statamic\Stache\Stache;
use Statamic\Statamic;
use Statamic\Tags\Context;

class ServiceProvider extends AddonServiceProvider
{
Expand Down Expand Up @@ -110,16 +109,17 @@ class ServiceProvider extends AddonServiceProvider
public function bootAddon(): void
{
$this
->bootAddonStores()
->bootAddonNav()
->bootAddonPermissions()
->bootStores()
->bootNav()
->bootPermissions()
->bootGit()
->bootCascade()
->bootViewCascade()
->bootBladeDirective()
->bootGraphQL()
->autoPublishConfig();
}

protected function bootAddonStores(): self
protected function bootStores(): self
{
$seoStore = app(SeoStore::class)->directory(config('advanced-seo.directory'));

Expand All @@ -128,7 +128,7 @@ protected function bootAddonStores(): self
return $this;
}

protected function bootAddonNav(): self
protected function bootNav(): self
{
Nav::extend(function ($nav) {
Defaults::enabled()
Expand All @@ -154,7 +154,7 @@ protected function bootAddonNav(): self
return $this;
}

protected function bootAddonPermissions(): self
protected function bootPermissions(): self
{
Permission::extend(function () {
Permission::group('advanced-seo', 'Advanced SEO', function () {
Expand Down Expand Up @@ -195,23 +195,22 @@ protected function bootGit(): self
return $this;
}

protected function bootCascade(): self
protected function bootViewCascade(): self
{
$views = [
View::composer([
...Arr::wrap(config('advanced-seo.view_composer', '*')),
'advanced-seo::head',
'advanced-seo::body',
'social_images.*',
];
], CascadeComposer::class);

View::composer($views, function ($view) {
$data = new Context($view->getData());

if (! ShouldProcessViewCascade::handle($data)) {
return;
}
return $this;
}

$view->with('seo', ViewCascade::from($data)->toAugmentedArray());
protected function bootBladeDirective(): self
{
Blade::directive('seo', function ($tag) {
return "<?php echo \Facades\Aerni\AdvancedSeo\Tags\AdvancedSeoDirective::render($tag, \$__data) ?>";
});

return $this;
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/CollectionSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

class CollectionSitemap extends BaseSitemap
{
public function __construct(protected EntriesCollection $model)
{
}
public function __construct(protected EntriesCollection $model) {}

public function urls(): Collection
{
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/CollectionSitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

class CollectionSitemapUrl extends BaseSitemapUrl
{
public function __construct(protected Entry $entry, protected CollectionSitemap $sitemap)
{
}
public function __construct(protected Entry $entry, protected CollectionSitemap $sitemap) {}

public function loc(): string
{
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/CollectionTaxonomySitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

class CollectionTaxonomySitemapUrl extends BaseSitemapUrl
{
public function __construct(protected Taxonomy $taxonomy, protected string $site, protected TaxonomySitemap $sitemap)
{
}
public function __construct(protected Taxonomy $taxonomy, protected string $site, protected TaxonomySitemap $sitemap) {}

public function loc(): string
{
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/CollectionTermSitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

class CollectionTermSitemapUrl extends BaseSitemapUrl
{
public function __construct(protected Term $term, protected TaxonomySitemap $sitemap)
{
}
public function __construct(protected Term $term, protected TaxonomySitemap $sitemap) {}

public function loc(): string
{
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/CustomSitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class CustomSitemapUrl extends BaseSitemapUrl
{
use FluentlyGetsAndSets;

public function __construct(protected string $loc)
{
}
public function __construct(protected string $loc) {}

public function loc(?string $loc = null): string|self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/TaxonomySitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

class TaxonomySitemap extends BaseSitemap
{
public function __construct(protected Taxonomy $model)
{
}
public function __construct(protected Taxonomy $model) {}

public function urls(): Collection
{
Expand Down
4 changes: 1 addition & 3 deletions src/Sitemap/TermSitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

class TermSitemapUrl extends BaseSitemapUrl
{
public function __construct(protected Term $term, protected TaxonomySitemap $sitemap)
{
}
public function __construct(protected Term $term, protected TaxonomySitemap $sitemap) {}

public function loc(): string
{
Expand Down
16 changes: 16 additions & 0 deletions src/Tags/AdvancedSeoDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Aerni\AdvancedSeo\Tags;

class AdvancedSeoDirective
{
public function __construct(protected AdvancedSeoTags $tags)
{
//
}

public function render(string $tag, array $context): mixed
{
return $this->tags->setContext($context)->$tag();
}
}
12 changes: 10 additions & 2 deletions src/Tags/AdvancedSeoTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ public function wildcard()
/**
* Renders the head view.
*/
public function head(): View
public function head(): ?View
{
if (! $this->context->has('seo')) {
return null;
}

return view('advanced-seo::head', $this->context->all());
}

/**
* Renders the body view.
*/
public function body(): View
public function body(): ?View
{
if (! $this->context->has('seo')) {
return null;
}

return view('advanced-seo::body', $this->context->all());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
<?php

namespace Aerni\AdvancedSeo\Actions;
namespace Aerni\AdvancedSeo\View;

use Aerni\AdvancedSeo\Support\Helpers;
use Illuminate\Contracts\View\View;
use Statamic\Facades\Cascade;
use Statamic\Tags\Context;

class ShouldProcessViewCascade
class CascadeComposer
{
public static function handle(Context $context): bool
public function compose(View $view): void
{
$context = new Context($view->getData());

if (! $context->has('current_template')) {
$context = $this->getContextFromCascade($context);
}

if (! $this->shouldProcessCascade($context)) {
return;
}

$view->with('seo', ViewCascade::from($context));
}

protected function getContextFromCascade(Context $context): Context
{
$cascade = Cascade::instance();

/**
* If the cascade has not yet been hydrated, ensure it is hydrated.
* This is important for people using custom route/controller/view implementations.
*/
if (empty($cascade->toArray())) {
$cascade->hydrate();
}

return $context->merge($cascade->toArray());
}

protected function shouldProcessCascade(Context $context): bool
{
// Don't process the cascade if it has been processed before.
if ($context->has('seo')) {
Expand Down

0 comments on commit ab454e7

Please sign in to comment.