1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaExportConfiguration; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
7
|
|
|
use NovaExportConfiguration\Export\CustomExport; |
8
|
|
|
use NovaExportConfiguration\Models\ExportConfig; |
9
|
|
|
use NovaExportConfiguration\Repositories\ExportRepository; |
10
|
|
|
use NovaExportConfiguration\Repositories\ExportRepositoryCollection; |
11
|
|
|
|
12
|
|
|
class NovaExportConfig |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Export repositories used by application |
16
|
|
|
*/ |
17
|
|
|
protected static ?ExportRepositoryCollection $repositories = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Export configuration model class |
21
|
|
|
*/ |
22
|
|
|
public static string $configurationModelClass = \NovaExportConfiguration\Models\ExportConfig::class; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Indicates if NovaExportConfiguration migrations will be run. |
26
|
|
|
*/ |
27
|
|
|
public static bool $runsMigrations = true; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Indicates if NovaExportConfiguration will provide download route. |
31
|
|
|
*/ |
32
|
|
|
public static bool $useRoutes = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Route Configuration callback |
36
|
|
|
*/ |
37
|
|
|
public static Closure|null $routeConfigurationCallback = null; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Additional actions |
42
|
|
|
*/ |
43
|
|
|
public static Closure|array|null $configurationActionsCallback = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
public static array $customExports = []; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
public static function ignoreMigrations(): static |
52
|
|
|
{ |
53
|
|
|
static::$runsMigrations = false; |
54
|
|
|
|
55
|
|
|
return new static; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function withoutRoutes(): static |
59
|
|
|
{ |
60
|
|
|
static::$useRoutes = false; |
61
|
|
|
|
62
|
|
|
return new static; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function routeConfiguration(?\Closure $callback = null): mixed |
66
|
|
|
{ |
67
|
|
|
if($callback) { |
68
|
|
|
static::$routeConfigurationCallback = $callback; |
69
|
|
|
|
70
|
|
|
return new static; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if(is_callable(static::$routeConfigurationCallback)) { |
74
|
|
|
return call_user_func(static::$routeConfigurationCallback); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return [ |
78
|
|
|
'prefix' => 'downloads/exports', |
79
|
|
|
'middleware' => config('nova.middleware'), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function useConfigurationModelClass(string $class): static |
84
|
|
|
{ |
85
|
|
|
if (!is_a($class, ExportConfig::class, true)) { |
86
|
|
|
throw new \InvalidArgumentException('Class should extend ExportConfig'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
static::$configurationModelClass = $class; |
90
|
|
|
|
91
|
|
|
return new static; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function useRepository(string|ExportRepository|array $repository): static |
95
|
|
|
{ |
96
|
|
|
if (is_null(static::$repositories)) { |
97
|
|
|
static::$repositories = new ExportRepositoryCollection(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (is_array($repository)) { |
|
|
|
|
101
|
|
|
foreach ($repository as $repo) { |
102
|
|
|
static::useRepository($repo); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return new static; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (is_string($repository)) { |
109
|
|
|
$repository = new $repository(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($repository instanceof ExportRepository) { |
113
|
|
|
static::$repositories->add($repository); |
114
|
|
|
static::$repositories = static::$repositories->uniqueNames(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return new static; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function getRepositories(): ExportRepositoryCollection |
121
|
|
|
{ |
122
|
|
|
if (is_null(static::$repositories)) { |
123
|
|
|
static::$repositories = new ExportRepositoryCollection(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return self::$repositories; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public static function useConfigurationActions(Closure|array $configurationActionsCallback): static |
130
|
|
|
{ |
131
|
|
|
self::$configurationActionsCallback = $configurationActionsCallback; |
132
|
|
|
|
133
|
|
|
return new static; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function configurationActions(NovaRequest $request): array |
137
|
|
|
{ |
138
|
|
|
if (is_callable(static::$configurationActionsCallback)) { |
139
|
|
|
return call_user_func(static::$configurationActionsCallback, $request); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return []; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public static function typeOptions(): array |
146
|
|
|
{ |
147
|
|
|
return static::getRepositories() |
148
|
|
|
->mapWithKeys(fn ($repo) => [$repo->name() => $repo->label()]) |
149
|
|
|
->toArray(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public static function useCustomExportsToFile(string|CustomExport|array $customExport): static |
153
|
|
|
{ |
154
|
|
|
if (is_array($customExport)) { |
|
|
|
|
155
|
|
|
foreach ($customExport as $customExportItem) { |
156
|
|
|
static::useCustomExportsToFile($customExportItem); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return new static; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!is_string($customExport)) { |
163
|
|
|
$customExport = $customExport::class; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (!is_subclass_of($customExport, CustomExport::class)) { |
167
|
|
|
throw new \Exception('Custom export should be subclass of ' . CustomExport::class); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
static::$customExports[] = $customExport; |
171
|
|
|
|
172
|
|
|
return new static; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public static function customExportsOptions(): array |
176
|
|
|
{ |
177
|
|
|
$exportsList = []; |
178
|
|
|
$customExports = NovaExportConfig::$customExports; |
179
|
|
|
if(!empty($customExports)) { |
180
|
|
|
/** @var CustomExport $customExport */ |
181
|
|
|
foreach ($customExports as $customExport) { |
182
|
|
|
$exportsList[$customExport::key()] = $customExport::name(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $exportsList; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public static function customExportsByKey(string $key): ?CustomExport |
190
|
|
|
{ |
191
|
|
|
$customExports = NovaExportConfig::$customExports; |
192
|
|
|
if(!empty($customExports)) { |
193
|
|
|
/** @var CustomExport $customExport */ |
194
|
|
|
foreach ($customExports as $customExport) { |
195
|
|
|
if($customExport::key() != $key) { |
196
|
|
|
continue; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return new $customExport; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return null; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|