-
Notifications
You must be signed in to change notification settings - Fork 1
/
HasRoutes.php
85 lines (76 loc) · 2.16 KB
/
HasRoutes.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
<?php
namespace Oddvalue\DbRouter\Traits;
use Oddvalue\DbRouter\RouteManager;
use Oddvalue\DbRouter\Contracts\Routable;
use Oddvalue\DbRouter\Contracts\RouteGenerator;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait HasRoutes
{
/**
* Boot the trait
*
* @return void
*/
public static function bootHasRoutes()
{
/**
* Update routes after instance is saved
*/
static::saved(function (Routable $model) {
$manager = new RouteManager;
$manager->updateRoutes($model);
});
/**
* Delete routes after instance is deleted
*/
static::deleted(function (Routable $model) {
$manager = new RouteManager;
$manager->deleteRoutes($model);
});
}
/**
* Get the fully qualified class name of the model's route generator
*
* @return string
*/
abstract public function getRouteGeneratorClass() : string;
/**
* Get the model's route generator
*
* @return \Oddvalue\DbRouter\Contracts\RouteGenerator
*/
public function getRouteGenerator() : RouteGenerator
{
$generatorClass = $this->getRouteGeneratorClass();
return new $generatorClass;
}
/**
* Relation to the canonical route for the model
*
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function canonicalRoute() : MorphOne
{
return $this->/** @scrutinizer ignore-call */morphOne(config('dbrouter.route_class'), 'routable')
->whereIsCanonical();
}
/**
* Relation to all the model's routes
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function routes() : MorphMany
{
return $this->/** @scrutinizer ignore-call */morphMany(config('dbrouter.route_class'), 'routable');
}
/**
* Relation to all the model's redirect routes
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function redirectRoutes() : MorphMany
{
return $this->routes()->isRedirect();
}
}