Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View\Compilers\Concerns;
4
 
5
use Closure;
6
use Illuminate\Support\Str;
7
 
8
trait CompilesEchos
9
{
10
    /**
11
     * Custom rendering callbacks for stringable objects.
12
     *
13
     * @var array
14
     */
15
    protected $echoHandlers = [];
16
 
17
    /**
18
     * Add a handler to be executed before echoing a given class.
19
     *
20
     * @param  string|callable  $class
21
     * @param  callable|null  $handler
22
     * @return void
23
     */
24
    public function stringable($class, $handler = null)
25
    {
26
        if ($class instanceof Closure) {
27
            [$class, $handler] = [$this->firstClosureParameterType($class), $class];
28
        }
29
 
30
        $this->echoHandlers[$class] = $handler;
31
    }
32
 
33
    /**
34
     * Compile Blade echos into valid PHP.
35
     *
36
     * @param  string  $value
37
     * @return string
38
     */
39
    public function compileEchos($value)
40
    {
41
        foreach ($this->getEchoMethods() as $method) {
42
            $value = $this->$method($value);
43
        }
44
 
45
        return $value;
46
    }
47
 
48
    /**
49
     * Get the echo methods in the proper order for compilation.
50
     *
51
     * @return array
52
     */
53
    protected function getEchoMethods()
54
    {
55
        return [
56
            'compileRawEchos',
57
            'compileEscapedEchos',
58
            'compileRegularEchos',
59
        ];
60
    }
61
 
62
    /**
63
     * Compile the "raw" echo statements.
64
     *
65
     * @param  string  $value
66
     * @return string
67
     */
68
    protected function compileRawEchos($value)
69
    {
70
        $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]);
71
 
72
        $callback = function ($matches) {
73
            $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
74
 
75
            return $matches[1]
76
                ? substr($matches[0], 1)
77
                : "<?php echo {$this->wrapInEchoHandler($matches[2])}; ?>{$whitespace}";
78
        };
79
 
80
        return preg_replace_callback($pattern, $callback, $value);
81
    }
82
 
83
    /**
84
     * Compile the "regular" echo statements.
85
     *
86
     * @param  string  $value
87
     * @return string
88
     */
89
    protected function compileRegularEchos($value)
90
    {
91
        $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
92
 
93
        $callback = function ($matches) {
94
            $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
95
 
96
            $wrapped = sprintf($this->echoFormat, $this->wrapInEchoHandler($matches[2]));
97
 
98
            return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}";
99
        };
100
 
101
        return preg_replace_callback($pattern, $callback, $value);
102
    }
103
 
104
    /**
105
     * Compile the escaped echo statements.
106
     *
107
     * @param  string  $value
108
     * @return string
109
     */
110
    protected function compileEscapedEchos($value)
111
    {
112
        $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]);
113
 
114
        $callback = function ($matches) {
115
            $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
116
 
117
            return $matches[1]
118
                ? $matches[0]
119
                : "<?php echo e({$this->wrapInEchoHandler($matches[2])}); ?>{$whitespace}";
120
        };
121
 
122
        return preg_replace_callback($pattern, $callback, $value);
123
    }
124
 
125
    /**
126
     * Add an instance of the blade echo handler to the start of the compiled string.
127
     *
128
     * @param  string  $result
129
     * @return string
130
     */
131
    protected function addBladeCompilerVariable($result)
132
    {
133
        return "<?php \$__bladeCompiler = app('blade.compiler'); ?>".$result;
134
    }
135
 
136
    /**
137
     * Wrap the echoable value in an echo handler if applicable.
138
     *
139
     * @param  string  $value
140
     * @return string
141
     */
142
    protected function wrapInEchoHandler($value)
143
    {
144
        $value = Str::of($value)
145
            ->trim()
146
            ->when(str_ends_with($value, ';'), function ($str) {
147
                return $str->beforeLast(';');
148
            });
149
 
150
        return empty($this->echoHandlers) ? $value : '$__bladeCompiler->applyEchoHandler('.$value.')';
151
    }
152
 
153
    /**
154
     * Apply the echo handler for the value if it exists.
155
     *
156
     * @param  string  $value
157
     * @return string
158
     */
159
    public function applyEchoHandler($value)
160
    {
161
        if (is_object($value) && isset($this->echoHandlers[get_class($value)])) {
162
            return call_user_func($this->echoHandlers[get_class($value)], $value);
163
        }
164
 
165
        return $value;
166
    }
167
}