|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\Core\System\Modules\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Epesi\Core\System\Modules\ModuleView; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
trait HasLinks |
|
9
|
|
|
{ |
|
10
|
|
|
use HasModule; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Create self module link |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $method |
|
16
|
|
|
* @param array $args |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
final public static function selfLink($method = 'body', $args = []) |
|
20
|
|
|
{ |
|
21
|
|
|
$defaultView = self::module()::view(); |
|
22
|
|
|
|
|
23
|
|
|
$viewClass = is_a(static::class, ModuleView::class, true)? static::class: $defaultView; |
|
24
|
|
|
|
|
25
|
|
|
$viewAlias = null; |
|
26
|
|
|
if ($viewClass !== $defaultView) { |
|
27
|
|
|
$names = array_slice(explode('\\', $viewClass), -1); |
|
28
|
|
|
|
|
29
|
|
|
$viewAlias = str_ireplace('_', '-', Str::snake(reset($names))); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return url(implode('/', ['view', implode(':', array_filter([self::alias(), $viewAlias])), $method, self::encodeArgs($args)])); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create module link |
|
37
|
|
|
* Associative $args are used to set module properties |
|
38
|
|
|
* Numeric key values are used as method arguments |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $module |
|
41
|
|
|
* @param string $method |
|
42
|
|
|
* @param array $args |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
final public static function moduleLink($module, $method = 'body', $args = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$alias = class_exists($module)? $module::alias(): $module; |
|
48
|
|
|
|
|
49
|
|
|
if (is_array($method) && !$args) { |
|
|
|
|
|
|
50
|
|
|
$args = $method; |
|
51
|
|
|
$method = 'body'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return url(implode('/', ['view', $alias, $method, self::encodeArgs($args)])); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Decode the arguments hash and return stored arguments |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $hash |
|
61
|
|
|
* @return array |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Illuminate\Http\Exceptions\HttpResponseException |
|
64
|
|
|
*/ |
|
65
|
|
|
final public static function decodeArgs($hash) { |
|
66
|
|
|
if (! $hash) return []; |
|
67
|
|
|
|
|
68
|
|
|
$args = session($hash); |
|
69
|
|
|
|
|
70
|
|
|
if (is_null($args)) abort(419); |
|
71
|
|
|
|
|
72
|
|
|
return (array) $args; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Encode arguments for the module method |
|
77
|
|
|
* |
|
78
|
|
|
* @param array|mixed $args |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
final public static function encodeArgs($args) { |
|
82
|
|
|
$args = (array) $args; |
|
83
|
|
|
|
|
84
|
|
|
if (! $args) return; |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
$hash = md5(serialize($args)); |
|
87
|
|
|
|
|
88
|
|
|
session([$hash => $args]); |
|
89
|
|
|
|
|
90
|
|
|
return $hash; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|