@@ -31,75 +31,75 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | class CodeGenerator |
| 33 | 33 | { |
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * @var AssetManager |
| 36 | 36 | */ |
| 37 | - private $xAssetManager; |
|
| 37 | +private $xAssetManager; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * The classes that generate code |
| 41 | 41 | * |
| 42 | 42 | * @var array<string> |
| 43 | 43 | */ |
| 44 | - protected $aCodeGenerators = []; |
|
| 44 | +protected $aCodeGenerators = []; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * @var string |
| 48 | 48 | */ |
| 49 | - protected $sJsOptions; |
|
| 49 | +protected $sJsOptions; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * @var array |
| 53 | 53 | */ |
| 54 | - protected $aCss = []; |
|
| 54 | +protected $aCss = []; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 56 | +/** |
|
| 57 | 57 | * @var array |
| 58 | 58 | */ |
| 59 | - protected $aJs = []; |
|
| 59 | +protected $aJs = []; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 61 | +/** |
|
| 62 | 62 | * @var array |
| 63 | 63 | */ |
| 64 | - protected $aCodeJs = []; |
|
| 64 | +protected $aCodeJs = []; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 66 | +/** |
|
| 67 | 67 | * @var array |
| 68 | 68 | */ |
| 69 | - protected $aCodeJsBefore = []; |
|
| 69 | +protected $aCodeJsBefore = []; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 71 | +/** |
|
| 72 | 72 | * @var array |
| 73 | 73 | */ |
| 74 | - protected $aCodeJsAfter = []; |
|
| 74 | +protected $aCodeJsAfter = []; |
|
| 75 | 75 | |
| 76 | - /** |
|
| 76 | +/** |
|
| 77 | 77 | * @var array |
| 78 | 78 | */ |
| 79 | - protected $aCodeJsFiles = []; |
|
| 79 | +protected $aCodeJsFiles = []; |
|
| 80 | 80 | |
| 81 | - /** |
|
| 81 | +/** |
|
| 82 | 82 | * @var string |
| 83 | 83 | */ |
| 84 | - protected $bGenerated = false; |
|
| 84 | +protected $bGenerated = false; |
|
| 85 | 85 | |
| 86 | - /** |
|
| 86 | +/** |
|
| 87 | 87 | * The constructor |
| 88 | 88 | * |
| 89 | 89 | * @param string $sVersion |
| 90 | 90 | * @param Container $di |
| 91 | 91 | * @param TemplateEngine $xTemplateEngine |
| 92 | 92 | */ |
| 93 | - public function __construct(private string $sVersion, private Container $di, |
|
| 94 | - private TemplateEngine $xTemplateEngine) |
|
| 95 | - { |
|
| 96 | - // The Jaxon library config is on top. |
|
| 97 | - $this->addCodeGenerator(ConfigScriptGenerator::class, 0); |
|
| 98 | - // The ready script comes after. |
|
| 99 | - $this->addCodeGenerator(ReadyScriptGenerator::class, 200); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 93 | +public function __construct(private string $sVersion, private Container $di, |
|
| 94 | +private TemplateEngine $xTemplateEngine) |
|
| 95 | +{ |
|
| 96 | +// The Jaxon library config is on top. |
|
| 97 | +$this->addCodeGenerator(ConfigScriptGenerator::class, 0); |
|
| 98 | +// The ready script comes after. |
|
| 99 | +$this->addCodeGenerator(ReadyScriptGenerator::class, 200); |
|
| 100 | +} |
|
| 101 | + |
|
| 102 | +/** |
|
| 103 | 103 | * Add a code generator to the list |
| 104 | 104 | * |
| 105 | 105 | * @param string $sClassName The code generator class |
@@ -107,40 +107,40 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return void |
| 109 | 109 | */ |
| 110 | - public function addCodeGenerator(string $sClassName, int $nPriority): void |
|
| 111 | - { |
|
| 112 | - while(isset($this->aCodeGenerators[$nPriority])) |
|
| 113 | - { |
|
| 114 | - $nPriority++; |
|
| 115 | - } |
|
| 116 | - $this->aCodeGenerators[$nPriority] = $sClassName; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 110 | +public function addCodeGenerator(string $sClassName, int $nPriority): void |
|
| 111 | +{ |
|
| 112 | +while(isset($this->aCodeGenerators[$nPriority])) |
|
| 113 | +{ |
|
| 114 | +$nPriority++; |
|
| 115 | +} |
|
| 116 | +$this->aCodeGenerators[$nPriority] = $sClassName; |
|
| 117 | +} |
|
| 118 | + |
|
| 119 | +/** |
|
| 120 | 120 | * @param string $sClassName |
| 121 | 121 | * |
| 122 | 122 | * @return CodeGeneratorInterface |
| 123 | 123 | */ |
| 124 | - private function getCodeGenerator(string $sClassName): CodeGeneratorInterface |
|
| 125 | - { |
|
| 126 | - return $this->di->g($sClassName); |
|
| 127 | - } |
|
| 124 | +private function getCodeGenerator(string $sClassName): CodeGeneratorInterface |
|
| 125 | +{ |
|
| 126 | +return $this->di->g($sClassName); |
|
| 127 | +} |
|
| 128 | 128 | |
| 129 | - /** |
|
| 129 | +/** |
|
| 130 | 130 | * Generate a hash for all the javascript code generated by the library |
| 131 | 131 | * |
| 132 | 132 | * @return string |
| 133 | 133 | */ |
| 134 | - public function getHash(): string |
|
| 135 | - { |
|
| 136 | - $aHashes = array_map(function($sClassName) { |
|
| 137 | - return $this->getCodeGenerator($sClassName)->getHash(); |
|
| 138 | - }, $this->aCodeGenerators); |
|
| 139 | - $aHashes[] = $this->sVersion; |
|
| 140 | - return md5(implode('', $aHashes)); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 134 | +public function getHash(): string |
|
| 135 | +{ |
|
| 136 | +$aHashes = array_map(function($sClassName) { |
|
| 137 | +return $this->getCodeGenerator($sClassName)->getHash(); |
|
| 138 | +}, $this->aCodeGenerators); |
|
| 139 | +$aHashes[] = $this->sVersion; |
|
| 140 | +return md5(implode('', $aHashes)); |
|
| 141 | +} |
|
| 142 | + |
|
| 143 | +/** |
|
| 144 | 144 | * Render a template in the 'plugins' subdir |
| 145 | 145 | * |
| 146 | 146 | * @param string $sTemplate The template filename |
@@ -148,180 +148,180 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return string |
| 150 | 150 | */ |
| 151 | - private function render(string $sTemplate, array $aVars = []): string |
|
| 152 | - { |
|
| 153 | - $aVars['sJsOptions'] = $this->sJsOptions; |
|
| 154 | - return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
|
| 155 | - } |
|
| 151 | +private function render(string $sTemplate, array $aVars = []): string |
|
| 152 | +{ |
|
| 153 | +$aVars['sJsOptions'] = $this->sJsOptions; |
|
| 154 | +return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
|
| 155 | +} |
|
| 156 | 156 | |
| 157 | - /** |
|
| 157 | +/** |
|
| 158 | 158 | * Generate the Jaxon CSS and js codes for a given plugin |
| 159 | 159 | * |
| 160 | 160 | * @param CodeGeneratorInterface $xGenerator |
| 161 | 161 | * |
| 162 | 162 | * @return void |
| 163 | 163 | */ |
| 164 | - private function generatePluginCodes(CodeGeneratorInterface $xGenerator): void |
|
| 165 | - { |
|
| 166 | - if(!is_subclass_of($xGenerator, AbstractPlugin::class) || |
|
| 167 | - $this->xAssetManager->shallIncludeAssets($xGenerator)) |
|
| 168 | - { |
|
| 169 | - // HTML tags for CSS |
|
| 170 | - if(($sCss = trim($xGenerator->getCss(), " \n")) !== '') |
|
| 171 | - { |
|
| 172 | - $this->aCss[] = $sCss; |
|
| 173 | - } |
|
| 174 | - // HTML tags for js |
|
| 175 | - if(($sJs = trim($xGenerator->getJs(), " \n")) !== '') |
|
| 176 | - { |
|
| 177 | - $this->aJs[] = $sJs; |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Additional js codes |
|
| 182 | - if(($xJsCode = $xGenerator->getJsCode()) !== null) |
|
| 183 | - { |
|
| 184 | - if(($sJs = trim($xJsCode->sJs, " \n")) !== '') |
|
| 185 | - { |
|
| 186 | - $this->aCodeJs[] = $sJs; |
|
| 187 | - } |
|
| 188 | - if(($sJsBefore = trim($xJsCode->sJsBefore, " \n")) !== '') |
|
| 189 | - { |
|
| 190 | - $this->aCodeJsBefore[] = $sJsBefore; |
|
| 191 | - } |
|
| 192 | - if(($sJsAfter = trim($xJsCode->sJsAfter, " \n")) !== '') |
|
| 193 | - { |
|
| 194 | - $this->aCodeJsAfter[] = $sJsAfter; |
|
| 195 | - } |
|
| 196 | - $this->aCodeJsFiles = array_merge($this->aCodeJsFiles, $xJsCode->aFiles); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 164 | +private function generatePluginCodes(CodeGeneratorInterface $xGenerator): void |
|
| 165 | +{ |
|
| 166 | +if(!is_subclass_of($xGenerator, AbstractPlugin::class) || |
|
| 167 | +$this->xAssetManager->shallIncludeAssets($xGenerator)) |
|
| 168 | +{ |
|
| 169 | +// HTML tags for CSS |
|
| 170 | +if(($sCss = trim($xGenerator->getCss(), " \n")) !== '') |
|
| 171 | +{ |
|
| 172 | + $this->aCss[] = $sCss; |
|
| 173 | +} |
|
| 174 | +// HTML tags for js |
|
| 175 | +if(($sJs = trim($xGenerator->getJs(), " \n")) !== '') |
|
| 176 | +{ |
|
| 177 | + $this->aJs[] = $sJs; |
|
| 178 | +} |
|
| 179 | +} |
|
| 180 | + |
|
| 181 | +// Additional js codes |
|
| 182 | +if(($xJsCode = $xGenerator->getJsCode()) !== null) |
|
| 183 | +{ |
|
| 184 | +if(($sJs = trim($xJsCode->sJs, " \n")) !== '') |
|
| 185 | +{ |
|
| 186 | + $this->aCodeJs[] = $sJs; |
|
| 187 | +} |
|
| 188 | +if(($sJsBefore = trim($xJsCode->sJsBefore, " \n")) !== '') |
|
| 189 | +{ |
|
| 190 | + $this->aCodeJsBefore[] = $sJsBefore; |
|
| 191 | +} |
|
| 192 | +if(($sJsAfter = trim($xJsCode->sJsAfter, " \n")) !== '') |
|
| 193 | +{ |
|
| 194 | + $this->aCodeJsAfter[] = $sJsAfter; |
|
| 195 | +} |
|
| 196 | +$this->aCodeJsFiles = array_merge($this->aCodeJsFiles, $xJsCode->aFiles); |
|
| 197 | +} |
|
| 198 | +} |
|
| 199 | + |
|
| 200 | +/** |
|
| 201 | 201 | * Generate the Jaxon CSS ans js codes |
| 202 | 202 | * |
| 203 | 203 | * @return void |
| 204 | 204 | * @throws UriException |
| 205 | 205 | */ |
| 206 | - private function generateCodes(): void |
|
| 207 | - { |
|
| 208 | - if($this->bGenerated) |
|
| 209 | - { |
|
| 210 | - return; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $this->xAssetManager = $this->di->getAssetManager(); |
|
| 214 | - $this->sJsOptions = $this->xAssetManager->getJsOptions(); |
|
| 215 | - |
|
| 216 | - // Sort the code generators by ascending priority |
|
| 217 | - ksort($this->aCodeGenerators); |
|
| 218 | - |
|
| 219 | - foreach($this->aCodeGenerators as $sClassName) |
|
| 220 | - { |
|
| 221 | - $this->generatePluginCodes($this->getCodeGenerator($sClassName)); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // Load the Jaxon lib js files, after the other libs js files. |
|
| 225 | - $this->aJs[] = trim($this->render('includes.js', [ |
|
| 226 | - 'aUrls' => $this->xAssetManager->getJsLibFiles(), |
|
| 227 | - ])); |
|
| 228 | - |
|
| 229 | - // The codes are already generated. |
|
| 230 | - $this->bGenerated = true; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 206 | +private function generateCodes(): void |
|
| 207 | +{ |
|
| 208 | +if($this->bGenerated) |
|
| 209 | +{ |
|
| 210 | +return; |
|
| 211 | +} |
|
| 212 | + |
|
| 213 | +$this->xAssetManager = $this->di->getAssetManager(); |
|
| 214 | +$this->sJsOptions = $this->xAssetManager->getJsOptions(); |
|
| 215 | + |
|
| 216 | +// Sort the code generators by ascending priority |
|
| 217 | +ksort($this->aCodeGenerators); |
|
| 218 | + |
|
| 219 | +foreach($this->aCodeGenerators as $sClassName) |
|
| 220 | +{ |
|
| 221 | +$this->generatePluginCodes($this->getCodeGenerator($sClassName)); |
|
| 222 | +} |
|
| 223 | + |
|
| 224 | +// Load the Jaxon lib js files, after the other libs js files. |
|
| 225 | +$this->aJs[] = trim($this->render('includes.js', [ |
|
| 226 | +'aUrls' => $this->xAssetManager->getJsLibFiles(), |
|
| 227 | +])); |
|
| 228 | + |
|
| 229 | +// The codes are already generated. |
|
| 230 | +$this->bGenerated = true; |
|
| 231 | +} |
|
| 232 | + |
|
| 233 | +/** |
|
| 234 | 234 | * Get the HTML tags to include Jaxon CSS code and files into the page |
| 235 | 235 | * |
| 236 | 236 | * @return string |
| 237 | 237 | * @throws UriException |
| 238 | 238 | */ |
| 239 | - public function getCss(): string |
|
| 240 | - { |
|
| 241 | - $this->generateCodes(); |
|
| 242 | - return implode("\n\n", $this->aCss); |
|
| 243 | - } |
|
| 239 | +public function getCss(): string |
|
| 240 | +{ |
|
| 241 | +$this->generateCodes(); |
|
| 242 | +return implode("\n\n", $this->aCss); |
|
| 243 | +} |
|
| 244 | 244 | |
| 245 | - /** |
|
| 245 | +/** |
|
| 246 | 246 | * Get the HTML tags to include Jaxon javascript files into the page |
| 247 | 247 | * |
| 248 | 248 | * @return string |
| 249 | 249 | * @throws UriException |
| 250 | 250 | */ |
| 251 | - public function getJs(): string |
|
| 252 | - { |
|
| 253 | - $this->generateCodes(); |
|
| 254 | - return implode("\n\n", $this->aJs); |
|
| 255 | - } |
|
| 251 | +public function getJs(): string |
|
| 252 | +{ |
|
| 253 | +$this->generateCodes(); |
|
| 254 | +return implode("\n\n", $this->aJs); |
|
| 255 | +} |
|
| 256 | 256 | |
| 257 | - /** |
|
| 257 | +/** |
|
| 258 | 258 | * Get the Javascript code |
| 259 | 259 | * |
| 260 | 260 | * @return string |
| 261 | 261 | */ |
| 262 | - public function getJsScript(): string |
|
| 263 | - { |
|
| 264 | - foreach($this->aCodeGenerators as $sClassName) |
|
| 265 | - { |
|
| 266 | - $xGenerator = $this->getCodeGenerator($sClassName); |
|
| 267 | - // Javascript code |
|
| 268 | - if(($sJsScript = trim($xGenerator->getScript(), " \n")) !== '') |
|
| 269 | - { |
|
| 270 | - $aJsScript[] = $sJsScript; |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - return implode("\n\n", $aJsScript); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 262 | +public function getJsScript(): string |
|
| 263 | +{ |
|
| 264 | +foreach($this->aCodeGenerators as $sClassName) |
|
| 265 | +{ |
|
| 266 | +$xGenerator = $this->getCodeGenerator($sClassName); |
|
| 267 | +// Javascript code |
|
| 268 | +if(($sJsScript = trim($xGenerator->getScript(), " \n")) !== '') |
|
| 269 | +{ |
|
| 270 | + $aJsScript[] = $sJsScript; |
|
| 271 | +} |
|
| 272 | +} |
|
| 273 | +return implode("\n\n", $aJsScript); |
|
| 274 | +} |
|
| 275 | + |
|
| 276 | +/** |
|
| 277 | 277 | * @param bool $bIncludeJs Also get the JS files |
| 278 | 278 | * @param bool $bIncludeCss Also get the CSS files |
| 279 | 279 | * |
| 280 | 280 | * @return array<string> |
| 281 | 281 | */ |
| 282 | - private function renderCodes(bool $bIncludeJs, bool $bIncludeCss): array |
|
| 283 | - { |
|
| 284 | - $aCodes = []; |
|
| 285 | - if($bIncludeCss) |
|
| 286 | - { |
|
| 287 | - $aCodes[] = $this->getCss(); |
|
| 288 | - } |
|
| 289 | - if($bIncludeJs) |
|
| 290 | - { |
|
| 291 | - $aCodes[] = $this->getJs(); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - $sUrl = !$this->xAssetManager->shallCreateJsFiles() ? '' : |
|
| 295 | - $this->xAssetManager->createJsFiles($this); |
|
| 296 | - // Wrap the js code into the corresponding HTML tag. |
|
| 297 | - $aCodes[] = $sUrl !== '' ? |
|
| 298 | - $this->render('include.js', ['sUrl' => $sUrl]) : |
|
| 299 | - $this->render('wrapper.js', ['sScript' => $this->getJsScript()]); |
|
| 300 | - |
|
| 301 | - // Wrap the js codes into HTML tags. |
|
| 302 | - if(count($this->aCodeJsBefore) > 0) |
|
| 303 | - { |
|
| 304 | - $sScript = implode("\n\n", $this->aCodeJsBefore); |
|
| 305 | - $aCodes[] = $this->render('wrapper.js', ['sScript' => $sScript]); |
|
| 306 | - } |
|
| 307 | - if(count($this->aCodeJs) > 0) |
|
| 308 | - { |
|
| 309 | - $sScript = implode("\n\n", $this->aCodeJs); |
|
| 310 | - $aCodes[] = $this->render('wrapper.js', ['sScript' => $sScript]); |
|
| 311 | - } |
|
| 312 | - if(count($this->aCodeJsFiles) > 0) |
|
| 313 | - { |
|
| 314 | - $aCodes[] = $this->render('includes.js', ['aUrls' => $this->aCodeJsFiles]); |
|
| 315 | - } |
|
| 316 | - if(count($this->aCodeJsAfter) > 0) |
|
| 317 | - { |
|
| 318 | - $sScript = implode("\n\n", $this->aCodeJsAfter); |
|
| 319 | - $aCodes[] = $this->render('wrapper.js', ['sScript' => $sScript]); |
|
| 320 | - } |
|
| 321 | - return $aCodes; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 282 | +private function renderCodes(bool $bIncludeJs, bool $bIncludeCss): array |
|
| 283 | +{ |
|
| 284 | +$aCodes = []; |
|
| 285 | +if($bIncludeCss) |
|
| 286 | +{ |
|
| 287 | +$aCodes[] = $this->getCss(); |
|
| 288 | +} |
|
| 289 | +if($bIncludeJs) |
|
| 290 | +{ |
|
| 291 | +$aCodes[] = $this->getJs(); |
|
| 292 | +} |
|
| 293 | + |
|
| 294 | +$sUrl = !$this->xAssetManager->shallCreateJsFiles() ? '' : |
|
| 295 | +$this->xAssetManager->createJsFiles($this); |
|
| 296 | +// Wrap the js code into the corresponding HTML tag. |
|
| 297 | +$aCodes[] = $sUrl !== '' ? |
|
| 298 | +$this->render('include.js', ['sUrl' => $sUrl]) : |
|
| 299 | +$this->render('wrapper.js', ['sScript' => $this->getJsScript()]); |
|
| 300 | + |
|
| 301 | +// Wrap the js codes into HTML tags. |
|
| 302 | +if(count($this->aCodeJsBefore) > 0) |
|
| 303 | +{ |
|
| 304 | +$sScript = implode("\n\n", $this->aCodeJsBefore); |
|
| 305 | +$aCodes[] = $this->render('wrapper.js', ['sScript' => $sScript]); |
|
| 306 | +} |
|
| 307 | +if(count($this->aCodeJs) > 0) |
|
| 308 | +{ |
|
| 309 | +$sScript = implode("\n\n", $this->aCodeJs); |
|
| 310 | +$aCodes[] = $this->render('wrapper.js', ['sScript' => $sScript]); |
|
| 311 | +} |
|
| 312 | +if(count($this->aCodeJsFiles) > 0) |
|
| 313 | +{ |
|
| 314 | +$aCodes[] = $this->render('includes.js', ['aUrls' => $this->aCodeJsFiles]); |
|
| 315 | +} |
|
| 316 | +if(count($this->aCodeJsAfter) > 0) |
|
| 317 | +{ |
|
| 318 | +$sScript = implode("\n\n", $this->aCodeJsAfter); |
|
| 319 | +$aCodes[] = $this->render('wrapper.js', ['sScript' => $sScript]); |
|
| 320 | +} |
|
| 321 | +return $aCodes; |
|
| 322 | +} |
|
| 323 | + |
|
| 324 | +/** |
|
| 325 | 325 | * Get the javascript code to be sent to the browser |
| 326 | 326 | * |
| 327 | 327 | * @param bool $bIncludeJs Also get the JS files |
@@ -330,10 +330,10 @@ discard block |
||
| 330 | 330 | * @return string |
| 331 | 331 | * @throws UriException |
| 332 | 332 | */ |
| 333 | - public function getScript(bool $bIncludeJs, bool $bIncludeCss): string |
|
| 334 | - { |
|
| 335 | - $this->generateCodes(); |
|
| 336 | - $aCodes = $this->renderCodes($bIncludeJs, $bIncludeCss); |
|
| 337 | - return implode("\n\n", $aCodes); |
|
| 338 | - } |
|
| 333 | +public function getScript(bool $bIncludeJs, bool $bIncludeCss): string |
|
| 334 | +{ |
|
| 335 | +$this->generateCodes(); |
|
| 336 | +$aCodes = $this->renderCodes($bIncludeJs, $bIncludeCss); |
|
| 337 | +return implode("\n\n", $aCodes); |
|
| 338 | +} |
|
| 339 | 339 | } |
@@ -16,31 +16,31 @@ |
||
| 16 | 16 | |
| 17 | 17 | class JsCode |
| 18 | 18 | { |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * The main javascript code |
| 21 | 21 | * |
| 22 | 22 | * @var string |
| 23 | 23 | */ |
| 24 | - public $sJs = ''; |
|
| 24 | +public $sJs = ''; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * The js files |
| 28 | 28 | * |
| 29 | 29 | * @var array |
| 30 | 30 | */ |
| 31 | - public $aFiles = []; |
|
| 31 | +public $aFiles = []; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * The javascript code to be inserted inline before the main code |
| 35 | 35 | * |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - public $sJsBefore = ''; |
|
| 38 | +public $sJsBefore = ''; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * The javascript code to be inserted inline after the main code |
| 42 | 42 | * |
| 43 | 43 | * @var string |
| 44 | 44 | */ |
| 45 | - public $sJsAfter = ''; |
|
| 45 | +public $sJsAfter = ''; |
|
| 46 | 46 | } |
@@ -18,11 +18,11 @@ |
||
| 18 | 18 | |
| 19 | 19 | class ReadyScriptGenerator extends AbstractCodeGenerator |
| 20 | 20 | { |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * @inheritDoc |
| 23 | 23 | */ |
| 24 | - public function getScript(): string |
|
| 25 | - { |
|
| 26 | - return "jaxon.dom.ready(() => jaxon.processCustomAttrs());"; |
|
| 27 | - } |
|
| 24 | +public function getScript(): string |
|
| 25 | +{ |
|
| 26 | +return "jaxon.dom.ready(() => jaxon.processCustomAttrs());"; |
|
| 27 | +} |
|
| 28 | 28 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | interface MinifierInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 7 | +/** |
|
| 8 | 8 | * Minify javascript code |
| 9 | 9 | * |
| 10 | 10 | * @param string $sJsFile The javascript file to be minified |
@@ -12,5 +12,5 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @return bool |
| 14 | 14 | */ |
| 15 | - public function minify(string $sJsFile, string $sMinFile): bool; |
|
| 15 | +public function minify(string $sJsFile, string $sMinFile): bool; |
|
| 16 | 16 | } |
@@ -43,60 +43,60 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | class PaginatorPlugin extends AbstractResponsePlugin |
| 45 | 45 | { |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * @const The plugin name |
| 48 | 48 | */ |
| 49 | - public const NAME = 'pg'; |
|
| 49 | +public const NAME = 'pg'; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * The constructor. |
| 53 | 53 | * |
| 54 | 54 | * @param RendererInterface $xRenderer |
| 55 | 55 | */ |
| 56 | - public function __construct(protected RendererInterface $xRenderer) |
|
| 57 | - {} |
|
| 56 | +public function __construct(protected RendererInterface $xRenderer) |
|
| 57 | +{} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @inheritDoc |
| 61 | 61 | */ |
| 62 | - public function getName(): string |
|
| 63 | - { |
|
| 64 | - return self::NAME; |
|
| 65 | - } |
|
| 62 | +public function getName(): string |
|
| 63 | +{ |
|
| 64 | +return self::NAME; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * @inheritDoc |
| 69 | 69 | */ |
| 70 | - public function getHash(): string |
|
| 71 | - { |
|
| 72 | - return '5.0.0'; // Use the version number as hash |
|
| 73 | - } |
|
| 70 | +public function getHash(): string |
|
| 71 | +{ |
|
| 72 | +return '5.0.0'; // Use the version number as hash |
|
| 73 | +} |
|
| 74 | 74 | |
| 75 | - /** |
|
| 75 | +/** |
|
| 76 | 76 | * @inheritDoc |
| 77 | 77 | */ |
| 78 | - public function getCss(): string |
|
| 79 | - { |
|
| 80 | - return ' |
|
| 78 | +public function getCss(): string |
|
| 79 | +{ |
|
| 80 | +return ' |
|
| 81 | 81 | <style> |
| 82 | 82 | .pagination li a { |
| 83 | 83 | cursor: pointer; |
| 84 | 84 | } |
| 85 | 85 | </style> |
| 86 | 86 | '; |
| 87 | - } |
|
| 87 | +} |
|
| 88 | 88 | |
| 89 | - /** |
|
| 89 | +/** |
|
| 90 | 90 | * Get the view renderer |
| 91 | 91 | * |
| 92 | 92 | * @return RendererInterface |
| 93 | 93 | */ |
| 94 | - public function renderer(): RendererInterface |
|
| 95 | - { |
|
| 96 | - return $this->xRenderer; |
|
| 97 | - } |
|
| 94 | +public function renderer(): RendererInterface |
|
| 95 | +{ |
|
| 96 | +return $this->xRenderer; |
|
| 97 | +} |
|
| 98 | 98 | |
| 99 | - /** |
|
| 99 | +/** |
|
| 100 | 100 | * Create a paginator |
| 101 | 101 | * |
| 102 | 102 | * @param int $nPageNumber The current page number |
@@ -105,8 +105,8 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @return Paginator |
| 107 | 107 | */ |
| 108 | - public function paginator(int $nPageNumber, int $nItemsPerPage, int $nTotalItems): Paginator |
|
| 109 | - { |
|
| 110 | - return new Paginator($this, $nPageNumber, $nItemsPerPage, $nTotalItems); |
|
| 111 | - } |
|
| 108 | +public function paginator(int $nPageNumber, int $nItemsPerPage, int $nTotalItems): Paginator |
|
| 109 | +{ |
|
| 110 | +return new Paginator($this, $nPageNumber, $nItemsPerPage, $nTotalItems); |
|
| 111 | +} |
|
| 112 | 112 | } |
@@ -26,56 +26,56 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | class ScriptPlugin extends AbstractResponsePlugin |
| 28 | 28 | { |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * @const The plugin name |
| 31 | 31 | */ |
| 32 | - public const NAME = 'script'; |
|
| 32 | +public const NAME = 'script'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * The class constructor |
| 36 | 36 | * |
| 37 | 37 | * @param CallFactory $xFactory |
| 38 | 38 | */ |
| 39 | - public function __construct(private CallFactory $xFactory) |
|
| 40 | - {} |
|
| 39 | +public function __construct(private CallFactory $xFactory) |
|
| 40 | +{} |
|
| 41 | 41 | |
| 42 | - /** |
|
| 42 | +/** |
|
| 43 | 43 | * @return Closure |
| 44 | 44 | */ |
| 45 | - private function getCallback(): Closure |
|
| 46 | - { |
|
| 47 | - // The closure needs to capture the response object the script plugin is called with. |
|
| 48 | - $xResponse = $this->response(); |
|
| 49 | - return function(JsExpr $xJsExpr) use($xResponse) { |
|
| 50 | - // Add the newly created expression to the response |
|
| 51 | - $aOptions = [ |
|
| 52 | - 'expr' => $xJsExpr, |
|
| 53 | - 'context' => is_a($xResponse, NodeResponse::class) ? |
|
| 54 | - ['component' => true] : [], |
|
| 55 | - ]; |
|
| 56 | - $xResponse->addCommand('script.exec.expr', $aOptions) |
|
| 57 | - ->setOption('plugin', $this->getName()); |
|
| 58 | - }; |
|
| 59 | - } |
|
| 45 | +private function getCallback(): Closure |
|
| 46 | +{ |
|
| 47 | +// The closure needs to capture the response object the script plugin is called with. |
|
| 48 | +$xResponse = $this->response(); |
|
| 49 | +return function(JsExpr $xJsExpr) use($xResponse) { |
|
| 50 | +// Add the newly created expression to the response |
|
| 51 | +$aOptions = [ |
|
| 52 | + 'expr' => $xJsExpr, |
|
| 53 | + 'context' => is_a($xResponse, NodeResponse::class) ? |
|
| 54 | + ['component' => true] : [], |
|
| 55 | +]; |
|
| 56 | +$xResponse->addCommand('script.exec.expr', $aOptions) |
|
| 57 | + ->setOption('plugin', $this->getName()); |
|
| 58 | +}; |
|
| 59 | +} |
|
| 60 | 60 | |
| 61 | - /** |
|
| 61 | +/** |
|
| 62 | 62 | * @inheritDoc |
| 63 | 63 | */ |
| 64 | - public function getName(): string |
|
| 65 | - { |
|
| 66 | - return self::NAME; |
|
| 67 | - } |
|
| 64 | +public function getName(): string |
|
| 65 | +{ |
|
| 66 | +return self::NAME; |
|
| 67 | +} |
|
| 68 | 68 | |
| 69 | - /** |
|
| 69 | +/** |
|
| 70 | 70 | * @inheritDoc |
| 71 | 71 | */ |
| 72 | - public function getHash(): string |
|
| 73 | - { |
|
| 74 | - // Use the version number as hash |
|
| 75 | - return '5.0.0'; |
|
| 76 | - } |
|
| 72 | +public function getHash(): string |
|
| 73 | +{ |
|
| 74 | +// Use the version number as hash |
|
| 75 | +return '5.0.0'; |
|
| 76 | +} |
|
| 77 | 77 | |
| 78 | - /** |
|
| 78 | +/** |
|
| 79 | 79 | * Create a JQuery selector expression, and link it to the current response. |
| 80 | 80 | * |
| 81 | 81 | * @param string $sPath The jQuery selector path |
@@ -83,32 +83,32 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return JqSelectorCall |
| 85 | 85 | */ |
| 86 | - public function jq(string $sPath = '', $xContext = null): JqSelectorCall |
|
| 87 | - { |
|
| 88 | - return $this->xFactory->jq($sPath, $xContext, $this->getCallback()); |
|
| 89 | - } |
|
| 86 | +public function jq(string $sPath = '', $xContext = null): JqSelectorCall |
|
| 87 | +{ |
|
| 88 | +return $this->xFactory->jq($sPath, $xContext, $this->getCallback()); |
|
| 89 | +} |
|
| 90 | 90 | |
| 91 | - /** |
|
| 91 | +/** |
|
| 92 | 92 | * Create a Javascript object expression, and link it to the current response. |
| 93 | 93 | * |
| 94 | 94 | * @param string $sObject |
| 95 | 95 | * |
| 96 | 96 | * @return JsObjectCall |
| 97 | 97 | */ |
| 98 | - public function jo(string $sObject = ''): JsObjectCall |
|
| 99 | - { |
|
| 100 | - return $this->xFactory->jo($sObject, $this->getCallback()); |
|
| 101 | - } |
|
| 98 | +public function jo(string $sObject = ''): JsObjectCall |
|
| 99 | +{ |
|
| 100 | +return $this->xFactory->jo($sObject, $this->getCallback()); |
|
| 101 | +} |
|
| 102 | 102 | |
| 103 | - /** |
|
| 103 | +/** |
|
| 104 | 104 | * Create a Javascript element selector expression, and link it to the current response. |
| 105 | 105 | * |
| 106 | 106 | * @param string $sElementId |
| 107 | 107 | * |
| 108 | 108 | * @return JsSelectorCall |
| 109 | 109 | */ |
| 110 | - public function je(string $sElementId = ''): JsSelectorCall |
|
| 111 | - { |
|
| 112 | - return $this->xFactory->je($sElementId, $this->getCallback()); |
|
| 113 | - } |
|
| 110 | +public function je(string $sElementId = ''): JsSelectorCall |
|
| 111 | +{ |
|
| 112 | +return $this->xFactory->je($sElementId, $this->getCallback()); |
|
| 113 | +} |
|
| 114 | 114 | } |
@@ -24,107 +24,107 @@ |
||
| 24 | 24 | |
| 25 | 25 | class DialogPlugin implements PluginInterface, ResponsePluginInterface, ModalInterface, AlertInterface |
| 26 | 26 | { |
| 27 | - use ResponsePluginTrait; |
|
| 27 | +use ResponsePluginTrait; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * @const The plugin name |
| 31 | 31 | */ |
| 32 | - public const NAME = 'dialog'; |
|
| 32 | +public const NAME = 'dialog'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * The constructor |
| 36 | 36 | * |
| 37 | 37 | * @param DialogCommand $xDialogCommand |
| 38 | 38 | */ |
| 39 | - public function __construct(private DialogCommand $xDialogCommand) |
|
| 40 | - {} |
|
| 39 | +public function __construct(private DialogCommand $xDialogCommand) |
|
| 40 | +{} |
|
| 41 | 41 | |
| 42 | - /** |
|
| 42 | +/** |
|
| 43 | 43 | * @inheritDoc |
| 44 | 44 | */ |
| 45 | - public function getName(): string |
|
| 46 | - { |
|
| 47 | - return self::NAME; |
|
| 48 | - } |
|
| 45 | +public function getName(): string |
|
| 46 | +{ |
|
| 47 | +return self::NAME; |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Initialize the plugin |
| 52 | 52 | * |
| 53 | 53 | * @return void |
| 54 | 54 | */ |
| 55 | - protected function init(): void |
|
| 56 | - {} |
|
| 55 | +protected function init(): void |
|
| 56 | +{} |
|
| 57 | 57 | |
| 58 | - /** |
|
| 58 | +/** |
|
| 59 | 59 | * Set the library to use for the next call. |
| 60 | 60 | * |
| 61 | 61 | * @param string $sLibrary The name of the library |
| 62 | 62 | * |
| 63 | 63 | * @return DialogPlugin |
| 64 | 64 | */ |
| 65 | - public function with(string $sLibrary): DialogPlugin |
|
| 66 | - { |
|
| 67 | - $this->xDialogCommand->library($sLibrary); |
|
| 68 | - return $this; |
|
| 69 | - } |
|
| 65 | +public function with(string $sLibrary): DialogPlugin |
|
| 66 | +{ |
|
| 67 | +$this->xDialogCommand->library($sLibrary); |
|
| 68 | +return $this; |
|
| 69 | +} |
|
| 70 | 70 | |
| 71 | - /** |
|
| 71 | +/** |
|
| 72 | 72 | * @inheritDoc |
| 73 | 73 | */ |
| 74 | - public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []): void |
|
| 75 | - { |
|
| 76 | - // Show the modal dialog |
|
| 77 | - $this->addCommand('dialog.modal.show', |
|
| 78 | - $this->xDialogCommand->show($sTitle, $sContent, $aButtons, $aOptions)); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 74 | +public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []): void |
|
| 75 | +{ |
|
| 76 | +// Show the modal dialog |
|
| 77 | +$this->addCommand('dialog.modal.show', |
|
| 78 | +$this->xDialogCommand->show($sTitle, $sContent, $aButtons, $aOptions)); |
|
| 79 | +} |
|
| 80 | + |
|
| 81 | +/** |
|
| 82 | 82 | * @inheritDoc |
| 83 | 83 | */ |
| 84 | - public function hide(): void |
|
| 85 | - { |
|
| 86 | - // Hide the modal dialog |
|
| 87 | - $this->addCommand('dialog.modal.hide', $this->xDialogCommand->hide()); |
|
| 88 | - } |
|
| 84 | +public function hide(): void |
|
| 85 | +{ |
|
| 86 | +// Hide the modal dialog |
|
| 87 | +$this->addCommand('dialog.modal.hide', $this->xDialogCommand->hide()); |
|
| 88 | +} |
|
| 89 | 89 | |
| 90 | - /** |
|
| 90 | +/** |
|
| 91 | 91 | * @inheritDoc |
| 92 | 92 | */ |
| 93 | - public function title(string $sTitle): AlertInterface |
|
| 94 | - { |
|
| 95 | - $this->xDialogCommand->title($sTitle); |
|
| 96 | - return $this; |
|
| 97 | - } |
|
| 93 | +public function title(string $sTitle): AlertInterface |
|
| 94 | +{ |
|
| 95 | +$this->xDialogCommand->title($sTitle); |
|
| 96 | +return $this; |
|
| 97 | +} |
|
| 98 | 98 | |
| 99 | - /** |
|
| 99 | +/** |
|
| 100 | 100 | * @inheritDoc |
| 101 | 101 | */ |
| 102 | - public function success(string $sMessage, ...$aArgs): void |
|
| 103 | - { |
|
| 104 | - $this->addCommand('dialog.alert.show', $this->xDialogCommand->success($sMessage, $aArgs)); |
|
| 105 | - } |
|
| 102 | +public function success(string $sMessage, ...$aArgs): void |
|
| 103 | +{ |
|
| 104 | +$this->addCommand('dialog.alert.show', $this->xDialogCommand->success($sMessage, $aArgs)); |
|
| 105 | +} |
|
| 106 | 106 | |
| 107 | - /** |
|
| 107 | +/** |
|
| 108 | 108 | * @inheritDoc |
| 109 | 109 | */ |
| 110 | - public function info(string $sMessage, ...$aArgs): void |
|
| 111 | - { |
|
| 112 | - $this->addCommand('dialog.alert.show', $this->xDialogCommand->info($sMessage, $aArgs)); |
|
| 113 | - } |
|
| 110 | +public function info(string $sMessage, ...$aArgs): void |
|
| 111 | +{ |
|
| 112 | +$this->addCommand('dialog.alert.show', $this->xDialogCommand->info($sMessage, $aArgs)); |
|
| 113 | +} |
|
| 114 | 114 | |
| 115 | - /** |
|
| 115 | +/** |
|
| 116 | 116 | * @inheritDoc |
| 117 | 117 | */ |
| 118 | - public function warning(string $sMessage, ...$aArgs): void |
|
| 119 | - { |
|
| 120 | - $this->addCommand('dialog.alert.show', $this->xDialogCommand->warning($sMessage, $aArgs)); |
|
| 121 | - } |
|
| 118 | +public function warning(string $sMessage, ...$aArgs): void |
|
| 119 | +{ |
|
| 120 | +$this->addCommand('dialog.alert.show', $this->xDialogCommand->warning($sMessage, $aArgs)); |
|
| 121 | +} |
|
| 122 | 122 | |
| 123 | - /** |
|
| 123 | +/** |
|
| 124 | 124 | * @inheritDoc |
| 125 | 125 | */ |
| 126 | - public function error(string $sMessage, ...$aArgs): void |
|
| 127 | - { |
|
| 128 | - $this->addCommand('dialog.alert.show', $this->xDialogCommand->error($sMessage, $aArgs)); |
|
| 129 | - } |
|
| 126 | +public function error(string $sMessage, ...$aArgs): void |
|
| 127 | +{ |
|
| 128 | +$this->addCommand('dialog.alert.show', $this->xDialogCommand->error($sMessage, $aArgs)); |
|
| 129 | +} |
|
| 130 | 130 | } |
@@ -23,56 +23,56 @@ |
||
| 23 | 23 | |
| 24 | 24 | class PsrPlugin extends AbstractResponsePlugin |
| 25 | 25 | { |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * @const The plugin name |
| 28 | 28 | */ |
| 29 | - public const NAME = 'psr'; |
|
| 29 | +public const NAME = 'psr'; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * The class constructor |
| 33 | 33 | * |
| 34 | 34 | * @param Psr17Factory $xPsr17Factory |
| 35 | 35 | * @param RequestInterface $xRequest |
| 36 | 36 | */ |
| 37 | - public function __construct(private Psr17Factory $xPsr17Factory, |
|
| 38 | - private RequestInterface $xRequest) |
|
| 39 | - {} |
|
| 37 | +public function __construct(private Psr17Factory $xPsr17Factory, |
|
| 38 | +private RequestInterface $xRequest) |
|
| 39 | +{} |
|
| 40 | 40 | |
| 41 | - /** |
|
| 41 | +/** |
|
| 42 | 42 | * @inheritDoc |
| 43 | 43 | */ |
| 44 | - public function getName(): string |
|
| 45 | - { |
|
| 46 | - return self::NAME; |
|
| 47 | - } |
|
| 44 | +public function getName(): string |
|
| 45 | +{ |
|
| 46 | +return self::NAME; |
|
| 47 | +} |
|
| 48 | 48 | |
| 49 | - /** |
|
| 49 | +/** |
|
| 50 | 50 | * @inheritDoc |
| 51 | 51 | */ |
| 52 | - public function getHash(): string |
|
| 53 | - { |
|
| 54 | - // Use the version number as hash |
|
| 55 | - return '5.0.0'; |
|
| 56 | - } |
|
| 52 | +public function getHash(): string |
|
| 53 | +{ |
|
| 54 | +// Use the version number as hash |
|
| 55 | +return '5.0.0'; |
|
| 56 | +} |
|
| 57 | 57 | |
| 58 | - /** |
|
| 58 | +/** |
|
| 59 | 59 | * Convert an ajax response to a PSR7 response object |
| 60 | 60 | * |
| 61 | 61 | * @return ResponseInterface |
| 62 | 62 | */ |
| 63 | - public function ajaxResponse(): ResponseInterface |
|
| 64 | - { |
|
| 65 | - $xPsrResponse = $this->xPsr17Factory->createResponse(200); |
|
| 66 | - if($this->xRequest->getMethod() === 'GET') |
|
| 67 | - { |
|
| 68 | - $xPsrResponse = $xPsrResponse |
|
| 69 | - ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT') |
|
| 70 | - ->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT') |
|
| 71 | - ->withHeader('Cache-Control', 'no-cache, must-revalidate') |
|
| 72 | - ->withHeader('Pragma', 'no-cache'); |
|
| 73 | - } |
|
| 74 | - return $xPsrResponse |
|
| 75 | - ->withHeader('content-type', $this->response()->getContentType()) |
|
| 76 | - ->withBody(Stream::create($this->response()->getOutput())); |
|
| 77 | - } |
|
| 63 | +public function ajaxResponse(): ResponseInterface |
|
| 64 | +{ |
|
| 65 | +$xPsrResponse = $this->xPsr17Factory->createResponse(200); |
|
| 66 | +if($this->xRequest->getMethod() === 'GET') |
|
| 67 | +{ |
|
| 68 | +$xPsrResponse = $xPsrResponse |
|
| 69 | + ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT') |
|
| 70 | + ->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT') |
|
| 71 | + ->withHeader('Cache-Control', 'no-cache, must-revalidate') |
|
| 72 | + ->withHeader('Pragma', 'no-cache'); |
|
| 73 | +} |
|
| 74 | +return $xPsrResponse |
|
| 75 | +->withHeader('content-type', $this->response()->getContentType()) |
|
| 76 | +->withBody(Stream::create($this->response()->getOutput())); |
|
| 77 | +} |
|
| 78 | 78 | } |
@@ -13,94 +13,94 @@ |
||
| 13 | 13 | |
| 14 | 14 | class DataBagPlugin extends AbstractResponsePlugin |
| 15 | 15 | { |
| 16 | - /** |
|
| 16 | +/** |
|
| 17 | 17 | * @const The plugin name |
| 18 | 18 | */ |
| 19 | - public const NAME = 'bags'; |
|
| 19 | +public const NAME = 'bags'; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * @var DataBag |
| 23 | 23 | */ |
| 24 | - protected $xDataBag = null; |
|
| 24 | +protected $xDataBag = null; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * The constructor |
| 28 | 28 | */ |
| 29 | - public function __construct(protected Container $di) |
|
| 30 | - {} |
|
| 29 | +public function __construct(protected Container $di) |
|
| 30 | +{} |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * @return void |
| 34 | 34 | */ |
| 35 | - private function initDataBag(): void |
|
| 36 | - { |
|
| 37 | - if($this->xDataBag !== null) |
|
| 38 | - { |
|
| 39 | - return; |
|
| 40 | - } |
|
| 35 | +private function initDataBag(): void |
|
| 36 | +{ |
|
| 37 | +if($this->xDataBag !== null) |
|
| 38 | +{ |
|
| 39 | +return; |
|
| 40 | +} |
|
| 41 | 41 | |
| 42 | - // Get the databag contents from the HTTP request parameters. |
|
| 43 | - $xRequest = $this->di->getRequest(); |
|
| 44 | - $aBody = $xRequest->getParsedBody(); |
|
| 45 | - $aParams = $xRequest->getQueryParams(); |
|
| 46 | - $aData = is_array($aBody) ? |
|
| 47 | - $this->readData($aBody['jxnbags'] ?? []) : |
|
| 48 | - $this->readData($aParams['jxnbags'] ?? []); |
|
| 49 | - $this->xDataBag = new DataBag($this, $aData); |
|
| 50 | - } |
|
| 42 | +// Get the databag contents from the HTTP request parameters. |
|
| 43 | +$xRequest = $this->di->getRequest(); |
|
| 44 | +$aBody = $xRequest->getParsedBody(); |
|
| 45 | +$aParams = $xRequest->getQueryParams(); |
|
| 46 | +$aData = is_array($aBody) ? |
|
| 47 | +$this->readData($aBody['jxnbags'] ?? []) : |
|
| 48 | +$this->readData($aParams['jxnbags'] ?? []); |
|
| 49 | +$this->xDataBag = new DataBag($this, $aData); |
|
| 50 | +} |
|
| 51 | 51 | |
| 52 | - /** |
|
| 52 | +/** |
|
| 53 | 53 | * @inheritDoc |
| 54 | 54 | */ |
| 55 | - public function getName(): string |
|
| 56 | - { |
|
| 57 | - return self::NAME; |
|
| 58 | - } |
|
| 55 | +public function getName(): string |
|
| 56 | +{ |
|
| 57 | +return self::NAME; |
|
| 58 | +} |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * @param mixed $xData |
| 62 | 62 | * |
| 63 | 63 | * @return array |
| 64 | 64 | */ |
| 65 | - private function readData($xData): array |
|
| 66 | - { |
|
| 67 | - // Todo: clean input data. |
|
| 68 | - // Todo: verify the checksums. |
|
| 69 | - return is_string($xData) ? |
|
| 70 | - (json_decode($xData, true) ?: []) : |
|
| 71 | - (is_array($xData) ? $xData : []); |
|
| 72 | - } |
|
| 65 | +private function readData($xData): array |
|
| 66 | +{ |
|
| 67 | +// Todo: clean input data. |
|
| 68 | +// Todo: verify the checksums. |
|
| 69 | +return is_string($xData) ? |
|
| 70 | +(json_decode($xData, true) ?: []) : |
|
| 71 | +(is_array($xData) ? $xData : []); |
|
| 72 | +} |
|
| 73 | 73 | |
| 74 | - /** |
|
| 74 | +/** |
|
| 75 | 75 | * @inheritDoc |
| 76 | 76 | */ |
| 77 | - public function getHash(): string |
|
| 78 | - { |
|
| 79 | - // Use the version number as hash |
|
| 80 | - return '4.0.0'; |
|
| 81 | - } |
|
| 77 | +public function getHash(): string |
|
| 78 | +{ |
|
| 79 | +// Use the version number as hash |
|
| 80 | +return '4.0.0'; |
|
| 81 | +} |
|
| 82 | 82 | |
| 83 | - /** |
|
| 83 | +/** |
|
| 84 | 84 | * @return void |
| 85 | 85 | */ |
| 86 | - public function writeCommand(): void |
|
| 87 | - { |
|
| 88 | - $this->initDataBag(); |
|
| 89 | - if($this->xDataBag->touched()) |
|
| 90 | - { |
|
| 91 | - // Todo: calculate the checksums. |
|
| 92 | - $this->addCommand('databag.set', ['values' => $this->xDataBag]); |
|
| 93 | - } |
|
| 94 | - } |
|
| 86 | +public function writeCommand(): void |
|
| 87 | +{ |
|
| 88 | +$this->initDataBag(); |
|
| 89 | +if($this->xDataBag->touched()) |
|
| 90 | +{ |
|
| 91 | +// Todo: calculate the checksums. |
|
| 92 | +$this->addCommand('databag.set', ['values' => $this->xDataBag]); |
|
| 93 | +} |
|
| 94 | +} |
|
| 95 | 95 | |
| 96 | - /** |
|
| 96 | +/** |
|
| 97 | 97 | * @param string $sName |
| 98 | 98 | * |
| 99 | 99 | * @return DataBagContext |
| 100 | 100 | */ |
| 101 | - public function bag(string $sName): DataBagContext |
|
| 102 | - { |
|
| 103 | - $this->initDataBag(); |
|
| 104 | - return new DataBagContext($this->xDataBag, $sName); |
|
| 105 | - } |
|
| 101 | +public function bag(string $sName): DataBagContext |
|
| 102 | +{ |
|
| 103 | +$this->initDataBag(); |
|
| 104 | +return new DataBagContext($this->xDataBag, $sName); |
|
| 105 | +} |
|
| 106 | 106 | } |