forked from tastphp/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernelTrait.php
More file actions
144 lines (125 loc) · 3.25 KB
/
Copy pathKernelTrait.php
File metadata and controls
144 lines (125 loc) · 3.25 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace TastPHP\Framework\Traits;
use TastPHP\Framework\Handler\AliasLoaderHandler;
use TastPHP\Framework\Event\AppEvent;
/**
* Class KernelTrait
* @package TastPHP\Framework\Traits
*/
trait KernelTrait
{
/**
* run app
*/
public function run()
{
$this['eventDispatcher']->dispatch(AppEvent::RESPONSE, new \TastPHP\Framework\Event\HttpEvent(null, $this['router']->matchCurrentRequest()));
}
/**
* @param $name
* @param null $callable
* @return mixed
*/
public function singleton($name, $callable = null)
{
if (!isset($this[$name]) && $callable) {
$this[$name] = call_user_func($callable);
}
return $this[$name];
}
/**
* @return Kernel
*/
public static function getInstance()
{
if (!(self::$instance instanceof self)) {
self::$instance = new self;
}
return self::$instance;
}
/**
* do the class alias
*/
public function aliasLoader()
{
AliasLoaderHandler::getInstance($this->aliases)->register();
}
/**
* register services
*/
public function registerServices()
{
foreach ($this->serviceProviders as $provider) {
$provider = new $provider(self::$instance);
$provider->register();
}
}
/**
* @param array $aliases
*/
protected function injectAliases(array $aliases)
{
$this->aliases = array_merge($this->aliases, $aliases);
}
/**
* @param array $serviceProviders
*/
protected function injectServiceProviders(array $serviceProviders)
{
$this->serviceProviders = array_merge($this->serviceProviders, $serviceProviders);
}
/**
* @return bool
*/
public function runningInConsole()
{
return php_sapi_name() == 'cli';
}
/**
* @param $alias string
* @param $class string
* @throws \Exception
*/
public function replaceAlias($alias, $class)
{
$key = ucfirst($alias);
if (!isset($this->aliases[$key])) {
throw new \InvalidArgumentException(sprintf('The alias "%s" is not defined.', $key));
}
$this->aliases[$key] = $class;
}
/**
* @param $key string
* @param $serviceProvider string
*/
public function replaceServiceProvider($key, $serviceProvider)
{
$key = ucfirst($key);
if (!isset($this->serviceProviders[$key])) {
throw new \InvalidArgumentException(sprintf('The serviceProvider "%s" is not defined.', $key));
}
$this->serviceProviders[$key] = $serviceProvider;
}
/**
* @param $key string
* @param $listener string
*/
public function replaceListener($key, $listener)
{
if (!isset($this->listeners[$key])) {
throw new \InvalidArgumentException(sprintf('The kernel listener "%s" is not defined.', $key));
}
$this->listeners[$key] = $listener;
}
/**
* @param string $key
* @return array|mixed
*/
public function getListeners($key = '')
{
if (!empty($key) && !empty($this->listeners[$key])) {
return $this->listeners[$key];
}
return $this->listeners;
}
}