• 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

95.56
/src/Tempest/View/src/Renderers/TempestViewRenderer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\View\Renderers;
6

7
use Stringable;
8
use function Tempest\Support\arr;
9
use function Tempest\Support\str;
10
use Tempest\View\Exceptions\ViewCompilationError;
11
use Tempest\View\GenericView;
12
use Tempest\View\View;
13
use Tempest\View\ViewCache;
14
use Tempest\View\ViewRenderer;
15
use Throwable;
16

17
final class TempestViewRenderer implements ViewRenderer
18
{
19
    private ?View $currentView = null;
20

21
    public function __construct(
54✔
22
        private readonly TempestViewCompiler $compiler,
23
        private readonly ViewCache $viewCache,
24
    ) {
25
    }
54✔
26

27
    public function __get(string $name): mixed
14✔
28
    {
29
        return $this->currentView?->get($name);
14✔
30
    }
31

32
    public function __call(string $name, array $arguments)
3✔
33
    {
34
        return $this->currentView?->{$name}(...$arguments);
3✔
35
    }
36

37
    public function render(string|View $view): string
49✔
38
    {
39
        $view = is_string($view) ? new GenericView($view) : $view;
49✔
40

41
        $path = $this->viewCache->getCachedViewPath(
49✔
42
            path: $view->getPath(),
49✔
43
            compiledView: fn () => $this->cleanupCompiled($this->compiler->compile($view->getPath()))
49✔
44
        );
49✔
45

46
        return $this->renderCompiled($view, $path);
48✔
47
    }
48

49
    private function cleanupCompiled(string $compiled): string
47✔
50
    {
51
        // Remove strict type declarations
52
        $compiled = str($compiled)->replace('declare(strict_types=1);', '');
47✔
53

54
        // Cleanup and bundle imports
55
        $imports = arr();
47✔
56
        $compiled = $compiled
47✔
57
            ->replaceRegex('/use .*;/', function (array $matches) use (&$imports) {
47✔
58
                $imports[$matches[0]] = $matches[0];
6✔
59

60
                return '';
6✔
61
            })
47✔
62
            ->prepend(
47✔
63
                sprintf(
47✔
64
                    '<?php
47✔
65
%s
66
?>',
47✔
67
                    $imports->implode(PHP_EOL),
47✔
68
                ),
47✔
69
            );
47✔
70

71
        // Remove empty PHP blocks
72
        $compiled = $compiled->replaceRegex('/<\?php\s*\?>/', '');
47✔
73

74
        return $compiled->toString();
47✔
75
    }
76

77
    private function renderCompiled(View $_view, string $_path): string
48✔
78
    {
79
        $this->currentView = $_view;
48✔
80

81
        ob_start();
48✔
82

83
        // Extract data from view into local variables so that they can be accessed directly
84
        $_data = $_view->getData();
48✔
85

86
        extract($_data, flags: EXTR_SKIP);
48✔
87

88
        try {
89
            include $_path;
48✔
NEW
90
        } catch (Throwable $throwable) {
×
NEW
91
            throw new ViewCompilationError(content: file_get_contents($_path), previous: $throwable);
×
92
        }
93

94
        // If the view defines local variables, we add them here to the view object as well
95
        foreach (get_defined_vars() as $key => $value) {
48✔
96
            if (! $_view->has($key)) {
48✔
97
                $_view->data(...[$key => $value]);
48✔
98
            }
99
        }
100

101
        $this->currentView = null;
48✔
102

103
        return trim(ob_get_clean());
48✔
104
    }
105

106
    public function escape(null|string|Stringable $value): string
8✔
107
    {
108
        return htmlentities((string)$value);
8✔
109
    }
110
}
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