1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Support\Models; |
6
|
|
|
|
7
|
|
|
use Hyde\Pages\Concerns\HydePage; |
8
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
9
|
|
|
use Illuminate\Support\Facades\View; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Contains data for the current page being rendered/compiled. |
14
|
|
|
* |
15
|
|
|
* All public data here will be available in the Blade views through @see ManagesViewData::shareViewData(). |
16
|
|
|
* |
17
|
|
|
* @see \Hyde\Support\Facades\Render |
18
|
|
|
* @see \Hyde\Framework\Testing\Feature\RenderHelperTest |
19
|
|
|
*/ |
20
|
|
|
class Render implements Arrayable |
21
|
|
|
{ |
22
|
|
|
protected HydePage $page; |
23
|
|
|
protected Route $currentRoute; |
24
|
|
|
protected string $currentPage; |
25
|
|
|
|
26
|
|
|
public function setPage(HydePage $page): void |
27
|
|
|
{ |
28
|
|
|
$this->page = $page; |
29
|
|
|
$this->currentRoute = $page->getRoute(); |
30
|
|
|
$this->currentPage = $page->getRouteKey(); |
31
|
|
|
|
32
|
|
|
$this->shareToView(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getPage(): ?HydePage |
36
|
|
|
{ |
37
|
|
|
return $this->page ?? self::handleFallback('page'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getCurrentRoute(): ?Route |
41
|
|
|
{ |
42
|
|
|
return $this->currentRoute ?? self::handleFallback('currentRoute'); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getCurrentPage(): ?string |
46
|
|
|
{ |
47
|
|
|
return $this->currentPage ?? self::handleFallback('currentPage'); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function shareToView(): void |
51
|
|
|
{ |
52
|
|
|
View::share($this->toArray()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function share(string $key, mixed $value): void |
56
|
|
|
{ |
57
|
|
|
if (property_exists($this, $key)) { |
58
|
|
|
$this->{$key} = $value; |
59
|
|
|
$this->shareToView(); |
60
|
|
|
} else { |
61
|
|
|
throw new InvalidArgumentException("Property '$key' does not exist on ".self::class); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function clearData(): void |
66
|
|
|
{ |
67
|
|
|
unset($this->page, $this->currentRoute, $this->currentPage); |
68
|
|
|
View::share(['page' => null, 'currentRoute' => null, 'currentPage' => null]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function toArray(): array |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
'render' => $this, |
75
|
|
|
'page' => $this->getPage(), |
76
|
|
|
'currentRoute' => $this->getCurrentRoute(), |
77
|
|
|
'currentPage' => $this->getCurrentPage(), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @deprecated |
83
|
|
|
* @codeCoverageIgnore |
84
|
|
|
*/ |
85
|
|
|
protected static function handleFallback(string $property): mixed |
86
|
|
|
{ |
87
|
|
|
$shared = View::shared($property); |
88
|
|
|
|
89
|
|
|
if ($shared !== null) { |
90
|
|
|
trigger_error("Setting page rendering data via the view facade is deprecated. Use `Render::share('$property', \$value)` instead", E_USER_DEPRECATED); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $shared; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|