1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tkanstantsin\fileupload\model; |
4
|
|
|
|
5
|
|
|
use tkanstantsin\fileupload\config\InvalidConfigException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Container |
9
|
|
|
*/ |
10
|
|
|
class Container |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Configures an object with the initial property values. Inspired by Yii2. |
14
|
|
|
* @see \yii\BaseYii::configure |
15
|
|
|
* @param object $object the object to be configured |
16
|
|
|
* @param array $properties the property initial values given in terms of |
17
|
|
|
* name-value pairs. |
18
|
|
|
* @return object the object itself |
19
|
|
|
* @throws InvalidConfigException |
20
|
|
|
*/ |
21
|
|
|
public static function configure($object, $properties) |
22
|
|
|
{ |
23
|
|
|
foreach ($properties as $name => $value) { |
24
|
|
|
if (!property_exists($object, $name)) { |
25
|
|
|
throw new InvalidConfigException(sprintf('Property %s in class %s not found.', $name, \get_class($object))); |
26
|
|
|
} |
27
|
|
|
$object->$name = $value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $object; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a new object using the given configuration. Inspired by Yii2. |
35
|
|
|
* @see \yii\BaseYii::createObject |
36
|
|
|
* @param string|array $type |
37
|
|
|
* @return mixed |
38
|
|
|
* @throws InvalidConfigException |
39
|
|
|
* @throws \ReflectionException |
40
|
|
|
*/ |
41
|
|
|
public static function createObject($type) |
42
|
|
|
{ |
43
|
|
|
if (\is_string($type)) { |
44
|
|
|
return static::get($type); |
45
|
|
|
} elseif (\is_array($type)) { |
46
|
|
|
if (!isset($type['class'])) { |
47
|
|
|
throw new InvalidConfigException('Object configuration must be an array containing a "class" element.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$class = $type['class']; |
51
|
|
|
unset($type['class']); |
52
|
|
|
|
53
|
|
|
return static::get($class, $type); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
throw new InvalidConfigException('Unsupported configuration type: ' . \gettype($type)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates an instance of the specified class. Inspired by Yii2 di. |
61
|
|
|
* @see \yii\di\Container::build |
62
|
|
|
* @param string $class the class name |
63
|
|
|
* @param array $config configurations to be applied to the new instance |
64
|
|
|
* @return mixed |
65
|
|
|
* @throws \ReflectionException |
66
|
|
|
* @throws InvalidConfigException |
67
|
|
|
*/ |
68
|
|
|
public static function get(string $class, array $config = []) |
69
|
|
|
{ |
70
|
|
|
$reflection = new \ReflectionClass($class); |
71
|
|
|
|
72
|
|
|
if (!$reflection->isInstantiable()) { |
73
|
|
|
throw new \ReflectionException(sprintf('`%s` is not instantiable.', $reflection->name)); |
74
|
|
|
} |
75
|
|
|
if ($reflection->implementsInterface(IConfigurable::class)) { |
76
|
|
|
// set $config as the last parameter (existing one will be overwritten) |
77
|
|
|
return $reflection->newInstanceArgs($config); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$object = $reflection->newInstance(); |
81
|
|
|
static::configure($object, $config); |
82
|
|
|
|
83
|
|
|
return $object; |
84
|
|
|
} |
85
|
|
|
} |