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

makeBackwardsCompatible()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 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