|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleImageManager; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use SimpleImageManager\Contracts\ImageManagerInterface; |
|
8
|
|
|
use SimpleImageManager\Managers\ImageManager; |
|
9
|
|
|
|
|
10
|
|
|
class SimpleImageManager |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Configuration filtration callback. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Closure|null |
|
16
|
|
|
*/ |
|
17
|
|
|
protected static ?\Closure $filterConfigCallback = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The application instance. |
|
21
|
|
|
*/ |
|
22
|
|
|
protected Application $app; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The array of resolved services. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected array $services = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Application $app |
|
33
|
|
|
*/ |
|
34
|
13 |
|
public function __construct(Application $app) |
|
35
|
|
|
{ |
|
36
|
13 |
|
$this->app = $app; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add additional filter for config data. Can be user in app service provider. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Closure|null $callback |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function filterConfigUsing(?\Closure $callback): void |
|
45
|
|
|
{ |
|
46
|
1 |
|
static::$filterConfigCallback = $callback; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get default driver name. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function getDefaultDriver(): string |
|
55
|
|
|
{ |
|
56
|
2 |
|
return $this->app->get('config')['simple-image-manager.default.driver']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
13 |
|
protected function getConfig(string $name): mixed |
|
65
|
|
|
{ |
|
66
|
13 |
|
$configData = $this->app->get('config')["simple-image-manager.drivers.{$name}"] ?? null; |
|
67
|
|
|
|
|
68
|
13 |
|
return $this->filterConfig($name, $configData); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $driver |
|
73
|
|
|
* @param mixed $config |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
13 |
|
protected function filterConfig(string $driver, mixed $config): mixed |
|
78
|
|
|
{ |
|
79
|
13 |
|
if (is_callable(static::$filterConfigCallback)) { |
|
80
|
1 |
|
return call_user_func(static::$filterConfigCallback, $driver, $config); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
12 |
|
return $config; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get a driver instance. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string|null $driver |
|
90
|
|
|
* |
|
91
|
|
|
* @return ImageManagerInterface |
|
92
|
|
|
*/ |
|
93
|
13 |
|
public function driver(?string $driver = null): ImageManagerInterface |
|
94
|
|
|
{ |
|
95
|
13 |
|
return $this->get($driver ?? $this->getDefaultDriver()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Create a driver instance. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* |
|
103
|
|
|
* @return ImageManagerInterface |
|
104
|
|
|
*/ |
|
105
|
13 |
|
protected function get(string $name): ImageManagerInterface |
|
106
|
|
|
{ |
|
107
|
13 |
|
return $this->services[ $name ] ?? ($this->services[ $name ] = $this->resolve($name)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $name |
|
112
|
|
|
* |
|
113
|
|
|
* @return ImageManagerInterface |
|
114
|
|
|
*/ |
|
115
|
13 |
|
protected function resolve(string $name): ImageManagerInterface |
|
116
|
|
|
{ |
|
117
|
13 |
|
$config = $this->getConfig($name); |
|
118
|
|
|
|
|
119
|
13 |
|
if (is_null($config)) { |
|
120
|
1 |
|
throw new InvalidArgumentException("Simple image manager driver [{$name}] is not defined."); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
12 |
|
if (!empty($config['manager'])) { |
|
124
|
2 |
|
$manager = $config['manager']; |
|
125
|
2 |
|
if (is_string($manager) && |
|
126
|
2 |
|
class_exists($manager) && |
|
127
|
2 |
|
is_subclass_of($manager, ImageManagerInterface::class)) { |
|
128
|
1 |
|
return new $manager($config); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
throw new InvalidArgumentException("Driver [{$name}] has wong manager."); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
10 |
|
return new ImageManager($config); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Dynamically call the default driver instance. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $method |
|
141
|
|
|
* @param array $parameters |
|
142
|
|
|
* |
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function __call(string $method, array $parameters) |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $this->driver()->$method(...$parameters); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|