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

conedevelopment / root / 12196927091

06 Dec 2024 10:11AM UTC coverage: 77.817%. Remained the same
12196927091

push

github

iamgergo
wip

0 of 1 new or added line in 1 file covered. (0.0%)

2659 of 3417 relevant lines covered (77.82%)

33.88 hits per line

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

93.4
/src/RootServiceProvider.php
1
<?php
2

3
namespace Cone\Root;
4

5
use Cone\Root\Exceptions\ResourceResolutionException;
6
use Cone\Root\Exceptions\SaveFormDataException;
7
use Cone\Root\Models\Medium;
8
use Cone\Root\Models\User;
9
use Cone\Root\Policies\MediumPolicy;
10
use Cone\Root\Resources\Resource;
11
use Cone\Root\Support\Alert;
12
use Illuminate\Cache\RateLimiting\Limit;
13
use Illuminate\Contracts\Debug\ExceptionHandler;
14
use Illuminate\Contracts\Foundation\Application;
15
use Illuminate\Contracts\View\View;
16
use Illuminate\Cookie\Middleware\EncryptCookies;
17
use Illuminate\Database\Eloquent\Model;
18
use Illuminate\Foundation\Console\AboutCommand;
19
use Illuminate\Foundation\Events\VendorTagPublished;
20
use Illuminate\Http\RedirectResponse;
21
use Illuminate\Http\Request;
22
use Illuminate\Routing\Route;
23
use Illuminate\Routing\Router;
24
use Illuminate\Support\Facades\Blade;
25
use Illuminate\Support\Facades\Gate;
26
use Illuminate\Support\Facades\RateLimiter;
27
use Illuminate\Support\Facades\Redirect;
28
use Illuminate\Support\ServiceProvider;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30

