Completed
Branch develop-3.0 (63d375)
by Mohamed
02:58
created

BladeServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Providers;
13
14
use Illuminate\Support\ServiceProvider;
15
16
/**
17
 * BladeServiceProvider is the blade service provider for extending blade template engine.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class BladeServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $replacements = [
27
        'macro'    => "<?php \$___tiny['%s']=function(%s)use(\$__env){ ob_start(); ?>\n",
28
        'usemacro' => "<?php echo \$___tiny['%s'](%s); ?>\n",
29
    ];
30
31
    /**
32
     * Bootstrap any application services.
33
     */
34
    public function boot()
35
    {
36
        require base_path('resources/macros/blade.php');
37
    }
38
39
    /**
40
     * Callback method for macro directive.
41
     *
42
     * @param string $name
43
     * @param string $expression
44
     *
45
     * @return string
46
     */
47
    protected function directive($name, $expression)
48
    {
49
        $arguments = $this->extractArgumentsAndName($expression);
50
51
        return sprintf($this->replacements[$name], $arguments['name'], $arguments['args']);
52
    }
53
54
    /**
55
     * Return arguments and name from blade expression.
56
     *
57
     * @param $expression
58
     *
59
     * @return array
60
     */
61
    protected function extractArgumentsAndName($expression)
62
    {
63
        $pattern = '/([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*)))/xim';
64
        $matches = [];
65
        preg_match_all($pattern, $expression, $matches);
66
67
        if (!isset($matches[3][0])) {
68
            throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: macro%s', $expression));
69
        }
70
71
        return [
72
            'name' => $matches[2][0],
73
            'args' => $matches[3][0],
74
        ];
75
    }
76
77
    /**
78
     * Register any application services.
79
     *
80
     * This service provider is a great spot to register your various container
81
     * bindings with the application. As you can see, we are registering our
82
     * "Registrar" implementation here. You can add your own bindings too!
83
     */
84
    public function register()
85
    {
86
    }
87
}
88