2026-08-27 11:42:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Providers;
|
|
|
|
|
|
2026-09-01 12:06:29 +03:00
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
2026-08-27 11:42:13 +03:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2026-09-01 12:06:29 +03:00
|
|
|
use Lunar\Models\Product;
|
2026-08-27 11:42:13 +03:00
|
|
|
use Lunar\Models\ProductOption;
|
|
|
|
|
use Lunar\Models\ProductOptionValue;
|
2026-09-01 12:06:29 +03:00
|
|
|
use Modules\Core\Catalog\Events\ProductDeleted;
|
|
|
|
|
use Modules\Core\Catalog\Events\ProductSaved;
|
|
|
|
|
use Modules\Core\Catalog\Listeners\ReindexProductsRecommendingProduct;
|
2026-08-27 11:42:13 +03:00
|
|
|
use Modules\Core\Catalog\Observers\ProductOptionReindexObserver;
|
|
|
|
|
use Modules\Core\Catalog\OptionTypes\ColorOptionType;
|
|
|
|
|
use Modules\Core\Catalog\Services\ProductOptionTypeManager;
|
|
|
|
|
|
|
|
|
|
class CatalogServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
2026-09-01 12:06:29 +03:00
|
|
|
public function register(): void
|
|
|
|
|
{
|
|
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../../config/catalog.php', 'catalog');
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 11:42:13 +03:00
|
|
|
public function boot(): void
|
|
|
|
|
{
|
2026-09-01 12:06:29 +03:00
|
|
|
$this->publishes([
|
|
|
|
|
__DIR__ . '/../../config/catalog.php' => config_path('catalog.php'),
|
|
|
|
|
], 'core-config');
|
|
|
|
|
|
2026-08-27 11:42:13 +03:00
|
|
|
ProductOptionTypeManager::get()->register([
|
|
|
|
|
ColorOptionType::class,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$observer = new ProductOptionReindexObserver;
|
|
|
|
|
|
|
|
|
|
ProductOption::saved(fn (ProductOption $option) => $observer->optionSaved($option));
|
|
|
|
|
ProductOption::deleted(fn (ProductOption $option) => $observer->optionDeleted($option));
|
|
|
|
|
|
|
|
|
|
ProductOptionValue::saved(fn (ProductOptionValue $value) => $observer->valueSaved($value));
|
|
|
|
|
ProductOptionValue::deleted(fn (ProductOptionValue $value) => $observer->valueDeleted($value));
|
2026-09-01 12:06:29 +03:00
|
|
|
|
|
|
|
|
Product::saved(fn (Product $product) => Event::dispatch(new ProductSaved($product)));
|
|
|
|
|
Product::deleted(fn (Product $product) => Event::dispatch(new ProductDeleted($product->id)));
|
|
|
|
|
|
|
|
|
|
Event::listen(ProductSaved::class, [ReindexProductsRecommendingProduct::class, 'handleSaved']);
|
|
|
|
|
Event::listen(ProductDeleted::class, [ReindexProductsRecommendingProduct::class, 'handleDeleted']);
|
|
|
|
|
|
|
|
|
|
$this->app->booted(function () {
|
|
|
|
|
// A full nightly reindex, on top of the per-event reindexing
|
|
|
|
|
// above — catches everything event-driven reindexing
|
|
|
|
|
// deliberately doesn't cover: a newly-created product not yet
|
|
|
|
|
// appearing as a recommendation elsewhere, in_stock/price
|
|
|
|
|
// drifting from an order decrementing stock outside a product
|
|
|
|
|
// save, and any other staleness ProductIndexer's own docblock
|
|
|
|
|
// already documents as accepted between reindexes. --refresh
|
|
|
|
|
// re-syncs filterable/sortable field settings too, not just
|
|
|
|
|
// documents, so a deploy that changed ProductIndexer's field
|
|
|
|
|
// list self-heals here even if `lunar:meilisearch:setup`
|
|
|
|
|
// wasn't run manually after that deploy.
|
|
|
|
|
$this->app->make(Schedule::class)
|
|
|
|
|
->command('lunar:search:index', ['Lunar\\Models\\Product', '--refresh'])
|
|
|
|
|
->dailyAt('03:00');
|
|
|
|
|
});
|
2026-08-27 11:42:13 +03:00
|
|
|
}
|
|
|
|
|
}
|