31
class RootServiceProvider extends ServiceProvider
32
{
33
    /**
34
     * All of the container bindings that should be registered.
35
     *
36
     * @var array
37
     */
38
    public $bindings = [
39
        Interfaces\Breadcrumbs\Registry::class => Breadcrumbs\Registry::class,
40
        Interfaces\Models\AuthCode::class => Models\AuthCode::class,
41
        Interfaces\Models\Medium::class => Models\Medium::class,
42
        Interfaces\Models\Meta::class => Models\Meta::class,
43
        Interfaces\Models\Notification::class => Models\Notification::class,
44
        Interfaces\Models\Setting::class => Models\Setting::class,
45
        Interfaces\Models\Translation::class => Models\Translation::class,
46
        Interfaces\Models\User::class => Models\User::class,
47
        Interfaces\Navigation\Registry::class => Navigation\Registry::class,
48
        Interfaces\Settings\Registry::class => Settings\Registry::class,
49
        Interfaces\Settings\Repository::class => Settings\Repository::class,
50
    ];
51

52
    /**
53
     * All of the container singletons that should be registered.
54
     *
55
     * @var array
56
     */
57
    public $singletons = [
58
        Interfaces\Conversion\Manager::class => Conversion\Manager::class,
59
    ];
60

61
    /**
62
     * Register any application services.
63
     */
64
    public function register(): void
65
    {
66
        $this->app->singleton(Root::class, static function (Application $app): Root {
198✔
67
            return new Root($app);
198✔
68
        });
198✔
69

70
        $this->app->alias(Root::class, 'root');
198✔
71

72
        if (! $this->app->configurationIsCached()) {
198✔
73
            $this->mergeConfigFrom(__DIR__.'/../config/root.php', 'root');
198✔
74
        }
75

76
        $this->app->afterResolving(EncryptCookies::class, static function (EncryptCookies $middleware): void {
198✔
77
            $middleware->disableFor('__root_theme');
42✔
78
        });
198✔
79

80
        $this->app->booted(static function (Application $app): void {
198✔
81
            $app->make(Root::class)->boot();
198✔
82
        });
198✔
83

84
        $this->app['request']->macro('isTurboFrameRequest', function (): bool {
198✔
85
            return $this->hasHeader('Turbo-Frame');
5✔
86
        });
198✔
87
    }
88

89
    /**
90
     * Bootstrap any application services.
91
     */
92
    public function boot(): void
93
    {
94
        if ($this->app->runningInConsole()) {
198✔
95
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
198✔
96
            $this->registerCommands();
198✔
97
            $this->registerPublishes();
198✔
98
        }
99

100
        $this->registerViews();
198✔
101
        $this->registerRoutes();
198✔
102
        $this->registerExceptions();
198✔
103
        $this->registerAuth();
198✔
104
        $this->registerEvents();
198✔
105
    }
106

107
    /**
108
     * Register publishes.
109
     */
110
    protected function registerPublishes(): void
111
    {
112
        $this->publishes([
198✔
113
            __DIR__.'/../config/root.php' => $this->app->configPath('root.php'),
198✔
114
        ], 'root-config');
198✔
115

116
        $this->publishes([
198✔
117
            __DIR__.'/../public' => $this->app->publicPath('vendor/root'),
198✔
118
        ], 'root-compiled');
198✔
119

120
        $this->publishes([
198✔
121
            __DIR__.'/../resources/js' => $this->app->resourcePath('js/vendor/root'),
198✔
122
            __DIR__.'/../resources/sass' => $this->app->resourcePath('sass/vendor/root'),
198✔
123
        ], 'root-vendor');
198✔
124

125
        $this->publishes([
198✔
126
            __DIR__.'/../resources/views' => $this->app->resourcePath('views/vendor/root'),
198✔
127
        ], 'root-views');
198✔
128

129
        $this->publishes([
198✔
130
            __DIR__.'/../stubs/RootServiceProvider.stub' => $this->app->basePath('app/Providers/RootServiceProvider.php'),
198✔
131
            __DIR__.'/../stubs/UserResource.stub' => $this->app->basePath('app/Root/Resources/UserResource.php'),
198✔
132
        ], 'root-stubs');
198✔
133
    }
134

135
    /**
136
     * Register the routes.
137
     */
138
    protected function registerRoutes(): void
139
    {
140
        $this->app['router']->middlewareGroup(
198✔
141
            'root', $this->app['config']->get('root.middleware', [])
198✔
142
        );
198✔
143

144
        $root = $this->app->make(Root::class);
198✔
145

146
        $this->app['router']->bind('resource', function (string $key) use ($root): Resource {
198✔
147
            try {
148
                return $root->resources->resolve($key);
7✔
149
            } catch (ResourceResolutionException $exception) {
×
NEW
150
                throw new NotFoundHttpException;
×
151
            }
152
        });
198✔
153

154
        $this->app['router']->bind('resourceModel', function (string $id, Route $route): Model {
198✔
155
            return $id === 'create'
20✔
156
                ? $route->parameter('_resource')->getModelInstance()
×
157
                : $route->parameter('_resource')->resolveRouteBinding($this->app['request'], $id);
20✔
158
        });
198✔
159

160
        $this->app['router']
198✔
161
            ->middleware(['web'])
198✔
162
            ->domain($root->getDomain())
198✔
163
            ->prefix($root->getPath())
198✔
164
            ->as('root.auth.')
198✔
165
            ->group(function (): void {
198✔
166
                $this->loadRoutesFrom(__DIR__.'/../routes/auth.php');
198✔
167
            });
198✔
168

169
        $root->routes(function (Router $router): void {
198✔
170
            $router->prefix('api')->as('api.')->group(function (): void {
198✔
171
                $this->loadRoutesFrom(__DIR__.'/../routes/api.php');
198✔
172
            });
198✔
173

174
            $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
198✔
175
        });
198✔
176

177
        RateLimiter::for('root.auth', static function (Request $request): Limit {
198✔
178
            return Limit::perMinute(6)->by($request->user()?->id ?: $request->ip());
×
179
        });
198✔
180
    }
181

182
    /**
183
     * Register commands.
184
     */
185
    protected function registerCommands(): void
186
    {
187
        AboutCommand::add('Root', fn (): array => ['Version' => Root::VERSION]);
198✔
188

189
        $this->commands([
198✔
190
            Console\Commands\ActionMake::class,
198✔
191
            Console\Commands\ClearChunks::class,
198✔
192
            Console\Commands\ClearMedia::class,
198✔
193
            Console\Commands\FieldMake::class,
198✔
194
            Console\Commands\FilterMake::class,
198✔
195
            Console\Commands\Install::class,
198✔
196
            Console\Commands\Publish::class,
198✔
197
            Console\Commands\ResourceMake::class,
198✔
198
            Console\Commands\TrendMake::class,
198✔
199
            Console\Commands\ValueMake::class,
198✔
200
            Console\Commands\WidgetMake::class,
198✔
201
        ]);
198✔
202
    }
203

204
    /**
205
     * Register the views.
206
     */
207
    protected function registerViews(): void
208
    {
209
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'root');
198✔
210

211
        Blade::componentNamespace('Cone\\Root\\View\\Components', 'root');
198✔
212

213
        $this->app['view']->composer('root::*', function (View $view): void {
198✔
214
            $request = $this->app->make('request');
19✔
215

216
            $view->with([
19✔
217
                'alerts' => $request->session()->get('alerts', []),
19✔
218
                'user' => $request->user(),
19✔
219
            ]);
19✔
220
        });
198✔
221
    }
222

223
    /**
224
     * Register the custom exceptions.
225
     */
226
    protected function registerExceptions(): void
227
    {
228
        $exceptions = $this->app->make(ExceptionHandler::class);
198✔
229

230
        $exceptions->renderable(static function (SaveFormDataException $exception): RedirectResponse {
198✔
231
            return Redirect::back()
×
232
                ->withInput()
×
233
                ->with('alerts.form-save', Alert::error($exception->getMessage()));
×
234
        });
198✔
235
    }
236

237
    /**
238
     * Register the auth features.
239
     */
240
    protected function registerAuth(): void
241
    {
242
        Gate::define('viewRoot', static function (User $user): bool {
198✔
243
            return Root::instance()->authorized($user);
33✔
244
        });
198✔
245

246
        Gate::policy(Medium::getProxiedClass(), MediumPolicy::class);
198✔
247
    }
248

249
    /**
250
     * Register the events.
251
     */
252
    protected function registerEvents(): void
253
    {
254
        $this->app['events']->listen(VendorTagPublished::class, Listeners\FormatRootStubs::class);
198✔
255
    }
256
}
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