-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathIsView.php
More file actions
51 lines (38 loc) · 1.18 KB
/
IsView.php
File metadata and controls
51 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Tempest\View;
use function Tempest\Support\path;
use function Tempest\Support\Path\normalize;
/** @phpstan-require-implements \Tempest\View\View */
trait IsView
{
public string $path;
public ?string $relativeRootPath = null;
public array $data = [];
public function __construct(
string $path,
array $data = [],
) {
$this->path = $path;
$this->data = $data;
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
if (str_ends_with(normalize($trace[0]['file']), 'view/src/functions.php')) {
$this->relativeRootPath = path($trace[1]['file'])->dirname()->toString();
} else {
$this->relativeRootPath = path($trace[0]['file'])->dirname()->toString();
}
}
public function get(string $key): mixed
{
return $this->{$key} ?? $this->data[$key] ?? null;
}
public function has(string $key): bool
{
return array_key_exists($key, $this->data) || property_exists($this, $key);
}
public function data(mixed ...$params): self
{
$this->data = [...$this->data, ...$params];
return $this;
}
}