|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\aws\s3; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
|
6
|
|
|
use diecoding\aws\s3\handlers\PlainCommandHandler; |
|
7
|
|
|
use diecoding\aws\s3\interfaces; |
|
8
|
|
|
use yii\base\Configurable; |
|
9
|
|
|
use yii\base\Exception; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class HandlerResolver |
|
13
|
|
|
* |
|
14
|
|
|
* @package diecoding\aws\s3 |
|
15
|
|
|
*/ |
|
16
|
|
|
class HandlerResolver implements interfaces\HandlerResolver, Configurable |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $handlers = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $plainCommandHandlerClassName = PlainCommandHandler::class; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \Aws\S3\S3Client */ |
|
25
|
|
|
protected $s3Client; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* HandlerResolver constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Aws\S3\S3Client $s3Client |
|
31
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(S3Client $s3Client, array $config = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->configure($config); |
|
36
|
|
|
$this->s3Client = $s3Client; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $properties |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
private function configure(array $properties) |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($properties as $name => $value) { |
|
47
|
|
|
$this->{$name} = $value; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \diecoding\aws\s3\interfaces\commands\Command $command |
|
53
|
|
|
* |
|
54
|
|
|
* @return \diecoding\aws\s3\interfaces\handlers\Handler |
|
55
|
|
|
* @throws \yii\base\Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
public function resolve(interfaces\commands\Command $command): interfaces\handlers\Handler |
|
58
|
|
|
{ |
|
59
|
|
|
$commandClass = get_class($command); |
|
60
|
|
|
|
|
61
|
|
|
if (isset($this->handlers[$commandClass])) { |
|
62
|
|
|
$handler = $this->handlers[$commandClass]; |
|
63
|
|
|
|
|
64
|
|
|
return is_object($handler) ? $handler : $this->createHandler($handler); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($command instanceof interfaces\commands\PlainCommand) { |
|
68
|
|
|
return $this->createHandler($this->plainCommandHandlerClassName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$handlerClass = $commandClass . 'Handler'; |
|
72
|
|
|
if (class_exists($handlerClass)) { |
|
73
|
|
|
return $this->createHandler($handlerClass); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$handlerClass = str_replace('\\commands\\', '\\handlers\\', $handlerClass); |
|
77
|
|
|
if (class_exists($handlerClass)) { |
|
78
|
|
|
return $this->createHandler($handlerClass); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
throw new Exception("Could not terminate the handler of a command of type \"{$commandClass}\""); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $commandClass |
|
86
|
|
|
* @param mixed $handler |
|
87
|
|
|
*/ |
|
88
|
|
|
public function bindHandler(string $commandClass, $handler) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->handlers[$commandClass] = $handler; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $handlers |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setHandlers(array $handlers) |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($handlers as $commandClass => $handler) { |
|
99
|
|
|
$this->bindHandler($commandClass, $handler); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $className |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setPlainCommandHandler(string $className) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->plainCommandHandlerClassName = $className; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string|array $type |
|
113
|
|
|
* |
|
114
|
|
|
* @return \diecoding\aws\s3\interfaces\handlers\Handler |
|
115
|
|
|
* @throws \yii\base\InvalidConfigException |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function createHandler($type): interfaces\handlers\Handler |
|
118
|
|
|
{ |
|
119
|
|
|
return \Yii::createObject($type, [$this->s3Client]); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|