1 | <?php |
||
26 | class BaseApp extends Container |
||
27 | { |
||
28 | /** |
||
29 | * @const string |
||
30 | */ |
||
31 | const CHARSET = 'UTF-8'; |
||
32 | |||
33 | /** |
||
34 | * @var Config |
||
35 | */ |
||
36 | protected $config; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $environment; |
||
42 | |||
43 | /** |
||
44 | * @var ServiceProviderInterface[] |
||
45 | */ |
||
46 | protected $providers = []; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $booted = false; |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * @param array|Config $config |
||
57 | * @param string $environment Defaults to "production" |
||
58 | */ |
||
59 | 20 | public function __construct($config = [], $environment = 'production') |
|
60 | { |
||
61 | 20 | if ($config instanceof Config) { |
|
62 | $this->config = $config; |
||
63 | } else { |
||
64 | 20 | $this->config = new Config($config); |
|
65 | } |
||
66 | |||
67 | 20 | $this->environment = $environment; |
|
68 | |||
69 | 20 | $this->init(); |
|
70 | 20 | } |
|
71 | |||
72 | /** |
||
73 | * Initializes the application object. |
||
74 | |||
75 | * Override and put initialization code that should always be run as early as |
||
76 | * possible here, but make sure no objects are actually instanced here, because then |
||
77 | * mock objects can't be injected in their place. Place object instance code in |
||
78 | * the preHandle method. |
||
79 | */ |
||
80 | 20 | protected function init() |
|
81 | { |
||
82 | 20 | $this->set('cli', (PHP_SAPI === 'cli')); |
|
83 | |||
84 | 20 | if ($this->config->has('phpSettings')) { |
|
85 | 20 | $this->setPhpSettings($this->config->get('phpSettings')); |
|
86 | } |
||
87 | |||
88 | 20 | $this->registerProviders(); |
|
89 | 20 | } |
|
90 | |||
91 | /** |
||
92 | * Register service providers. |
||
93 | */ |
||
94 | 20 | protected function registerProviders() |
|
99 | |||
100 | /** |
||
101 | * Register service provider. |
||
102 | * |
||
103 | * @param ServiceProviderInterface $provider |
||
104 | */ |
||
105 | 20 | public function register(ServiceProviderInterface $provider) |
|
111 | |||
112 | /** |
||
113 | * @param array $phpSettings |
||
114 | * @param string $prefix |
||
115 | */ |
||
116 | 20 | protected function setPhpSettings($phpSettings, $prefix = '') |
|
117 | { |
||
118 | 20 | foreach ($phpSettings as $key => $val) { |
|
119 | 20 | $key = $prefix . $key; |
|
120 | 20 | if (is_scalar($val)) { |
|
121 | 20 | ini_set($key, $val); |
|
122 | 20 | } elseif (is_array($val)) { |
|
123 | 20 | $this->setPhpSettings($val, $key . '.'); // Set sub setting with a recursive call |
|
124 | } |
||
125 | } |
||
126 | 20 | } |
|
127 | |||
128 | /** |
||
129 | * Boot the application and its service providers. |
||
130 | * |
||
131 | * This is normally called by handle(). If requests are not handled |
||
132 | * this method will have to called manually to boot. |
||
133 | */ |
||
134 | 5 | public function boot() |
|
135 | { |
||
136 | 5 | if ($this->booted) { |
|
137 | return; |
||
138 | } |
||
139 | |||
140 | 5 | foreach ($this->providers as $provider) { |
|
141 | 5 | if ($provider instanceof BootableServiceProviderInterface) { |
|
142 | 5 | $provider->boot($this); |
|
143 | } |
||
144 | } |
||
145 | |||
146 | 5 | $this->booted = true; |
|
147 | 5 | } |
|
148 | |||
149 | /** |
||
150 | * Pre handle method meant to be overridden in descendant classes (optional). |
||
151 | * |
||
152 | * This method is called before an request is handled. Object instance code should be |
||
153 | * place here and not in init() (more info about this at init()). |
||
154 | * |
||
155 | * @param Request $request |
||
156 | * @return Response|null |
||
157 | */ |
||
158 | 3 | protected function preHandle(Request $request) |
|
161 | |||
162 | /** |
||
163 | * Post route method meant to be overridden in descendant classes (optional). |
||
164 | * This method is called before an request is dispatched but after it's routed. This means that we know |
||
165 | * it's a valid route and have access to the route attributes at this stage. |
||
166 | * |
||
167 | * @param Request $request |
||
168 | * @return Response|null |
||
169 | */ |
||
170 | 1 | protected function postRoute(Request $request) |
|
173 | |||
174 | /** |
||
175 | * Handles an http request and returns a response. |
||
176 | * |
||
177 | * @param Request $request |
||
178 | * @return Response |
||
179 | */ |
||
180 | 4 | public function handle(Request $request) |
|
206 | |||
207 | /** |
||
208 | * Returns a response for no route / resource not found. |
||
209 | * |
||
210 | * @param Request $request |
||
211 | * @return Response |
||
212 | */ |
||
213 | 1 | protected function getNoRouteResponse(Request $request) |
|
217 | |||
218 | /** |
||
219 | * Post handle method meant to be overridden in descendant classes (optional). |
||
220 | * This method is called after an request has been handled but before |
||
221 | * the response is returned from the handle method. |
||
222 | * |
||
223 | * @param Request $request |
||
224 | */ |
||
225 | 2 | protected function postHandle(Request $request) |
|
228 | |||
229 | /** |
||
230 | * @return Config |
||
231 | */ |
||
232 | 20 | public function getConfig() |
|
236 | |||
237 | /** |
||
238 | * @return bool |
||
239 | */ |
||
240 | 20 | public function isCli() |
|
244 | |||
245 | /** |
||
246 | * @return Session |
||
247 | */ |
||
248 | public function getSession() |
||
252 | |||
253 | /** |
||
254 | * @return Router |
||
255 | */ |
||
256 | 3 | public function getRouter() |
|
260 | |||
261 | /** |
||
262 | * @return Request|null |
||
263 | */ |
||
264 | 2 | public function getRequest() |
|
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | 1 | public function getEnvironment() |
|
276 | } |
||
277 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.