Completed
Push — master ( 0d121e...4e7962 )
by Maksim (Ellrion)
9s
created

BladeLoopServiceProvider::extendBladeEngine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Advmaker\BladeLoop;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\View\Compilers\BladeCompiler;
8
9
class BladeLoopServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Register the service provider.
13
     *
14
     * @return void
15
     */
16 10
    public function register()
17
    {
18 10
        $this->app->singleton('blade.loop', LoopFactory::class);
19
20 5
        $this->app->extend('blade.compiler', function (BladeCompiler $blade, Application $app) {
21 15
            $blade = $this->addLoopDirectives($blade);
22
23 15
            if (version_compare($app->version(), '5.2.21', '<')) {
24 5
                $blade = $this->addLoopControlDirectives($blade);
25 5
            }
26
27 15
            return $blade;
28 15
        });
29 15
    }
30
31
    /**
32
     * Extend blade by loop directives.
33
     *
34
     * @param  BladeCompiler $blade
35
     *
36
     * @return BladeCompiler
37
     */
38 10 View Code Duplication
    private function addLoopDirectives(BladeCompiler $blade)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $blade->extend(function ($value) {
41 10
            $pattern = '/(?<!\\w)(\\s*)@loop(?:\\s*)\\((.*)(?:\\sas\\s)([^)]*)\\)/';
42
            $replacement = <<<'EOT'
43
$1<?php
44
$loop = app('blade.loop')->newLoop($2);
45
foreach($loop->getItems() as $3):
46
    $loop = app('blade.loop')->loop();
47
?>
48 10
EOT;
49 10
            return preg_replace($pattern, $replacement, $value);
50 10
        });
51
52
        $blade->extend(function ($value) {
53 10
            $pattern = '/(?<!\\w)(\\s*)@endloop(\\s*)/';
54
            $replacement = <<<'EOT'
55
$1<?php
56
endforeach;
57
app('blade.loop')->endLoop($loop);
58
?>$2
59 10
EOT;
60 10
            return preg_replace($pattern, $replacement, $value);
61 10
        });
62
63 10
        return $blade;
64
    }
65
66
    /**
67
     * Extend blade by continue and break directives.
68
     *
69
     * @param  BladeCompiler $blade
70
     *
71
     * @return BladeCompiler
72
     */
73 View Code Duplication
    private function addLoopControlDirectives(BladeCompiler $blade)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $blade->extend(function ($value) {
76
            $pattern = '/(?<!\\w)(\\s*)@continue\\s*\\(([^)]*)\\)/';
77
            $replacement = '$1<?php if ($2) { continue; } ?>';
78
79
            return preg_replace($pattern, $replacement, $value);
80
        });
81
82
        $blade->extend(function ($value) {
83
            $pattern = '/(?<!\\w)(\\s*)@break\\s*\\(([^)]*)\\)/';
84
            $replacement = '$1<?php if ($2) { break; } ?>';
85
86
            return preg_replace($pattern, $replacement, $value);
87
        });
88
89
        return $blade;
90
    }
91
}
92