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