Completed
Push — master ( 2263ae...182551 )
by Freek
01:13
created

BladeXCompiler::parseComponentInnerHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
        foreach ($this->bladeX->getRegisteredComponents() as $componentName => $viewPath) {
20
            $viewContents = $this->replaceBladeXComponentWithRegularBladeComponent($viewContents, $componentName, $viewPath);
21
        }
22
23
        return $viewContents;
24
    }
25
26
    protected function replaceBladeXComponentWithRegularBladeComponent(string $viewContents, $bladeXComponentName, string $bladeViewPath)
27
    {
28
        $pattern = '/<\s*' . $bladeXComponentName . '[^>]*>((.|\n)*?)<\s*\/\s*' . $bladeXComponentName . '>/m';
29
30
        $viewContents = preg_replace_callback($pattern, function (array $regexResult) use ($bladeViewPath) {
31
            [$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...
32
33
            return "@component('{$bladeViewPath}', [{$this->getComponentAttributes($componentHtml)}])"
34
                . $this->parseComponentInnerHtml($componentInnerHtml)
35
                . '@endcomponent';
36
        }, $viewContents);
37
        return $viewContents;
38
    }
39
40
    protected function getComponentAttributes(string $componentHtml): string
41
    {
42
        $componentXml = new SimpleXMLElement($componentHtml);
43
44
        return collect($componentXml->attributes())
45
            ->map(function ($value, $attribute) {
46
                $value = str_replace("'", "\\'", $value);
47
48
                return "'{$attribute}' => '{$value}',";
49
            })->implode('');
50
    }
51
52
    protected function parseComponentInnerHtml(string $componentInnerHtml): string
53
    {
54
        $pattern = '/<\s*slot[^>]*name=[\'"](.*)[\'"][^>]*>((.|\n)*?)<\s*\/\s*slot>/m';
55
56
        return preg_replace_callback($pattern, function ($result) {
57
            [$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...
58
59
            return "@slot('{$name}'){$contents}@endslot";
60
        }, $componentInnerHtml);
61
    }
62
}
63