1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jaxon\Di\Traits; |
4
|
|
|
|
5
|
|
|
use Jaxon\Jaxon; |
6
|
|
|
use Jaxon\App\Config\ConfigManager; |
7
|
|
|
use Jaxon\App\I18n\Translator; |
8
|
|
|
use Jaxon\App\View\ViewRenderer; |
9
|
|
|
use Jaxon\Di\Container; |
10
|
|
|
use Jaxon\Exception\SetupException; |
11
|
|
|
use Jaxon\Script\Factory\CallFactory; |
12
|
|
|
use Jaxon\Plugin\Code\AssetManager; |
13
|
|
|
use Jaxon\Plugin\Code\CodeGenerator; |
14
|
|
|
use Jaxon\Plugin\Code\MinifierInterface; |
15
|
|
|
use Jaxon\Plugin\Manager\PackageManager; |
16
|
|
|
use Jaxon\Plugin\Manager\PluginManager; |
17
|
|
|
use Jaxon\Plugin\Request\CallableClass\CallableRegistry; |
18
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagPlugin; |
19
|
|
|
use Jaxon\Plugin\Response\Dialog\DialogCommand; |
20
|
|
|
use Jaxon\Plugin\Response\Dialog\DialogPlugin; |
21
|
|
|
use Jaxon\Plugin\Response\Dialog\DialogManager; |
22
|
|
|
use Jaxon\Plugin\Response\Script\ScriptPlugin; |
23
|
|
|
use Jaxon\Request\Handler\CallbackManager; |
24
|
|
|
use Jaxon\Request\Handler\ParameterReader; |
25
|
|
|
use Jaxon\Utils\Config\Config; |
26
|
|
|
use Jaxon\Utils\File\FileMinifier; |
27
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
28
|
|
|
|
29
|
|
|
use function call_user_func; |
30
|
|
|
|
31
|
|
|
trait PluginTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Register the values into the container |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
private function registerPlugins() |
39
|
|
|
{ |
40
|
|
|
// Plugin manager |
41
|
|
|
$this->set(PluginManager::class, function($di) { |
|
|
|
|
42
|
|
|
$xPluginManager = new PluginManager($di->g(Container::class), |
43
|
|
|
$di->g(CodeGenerator::class), $di->g(Translator::class)); |
44
|
|
|
// Register the Jaxon request and response plugins |
45
|
|
|
$xPluginManager->registerPlugins(); |
46
|
|
|
return $xPluginManager; |
47
|
|
|
}); |
48
|
|
|
// Package manager |
49
|
|
|
$this->set(PackageManager::class, function($di) { |
50
|
|
|
return new PackageManager($di->g(Container::class), $di->g(Translator::class), |
51
|
|
|
$di->g(PluginManager::class), $di->g(ConfigManager::class), |
52
|
|
|
$di->g(CodeGenerator::class), $di->g(ViewRenderer::class), |
53
|
|
|
$di->g(CallbackManager::class), $di->g(CallableRegistry::class)); |
54
|
|
|
}); |
55
|
|
|
// Code Generation |
56
|
|
|
$this->set(MinifierInterface::class, function() { |
57
|
|
|
return new class extends FileMinifier implements MinifierInterface |
58
|
|
|
{}; |
59
|
|
|
}); |
60
|
|
|
$this->set(AssetManager::class, function($di) { |
61
|
|
|
return new AssetManager($di->g(ConfigManager::class), $di->g(ParameterReader::class), |
62
|
|
|
$di->g(MinifierInterface::class)); |
63
|
|
|
}); |
64
|
|
|
$this->set(CodeGenerator::class, function($di) { |
65
|
|
|
return new CodeGenerator(Jaxon::VERSION, $di->g(Container::class), |
66
|
|
|
$di->g(Translator::class), $di->g(TemplateEngine::class)); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
// Script response plugin |
70
|
|
|
$this->set(ScriptPlugin::class, function($di) { |
71
|
|
|
return new ScriptPlugin($di->g(CallFactory::class)); |
72
|
|
|
}); |
73
|
|
|
// DataBag response plugin |
74
|
|
|
$this->set(DataBagPlugin::class, function($di) { |
75
|
|
|
return new DataBagPlugin($di->g(Container::class)); |
76
|
|
|
}); |
77
|
|
|
// Dialog response plugin |
78
|
|
|
$this->set(DialogPlugin::class, function($di) { |
79
|
|
|
return new DialogPlugin($di->g(DialogCommand::class), $di->g(DialogManager::class)); |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the plugin manager |
85
|
|
|
* |
86
|
|
|
* @return PluginManager |
87
|
|
|
*/ |
88
|
|
|
public function getPluginManager(): PluginManager |
89
|
|
|
{ |
90
|
|
|
return $this->g(PluginManager::class); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the package manager |
95
|
|
|
* |
96
|
|
|
* @return PackageManager |
97
|
|
|
*/ |
98
|
|
|
public function getPackageManager(): PackageManager |
99
|
|
|
{ |
100
|
|
|
return $this->g(PackageManager::class); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the code generator |
105
|
|
|
* |
106
|
|
|
* @return CodeGenerator |
107
|
|
|
*/ |
108
|
|
|
public function getCodeGenerator(): CodeGenerator |
109
|
|
|
{ |
110
|
|
|
return $this->g(CodeGenerator::class); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the asset manager |
115
|
|
|
* |
116
|
|
|
* @return AssetManager |
117
|
|
|
*/ |
118
|
|
|
public function getAssetManager(): AssetManager |
119
|
|
|
{ |
120
|
|
|
return $this->g(AssetManager::class); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the jQuery plugin |
125
|
|
|
* |
126
|
|
|
* @return ScriptPlugin |
127
|
|
|
*/ |
128
|
|
|
public function getScriptPlugin(): ScriptPlugin |
129
|
|
|
{ |
130
|
|
|
return $this->g(ScriptPlugin::class); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the dialog plugin |
135
|
|
|
* |
136
|
|
|
* @return DialogPlugin |
137
|
|
|
*/ |
138
|
|
|
public function getDialogPlugin(): DialogPlugin |
139
|
|
|
{ |
140
|
|
|
return $this->g(DialogPlugin::class); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $sClassName The package class name |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
private function getPackageConfigKey(string $sClassName): string |
149
|
|
|
{ |
150
|
|
|
return $sClassName . '_PackageConfig'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Register a package |
155
|
|
|
* |
156
|
|
|
* @param string $sClassName The package class name |
157
|
|
|
* @param Config $xPkgConfig The user provided package options |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
* @throws SetupException |
161
|
|
|
*/ |
162
|
|
|
public function registerPackage(string $sClassName, Config $xPkgConfig) |
163
|
|
|
{ |
164
|
|
|
// Register the user class, but only if the user didn't already. |
165
|
|
|
if(!$this->h($sClassName)) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$this->set($sClassName, function() use($sClassName) { |
168
|
|
|
return $this->make($sClassName); |
|
|
|
|
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Save the package config in the container. |
173
|
|
|
$this->val($this->getPackageConfigKey($sClassName), $xPkgConfig); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// Initialize the package instance. |
176
|
|
|
$this->xLibContainer->extend($sClassName, function($xPackage) use($sClassName) { |
177
|
|
|
$xPkgConfig = $this->getPackageConfig($sClassName); |
178
|
|
|
$xViewRenderer = $this->g(ViewRenderer::class); |
179
|
|
|
$cSetter = function() use($xPkgConfig, $xViewRenderer) { |
180
|
|
|
// Set the protected attributes of the Package instance. |
181
|
|
|
$this->xPkgConfig = $xPkgConfig; |
|
|
|
|
182
|
|
|
$this->xRenderer = $xViewRenderer; |
|
|
|
|
183
|
|
|
$this->init(); |
|
|
|
|
184
|
|
|
}; |
185
|
|
|
// Can now access protected attributes |
186
|
|
|
call_user_func($cSetter->bindTo($xPackage, $xPackage)); |
187
|
|
|
return $xPackage; |
188
|
|
|
}); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the config of a package |
193
|
|
|
* |
194
|
|
|
* @param string $sClassName The package class name |
195
|
|
|
* |
196
|
|
|
* @return Config |
197
|
|
|
*/ |
198
|
|
|
public function getPackageConfig(string $sClassName): Config |
199
|
|
|
{ |
200
|
|
|
return $this->g($this->getPackageConfigKey($sClassName)); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|