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
|
|
|
* Additional actions |
31
|
|
|
*/ |
32
|
|
|
public static Closure|array|null $configurationActionsCallback = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public static array $customExports = []; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public static function ignoreMigrations(): static |
41
|
|
|
{ |
42
|
|
|
static::$runsMigrations = false; |
43
|
|
|
|
44
|
|
|
return new static; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function useConfigurationModelClass(string $class): static |
48
|
|
|
{ |
49
|
|
|
if (!is_a($class, ExportConfig::class, true)) { |
50
|
|
|
throw new \InvalidArgumentException('Class should extend ExportConfig'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
static::$configurationModelClass = $class; |
54
|
|
|
|
55
|
|
|
return new static; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function useRepository(string|ExportRepository|array $repository): static |
59
|
|
|
{ |
60
|
|
|
if (is_null(static::$repositories)) { |
61
|
|
|
static::$repositories = new ExportRepositoryCollection(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (is_array($repository)) { |
|
|
|
|
65
|
|
|
foreach ($repository as $repo) { |
66
|
|
|
static::useRepository($repo); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new static; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (is_string($repository)) { |
73
|
|
|
$repository = new $repository(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($repository instanceof ExportRepository) { |
77
|
|
|
static::$repositories->add($repository); |
78
|
|
|
static::$repositories = static::$repositories->uniqueNames(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new static; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function getRepositories(): ExportRepositoryCollection |
85
|
|
|
{ |
86
|
|
|
if (is_null(static::$repositories)) { |
87
|
|
|
static::$repositories = new ExportRepositoryCollection(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return self::$repositories; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function useConfigurationActions(Closure|array $configurationActionsCallback): static |
94
|
|
|
{ |
95
|
|
|
self::$configurationActionsCallback = $configurationActionsCallback; |
96
|
|
|
|
97
|
|
|
return new static; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function configurationActions(NovaRequest $request): array |
101
|
|
|
{ |
102
|
|
|
if (is_callable(static::$configurationActionsCallback)) { |
103
|
|
|
return call_user_func(static::$configurationActionsCallback, $request); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function typeOptions(): array |
110
|
|
|
{ |
111
|
|
|
return static::getRepositories() |
112
|
|
|
->mapWithKeys(fn ($repo) => [$repo->name() => $repo->label()]) |
113
|
|
|
->toArray(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
public static function useCustomExportsToFile(string|CustomExport|array $customExport): static |
119
|
|
|
{ |
120
|
|
|
if (is_array($customExport)) { |
|
|
|
|
121
|
|
|
foreach ($customExport as $customExportItem) { |
122
|
|
|
static::useCustomExportsToFile($customExportItem); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return new static; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!is_string($customExport)) { |
129
|
|
|
$customExport = $customExport::class; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (!is_subclass_of($customExport, CustomExport::class)) { |
133
|
|
|
throw new \Exception('Custom export should be subclass of ' . CustomExport::class); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
static::$customExports[] = $customExport; |
137
|
|
|
|
138
|
|
|
return new static; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public static function customExportsOptions(): array |
142
|
|
|
{ |
143
|
|
|
$exportsList = []; |
144
|
|
|
$customExports = NovaExportConfig::$customExports; |
145
|
|
|
if(!empty($customExports)) { |
146
|
|
|
/** @var CustomExport $customExport */ |
147
|
|
|
foreach ($customExports as $customExport) { |
148
|
|
|
$exportsList[$customExport::key()] = $customExport::name(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $exportsList; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public static function customExportsByKey(string $key): ?CustomExport |
156
|
|
|
{ |
157
|
|
|
$customExports = NovaExportConfig::$customExports; |
158
|
|
|
if(!empty($customExports)) { |
159
|
|
|
/** @var CustomExport $customExport */ |
160
|
|
|
foreach ($customExports as $customExport) { |
161
|
|
|
if($customExport::key() != $key) { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return new $customExport; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|