Completed
Push — master ( ec58d1...1dd87c )
by Freek
01:15
created

BladeXCompiler::parseComponentHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use SimpleXMLElement;
6
7
class BladeXCompiler
8
{
9
    /** @var \Spatie\BladeX\BladeX */
10
    protected $bladeX;
11
12
    public function __construct(BladeX $bladeX)
13
    {
14
        return $this->bladeX = $bladeX;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
15
    }
16
17
    public function compile(string $viewContents): string
18
    {
19
        return array_reduce(
20
            $this->bladeX->getRegisteredComponents(),
21
            [$this, 'parseComponentHtml'],
22
            $viewContents);
23
    }
24
25
    protected function parseComponentHtml(string $viewContents, BladeXComponent $bladeXComponent)
26
    {
27
        $pattern = "/<\s*{$bladeXComponent->name}[^>]*>((.|\n)*?)<\s*\/\s*{$bladeXComponent->name}>/m";
28
29
        return preg_replace_callback($pattern, function (array $regexResult) use ($bladeXComponent) {
30
            [$componentHtml, $componentInnerHtml] = $regexResult;
0 ignored issues
show
Bug introduced by
The variable $componentHtml does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $componentInnerHtml does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
31
32
            return "@component('{$bladeXComponent->bladeViewName}', [{$this->getComponentAttributes($componentHtml)}])"
33
                . $this->parseComponentInnerHtml($componentInnerHtml)
34
                . '@endcomponent';
35
        }, $viewContents);
36
    }
37
38
    protected function getComponentAttributes(string $componentHtml): string
39
    {
40
        $componentXml = new SimpleXMLElement($componentHtml);
41
42
        return collect($componentXml->attributes())
43
            ->map(function ($value, $attribute) {
44
                $value = str_replace("'", "\\'", $value);
45
46
                return "'{$attribute}' => '{$value}',";
47
            })->implode('');
48
    }
49
50
    protected function parseComponentInnerHtml(string $componentInnerHtml): string
51
    {
52
        $pattern = '/<\s*slot[^>]*name=[\'"](.*)[\'"][^>]*>((.|\n)*?)<\s*\/\s*slot>/m';
53
54
        return preg_replace_callback($pattern, function ($result) {
55
            [$slot, $name, $contents] = $result;
0 ignored issues
show
Bug introduced by
The variable $slot does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $contents does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
57
            return "@slot('{$name}'){$contents}@endslot";
58
        }, $componentInnerHtml);
59
    }
60
}
61