-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
580 lines (492 loc) · 13.6 KB
/
Application.php
File metadata and controls
580 lines (492 loc) · 13.6 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Base;
use Hubzero\Container\Container;
use Hubzero\Error\Exception\NotAuthorizedException;
use Hubzero\Error\Exception\NotFoundException;
use Hubzero\Error\Exception\MethodNotAllowedException;
use Hubzero\Error\Exception\RuntimeException;
use Hubzero\Facades\Facade;
use Hubzero\Http\RedirectResponse;
use Hubzero\Http\Request;
use Hubzero\Http\Response;
/**
* Application class
*
* Heavily influenced by Laravel's Application class
* http://laravel.com
*/
class Application extends Container
{
/**
* The framework version.
*
* @var string
*/
const VERSION = '2.1.0-dev';
/**
* Indicates if the application has "booted".
*
* @var boolean
*/
protected $booted = false;
/**
* Indicates if the application has "loaded".
*
* @var boolean
*/
protected $loaded = false;
/**
* All of the registered service providers.
*
* @var array
*/
protected $serviceProviders = array();
protected $startms = 0;
protected $endms = 0;
/**
* Create a new application instance.
*
* @param object $request Request object
* @param object $response Response object
* @return void
*/
public function __construct($client = '', Request $request = null, Response $response = null)
{
$this->startms = microtime(true);
// Work around for issues with SCRIPT_NAME and PHP_SELF set incorrectly by php-fpm
// GH-12996 https://github.com/php/php-src/issues/12996 fixed in 8.2.16
// GH-10869 https://github.com/php/php-src/issues/10869 fixed in 8.1.18
// Don't overrite ORIG_SCRIPT_NAME if already set (e.g. between above versions)
if (PHP_VERSION_ID < 80216 && isset($_SERVER['PATH_INFO']) && strpos($_SERVER['PATH_INFO'], '%') !== false)
{
if (!isset($_SERVER['ORIG_SCRIPT_NAME']))
{
$_SERVER['ORIG_SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME'];
}
$_SERVER['SCRIPT_NAME'] = str_replace(rawurldecode($_SERVER['PATH_INFO']), '', $_SERVER['SCRIPT_NAME']);
$_SERVER['PHP_SELF'] = str_replace(rawurldecode($_SERVER['PATH_INFO']), '', $_SERVER['PHP_SELF']);
}
parent::__construct();
$this['request'] = ($request ?: Request::createFromGlobals());
$this['response'] = ($response ?: new Response());
$this['app'] = $this;
}
/**
* Dynamically access application services.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this[$key];
}
/**
* Dynamically set application services.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this[$key] = $value;
}
/**
* Handle dynamic, static calls to the object.
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
$method = strtolower($method);
if (substr($method, 0, 2) == 'is')
{
$client = substr($method, 2);
$name = $this['client']->name;
if (isset($this['client']->alias))
{
$name = $this['client']->alias;
}
return ($name == $client);
}
throw new RuntimeException(sprintf('Method [%s] not found.', $method));
}
/**
* Get the version number of the application.
*
* @return string
*/
public function version()
{
if (!defined('HVERSION'))
{
return static::VERSION;
}
else
{
return HVERSION;
}
}
/**
* Register facades with the autoloader
*
* @param array $aliases
* @return void
*/
public function registerFacades($aliases = array())
{
// Set the application to resolve Facades
Facade::setApplication($this);
// Create aliaes for runtime
Facade::createAliases((array) $aliases);
}
/**
* Register a service provider with the application.
*
* @param mixed $provider \Hubzero\Base\ServiceProvider|string
* @param array $options
* @param bool $force
* @return object
*/
public function register($provider, $options = array()) //, $force = false)
{
/*if ($registered = $this->getRegistered($provider) && !$force)
{
return $registered;
}*/
// If the given "provider" is a string, we will resolve it, passing in the
// application instance automatically for the developer. This is simply
// a more convenient way of specifying your service provider classes.
if (is_string($provider))
{
$provider = $this->resolveProviderClass($provider);
}
$provider->register();
// Once we have registered the service we will iterate through the options
// and set each of them on the application so they will be available on
// the actual loading of the service objects and for developer usage.
foreach ($options as $key => $value)
{
$this[$key] = $value;
}
// Since service providers can do more than just register callbacks,
// we need to track the loaded providers for futher use later in the
// application.
$this->markAsRegistered($provider);
// If the application has already booted, we will call this boot method on
// the provider class so it has an opportunity to do its boot logic and
// will be ready for any usage by the developer's application logics.
if ($this->booted)
{
$this->bootProvider($provider);
}
return $this;
}
/**
* Get the registered service provider instance if it exists.
*
* @param mixed $provider \Hubzero\Base\ServiceProvider|string
* @return mixed \Hubzero\Base\ServiceProvider|null
*/
/*public function getRegistered($provider)
{
$name = is_string($provider) ? $provider : get_class($provider);
if (array_key_exists($name, $this->serviceProviders))
{
return $this->serviceProviders[$name];
}
return null;
}*/
/**
* Resolve a service provider instance from the class name.
*
* @param string $provider
* @return object \Hubzero\Base\ServiceProvider
*/
protected function resolveProviderClass($provider)
{
return new $provider($this);
}
/**
* Mark the given provider as registered.
*
* @param object \Hubzero\Base\ServiceProvider
* @return void
*/
protected function markAsRegistered($provider)
{
$class = get_class($provider);
$this->serviceProviders[$class] = $provider;
}
/**
* Detect the application's current environment.
*
* @param array|string $clients
* @return string
*/
public function detectClient($clients)
{
$args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null;
return $this['client'] = with(new ClientDetector($this['request']))->detect($clients, $args);
}
/**
* Determine if we are running in the console.
*
* @return bool
*/
public function runningInConsole()
{
return php_sapi_name() == 'cli';
}
/**
* Abort
*
* @param integer $code Error code
* @param string $message Error message
* @return void
*/
public function abort($code, $message='')
{
switch ($code)
{
case 405:
throw new MethodNotAllowedException($message, $code);
break;
case 404:
throw new NotFoundException($message, $code);
break;
case 403:
throw new NotAuthorizedException($message, $code);
break;
default:
throw new RuntimeException($message, $code);
break;
}
}
/**
* Redirect current request to new request (sub requests)
*
* @param string $url Url to redirect to
* @param string $message Message to display on redirect.
* @param array $type Message type.
* @return void
*/
public function redirect($url, $message = null, $type = 'success')
{
$redirect = new RedirectResponse($url == null ? '' : $url);
$redirect->setRequest($this['request']);
if ($message && $this->has('notification'))
{
$this['notification']->message($message, $type);
}
$redirect->send();
$this->close();
}
/**
* Terminate the application
*
* @return void
*/
public function close()
{
exit();
}
/**
* Provides a secure hash based on a seed
*
* @param string $seed Seed string.
* @return string A secure hash
*/
public function hash($seed)
{
return md5($this['config']->get('secret') . $seed);
}
public function load($client = null, $environments = null)
{
if ($this->loaded)
{
return;
}
if ($client == null)
{
if ($environments == null)
{
$environments = array(
'administrator' => 'administrator',
'api' => 'api',
'cli' => 'cli',
'install' => 'install',
'files' => 'files',
);
}
$this->detectClient($environments);
}
else
{
$this['client'] = ClientManager::client($client, true);
}
$client = $this['client']->name;
$this['config'] = new \Hubzero\Config\Repository($client);
$providers = PATH_CORE . '/bootstrap/' . $client . '/services.php';
$services = file_exists($providers) ? require $providers : array();
$providers = PATH_CORE . '/bootstrap/' . ucfirst($client) . '/services.php';
$services = file_exists($providers) ? array_merge($services, require $providers) : $services;
$providers = PATH_APP . '/bootstrap/' . $client . '/services.php';
$services = file_exists($providers) ? array_merge($services, require $providers) : $services;
foreach ($services as $service)
{
$this->register($service);
}
$facades = PATH_CORE . '/bootstrap/' . $client . '/aliases.php';
$aliases = file_exists($facades) ? require $facades : array();
$facades = PATH_CORE . '/bootstrap/' . ucfirst($client) . '/aliases.php';
$aliases = file_exists($facades) ? array_merge($aliases, require $facades) : $aliases;
$facades = PATH_APP . '/bootstrap/' . $client . '/aliases.php';
$aliases = file_exists($facades) ? array_merge($aliases, require $facades) : $aliases;
$this->registerFacades($aliases);
$this->loaded = true;
}
/**
* Boot the application's service providers.
*
* @return void
*/
public function boot()
{
if ($this->booted)
{
return;
}
$this->load();
array_walk($this->serviceProviders, function($p)
{
$this->bootProvider($p);
});
$this->booted = true;
}
/**
* Boot the given service provider.
*
* @param object $provider
* @return void
*/
protected function bootProvider(ServiceProvider $provider)
{
if (method_exists($provider, 'boot'))
{
return $provider->boot();
}
}
/**
* Get only runnable services
*
* @param array $layers Unfiltered services
* @return array Filtered runnable services
*/
protected function middleware($services)
{
return array_filter($services, function($service)
{
return $service instanceof Middleware;
});
}
/**
* Application layer is responsible for dispatching request
*
* @param object $request Request object
* @return object Response object
*/
public function handle(Request $request)
{
return $this['response']->compress($this['config']->get('gzip', false));
}
/**
* Run the application and send the response.
*
* @return void
*/
public function run()
{
// Boot the application
//
// This allows service providers to finish performing any
// needed setup.
$this->boot();
// Start handling errors before doing anything else
if ($this->has('error'))
{
array_walk($this->serviceProviders, function($p)
{
if (method_exists($p, 'startHandling'))
{
return $p->startHandling();
}
});
}
// Initialise
if (!$this->runningInConsole() && $this->has('dispatcher'))
{
$this['dispatcher']->trigger('system.onAfterInitialise');
if ($this->has('profiler') && $this->get('profiler'))
{
$this['profiler']->mark('afterInitialise');
}
}
// Create a new stack and bind to application then
$this['stack'] = new Stack($this);
// Send request throught stack and finally send response
$this['stack']
->send($this['request'])
->through($this->middleware($this->serviceProviders))
->then(function($request, $response)
{
$response->prepare($request);
$response->send();
});
}
public function __destruct()
{
ignore_user_abort(true);
session_write_close();
while(ob_get_level())
ob_end_flush();
flush();
if (function_exists('fastcgi_finish_request'))
{
fastcgi_finish_request();
}
$this->endms = microtime(true);
if ( function_exists("apcu_enabled") && apcu_enabled())
{
$time = (int) $this->startms;
$ms = $this->endms - $this->startms;
// The following is subject to race conditions, restarts, maybe garbage collection
// and other vagaries of the APCu cache. This data is intended as an
// estimate so being exact really should not matter.
$new_sec_count = (float) apcu_inc($time, 1, $success, 61);
$old_sec_avg = (float) apcu_fetch('avg'.$time, $success);
$new_sec_avg = ((($new_sec_count - 1) * $old_sec_avg) + $ms)/$new_sec_count;
$sec_avg = (float) apcu_store('avg'.$time, $new_sec_avg, 61);
$new_min_count = (float) apcu_inc(intdiv($time,60), 1, $success, 3601);
$old_min_avg = (float) apcu_fetch('avg'.intdiv($time,60), $success);
$new_min_avg = ((($new_min_count - 1) * $old_min_avg) + $ms)/$new_min_count;
$min_avg = (float) apcu_store('avg'.intdiv($time,60), $new_min_avg, 3601);
$new_hour_count = (float) apcu_inc(intdiv($time,3600), 1, $success, 86401);
$old_hour_avg = (float) apcu_fetch('avg'.intdiv($time,3600), $success);
$new_hour_avg = ((($new_hour_count - 1) * $old_hour_avg) + $ms)/$new_hour_count;
$hour_avg = (float) apcu_store('avg'.intdiv($time,3600), $new_hour_avg, 86401);
$new_day_count = (float) apcu_inc(intdiv($time,86400), 1, $success, 2678401);
$old_day_avg = (float) apcu_fetch('avg'.intdiv($time,86400), $success);
$new_day_avg = ((($new_day_count - 1) * $old_day_avg) + $ms)/$new_day_count;
$day_avg = (float) apcu_store('avg'.intdiv($time,86400), $new_day_avg, 2678401);
}
}
}