Completed
Push — master ( 6ec8b5...17954d )
by Maksim (Ellrion)
8s
created

BladeLoopServiceProvider::extendBladeEngine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Advmaker\BladeLoop;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
8
class BladeLoopServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Register the service provider.
12
     *
13
     * @return void
14
     */
15
    public function register()
16
    {
17
        $this->app->singleton('blade.loop', LoopFactory::class);
18
19
        $this->app->extend('blade.compiler', function ($blade) {
20
            return $this->extendBladeEngine($blade);
21
        });
22
    }
23
24
    /**
25
     * Extend blade by new directives.
26
     *
27
     * @param  BladeCompiler $blade
28
     *
29
     * @return BladeCompiler
30
     */
31
    public function extendBladeEngine(BladeCompiler $blade)
32
    {
33
        $directives = $this->app->make('files')->getRequire(__DIR__ . '/directives.php');
34
35
        foreach ($directives as $name => $directive) {
36
            $blade->extend(function ($value) use ($directive) {
37
                return preg_replace($directive['pattern'], $directive['replacement'], $value);
38
            });
39
        }
40
41
        return $blade;
42
    }
43
}
44