AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Storefront.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Storefront;
  3. use App\Models\Store;
  4. use Illuminate\Http\Request;
  5. /**
  6. * One visit to a store's pages, in one of two ways:
  7. *
  8. * - Live: a shopper on the store's own address (such as sambal-nenek.localhost). No login.
  9. * Shows the published copy of the design. Only published stores can be opened.
  10. * - Preview: the owner, logged in, inside the dashboard address. Shows the working design.
  11. *
  12. * The same controllers and pages serve both. This class is the only place that knows the difference.
  13. */
  14. final readonly class Storefront
  15. {
  16. /**
  17. * @param array<string, mixed> $plan The design to draw.
  18. * @param string $baseUrl Path of the store's home page. Every other store address is built on it.
  19. */
  20. public function __construct(
  21. public Store $store,
  22. public array $plan,
  23. public string $baseUrl,
  24. public bool $preview,
  25. private string $origin,
  26. ) {}
  27. /**
  28. * Work out which store this request is for. Gives "not found" when the visitor may not see it.
  29. */
  30. public static function fromRequest(Request $request): self
  31. {
  32. $liveSlug = $request->route('storeSlug');
  33. if (is_string($liveSlug)) {
  34. $store = Store::query()->published()->where('slug', $liveSlug)->firstOrFail();
  35. return new self($store, $store->published_plan ?? [], '', false, $request->getSchemeAndHttpHost());
  36. }
  37. $store = $request->user()?->stores()->where('slug', (string) $request->route('slug'))->first();
  38. abort_if($store === null || $store->plan === null, 404);
  39. return new self($store, $store->plan, route('stores.preview', $store->slug, false), true, $request->getSchemeAndHttpHost());
  40. }
  41. /**
  42. * A path inside this store, such as "/cart".
  43. */
  44. public function path(string $path = ''): string
  45. {
  46. return $this->baseUrl.$path ?: '/';
  47. }
  48. /**
  49. * A full web address inside this store, for places outside our pages (the payment provider, emails).
  50. */
  51. public function url(string $path = ''): string
  52. {
  53. return $this->origin.$this->path($path);
  54. }
  55. /**
  56. * The address shoppers open a published store at, built from the address the app itself is on.
  57. * "http://localhost:8000" and store "luna-skin" give "http://luna-skin.localhost:8000".
  58. */
  59. public static function liveUrl(Store $store, Request $request): string
  60. {
  61. $port = $request->getPort();
  62. $standard = ($request->getScheme() === 'https' && $port === 443) || ($request->getScheme() === 'http' && $port === 80);
  63. return $request->getScheme().'://'.$store->slug.'.'.config('storefront.domain').($standard || $port === null ? '' : ':'.$port);
  64. }
  65. }