These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\BladeX; |
||
4 | |||
5 | class BladeXCompiler |
||
6 | { |
||
7 | /** @var \Spatie\BladeX\BladeX */ |
||
8 | protected $bladeX; |
||
9 | |||
10 | public function __construct(BladeX $bladeX) |
||
11 | { |
||
12 | return $this->bladeX = $bladeX; |
||
13 | } |
||
14 | |||
15 | public function compile(string $viewContents): string |
||
16 | { |
||
17 | return array_reduce( |
||
18 | $this->bladeX->getRegisteredComponents(), |
||
19 | [$this, 'parseComponentHtml'], |
||
20 | $viewContents |
||
21 | ); |
||
22 | } |
||
23 | |||
24 | protected function parseComponentHtml(string $viewContents, BladeXComponent $bladeXComponent) |
||
25 | { |
||
26 | $viewContents = $this->parseSlots($viewContents); |
||
27 | |||
28 | $viewContents = $this->parseSelfClosingTags($viewContents, $bladeXComponent); |
||
29 | |||
30 | $viewContents = $this->parseOpeningTags($viewContents, $bladeXComponent); |
||
31 | |||
32 | $viewContents = $this->parseClosingTags($viewContents, $bladeXComponent); |
||
33 | |||
34 | return $viewContents; |
||
35 | } |
||
36 | |||
37 | View Code Duplication | protected function parseSelfClosingTags(string $viewContents, BladeXComponent $bladeXComponent): string |
|
0 ignored issues
–
show
|
|||
38 | { |
||
39 | $prefix = $this->bladeX->getPrefix(); |
||
40 | |||
41 | $pattern = "/<\s*{$prefix}{$bladeXComponent->name}\s*(?<attributes>.*)\s*\/>/"; |
||
42 | |||
43 | return preg_replace_callback($pattern, function (array $matches) use ($bladeXComponent) { |
||
44 | $attributes = $this->getAttributesFromAttributeString($matches['attributes']); |
||
45 | |||
46 | return $this->componentString($bladeXComponent, $attributes); |
||
47 | }, $viewContents); |
||
48 | } |
||
49 | |||
50 | View Code Duplication | protected function parseOpeningTags(string $viewContents, BladeXComponent $bladeXComponent): string |
|
0 ignored issues
–
show
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. ![]() |
|||
51 | { |
||
52 | $prefix = $this->bladeX->getPrefix(); |
||
53 | |||
54 | $pattern = "/<\s*{$prefix}{$bladeXComponent->name}(?<attributes>(?:\s+[\w\-:]+=(?:\\\"[^\\\"]+\\\"|\'[^\']+\'|[^\'\\\"=<>]+))*\s*)(?<![\/=\-])>/"; |
||
55 | |||
56 | return preg_replace_callback($pattern, function (array $matches) use ($bladeXComponent) { |
||
57 | $attributes = $this->getAttributesFromAttributeString($matches['attributes']); |
||
58 | |||
59 | return $this->componentStartString($bladeXComponent, $attributes); |
||
60 | }, $viewContents); |
||
61 | } |
||
62 | |||
63 | protected function parseClosingTags(string $viewContents, BladeXComponent $bladeXComponent): string |
||
64 | { |
||
65 | $prefix = $this->bladeX->getPrefix(); |
||
66 | |||
67 | $pattern = "/<\/\s*{$prefix}{$bladeXComponent->name}[^>]*>/"; |
||
68 | |||
69 | return preg_replace($pattern, $this->componentEndString($bladeXComponent), $viewContents); |
||
70 | } |
||
71 | |||
72 | protected function componentString(BladeXComponent $bladeXComponent, array $attributes = []): string |
||
73 | { |
||
74 | return $this->componentStartString($bladeXComponent, $attributes).$this->componentEndString($bladeXComponent); |
||
75 | } |
||
76 | |||
77 | protected function componentStartString(BladeXComponent $bladeXComponent, array $attributes = []): string |
||
78 | { |
||
79 | $attributesString = $this->attributesToString($attributes); |
||
80 | |||
81 | $componentAttributeString = "[{$attributesString}]"; |
||
82 | |||
83 | if ($bladeXComponent->bladeViewName === 'bladex::context') { |
||
84 | return "@php(app(Spatie\BladeX\ContextStack::class)->push({$componentAttributeString}))"; |
||
85 | } |
||
86 | |||
87 | if ($bladeXComponent->viewModel) { |
||
88 | $componentAttributeString = " |
||
89 | array_merge( |
||
90 | app(Spatie\BladeX\ContextStack::class)->read(), |
||
91 | {$componentAttributeString}, |
||
92 | app( |
||
93 | '{$bladeXComponent->viewModel}', |
||
94 | array_merge( |
||
95 | app(Spatie\BladeX\ContextStack::class)->read(), |
||
96 | {$componentAttributeString} |
||
97 | ) |
||
98 | )->toArray() |
||
99 | )"; |
||
100 | } |
||
101 | |||
102 | return " @component( |
||
103 | '{$bladeXComponent->bladeViewName}', |
||
104 | array_merge(app(Spatie\BladeX\ContextStack::class)->read(), {$componentAttributeString})) "; |
||
105 | } |
||
106 | |||
107 | protected function componentEndString(BladeXComponent $bladeXComponent): string |
||
108 | { |
||
109 | if ($bladeXComponent->bladeViewName === 'bladex::context') { |
||
110 | return "@php(app(Spatie\BladeX\ContextStack::class)->pop())"; |
||
111 | } |
||
112 | |||
113 | return ' @endcomponent '; |
||
114 | } |
||
115 | |||
116 | protected function getAttributesFromAttributeString(string $attributeString): array |
||
117 | { |
||
118 | $attributeString = $this->parseBindAttributes($attributeString); |
||
119 | |||
120 | $pattern = '/(?<attribute>[\w:-]+)(=(?<value>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+)))?/'; |
||
121 | |||
122 | if (! preg_match_all($pattern, $attributeString, $matches, PREG_SET_ORDER)) { |
||
123 | return []; |
||
124 | } |
||
125 | |||
126 | return collect($matches)->mapWithKeys(function ($match) { |
||
127 | $attribute = camel_case($match['attribute']); |
||
128 | $value = $match['value'] ?? null; |
||
129 | |||
130 | if (is_null($value)) { |
||
131 | $value = 'true'; |
||
132 | $attribute = str_start($attribute, 'bind:'); |
||
133 | } |
||
134 | |||
135 | $value = $this->stripQuotes($value); |
||
136 | |||
137 | if (! starts_with($attribute, 'bind:')) { |
||
138 | $value = str_replace("'", "\\'", $value); |
||
139 | $value = "'{$value}'"; |
||
140 | } |
||
141 | |||
142 | if (starts_with($attribute, 'bind:')) { |
||
143 | $attribute = str_after($attribute, 'bind:'); |
||
144 | } |
||
145 | |||
146 | return [$attribute => $value]; |
||
147 | })->toArray(); |
||
148 | } |
||
149 | |||
150 | protected function parseSlots(string $viewContents): string |
||
151 | { |
||
152 | $openingPattern = '/<\s*slot\s+name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+))\s*>/'; |
||
153 | $viewContents = preg_replace_callback($openingPattern, function ($matches) { |
||
154 | $name = $this->stripQuotes($matches['name']); |
||
155 | |||
156 | return " @slot('{$name}') "; |
||
157 | }, $viewContents); |
||
158 | |||
159 | $closingPattern = '/<\/\s*slot[^>]*>/'; |
||
160 | $viewContents = preg_replace($closingPattern, ' @endslot', $viewContents); |
||
161 | |||
162 | return $viewContents; |
||
163 | } |
||
164 | |||
165 | protected function isOpeningHtmlTag(string $tagName, string $html): bool |
||
166 | { |
||
167 | return ! ends_with($html, ["</{$tagName}>", '/>']); |
||
168 | } |
||
169 | |||
170 | protected function parseBindAttributes(string $attributeString): string |
||
171 | { |
||
172 | return preg_replace("/\s*:([\w-]+)=/m", ' bind:$1=', $attributeString); |
||
173 | } |
||
174 | |||
175 | protected function attributesToString(array $attributes): string |
||
176 | { |
||
177 | return collect($attributes) |
||
178 | ->map(function (string $value, string $attribute) { |
||
179 | return "'{$attribute}' => {$value}"; |
||
180 | }) |
||
181 | ->implode(','); |
||
182 | } |
||
183 | |||
184 | protected function stripQuotes(string $string): string |
||
185 | { |
||
186 | if (starts_with($string, ['"', '\''])) { |
||
187 | return substr($string, 1, -1); |
||
188 | } |
||
189 | |||
190 | return $string; |
||
191 | } |
||
192 | } |
||
193 |
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.