Skip to content

Commit

Permalink
Don't hydrate globals twice (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 authored Jan 29, 2025
1 parent 3514d0c commit 787f4d4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Rapidez\Statamic\Commands\ImportCategories;
use Rapidez\Core\Facades\Rapidez;
use Rapidez\Statamic\Commands\InvalidateCacheCommand;
use Rapidez\Statamic\Eloquent\Assets\AssetRepository;
use Rapidez\Statamic\Extend\SitesLinkedToMagentoStores;
use Rapidez\Statamic\Fieldtypes\Assets\Assets;
use Rapidez\Statamic\Http\Controllers\ImportsController;
use Rapidez\Statamic\Http\Controllers\StatamicRewriteController;
use Rapidez\Statamic\Http\ViewComposers\StatamicGlobalDataComposer;
Expand All @@ -31,10 +33,11 @@
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Facades\Utility;
use Statamic\Http\Controllers\FrontendController;
use Statamic\Sites\Sites;
use Statamic\StaticCaching\Middleware\Cache as StaticCache;
use TorMorten\Eventy\Facades\Eventy;
use Statamic\Facades\Site as SiteFacade;
use Statamic\View\Cascade as StatamicCascade;

class RapidezStatamicServiceProvider extends ServiceProvider
{
Expand All @@ -46,6 +49,13 @@ public function register()

$this->app->singleton(RapidezStatamic::class);

// Since we have our own way of exposing the globals to the view,
// we can overwrite Statamic's functionality so we don't query for the
// globals multiple times. In our way we cache the globals to increase performance.
$this->app->extend(StatamicCascade::class, function () {
return new \Rapidez\Statamic\View\Cascade(app()->request, SiteFacade::current());
});

$this->app->booted(function () {
$router = app(Router::class);
$router->pushMiddlewareToGroup('web', StaticCache::class);
Expand Down Expand Up @@ -76,7 +86,7 @@ public function boot()
Alternates::register();
}

public function bootCommands() : self
public function bootCommands(): self
{
$this->commands([
ImportCategories::class,
Expand All @@ -89,14 +99,14 @@ public function bootCommands() : self
return $this;
}

public function bootConfig() : self
public function bootConfig(): self
{
$this->mergeConfigFrom(__DIR__.'/../config/rapidez/statamic.php', 'rapidez.statamic');
$this->mergeConfigFrom(__DIR__ . '/../config/rapidez/statamic.php', 'rapidez.statamic');

return $this;
}

public function bootRoutes() : self
public function bootRoutes(): self
{
if (config('rapidez.statamic.routes') && $this->currentSiteIsEnabled()) {
Rapidez::addFallbackRoute(StatamicRewriteController::class);
Expand All @@ -105,14 +115,14 @@ public function bootRoutes() : self
return $this;
}

public function bootViews() : self
public function bootViews(): self
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'rapidez-statamic');
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'rapidez-statamic');

return $this;
}

public function bootListeners() : self
public function bootListeners(): self
{
if ($this->currentSiteIsEnabled()) {
Event::listen([GlobalSetSaved::class, GlobalSetDeleted::class], function () {
Expand Down Expand Up @@ -141,7 +151,7 @@ public function bootListeners() : self
return $this;
}

public function bootRunway() : self
public function bootRunway(): self
{
if (config('rapidez.statamic.runway.configure') && $this->currentSiteIsEnabled()) {
config(['runway.resources' => array_merge(
Expand All @@ -153,7 +163,7 @@ public function bootRunway() : self
return $this;
}

public function bootComposers() : self
public function bootComposers(): self
{
if (config('rapidez.statamic.fetch.product') && $this->currentSiteIsEnabled()) {
View::composer('rapidez::product.overview', function (RenderedView $view) {
Expand Down Expand Up @@ -187,37 +197,37 @@ public function bootComposers() : self
return $this;
}

public function bootPublishables() : self
public function bootPublishables(): self
{
$this->publishes([
__DIR__.'/../resources/blueprints/collections' => resource_path('blueprints/collections'),
__DIR__.'/../resources/content/collections' => base_path('content/collections'),
__DIR__.'/../resources/content/assets' => base_path('content/assets'),
__DIR__.'/../resources/fieldsets' => resource_path('fieldsets'),
__DIR__.'/../resources/blueprints/runway' => resource_path('blueprints/vendor/runway'),
__DIR__ . '/../resources/blueprints/collections' => resource_path('blueprints/collections'),
__DIR__ . '/../resources/content/collections' => base_path('content/collections'),
__DIR__ . '/../resources/content/assets' => base_path('content/assets'),
__DIR__ . '/../resources/fieldsets' => resource_path('fieldsets'),
__DIR__ . '/../resources/blueprints/runway' => resource_path('blueprints/vendor/runway'),
], 'rapidez-statamic-content');

$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/rapidez-statamic'),
__DIR__ . '/../resources/views' => resource_path('views/vendor/rapidez-statamic'),
], 'views');

$this->publishes([
__DIR__.'/../config/rapidez/statamic.php' => config_path('rapidez/statamic.php'),
__DIR__ . '/../config/rapidez/statamic.php' => config_path('rapidez/statamic.php'),
], 'config');

return $this;
}

public function bootUtilities() : static
public function bootUtilities(): static
{
Utility::extend(function () : void {
Utility::extend(function (): void {
Utility::register('imports')
->icon('synchronize')
->action(ImportsController::class)
->title(__('Import'))
->navTitle(__('Import'))
->description(__('Import products or categories from Magento'))
->routes(function (Router $router) : void {
->routes(function (Router $router): void {
$router->post('/import-categories', [ImportsController::class, 'importCategories'])
->name('import-categories');

Expand All @@ -240,7 +250,7 @@ public function bootSitemaps(): static
}


public function bootStack() : static
public function bootStack(): static
{
View::composer('rapidez::layouts.app', fn($view) => $view->getFactory()->startPush('head', view('statamic-glide-directive::partials.head')));

Expand Down
13 changes: 13 additions & 0 deletions src/View/Cascade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rapidez\Statamic\View;

use Statamic\View\Cascade as StatamicCascade;

class Cascade extends StatamicCascade
{
protected function hydrateGlobals()
{
return $this;
}
}

0 comments on commit 787f4d4

Please sign in to comment.