Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
File
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 get_contents
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 execute
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2
3namespace Computator\FrameworkUtils\PHPTemplate\Templates;
4
5use Computator\FrameworkUtils\PHPTemplate\Exceptions;
6use Computator\FrameworkUtils\PHPTemplate\TemplateRuntimeController;
7
8/**
9 * PHP file template.
10 *
11 * Directly executes the named PHP file as a template.
12 */
13class File extends Base {
14    public readonly string $path;
15
16    public function __construct(string $path) {
17        if ($path == '' || !is_readable($path))
18            throw new Exceptions\TemplateNotFoundException("template '{$path}' does not exist or is not readable");
19        $this->path = $path;
20    }
21
22    public function get_contents(int $offset = 0, ?int $length = null): string {
23        $rv = @file_get_contents($this->path, offset: $offset, length: $length);
24        if ($rv === false)
25            throw new Exceptions\TemplateRenderException("error reading template file '{$this->path}'");
26        return $rv;
27    }
28
29    public function execute(array $context, mixed ...$controller_args): mixed {
30        $rc = new TemplateRuntimeController(...$controller_args);
31        return (function (...$__exec_args) {
32            extract($__exec_args['context']);
33            unset($__exec_args['context']);
34            return include $__exec_args['path'];
35        })->call($rc, context: $context, path: $this->path);
36    }
37}