1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Routing; |
4
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\Routing\LocaleRouter; |
6
|
|
|
use CaribouFute\LocaleRoute\Traits\ConfigParams; |
7
|
|
|
use Illuminate\Routing\ResourceRegistrar as IlluminateResourceRegistrar; |
8
|
|
|
use Illuminate\Routing\Router as IlluminateRouter; |
9
|
|
|
use Illuminate\Translation\Translator; |
10
|
|
|
|
11
|
|
|
class ResourceRegistrar extends IlluminateResourceRegistrar |
12
|
|
|
{ |
13
|
|
|
use ConfigParams; |
14
|
|
|
|
15
|
|
|
protected $localeRouter; |
16
|
|
|
protected $translator; |
17
|
|
|
|
18
|
3 |
|
public function __construct(LocaleRouter $localeRouter, IlluminateRouter $router, Translator $translator) |
19
|
|
|
{ |
20
|
3 |
|
$this->localeRouter = $localeRouter; |
21
|
3 |
|
$this->translator = $translator; |
22
|
|
|
|
23
|
3 |
|
parent::__construct($router); |
24
|
3 |
|
} |
25
|
|
|
|
26
|
3 |
|
protected function getLocaleResourceAction($controller, $method) |
27
|
|
|
{ |
28
|
3 |
|
return $controller . '@' . $method; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get resource name for both Laravel 5.4 (getResourceRouteName) and Laravel <5.4 (getResourceName) |
33
|
|
|
* @param $resource: the resource name |
34
|
|
|
* @param $method: the controller method |
35
|
|
|
* @param $options: different options |
36
|
|
|
* @return the route name |
37
|
|
|
*/ |
38
|
3 |
|
protected function getResourceName($resource, $method, $options) |
39
|
|
|
{ |
40
|
3 |
|
if (method_exists($this, 'getResourceRouteName')) { |
41
|
3 |
|
return $this->getResourceRouteName($resource, $method, $options); |
|
|
|
|
42
|
|
|
} else { |
43
|
|
|
return parent::getResourceName($resource, $method, $options); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
protected function addResourceIndex($name, $base, $controller, $options) |
48
|
|
|
{ |
49
|
1 |
|
$uri = $this->getResourceUri($name); |
50
|
1 |
|
$name = $this->getResourceName($name, 'index', $options); |
51
|
1 |
|
$action = $this->getLocaleResourceAction($controller, 'index'); |
52
|
|
|
|
53
|
1 |
|
return $this->localeRouter->get($name, $action, $uri); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
protected function addResourceCreate($name, $base, $controller, $options) |
57
|
|
|
{ |
58
|
3 |
|
$base = $this->getResourceUri($name); |
59
|
3 |
|
$uris = $this->getLocaleUris($base, 'create', $options); |
60
|
3 |
|
$name = $this->getResourceName($name, 'create', $options); |
61
|
3 |
|
$action = $this->getLocaleResourceAction($controller, 'create'); |
62
|
|
|
|
63
|
3 |
|
return $this->localeRouter->get($name, $action, $uris); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
protected function getLocaleUris($base, $label, $options) |
67
|
|
|
{ |
68
|
3 |
|
$uris = []; |
69
|
|
|
|
70
|
3 |
|
foreach ($this->locales($options) as $locale) { |
71
|
3 |
|
$uris[$locale] = $base . '/' . $this->getTranslation($locale, $label); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $uris; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
protected function getTranslation($locale, $label) |
78
|
|
|
{ |
79
|
3 |
|
$untranslated = 'route-labels.' . $label; |
80
|
3 |
|
$translated = $this->translator->get($untranslated, [], $locale); |
81
|
3 |
|
return $translated === $untranslated ? $label : $translated; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
protected function addResourceShow($name, $base, $controller, $options) |
85
|
|
|
{ |
86
|
2 |
|
$uri = $this->getResourceUri($name) . '/{' . $base . '}'; |
87
|
2 |
|
$name = $this->getResourceName($name, 'show', $options); |
88
|
2 |
|
$action = $this->getLocaleResourceAction($controller, 'show'); |
89
|
|
|
|
90
|
2 |
|
return $this->localeRouter->get($name, $action, $uri); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
protected function addResourceEdit($name, $base, $controller, $options) |
94
|
|
|
{ |
95
|
3 |
|
$base = $this->getResourceUri($name) . '/{' . $base . '}'; |
96
|
3 |
|
$uris = $this->getLocaleUris($base, 'edit', $options); |
97
|
3 |
|
$name = $this->getResourceName($name, 'edit', $options); |
98
|
3 |
|
$action = $this->getLocaleResourceAction($controller, 'edit'); |
99
|
|
|
|
100
|
3 |
|
return $this->localeRouter->get($name, $action, $uris); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.