1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraComponents\GraphQL\Helpers; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class TypeRegistry |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The application instance. |
11
|
|
|
* |
12
|
|
|
* @var \Illuminate\Foundation\Application |
13
|
|
|
*/ |
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The array of types. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $types = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The array of resolved types. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $instances = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new instance. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Foundation\Application $app |
34
|
|
|
* @param array $types |
35
|
|
|
* @return void |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct($app, array $types = []) |
38
|
|
|
{ |
39
|
|
|
$this->app = $app; |
40
|
|
|
|
41
|
|
|
foreach ($types as $name => $class) { |
42
|
|
|
$this->setType($class, is_numeric($name) ? null : $name); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set type into registry |
48
|
|
|
* |
49
|
|
|
* @param string $class |
50
|
|
|
* @param string $name |
51
|
|
|
*/ |
52
|
|
|
public function setType($class, $name = null) |
53
|
|
|
{ |
54
|
|
|
$name = $this->getName($class, $name); |
55
|
|
|
$this->types[$name] = $class; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Resolve type from registry |
60
|
|
|
* |
61
|
|
|
* @param string $type |
62
|
|
|
* @param string $parameters |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
*/ |
66
|
|
|
protected function resolveType($type, array $parameters) |
67
|
|
|
{ |
68
|
|
|
if (! array_key_exists($type, $this->types)) { |
69
|
|
|
throw new InvalidArgumentException(sprintf('The type "%s" is not found.', $type)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (count($parameters)) { |
73
|
|
|
return $this->make($type, $parameters); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (! array_key_exists($type, $this->instances)) { |
77
|
|
|
$this->instances[$type] = $this->make($type, $parameters); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->instances[$type]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Make a new instance of type |
85
|
|
|
* |
86
|
|
|
* @param string $type |
87
|
|
|
* @param string $parameters |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
protected function make($type, $parameters) |
91
|
|
|
{ |
92
|
|
|
$class = $this->types[$type]; |
93
|
|
|
|
94
|
|
|
if (count($parameters)) { |
95
|
|
|
return new $class(...$parameters); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new $class(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get name from className |
103
|
|
|
* @param string $class |
104
|
|
|
* @param string $name |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function getName($class, $name = null) |
108
|
|
|
{ |
109
|
|
|
if ($name) { |
|
|
|
|
110
|
|
|
return $name; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($prevPos = strrpos($class, '\\')) { |
114
|
|
|
$class = substr($class, $prevPos + 1); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (substr($class, -4) == 'Type') { |
118
|
|
|
$class = substr($class, 0, -4); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return lcfirst($class); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Dynamically call the type instance. |
126
|
|
|
* |
127
|
|
|
* @param string $type |
128
|
|
|
* @param array $parameters |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function __call($type, $parameters) |
132
|
|
|
{ |
133
|
|
|
return $this->resolveType($type, $parameters); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.