Completed
Push — master ( 3bdb8b...8aadce )
by Freek
05:25 queued 01:17
created

BladeJavaScriptServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 13 1
A makeBackwardsCompatible() 0 8 2
A register() 0 7 1
1
<?php
2
3
namespace Spatie\BladeJavaScript;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
8
class BladeJavaScriptServiceProvider extends ServiceProvider
9
{
10
    public function boot()
11
    {
12
        $this->publishes([
13
            __DIR__.'/../config/laravel-blade-javascript.php' => config_path('laravel-blade-javascript.php'),
14
        ], 'config');
15
16
        Blade::directive('javascript', function ($expression) {
17
18
            $expression = $this->makeBackwardsCompatible($expression);
19
20
            return "<?= app('\Spatie\BladeJavaScript\Renderer')->render{$expression}; ?>";
21
        });
22
    }
23
24
    public function makeBackwardsCompatible($expression)
25
    {
26
        if (starts_with(app()->version(), ['5.1', '5.2'])) {
27
            return $expression;
28
        }
29
30
        return "({$expression})";
31
    }
32
33
    public function register()
34
    {
35
        $this->mergeConfigFrom(
36
            __DIR__.'/../config/laravel-blade-javascript.php',
37
            'laravel-blade-javascript'
38
        );
39
    }
40
}
41