1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Di\Traits; |
4
|
|
|
|
5
|
|
|
use Jaxon\App\App; |
6
|
|
|
use Jaxon\App\AppInterface; |
7
|
|
|
use Jaxon\App\Bootstrap; |
8
|
|
|
use Jaxon\App\Config\ConfigEventManager; |
9
|
|
|
use Jaxon\App\Config\ConfigManager; |
10
|
|
|
use Jaxon\App\Dialog\Library\DialogLibraryManager; |
11
|
|
|
use Jaxon\App\I18n\Translator; |
12
|
|
|
use Jaxon\App\View\ViewRenderer; |
13
|
|
|
use Jaxon\Di\Container; |
14
|
|
|
use Jaxon\Jaxon; |
15
|
|
|
use Jaxon\Plugin\Manager\PackageManager; |
16
|
|
|
use Jaxon\Request\Handler\CallbackManager; |
17
|
|
|
use Jaxon\Utils\Config\ConfigReader; |
18
|
|
|
|
19
|
|
|
trait AppTrait |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $sJsLibVersion = 'jaxon_javascript_library_version'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The default config options |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $aConfig = [ |
|
|
|
|
32
|
|
|
'core' => [ |
33
|
|
|
'version' => Jaxon::VERSION, |
34
|
|
|
'language' => 'en', |
35
|
|
|
'encoding' => 'utf-8', |
36
|
|
|
'decode_utf8' => false, |
37
|
|
|
'prefix' => [ |
38
|
|
|
'function' => 'jaxon_', |
39
|
|
|
'class' => 'Jaxon', |
40
|
|
|
], |
41
|
|
|
'request' => [ |
42
|
|
|
// 'uri' => '', |
43
|
|
|
'mode' => 'asynchronous', |
44
|
|
|
'method' => 'POST', // W3C: Method is case-sensitive |
45
|
|
|
], |
46
|
|
|
'response' => [ |
47
|
|
|
'send' => true, |
48
|
|
|
], |
49
|
|
|
'debug' => [ |
50
|
|
|
'on' => false, |
51
|
|
|
'verbose' => false, |
52
|
|
|
], |
53
|
|
|
'process' => [ |
54
|
|
|
'exit' => true, |
55
|
|
|
'timeout' => 6000, |
56
|
|
|
], |
57
|
|
|
'error' => [ |
58
|
|
|
'handle' => false, |
59
|
|
|
'log_file' => '', |
60
|
|
|
], |
61
|
|
|
'jquery' => [ |
62
|
|
|
'no_conflict' => false, |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
'js' => [ |
66
|
|
|
'lib' => [ |
67
|
|
|
'output_id' => 0, |
68
|
|
|
'queue_size' => 0, |
69
|
|
|
'load_timeout' => 2000, |
70
|
|
|
'show_status' => false, |
71
|
|
|
'show_cursor' => true, |
72
|
|
|
], |
73
|
|
|
'app' => [ |
74
|
|
|
'dir' => '', |
75
|
|
|
'options' => '', |
76
|
|
|
], |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Register the values into the container |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
private function registerApp() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
// Translator |
88
|
|
|
$this->set(Translator::class, function($c) { |
|
|
|
|
89
|
|
|
$xTranslator = new Translator(); |
90
|
|
|
$sResourceDir = rtrim(trim($c->g('jaxon.core.dir.translation')), '/\\'); |
91
|
|
|
// Load the Jaxon package translations |
92
|
|
|
$xTranslator->loadTranslations($sResourceDir . '/en/errors.php', 'en'); |
93
|
|
|
$xTranslator->loadTranslations($sResourceDir . '/fr/errors.php', 'fr'); |
94
|
|
|
$xTranslator->loadTranslations($sResourceDir . '/es/errors.php', 'es'); |
95
|
|
|
// Load the config translations |
96
|
|
|
$xTranslator->loadTranslations($sResourceDir . '/en/config.php', 'en'); |
97
|
|
|
$xTranslator->loadTranslations($sResourceDir . '/fr/config.php', 'fr'); |
98
|
|
|
$xTranslator->loadTranslations($sResourceDir . '/es/config.php', 'es'); |
99
|
|
|
return $xTranslator; |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
// Config Manager |
103
|
|
|
$this->set(ConfigEventManager::class, function($c) { |
104
|
|
|
return new ConfigEventManager($c->g(Container::class)); |
105
|
|
|
}); |
106
|
|
|
$this->set(ConfigManager::class, function($c) { |
107
|
|
|
$xEventManager = $c->g(ConfigEventManager::class); |
108
|
|
|
$xConfigManager = new ConfigManager($c->g(ConfigReader::class), $xEventManager, $c->g(Translator::class)); |
109
|
|
|
$xConfigManager->setOptions($this->aConfig); |
110
|
|
|
// It's important to call this after the $xConfigManager->setOptions(), |
111
|
|
|
// because we don't want to trigger the events since the listeners cannot yet be instantiated. |
112
|
|
|
$xEventManager->addListener(Translator::class); |
113
|
|
|
$xEventManager->addListener(DialogLibraryManager::class); |
114
|
|
|
return $xConfigManager; |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
// Jaxon App |
118
|
|
|
$this->set(AppInterface::class, function($c) { |
119
|
|
|
return new App($c->g(Container::class)); |
120
|
|
|
}); |
121
|
|
|
// Jaxon App bootstrap |
122
|
|
|
$this->set(Bootstrap::class, function($c) { |
123
|
|
|
return new Bootstrap($c->g(ConfigManager::class), $c->g(PackageManager::class), |
124
|
|
|
$c->g(CallbackManager::class), $c->g(ViewRenderer::class)); |
125
|
|
|
}); |
126
|
|
|
// The javascript library version |
127
|
|
|
$this->set($this->sJsLibVersion, function($c) { |
128
|
|
|
$xRequest = $c->getRequest(); |
129
|
|
|
$aParams = $xRequest->getMethod() === 'POST' ? |
|
|
|
|
130
|
|
|
$xRequest->getParsedBody() : $xRequest->getQueryParams(); |
131
|
|
|
return $aParams['jxnv'] ?? '3.3.0'; |
132
|
|
|
}); |
133
|
|
|
} |
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the App instance |
137
|
|
|
* |
138
|
|
|
* @return AppInterface |
139
|
|
|
*/ |
140
|
|
|
public function getApp(): AppInterface |
141
|
|
|
{ |
142
|
|
|
return $this->g(AppInterface::class); |
|
|
|
|
143
|
|
|
} |
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the App bootstrap |
147
|
|
|
* |
148
|
|
|
* @return Bootstrap |
149
|
|
|
*/ |
150
|
|
|
public function getBootstrap(): Bootstrap |
151
|
|
|
{ |
152
|
|
|
return $this->g(Bootstrap::class); |
153
|
|
|
} |
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the javascript library version |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getJsLibVersion(): string |
161
|
|
|
{ |
162
|
|
|
return $this->g($this->sJsLibVersion); |
163
|
|
|
} |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|