|
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
|
|
|
[config('backpack.ui.view_namespace').$domain] ?? |
|
60
|
|
|
[config('backpack.ui.view_namespace_fallback').$domain]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return all the view namespaces using a developer provided config key. |
|
65
|
|
|
* Allow developer to use view namespaces from other config keys. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $domain (eg. fields, filters, buttons) |
|
68
|
|
|
* @param string $viewNamespacesFromConfigKey |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function getWithFallbackFor(string $domain, string $viewNamespacesFromConfigKey) |
|
72
|
|
|
{ |
|
73
|
|
|
$viewNamespacesFromConfig = self::getFromConfigFor($domain, $viewNamespacesFromConfigKey); |
|
74
|
|
|
|
|
75
|
|
|
return array_unique(array_merge($viewNamespacesFromConfig, self::getFor($domain))); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* This is an helper function that returns the view namespace with the view name appended. |
|
80
|
|
|
* It's useful to use in blade templates with `@includeFirst(ViewNamespaces::getViewPathsFor('columns', 'some_column'))`. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $domain |
|
83
|
|
|
* @param string $viewName |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function getViewPathsFor(string $domain, string $viewName) |
|
87
|
|
|
{ |
|
88
|
|
|
return array_map(function ($item) use ($viewName) { |
|
89
|
|
|
return $item.'.'.$viewName; |
|
90
|
|
|
}, self::getFor($domain)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get view paths for a domain and view name with fallback support. |
|
95
|
|
|
* This is useful for @includeFirst() calls that need a guaranteed fallback. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $domain (eg. fields, filters, buttons, columns) |
|
98
|
|
|
* @param string $viewName (eg. text, select, checkbox) |
|
99
|
|
|
* @param string|null $fallbackViewPath (eg. 'crud::columns.text') |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function getViewPathsWithFallbackFor(string $domain, string $viewName, ?string $fallbackViewPath = null) |
|
103
|
|
|
{ |
|
104
|
|
|
$paths = self::getViewPathsFor($domain, $viewName); |
|
105
|
|
|
|
|
106
|
|
|
// Add fallback if provided and not already in the list |
|
107
|
|
|
if ($fallbackViewPath && ! in_array($fallbackViewPath, $paths)) { |
|
108
|
|
|
$paths[] = $fallbackViewPath; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $paths; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|