This repository has been archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
ClassDefinition.hh
105 lines (89 loc) · 3.02 KB
/
ClassDefinition.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Context\Definition;
use ReflectionClass;
use ReflectionMethod;
use Titon\Context\Depository;
use Titon\Context\MethodList;
/**
* A class definition determines how a class is created, including necessary
* arguments passed into the constructor as well as methods to be called after
* instantiation before returning the object for use.
*
* @package Titon\Context\Definition
*/
class ClassDefinition extends AbstractDefinition {
/**
* The name of the class defined.
*
* @var string
*/
protected string $class;
/**
* Data structure of methods and arguments to be called on the class after
* instantiation.
*
* @var \Titon\Context\MethodList
*/
protected MethodList $methods = Vector {};
/**
* Construct a new class definition.
*
* @param string $key The class name or key associated with
* @param mixed $class The class to define
* @param Depository $depository The container that the definition is
* contained in
*/
public function __construct(string $key, string $class, Depository $depository) {
parent::__construct($key, $depository);
$this->class = $class;
}
/**
* {@inheritdoc}
*/
public function create<T>(/* HH_FIXME[4033]: variadic + strict */ ...$arguments): T {
$reflection = new ReflectionClass($this->class);
$object = $reflection->newInstanceArgs(
$this->resolveArguments(...$arguments)
);
return $this->callMethods($object);
}
/**
* Add a method to be called on the class after instantiating a new instance.
*
* @param string $method Name of the method
* @param ...$arguments Arguments to pass into the method
*
* @return $this The definition for fluent method chaining
*/
public function call(string $method, /* HH_FIXME[4033]: variadic + strict */ ...$arguments): this {
$this->methods[] = shape(
'method' => $method,
'arguments' => $arguments,
);
return $this;
}
/**
* After instantiating the instance, this method will call any necessary
* methods on the instance with the specified arguments.
*
* @param object $object The object to call the methods on
*
* @return mixed The new object instance
*/
protected function callMethods<T>(T $object): T {
foreach ($this->methods as $method) {
$reflection = new ReflectionMethod($object, $method['method']);
$args = [];
foreach ($method['arguments'] as $arg) {
$args[] = (is_string($arg) && class_exists($arg)) ? $this->depository->make($arg) : $arg;
}
$reflection->invokeArgs($object, $args);
}
return $object;
}
}