Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AnonymousViewCompiler
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Actions;
6
7use Hyde\Facades\Filesystem;
8use Hyde\Framework\Exceptions\FileNotFoundException;
9use Illuminate\Support\Facades\Blade;
10
11/**
12 * Compile any Blade file using the Blade facade as it allows us to render
13 * it without having to register the directory with the view finder.
14 */
15class AnonymousViewCompiler
16{
17    protected string $viewPath;
18    protected array $data;
19
20    public static function handle(string $viewPath, array $data = []): string
21    {
22        return (new self($viewPath, $data))->__invoke();
23    }
24
25    public function __construct(string $viewPath, array $data = [])
26    {
27        $this->viewPath = $viewPath;
28        $this->data = $data;
29    }
30
31    public function __invoke(): string
32    {
33        if (Filesystem::missing($this->viewPath)) {
34            throw new FileNotFoundException($this->viewPath);
35        }
36
37        return Blade::render(
38            Filesystem::getContents($this->viewPath),
39            $this->data
40        );
41    }
42}