Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.07% covered (warning)
62.07%
18 / 29
83.33% covered (warning)
83.33%
5 / 6
CRAP
n/a
0 / 0
permissions
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
feature
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
settings
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
nav_menu
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
theme
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
7.91
money
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3use App\Services\Builder\MenuService;
4use App\Services\Feature\FeatureService;
5use App\Services\Permission\PermissionService;
6use App\Services\Setting\SettingService;
7use App\Services\Theme\ThemeService;
8
9if (! function_exists('permissions')) {
10    /**
11     * The permission service, or a quick allows() check.
12     *
13     *   permissions()->allows('manage_products')
14     *   permissions('manage_products')   // bool
15     */
16    function permissions(?string $ability = null): mixed
17    {
18        $service = app(PermissionService::class);
19
20        return $ability === null ? $service : $service->allows($ability);
21    }
22}
23
24if (! function_exists('feature')) {
25    /**
26     * Check a feature flag, or get the service with no arguments.
27     *
28     *   feature('coupons')          // bool
29     *   feature()->allEnabled()
30     */
31    function feature(?string $key = null): mixed
32    {
33        $service = app(FeatureService::class);
34
35        return $key === null ? $service : $service->enabled($key);
36    }
37}
38
39if (! function_exists('settings')) {
40    /**
41     * Read a setting, or get the service when called with no arguments.
42     *
43     *   settings('general.site_name')
44     *   settings('general.site_name', 'Fallback')
45     *   settings()->set('general.site_name', 'X')
46     */
47    function settings(?string $path = null, mixed $default = null): mixed
48    {
49        $service = app(SettingService::class);
50
51        return $path === null ? $service : $service->get($path, $default);
52    }
53}
54
55if (! function_exists('nav_menu')) {
56    /**
57     * Resolved menu tree for a builder location (config/builder.php), or the
58     * MenuService when called with no argument.
59     *
60     *   nav_menu('header_primary')   // array of ['label','url','active','children',...]
61     */
62    function nav_menu(?string $location = null): mixed
63    {
64        $service = app(MenuService::class);
65
66        return $location === null ? $service : $service->forLocation($location);
67    }
68}
69
70if (! function_exists('theme')) {
71    /**
72     * Active storefront theme info, a customizer value, or the ThemeService.
73     *
74     *   theme()                       // ThemeService
75     *   theme('handle')               // active theme handle
76     *   theme('customizer.primary')   // active theme customizer value
77     */
78    function theme(?string $key = null): mixed
79    {
80        $service = app(ThemeService::class);
81
82        if ($key === null) {
83            return $service;
84        }
85
86        if ($key === 'handle') {
87            return $service->activeHandle();
88        }
89
90        if (str_starts_with($key, 'customizer.')) {
91            return $service->activeCustomizer()[substr($key, 11)] ?? null;
92        }
93
94        return $service->active()[$key] ?? null;
95    }
96}
97
98if (! function_exists('money')) {
99    /**
100     * Format an amount using the store currency settings (Phase 1).
101     * Single currency (BDT) — no FX.
102     */
103    function money(int|float|string|null $amount): string
104    {
105        $amount = (float) $amount;
106
107        $symbol = settings('general.currency_symbol', '৳');
108        $decimals = (int) settings('general.currency_decimals', 2);
109        $thousands = settings('general.thousand_separator', ',');
110        $position = settings('general.currency_position', 'prefix'); // prefix | suffix
111
112        $number = number_format($amount, $decimals, '.', $thousands);
113
114        return $position === 'suffix' ? "{$number}{$symbol}" : "{$symbol}{$number}";
115    }
116}