|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* App.php - Jaxon application |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @copyright 2019 Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\App; |
|
14
|
|
|
|
|
15
|
|
|
use Jaxon\Jaxon; |
|
16
|
|
|
use Jaxon\Exception\SetupException; |
|
17
|
|
|
use Jaxon\Response\Manager as ResponseManager; |
|
18
|
|
|
use Jaxon\Utils\Config\Reader as ConfigReader; |
|
19
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
20
|
|
|
|
|
21
|
|
|
use function intval; |
|
22
|
|
|
use function file_exists; |
|
23
|
|
|
use function is_array; |
|
24
|
|
|
use function http_response_code; |
|
25
|
|
|
|
|
26
|
|
|
class App |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
use \Jaxon\Features\App; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ResponseManager |
|
32
|
|
|
*/ |
|
33
|
|
|
private $xResponseManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ConfigReader |
|
37
|
|
|
*/ |
|
38
|
|
|
private $xConfigReader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Translator |
|
42
|
|
|
*/ |
|
43
|
|
|
private $xTranslator; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The class constructor |
|
47
|
|
|
* |
|
48
|
|
|
* @param Jaxon $jaxon |
|
|
|
|
|
|
49
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
|
|
50
|
|
|
* @param ConfigReader $xConfigReader |
|
|
|
|
|
|
51
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Jaxon $jaxon, ResponseManager $xResponseManager, |
|
|
|
|
|
|
54
|
|
|
ConfigReader $xConfigReader, Translator $xTranslator) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->jaxon = $jaxon; |
|
|
|
|
|
|
57
|
|
|
$this->xResponseManager = $xResponseManager; |
|
58
|
|
|
$this->xConfigReader = $xConfigReader; |
|
|
|
|
|
|
59
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
60
|
|
|
} |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return Translator |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function translator(): Translator |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->xTranslator; |
|
68
|
|
|
} |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Read config options from a config file and setup the library |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $sConfigFile The full path to the config file |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
* @throws SetupException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setup(string $sConfigFile) |
|
79
|
|
|
{ |
|
80
|
|
|
if(!file_exists($sConfigFile)) |
|
81
|
|
|
{ |
|
82
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
|
83
|
|
|
throw new SetupException($sMessage); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Read the config options. |
|
87
|
|
|
$aOptions = $this->jaxon->readConfig($sConfigFile); |
|
|
|
|
|
|
88
|
|
|
$aLibOptions = $aOptions['lib'] ?? []; |
|
89
|
|
|
$aAppOptions = $aOptions['app'] ?? []; |
|
90
|
|
|
if(!is_array($aLibOptions) || !is_array($aAppOptions)) |
|
91
|
|
|
{ |
|
92
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
|
93
|
|
|
throw new SetupException($sMessage); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->bootstrap() |
|
97
|
|
|
->lib($aLibOptions) |
|
|
|
|
|
|
98
|
|
|
->app($aAppOptions) |
|
|
|
|
|
|
99
|
|
|
// ->uri($sUri) |
|
100
|
|
|
// ->js(!$bIsDebug, $sJsUrl, $sJsDir, !$bIsDebug) |
|
101
|
|
|
->setup(); |
|
|
|
|
|
|
102
|
|
|
} |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
|
|
|
|
|
105
|
|
|
* @inheritDoc |
|
106
|
|
|
*/ |
|
|
|
|
|
|
107
|
|
|
public function httpResponse(string $sCode = '200') |
|
108
|
|
|
{ |
|
109
|
|
|
// Set the HTTP response code |
|
110
|
|
|
http_response_code(intval($sCode)); |
|
111
|
|
|
// Send the response |
|
112
|
|
|
$this->xResponseManager->sendOutput(); |
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|