1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
4
|
|
|
|
5
|
|
|
class ViewNamespaces |
6
|
|
|
{ |
7
|
|
|
private static $viewNamespaces = []; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Return all the view namespaces including the ones stored in the laravel config files. |
11
|
|
|
* |
12
|
|
|
* @param string $domain (eg. fields, filters, buttons) |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
public static function getFor(string $domain) |
16
|
|
|
{ |
17
|
|
|
$viewNamespacesFromConfig = self::getFromConfigFor($domain); |
18
|
|
|
|
19
|
|
|
return array_unique(array_merge($viewNamespacesFromConfig, self::getForDomain($domain))); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Add view namespaces for a given domain. |
24
|
|
|
* |
25
|
|
|
* @param string $domain (eg. fields, filters, buttons) |
26
|
|
|
* @param string|array $viewNamespaces |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public static function addFor(string $domain, $viewNamespaces) |
30
|
|
|
{ |
31
|
|
|
foreach ((array) $viewNamespaces as $viewNamespace) { |
32
|
|
|
if (! in_array($viewNamespace, self::getForDomain($domain))) { |
33
|
|
|
self::$viewNamespaces[$domain][] = $viewNamespace; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return the namespaces stored for a given domain. |
40
|
|
|
* |
41
|
|
|
* @param string $domain |
42
|
|
|
*/ |
43
|
|
|
private static function getForDomain(string $domain) |
44
|
|
|
{ |
45
|
|
|
return self::$viewNamespaces[$domain] ?? []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return the array of view namespaces for backpack components from the Laravel config files. |
50
|
|
|
* It uses the default `backpack.crud.view_namespaces` key or a custom provided key. |
51
|
|
|
* |
52
|
|
|
* @param string $domain |
53
|
|
|
* @param mixed $customConfigKey |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private static function getFromConfigFor(string $domain, $customConfigKey = null) |
57
|
|
|
{ |
58
|
|
|
return config($customConfigKey ?? 'backpack.crud.view_namespaces.'.$domain) ?? []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return all the view namespaces using a developer provided config key. |
63
|
|
|
* Allow developer to use view namespaces from other config keys. |
64
|
|
|
* |
65
|
|
|
* @param string $domain (eg. fields, filters, buttons) |
66
|
|
|
* @param string $viewNamespacesFromConfigKey |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public static function getWithFallbackFor(string $domain, string $viewNamespacesFromConfigKey) |
70
|
|
|
{ |
71
|
|
|
$viewNamespacesFromConfig = self::getFromConfigFor($domain, $viewNamespacesFromConfigKey); |
72
|
|
|
|
73
|
|
|
return array_unique(array_merge($viewNamespacesFromConfig, self::getFor($domain))); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This is an helper function that returns the view namespace with the view name appended. |
78
|
|
|
* It's usefull to use in blade templates with `@includeFirst(ViewNamespaces::getViewPathsFor('columns', 'some_column'))`. |
79
|
|
|
* |
80
|
|
|
* @param string $domain |
81
|
|
|
* @param string $viewName |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public static function getViewPathsFor(string $domain, string $viewName) |
85
|
|
|
{ |
86
|
|
|
return array_map(function ($item) use ($viewName) { |
87
|
|
|
return $item.'.'.$viewName; |
88
|
|
|
}, self::getFor($domain)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|