Files
core/src/Providers/CheckoutModuleServiceProvider.php
T

62 lines
2.7 KiB
PHP

<?php
namespace Modules\Core\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View as ViewInstance;
use Modules\Core\Cart\Services\CartService;
/**
* The cart + checkout module — its view/component namespace, its routes,
* and the composer that feeds the always-present cart drawer. Strings
* (__('checkout.cart.*')) are NOT wired up here — same as storefront.*
* elsewhere — they resolve through Lunar's DB-backed translation UI
* (spatie/laravel-translation-loader), seeded by Checkout\Database\Seeders\
* CheckoutTranslationsSeeder rather than shipped as lang/ files.
*
* A consuming app wires this module in with:
* 1. `php artisan vendor:publish --tag=core-checkout-assets` — copies
* resources/js/checkout/** and resources/css/checkout.css into the
* host's own resources/ tree. Vite only ever bundles from a host's
* own resources/ directory, so these are published (an explicit,
* host-owned, re-publishable copy) rather than imported cross-package.
* 2. `import { registerCheckout } from './checkout'` in the host's own
* JS entry point, and a @vite entry for the published checkout.css.
* 3. `@include('checkout::drawer')` in the host's own layout.
* See config/checkout.php for the handful of per-site settings (login
* route, single-country mode, ...) a host is expected to publish and
* override.
*/
class CheckoutModuleServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->loadViewsFrom(__DIR__.'/../../resources/views/checkout', 'checkout');
Blade::anonymousComponentNamespace('checkout::components', 'checkout');
Route::middleware('web')->group(__DIR__.'/../Checkout/routes/checkout.php');
$this->publishes([
__DIR__.'/../../resources/js/checkout' => resource_path('js/checkout'),
__DIR__.'/../../resources/css/checkout.css' => resource_path('css/checkout.css'),
], 'core-checkout-assets');
// The drawer is rendered on every page (from the layout) and its body
// partial is re-rendered on every cart mutation — both need the current
// cart without a controller in the loop.
View::composer(
['checkout::drawer', 'checkout::partials.cart-body'],
function (ViewInstance $view) {
$service = app(CartService::class);
$cart = $service->current();
$view->with('cart', $cart);
$view->with('lines', $cart ? $service->activeLines($cart) : collect());
},
);
}
}