• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

conedevelopment / root / 15084089635

17 May 2025 10:00AM UTC coverage: 77.93% (+0.04%) from 77.891%
15084089635

push

github

web-flow
Modernize back-end.yml (#240)

3291 of 4223 relevant lines covered (77.93%)

36.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.68
/src/Root.php
1
<?php
2

3
namespace Cone\Root;
4

5
use Closure;
6
use Cone\Root\Interfaces\Breadcrumbs\Registry as Breadcrumbs;
7
use Cone\Root\Interfaces\Navigation\Registry as Navigation;
8
use Cone\Root\Interfaces\Settings\Registry as Settings;
9
use Cone\Root\Models\User;
10
use Cone\Root\Resources\Resources;
11
use Cone\Root\Widgets\Widgets;
12
use DateTimeZone;
13
use Illuminate\Contracts\Foundation\Application;
14
use Illuminate\Http\Request;
15
use Illuminate\Routing\Router;
16
use Illuminate\Support\Facades\App;
17
use Illuminate\Support\Facades\Config;
18
use Illuminate\Support\Facades\Route;
19
use Illuminate\Support\Str;
20

21
class Root
22
{
23
    /**
24
     * The package version.
25
     *
26
     * @var string
27
     */
28
    public const string VERSION = '2.6.4';
29

30
    /**
31
     * The registered booting callbacks.
32
     */
33
    protected array $booting = [];
34

35
    /**
36
     * The Application instance.
37
     */
38
    public readonly Application $app;
39

40
    /**
41
     * The resources collection.
42
     */
43
    public readonly Resources $resources;
44

45
    /**
46
     * The widgets collection.
47
     */
48
    public readonly Widgets $widgets;
49

50
    /**
51
     * The navigation instance.
52
     */
53
    public readonly Navigation $navigation;
54

55
    /**
56
     * The breadcrumbs instance.
57
     */
58
    public readonly Breadcrumbs $breadcrumbs;
59

60
    /**
61
     * The settings instance.
62
     */
63
    public readonly Settings $settings;
64

65
    /**
66
     * The auth resolver.
67
     */
68
    protected ?Closure $authResolver = null;
69

70
    /**
71
     * The Root timezone.
72
     */
73
    protected string $timezone;
74

75
    /**
76
     * Create a new Root instance.
77
     */
78
    public function __construct(Application $app)
198✔
79
    {
80
        $this->app = $app;
198✔
81
        $this->resources = new Resources;
198✔
82
        $this->widgets = new Widgets;
198✔
83
        $this->navigation = $app->make(Navigation::class);
198✔
84
        $this->breadcrumbs = $app->make(Breadcrumbs::class);
198✔
85
        $this->settings = $app->make(Settings::class);
198✔
86
        $this->timezone = $app['config']->get('app.timezone');
198✔
87
    }
88

89
    /**
90
     * Resolve the Root instance from the container.
91
     */
92
    public static function instance(): static
198✔
93
    {
94
        return App::make(static::class);
198✔
95
    }
96

97
    /**
98
     * Boot the Root instance.
99
     */
100
    public function boot(): void
198✔
101
    {
102
        $this->routes(function (Router $router): void {
198✔
103
            $this->widgets->registerRoutes($this->app['request'], $router);
198✔
104
        });
198✔
105

106
        $this->resources->discoverIn($this->app->path('Root/Resources'));
198✔
107

108
        $this->resources->each->boot($this);
198✔
109

110
        foreach ($this->booting as $callback) {
198✔
111
            call_user_func_array($callback, [$this]);
×
112
        }
113

114
        $path = $this->getPath();
198✔
115

116
        $this->breadcrumbs->patterns([
198✔
117
            $path => __('Dashboard'),
198✔
118
            sprintf('%s/resources/{resource}', $path) => static fn (Request $request): string => $request->route('_resource')->getName(),
198✔
119
            sprintf('%s/resources/{resource}/create', $path) => __('Create'),
198✔
120
            sprintf('%s/resources/{resource}/{resourceModel}', $path) => static function (Request $request): string {
198✔
121
                return $request->route('_resource')->modelTitle($request->route('resourceModel'));
7✔
122
            },
198✔
123
            sprintf('%s/resources/{resource}/{resourceModel}/edit', $path) => __('Edit'),
198✔
124
        ]);
198✔
125
    }
126

127
    /**
128
     * Register a booting callback.
129
     */
130
    public function booting(Closure $callback): void
×
131
    {
132
        $this->booting[] = $callback;
×
133
    }
134

135
    /**
136
     * Determine if Root should run on the given request.
137
     */
138
    public function shouldRun(Request $request): bool
×
139
    {
140
        $host = empty($this->getDomain())
×
141
            ? parse_url(Config::get('app.url'), PHP_URL_HOST)
×
142
            : $this->getDomain();
×
143

144
        $segments = explode('/', $request->getRequestUri());
×
145

146
        return (empty($this->getDomain()) || $request->getHost() === $host)
×
147
            && ($this->getPath() === '/' || $segments[1] === trim($this->getPath(), '/'));
×
148
    }
149

150
    /**
151
     * Register the root routes.
152
     */
153
    public function routes(Closure $callback): void
198✔
154
    {
155
        Route::as('root.')
198✔
156
            ->domain($this->getDomain())
198✔
157
            ->prefix($this->getPath())
198✔
158
            ->middleware(['root'])
198✔
159
            ->group($callback);
198✔
160
    }
161

162
    /**
163
     * Get the Root URI path.
164
     */
165
    public function getPath(): string
198✔
166
    {
167
        return Str::start(Config::get('root.path', 'root'), '/');
198✔
168
    }
169

170
    /**
171
     * Get the Root domain.
172
     */
173
    public function getDomain(): string
198✔
174
    {
175
        return (string) Config::get('root.domain', null);
198✔
176
    }
177

178
    /**
179
     * Determine whether the given user is authorized.
180
     */
181
    public function authorized(User $user): bool
33✔
182
    {
183
        return ! is_null($this->authResolver)
33✔
184
            ? call_user_func_array($this->authResolver, [$user])
×
185
            : true;
33✔
186
    }
187

188
    /**
189
     * Set the auth resolver callback.
190
     */
191
    public function authorize(Closure $callback): void
×
192
    {
193
        $this->authResolver = $callback;
×
194
    }
195

196
    /**
197
     * Set the Root timezone.
198
     */
199
    public function setTimezone(string|DateTimeZone $value): void
×
200
    {
201
        $this->timezone = $value instanceof DateTimeZone ? $value->getName() : $value;
×
202
    }
203

204
    /**
205
     * Get the Root timezone.
206
     */
207
    public function getTimezone(): string
198✔
208
    {
209
        return $this->timezone;
198✔
210
    }
211
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc