Completed
Push — master ( b015a1...0e1003 )
by Abdelrahman
49:45
created

FoundationServiceProvider::bindBladeCompiler()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Cortex Foundation Module.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Cortex Foundation Module
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
declare(strict_types=1);
17
18
namespace Cortex\Foundation\Providers;
19
20
use Illuminate\Routing\Router;
21
use Illuminate\Contracts\Http\Kernel;
22
use Illuminate\Support\ServiceProvider;
23
use Illuminate\View\Compilers\BladeCompiler;
24
use Cortex\Foundation\Http\Middleware\TrailingSlashEnforce;
25
use Cortex\Foundation\Http\Middleware\NotificationMiddleware;
26
use Cortex\Foundation\Overrides\Illuminate\Routing\Redirector;
27
use Cortex\Foundation\Overrides\Illuminate\Routing\UrlGenerator;
28
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter;
29
use Cortex\Foundation\Overrides\Mcamara\LaravelLocalization\LaravelLocalization;
30
31
class FoundationServiceProvider extends ServiceProvider
32
{
33
    /**
34
     * Bootstrap any application services.
35
     *
36
     * @return void
37
     */
38
    public function boot(Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        // Early set application locale globaly
41
        $this->app['laravellocalization']->setLocale();
42
43
        // Require Support Files
44
        $this->requireSupportFiles();
45
    }
46
47
    /**
48
     * Register any application services.
49
     *
50
     * This service provider is a great spot to register your various container
51
     * bindings with the application. As you can see, we are registering our
52
     * "Registrar" implementation here. You can add your own bindings too!
53
     *
54
     * @return void
55
     */
56
    public function register()
57
    {
58
        $this->overrideNotificationMiddleware();
59
        $this->registerDevelopmentProviders();
60
        $this->overrideLaravelLocalization();
61
        $this->overrideUrlGenerator();
62
        $this->overrideRedirector();
63
        $this->bindBladeCompiler();
64
        $this->setBackendUri();
65
66
        // Add required middleware to the stack
67
        $this->prependMiddleware();
68
    }
69
70
    /**
71
     * Override notification middleware.
72
     *
73
     * @return void
74
     */
75
    protected function overrideNotificationMiddleware()
76
    {
77
        $this->app->singleton('Cortex\Foundation\Http\Middleware\NotificationMiddleware', function ($app) {
78
            return new NotificationMiddleware(
79
                $app['session.store'],
80
                $app['notification'],
81
                $app['config']->get('notification.session_key')
82
            );
83
        });
84
    }
85
86
    /**
87
     * Bind blade compiler.
88
     *
89
     * @return void
90
     */
91
    protected function bindBladeCompiler()
92
    {
93
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
94
95
            // @alerts('container')
96
            $bladeCompiler->directive('alerts', function ($container = null) {
97
                if (strcasecmp('()', $container) === 0) {
98
                    $container = null;
99
                }
100
101
                return "<?php echo app('notification')->container({$container})->show(); ?>";
102
            });
103
        });
104
    }
105
106
    /**
107
     * Prepends the bootstrap middleware.
108
     *
109
     * @return void
110
     */
111
    protected function prependMiddleware()
112
    {
113
        if ($this->app['config']->get('rinvex.cortex.route.locale_prefix') && $this->app['config']->get('rinvex.cortex.route.locale_redirect')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
114
            $this->app[Kernel::class]->prependMiddleware(LaravelLocalizationRedirectFilter::class);
115
        }
116
117
        if ($this->app['config']->get('rinvex.cortex.route.trailing_slash')) {
118
            $this->app[Kernel::class]->prependMiddleware(TrailingSlashEnforce::class);
119
        }
120
    }
121
122
    /**
123
     * Require support files.
124
     *
125
     * @return void
126
     */
127
    protected function requireSupportFiles()
128
    {
129
        // Load the functions
130
        $helpers = $this->app->path().'/Support/helpers.php';
131
132
        if ($this->app['files']->exists($helpers)) {
133
            require $helpers;
134
        }
135
136
        // Load the form macros
137
        $macros = $this->app->path().'/Support/macros.php';
138
139
        if ($this->app['files']->exists($macros)) {
140
            require $macros;
141
        }
142
    }
143
144
    /**
145
     * Registers the Generic bindings.
146
     *
147
     * @return void
148
     */
149
    protected function registerDevelopmentProviders()
150
    {
151
        if ($this->app->environment() !== 'production') {
152
            $this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
153
            $this->app->register(\Clockwork\Support\Laravel\ClockworkServiceProvider::class);
154
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
155
        }
156
    }
157
158
    /**
159
     * Override the Redirector instance.
160
     *
161
     * @return void
162
     */
163
    protected function overrideRedirector()
164
    {
165
        $this->app->singleton('redirect', function ($app) {
166
            $redirector = new Redirector($app['url']);
167
168
            // If the session is set on the application instance, we'll inject it into
169
            // the redirector instance. This allows the redirect responses to allow
170
            // for the quite convenient "with" methods that flash to the session.
171
            if (isset($app['session.store'])) {
172
                $redirector->setSession($app['session.store']);
173
            }
174
175
            return $redirector;
176
        });
177
    }
178
179
    /**
180
     * Override the UrlGenerator instance.
181
     *
182
     * @return void
183
     */
184
    protected function overrideUrlGenerator()
185
    {
186
        $this->app->singleton('url', function ($app) {
187
            $routes = $app['router']->getRoutes();
188
189
            // The URL generator needs the route collection that exists on the router.
190
            // Keep in mind this is an object, so we're passing by references here
191
            // and all the registered routes will be available to the generator.
192
            $app->instance('routes', $routes);
193
194
            $url = new UrlGenerator(
195
                $routes, $app->rebinding(
196
                    'request', $this->requestRebinder()
197
                )
198
            );
199
200
            $url->setSessionResolver(function () {
201
                return $this->app['session'];
202
            });
203
204
            // If the route collection is "rebound", for example, when the routes stay
205
            // cached for the application, we will need to rebind the routes on the
206
            // URL generator instance so it has the latest version of the routes.
207
            $app->rebinding('routes', function ($app, $routes) {
208
                $app['url']->setRoutes($routes);
209
            });
210
211
            return $url;
212
        });
213
    }
214
215
    /**
216
     * Get the URL generator request rebinder.
217
     *
218
     * @return \Closure
219
     */
220
    protected function requestRebinder()
221
    {
222
        return function ($app, $request) {
223
            $app['url']->setRequest($request);
224
        };
225
    }
226
227
    /**
228
     * Override the LaravelLocalization instance.
229
     *
230
     * @return void
231
     */
232
    protected function overrideLaravelLocalization()
233
    {
234
        $this->app->singleton('laravellocalization', function () {
235
            return new LaravelLocalization();
236
        });
237
    }
238
239
    /**
240
     * Set the backend uri on the url generator.
241
     *
242
     * @return void
243
     */
244
    protected function setBackendUri()
245
    {
246
        $this->app['url']->setBackendUri(backend_uri());
247
    }
248
}
249