1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* App.php |
5
|
|
|
* |
6
|
|
|
* Jaxon application |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2019 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace Jaxon\App; |
16
|
|
|
|
17
|
|
|
use Jaxon\Di\Container; |
18
|
|
|
use Jaxon\App\Traits\AjaxSendTrait; |
19
|
|
|
use Jaxon\App\Traits\AppTrait; |
20
|
|
|
use Jaxon\Exception\RequestException; |
21
|
|
|
use Jaxon\Exception\SetupException; |
22
|
|
|
|
23
|
|
|
use function file_exists; |
24
|
|
|
use function http_response_code; |
25
|
|
|
use function intval; |
26
|
|
|
use function is_array; |
27
|
|
|
|
28
|
|
|
class App implements AppInterface |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
use AppTrait; |
31
|
|
|
use AjaxSendTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The class constructor |
35
|
|
|
* |
36
|
|
|
* @param Container $xContainer |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct(Container $xContainer) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->initApp($xContainer); |
41
|
|
|
} |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Read config options from a config file and set up the library |
45
|
|
|
* |
46
|
|
|
* @param string $sConfigFile The full path to the config file |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
* @throws SetupException |
50
|
|
|
*/ |
51
|
|
|
public function setup(string $sConfigFile) |
52
|
|
|
{ |
53
|
|
|
if(!file_exists($sConfigFile)) |
54
|
|
|
{ |
55
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
56
|
|
|
throw new SetupException($sMessage); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Read the config options. |
60
|
|
|
$aOptions = $this->xConfigManager->read($sConfigFile); |
|
|
|
|
61
|
|
|
$aLibOptions = $aOptions['lib'] ?? []; |
62
|
|
|
$aAppOptions = $aOptions['app'] ?? []; |
63
|
|
|
if(!is_array($aLibOptions) || !is_array($aAppOptions)) |
64
|
|
|
{ |
65
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
66
|
|
|
throw new SetupException($sMessage); |
67
|
|
|
} |
68
|
|
|
// The bootstrap set this to false. It needs to be changed. |
69
|
|
|
if(!isset($aLibOptions['core']['response']['send'])) |
70
|
|
|
{ |
71
|
|
|
$aLibOptions['core']['response']['send'] = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->bootstrap() |
75
|
|
|
->lib($aLibOptions) |
|
|
|
|
76
|
|
|
->app($aAppOptions) |
|
|
|
|
77
|
|
|
->setup(); |
|
|
|
|
78
|
|
|
} |
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @inheritDoc |
82
|
|
|
* @throws RequestException |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function httpResponse(string $sCode = '200') |
85
|
|
|
{ |
86
|
|
|
// Set the HTTP response code |
87
|
|
|
http_response_code(intval($sCode)); |
88
|
|
|
|
89
|
|
|
// Send the response |
90
|
|
|
$this->sendResponse(); |
91
|
|
|
} |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|