|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author : Jagepard <[email protected]"> |
|
7
|
|
|
* @copyright Copyright (c) 2019, Jagepard |
|
8
|
|
|
* @license https://mit-license.org/ MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Rudra\Container; |
|
12
|
|
|
|
|
13
|
|
|
use Rudra\Container\Interfaces\{ApplicationInterface, ContainerInterface, RequestInterface, ResponseInterface}; |
|
14
|
|
|
|
|
15
|
|
|
class Application implements ApplicationInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ApplicationInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $application; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $instances = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return ApplicationInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function run(): ApplicationInterface |
|
30
|
|
|
{ |
|
31
|
|
|
if (!static::$application instanceof static) { |
|
32
|
|
|
static::$application = new static(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return static::$application; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $services |
|
40
|
|
|
* @throws \ReflectionException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setServices(array $services): void |
|
43
|
|
|
{ |
|
44
|
|
|
foreach ($services['contracts'] as $interface => $contract) { |
|
45
|
|
|
$this->binding()->set([$interface => $contract]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
foreach ($services['services'] as $name => $service) { |
|
49
|
|
|
$this->objects()->set([$name, $service]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return ContainerInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
public function objects(): ContainerInterface |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->instantiate('objects', Objects::class, $this->binding()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return RequestInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public function request(): RequestInterface |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->instantiate('request', Request::class); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return ContainerInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
public function cookie(): ContainerInterface |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->instantiate('cookie', Cookie::class); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return ContainerInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
public function session(): ContainerInterface |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->instantiate('session', Session::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return ResponseInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function response(): ResponseInterface |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->instantiate('response', Response::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return ContainerInterface |
|
95
|
|
|
*/ |
|
96
|
|
|
public function config(): ContainerInterface |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->instantiate('config', Container::class); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return ContainerInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function binding(): ContainerInterface |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->instantiate('binding', Container::class); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $varName |
|
111
|
|
|
* @param $instance |
|
112
|
|
|
* @param $data |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
|
|
private function instantiate($varName, $instance, $data = null) |
|
116
|
|
|
{ |
|
117
|
|
|
if (!array_key_exists($varName, $this->instances)) { |
|
118
|
|
|
$this->instances[$varName] = new $instance($data); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->instances[$varName]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|