1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Export; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Export\Handler\Airtable\AirtableHandler; |
6
|
|
|
use BristolSU\ControlDB\Export\Handler\DumpHandler; |
7
|
|
|
use BristolSU\ControlDB\Export\Handler\Handler; |
8
|
|
|
use BristolSU\ControlDB\Export\Handler\SaveCsvHandler; |
9
|
|
|
use Closure; |
10
|
|
|
use Illuminate\Contracts\Container\Container; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
class ExportManager |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The container instance. |
19
|
|
|
* |
20
|
|
|
* @var Container |
21
|
|
|
*/ |
22
|
|
|
protected $container; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The registered custom driver creators. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $customCreators = []; |
30
|
|
|
|
31
|
|
|
protected $formatters = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new Export manager instance. |
35
|
|
|
* |
36
|
|
|
* @param Container $container |
|
|
|
|
37
|
|
|
* @return void |
|
|
|
|
38
|
|
|
*/ |
39
|
15 |
|
public function __construct(Container $container) |
40
|
|
|
{ |
41
|
15 |
|
$this->container = $container; |
42
|
15 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get a export driver instance. |
46
|
|
|
* |
47
|
|
|
* @param string|null $driver |
|
|
|
|
48
|
|
|
* @return mixed |
|
|
|
|
49
|
|
|
*/ |
50
|
15 |
|
public function driver($driver = null) |
51
|
|
|
{ |
52
|
15 |
|
return $this->resolve($driver ?? $this->getDefaultDriver()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Resolve the given export instance by name. |
57
|
|
|
* |
58
|
|
|
* @param string $name |
|
|
|
|
59
|
|
|
* @return Handler |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
*/ |
63
|
15 |
|
protected function resolve($name) |
64
|
|
|
{ |
65
|
15 |
|
$config = $this->configurationFor($name); |
66
|
|
|
|
67
|
15 |
|
if (is_null($config)) { |
|
|
|
|
68
|
1 |
|
throw new InvalidArgumentException("Exporter [{$name}] is not defined."); |
69
|
|
|
} |
70
|
14 |
|
if (isset($this->customCreators[$config['driver']])) { |
71
|
8 |
|
return $this->callCustomCreator($config); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
$driverMethod = 'create'.Str::studly($config['driver']).'Driver'; |
75
|
|
|
|
76
|
6 |
|
if (method_exists($this, $driverMethod)) { |
77
|
5 |
|
return $this->{$driverMethod}($config); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Call a custom driver creator. |
85
|
|
|
* |
86
|
|
|
* @param array $config |
|
|
|
|
87
|
|
|
* @return mixed |
|
|
|
|
88
|
|
|
*/ |
89
|
8 |
|
protected function callCustomCreator(array $config) |
90
|
|
|
{ |
91
|
8 |
|
return $this->customCreators[$config['driver']]($this->container, $config); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the export connection configuration. |
96
|
|
|
* |
97
|
|
|
* @param string $name |
|
|
|
|
98
|
|
|
* @return array |
|
|
|
|
99
|
|
|
*/ |
100
|
15 |
|
protected function configurationFor($name) |
101
|
|
|
{ |
102
|
15 |
|
$config = $this->container['config']["control.export.{$name}"]; |
103
|
15 |
|
if(is_null($config)) { |
|
|
|
|
104
|
1 |
|
return null; |
105
|
|
|
} |
106
|
14 |
|
if(!isset($config['formatters'])) { |
|
|
|
|
107
|
13 |
|
$config['formatters'] = []; |
108
|
|
|
} |
109
|
14 |
|
foreach($this->formatters() as $formatter => $formatterConfig) { |
|
|
|
|
110
|
2 |
|
$config['formatters'][$formatter] = $formatterConfig; |
111
|
|
|
} |
112
|
|
|
|
113
|
14 |
|
return $config; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the default export driver name. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
6 |
|
public function getDefaultDriver() |
122
|
|
|
{ |
123
|
6 |
|
return $this->container['config']['control.export.default']; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Register a custom driver creator Closure. |
128
|
|
|
* |
129
|
|
|
* @param string $driver |
|
|
|
|
130
|
|
|
* @param \Closure $callback |
|
|
|
|
131
|
|
|
* @return $this |
|
|
|
|
132
|
|
|
*/ |
133
|
8 |
|
public function extend($driver, Closure $callback) |
134
|
|
|
{ |
135
|
8 |
|
$this->customCreators[$driver] = $callback->bindTo($this, $this); |
136
|
|
|
|
137
|
8 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
public function withFormatter(string $formatter, array $config = []) |
|
|
|
|
141
|
|
|
{ |
142
|
2 |
|
$this->formatters[$formatter] = $config; |
143
|
|
|
|
144
|
2 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Dynamically call the default driver instance. |
149
|
|
|
* |
150
|
|
|
* @param string $method |
|
|
|
|
151
|
|
|
* @param array $parameters |
|
|
|
|
152
|
|
|
* @return mixed |
|
|
|
|
153
|
|
|
*/ |
154
|
1 |
|
public function __call($method, $parameters) |
155
|
|
|
{ |
156
|
1 |
|
return $this->driver()->$method(...$parameters); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function createDumpDriver(array $config) |
|
|
|
|
160
|
|
|
{ |
161
|
1 |
|
return new DumpHandler($config); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public function createSaveCsvDriver(array $config) |
|
|
|
|
165
|
|
|
{ |
166
|
1 |
|
return new SaveCsvHandler($config); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function createAirtableDriver(array $config) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
return new AirtableHandler($config); |
172
|
|
|
} |
173
|
|
|
|
174
|
14 |
|
protected function formatters() |
|
|
|
|
175
|
|
|
{ |
176
|
14 |
|
return $this->formatters; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |