-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
RunwayRoutes.php
108 lines (85 loc) · 2.53 KB
/
RunwayRoutes.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace StatamicRadPack\Runway\Routing\Traits;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;
use Statamic\Facades\Antlers;
use Statamic\StaticCaching\Cacher;
use Statamic\Support\Arr;
use StatamicRadPack\Runway\Routing\Routable;
use StatamicRadPack\Runway\Routing\RoutingModel;
use StatamicRadPack\Runway\Routing\RunwayUri;
use StatamicRadPack\Runway\Runway;
trait RunwayRoutes
{
protected $routingModel;
use Routable {
uri as routableUri;
}
public function routingModel(): RoutingModel
{
$this->routingModel = new RoutingModel($this);
return $this->routingModel;
}
public function route(): ?string
{
if (! $this->runwayUri) {
return null;
}
return $this->runwayUri->uri;
}
public function routeData()
{
return $this->routingModel()->routeData();
}
public function uri(): ?string
{
return $this->routingModel()->uri();
}
public function toResponse($request)
{
return $this->routingModel()->toResponse($request);
}
public function template(): string
{
return $this->routingModel()->template();
}
public function layout(): string
{
return $this->routingModel()->layout();
}
public function getRouteKey()
{
return $this->routingModel()->getRouteKey();
}
public function runwayUri(): MorphOne
{
return $this->morphOne(RunwayUri::class, 'model');
}
public static function bootRunwayRoutes()
{
static::saved(function ($model) {
$resource = Runway::findResourceByModel($model);
if (! $resource->hasRouting()) {
return;
}
$uri = Antlers::parser()
->parse($resource->route(), $model->toAugmentedArray())
->__toString();
$uri = Str::start($uri, '/');
if ($model->runwayUri()->exists()) {
$model->runwayUri()->first()->update(['uri' => $uri]);
} else {
$model->runwayUri()->create(['uri' => $uri]);
}
app(Cacher::class)->invalidateUrl($uri);
app(Cacher::class)->invalidateUrls(
Arr::get(config('statamic.static_caching.invalidation.rules'), "runway.{$resource->handle()}.urls")
);
});
static::deleting(function ($model) {
if ($model->runwayUri) {
$model->runwayUri()->delete();
}
});
}
}