1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This program is free software. It comes without any warranty, to |
4
|
|
|
* the extent permitted by applicable law. You can redistribute it |
5
|
|
|
* and/or modify it under the terms of the Do What The Fuck You Want |
6
|
|
|
* To Public License, Version 2, as published by Sam Hocevar. See |
7
|
|
|
* http://www.wtfpl.net/ for more details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types = 1); |
11
|
|
|
|
12
|
|
|
namespace hanneskod\classtools\Instantiator; |
13
|
|
|
|
14
|
|
|
use hanneskod\classtools\Exception\LogicException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Instantiate reflected class |
18
|
|
|
* |
19
|
|
|
* @author Hannes Forsgård <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Instantiator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \ReflectionClass |
25
|
|
|
*/ |
26
|
|
|
private $class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set class to instantiate |
30
|
|
|
*/ |
31
|
|
|
public function setReflectionClass(\ReflectionClass $class): void |
32
|
|
|
{ |
33
|
|
|
$this->class = $class; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get loaded reflection class |
38
|
|
|
* |
39
|
|
|
* @throws LogicException If reflected class is not loaded |
40
|
|
|
*/ |
41
|
|
|
public function getReflectionClass(): \ReflectionClass |
42
|
|
|
{ |
43
|
|
|
if (!isset($this->class)) { |
44
|
|
|
throw new LogicException("Reflected class not loaded"); |
45
|
|
|
} |
46
|
|
|
return $this->class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get number of required constructor parameters |
51
|
|
|
*/ |
52
|
|
|
public function countConstructorArgs(): int |
53
|
|
|
{ |
54
|
|
|
if ($constructor = $this->class->getConstructor()) { |
55
|
|
|
return $constructor->getNumberOfRequiredParameters(); |
56
|
|
|
} |
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if class is instantiable |
62
|
|
|
*/ |
63
|
|
|
public function isInstantiable(): bool |
64
|
|
|
{ |
65
|
|
|
return $this->getReflectionClass()->isInstantiable(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check if class is instantiable without constructor parameters |
70
|
|
|
*/ |
71
|
|
|
public function isInstantiableWithoutArgs(): bool |
72
|
|
|
{ |
73
|
|
|
return $this->isInstantiable() && !$this->countConstructorArgs(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create instance |
78
|
|
|
* |
79
|
|
|
* @param array $args Optional constructor arguments |
80
|
|
|
* @return mixed Instance of reflected class |
81
|
|
|
* @throws LogicException If reflected class is not instantiable |
82
|
|
|
*/ |
83
|
|
|
public function instantiate(array $args = array()) |
84
|
|
|
{ |
85
|
|
|
if (!$this->isInstantiable()) { |
86
|
|
|
throw new LogicException("Reflected class is not instantiable"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (count($args) < $this->countConstructorArgs()) { |
90
|
|
|
throw new LogicException("Unable to instantiate, too few constructor arguments"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->getReflectionClass()->newInstanceArgs($args); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|