• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 11292226413

11 Oct 2024 12:21PM UTC coverage: 82.134%. First build
11292226413

Pull #560

github

web-flow
Merge 5007f5fd5 into 571879663
Pull Request #560: chore: refactor view engine

429 of 471 new or added lines in 34 files covered. (91.08%)

6758 of 8228 relevant lines covered (82.13%)

38.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/Tempest/View/src/Elements/ViewComponentElement.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\View\Elements;
6

7
use function Tempest\Support\str;
8
use Tempest\View\Element;
9
use Tempest\View\Renderers\TempestViewCompiler;
10
use Tempest\View\ViewComponent;
11

12
final class ViewComponentElement implements Element
13
{
14
    use IsElement;
15

16
    public function __construct(
32✔
17
        private readonly TempestViewCompiler $compiler,
18
        private readonly ViewComponent $viewComponent,
19
        array $attributes,
20
    ) {
21
        $this->attributes = $attributes;
32✔
22
    }
23

NEW
24
    public function getViewComponent(): ViewComponent
×
25
    {
NEW
26
        return $this->viewComponent;
×
27
    }
28

29
    public function getSlot(string $name = 'slot'): ?Element
21✔
30
    {
31
        foreach ($this->getChildren() as $child) {
21✔
32
            if (! $child instanceof SlotElement) {
19✔
33
                continue;
18✔
34
            }
35

36
            if ($child->matches($name)) {
3✔
37
                return $child;
3✔
38
            }
39
        }
40

41
        if ($name === 'slot') {
20✔
42
            $elements = [];
20✔
43

44
            foreach ($this->getChildren() as $child) {
20✔
45
                if ($child instanceof SlotElement) {
18✔
46
                    continue;
2✔
47
                }
48

49
                $elements[] = $child;
18✔
50
            }
51

52
            return new CollectionElement($elements);
20✔
53
        }
54

NEW
55
        return null;
×
56
    }
57

58
    public function compile(): string
32✔
59
    {
60
        $compiled = str($this->viewComponent->compile($this))
32✔
61
            // Compile slots
32✔
62
            ->replaceRegex(
32✔
63
                regex: '/<x-slot\s*(name="(?<name>\w+)")?((\s*\/>)|><\/x-slot>)/',
32✔
64
                replace: function ($matches) {
32✔
65
                    $name = $matches['name'] ?: 'slot';
21✔
66

67
                    $slot = $this->getSlot($name);
21✔
68

69
                    if ($slot === null) {
21✔
NEW
70
                        return $matches[0];
×
71
                    }
72

73
                    return $slot->compile();
21✔
74
                },
32✔
75
            );
32✔
76

77
        return $this->compiler->compile($compiled->toString());
32✔
78
    }
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc