1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Infinitypaul\MultiStep\Controller; |
4
|
|
|
|
5
|
|
|
use Infinitypaul\MultiStep\MultiStepSystem; |
6
|
|
|
use Infinitypaul\MultiStep\Store\Contracts\StepStorage; |
7
|
|
|
use Infinitypaul\MultiStep\Store\DatabaseStorage; |
8
|
|
|
use Infinitypaul\MultiStep\Store\JsonOutput; |
9
|
|
|
use Infinitypaul\MultiStep\Store\SessionStorage; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
class MultiStepManager |
13
|
|
|
{ |
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The array of resolved filesystem drivers. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $disks = []; |
22
|
|
|
|
23
|
|
|
public function __construct($app) |
24
|
|
|
{ |
25
|
|
|
$this->app = $app; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function disk($name = null) |
29
|
|
|
{ |
30
|
|
|
$name = $name ?: $this->getDefaultDriver(); |
31
|
|
|
|
32
|
|
|
return $this->disks[$name] = $this->get($name); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function get($name) |
36
|
|
|
{ |
37
|
|
|
return $this->disks[$name] ?? $this->resolve($name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function resolve($name) |
41
|
|
|
{ |
42
|
|
|
$config = $this->getConfig(); |
43
|
|
|
if (is_null($config)) { |
44
|
|
|
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver."); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$driverMethod = 'create'.ucfirst($name).'Driver'; |
48
|
|
|
|
49
|
|
|
if (method_exists($this, $driverMethod)) { |
50
|
|
|
return $this->{$driverMethod}($config); |
51
|
|
|
} else { |
52
|
|
|
throw new InvalidArgumentException("Driver [{$name}] is not supported."); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getConfig() |
57
|
|
|
{ |
58
|
|
|
return $this->getDefaultDriver() ?: null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function createSessionDriver($config) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return $this->createStepSystem(new SessionStorage(request())); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function createDatabaseDriver($config) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return $this->createStepSystem(new DatabaseStorage); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function createJsonDriver() |
72
|
|
|
{ |
73
|
|
|
return $this->createStepSystem(new JsonOutput); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function createStepSystem(StepStorage $stepStorage) |
77
|
|
|
{ |
78
|
|
|
return new MultiStepSystem($stepStorage); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the default file driver. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getDefaultDriver() |
87
|
|
|
{ |
88
|
|
|
return $this->app['config']['steps.default']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Dynamically call the default driver instance. |
93
|
|
|
* |
94
|
|
|
* @param string $method |
95
|
|
|
* @param array $parameters |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function __call($method, $parameters) |
99
|
|
|
{ |
100
|
|
|
return $this->disk()->$method(...$parameters); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.