|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oddvalue\DbRouter\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Oddvalue\DbRouter\RouteManager; |
|
6
|
|
|
use Oddvalue\DbRouter\Contracts\Routable; |
|
7
|
|
|
use Oddvalue\DbRouter\Contracts\RouteGenerator; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
10
|
|
|
|
|
11
|
|
|
trait HasRoutes |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Boot the trait |
|
15
|
|
|
* |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
36 |
|
public static function bootHasRoutes() |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Update routes after instance is saved |
|
22
|
|
|
*/ |
|
23
|
|
|
static::saved(function (Routable $model) { |
|
24
|
36 |
|
$manager = new RouteManager; |
|
25
|
36 |
|
$manager->updateRoutes($model); |
|
26
|
36 |
|
}); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Delete routes after instance is deleted |
|
30
|
|
|
*/ |
|
31
|
|
|
static::deleted(function (Routable $model) { |
|
32
|
6 |
|
$manager = new RouteManager; |
|
33
|
6 |
|
$manager->deleteRoutes($model); |
|
34
|
36 |
|
}); |
|
35
|
36 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get the fully qualified class name of the model's route generator |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public function getRouteGeneratorClass() : string; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the model's route generator |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Oddvalue\DbRouter\Contracts\RouteGenerator |
|
48
|
|
|
*/ |
|
49
|
36 |
|
public function getRouteGenerator() : RouteGenerator |
|
50
|
|
|
{ |
|
51
|
36 |
|
$generatorClass = $this->getRouteGeneratorClass(); |
|
52
|
36 |
|
return new $generatorClass; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Relation to the canonical route for the model |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
|
59
|
|
|
*/ |
|
60
|
15 |
|
public function canonicalRoute() : MorphOne |
|
61
|
|
|
{ |
|
62
|
15 |
|
return $this->/** @scrutinizer ignore-call */morphOne(config('dbrouter.route_class'), 'routable') |
|
63
|
15 |
|
->whereIsCanonical(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Relation to all the model's routes |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
70
|
|
|
*/ |
|
71
|
36 |
|
public function routes() : MorphMany |
|
72
|
|
|
{ |
|
73
|
36 |
|
return $this->/** @scrutinizer ignore-call */morphMany(config('dbrouter.route_class'), 'routable'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Relation to all the model's redirect routes |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function redirectRoutes() : MorphMany |
|
82
|
|
|
{ |
|
83
|
3 |
|
return $this->routes()->isRedirect(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|