Files
core/src/Providers/CheckoutModuleServiceProvider.php
T

65 lines
2.8 KiB
PHP
Raw Normal View History

<?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. `"@boboko/core": "file:../boboko-core"` as an npm dependency (see this
* package's own package.json `exports`), with a bind-mount of the core
* checkout into the host's Vite container so the `file:` symlink
* resolves in dev (see 3dealer's docker-compose.core-dev.yml) and
* `resolve.preserveSymlinks: true` in the host's vite.config.js so bare
* imports (stimulus, leaflet) still resolve against the host's own
* node_modules through that symlink.
* 2. `import { registerCheckout } from '@boboko/core/checkout'` in the
* host's own JS entry point, and a @vite entry for
* `node_modules/@boboko/core/resources/css/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());
},
);
}
}