@@ -63,7 +63,7 @@ |
||
| 63 | 63 | public function ajaxResponse(): ResponseInterface |
| 64 | 64 | { |
| 65 | 65 | $xPsrResponse = $this->xPsr17Factory->createResponse(200); |
| 66 | - if($this->xRequest->getMethod() === 'GET') |
|
| 66 | + if ($this->xRequest->getMethod() === 'GET') |
|
| 67 | 67 | { |
| 68 | 68 | $xPsrResponse = $xPsrResponse |
| 69 | 69 | ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT') |
@@ -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 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | public function shallIncludeAssets(AbstractPlugin $xPlugin): bool |
| 74 | 74 | { |
| 75 | 75 | $sPluginOptionName = 'assets.include.' . $xPlugin->getName(); |
| 76 | - if($this->hasLibOption($sPluginOptionName)) |
|
| 76 | + if ($this->hasLibOption($sPluginOptionName)) |
|
| 77 | 77 | { |
| 78 | 78 | return $this->getLibOption($sPluginOptionName); |
| 79 | 79 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | $this->getLibOption('js.lib.jq', "$sJsLibUri/libs/chibi/chibi$sJsExtension"), |
| 98 | 98 | "$sJsLibUri/jaxon.core$sJsExtension", |
| 99 | 99 | ]; |
| 100 | - if($this->getLibOption('core.debug.on')) |
|
| 100 | + if ($this->getLibOption('core.debug.on')) |
|
| 101 | 101 | { |
| 102 | 102 | $sLanguage = $this->getLibOption('core.language'); |
| 103 | 103 | $aJsFiles[] = "$sJsLibUri/jaxon.debug$sJsExtension"; |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | // Check config options |
| 118 | 118 | // - The js.app.export option must be set to true |
| 119 | 119 | // - The js.app.uri and js.app.dir options must be set to non null values |
| 120 | - if(!$this->getLibOption('js.app.export', false) || |
|
| 120 | + if (!$this->getLibOption('js.app.export', false) || |
|
| 121 | 121 | !$this->getLibOption('js.app.uri') || !$this->getLibOption('js.app.dir')) |
| 122 | 122 | { |
| 123 | 123 | return false; |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | $sJsFileName = $this->getLibOption('js.app.file') ?: $xCodeGenerator->getHash(); |
| 139 | 139 | $sJsDirectory = rtrim($this->getLibOption('js.app.dir'), '\/') . DIRECTORY_SEPARATOR; |
| 140 | 140 | // - The js.app.dir must be writable |
| 141 | - if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
|
| 141 | + if (!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
|
| 142 | 142 | { |
| 143 | 143 | return ''; |
| 144 | 144 | } |
@@ -147,16 +147,16 @@ discard block |
||
| 147 | 147 | $sJsMinFilePath = $sJsDirectory . $sJsFileName . '.min.js'; |
| 148 | 148 | $sJsFileUri = rtrim($this->getLibOption('js.app.uri'), '/') . "/$sJsFileName"; |
| 149 | 149 | |
| 150 | - if(!is_file($sJsFilePath) && |
|
| 150 | + if (!is_file($sJsFilePath) && |
|
| 151 | 151 | !@file_put_contents($sJsFilePath, $xCodeGenerator->getJsScript())) |
| 152 | 152 | { |
| 153 | 153 | return ''; |
| 154 | 154 | } |
| 155 | - if(!$this->getLibOption('js.app.minify', false)) |
|
| 155 | + if (!$this->getLibOption('js.app.minify', false)) |
|
| 156 | 156 | { |
| 157 | 157 | return $sJsFileUri . '.js'; |
| 158 | 158 | } |
| 159 | - if(!is_file($sJsMinFilePath) && |
|
| 159 | + if (!is_file($sJsMinFilePath) && |
|
| 160 | 160 | !$this->xMinifier->minify($sJsFilePath, $sJsMinFilePath)) |
| 161 | 161 | { |
| 162 | 162 | // If the file cannot be minified, return the plain js file. |
@@ -26,142 +26,142 @@ |
||
| 26 | 26 | |
| 27 | 27 | class AssetManager |
| 28 | 28 | { |
| 29 | - use ConfigTrait; |
|
| 29 | +use ConfigTrait; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * Default library URL |
| 33 | 33 | * |
| 34 | 34 | * @var string |
| 35 | 35 | */ |
| 36 | - const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
|
| 36 | +const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * The constructor |
| 40 | 40 | * |
| 41 | 41 | * @param ConfigManager $xConfigManager |
| 42 | 42 | * @param MinifierInterface $xMinifier |
| 43 | 43 | */ |
| 44 | - public function __construct(private ConfigManager $xConfigManager, |
|
| 45 | - private MinifierInterface $xMinifier) |
|
| 46 | - {} |
|
| 44 | +public function __construct(private ConfigManager $xConfigManager, |
|
| 45 | +private MinifierInterface $xMinifier) |
|
| 46 | +{} |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * @return ConfigManager |
| 50 | 50 | */ |
| 51 | - protected function config(): ConfigManager |
|
| 52 | - { |
|
| 53 | - return $this->xConfigManager; |
|
| 54 | - } |
|
| 51 | +protected function config(): ConfigManager |
|
| 52 | +{ |
|
| 53 | +return $this->xConfigManager; |
|
| 54 | +} |
|
| 55 | 55 | |
| 56 | - /** |
|
| 56 | +/** |
|
| 57 | 57 | * Get app js options |
| 58 | 58 | * |
| 59 | 59 | * @return string |
| 60 | 60 | */ |
| 61 | - public function getJsOptions(): string |
|
| 62 | - { |
|
| 63 | - return $this->getLibOption('js.app.options', ''); |
|
| 64 | - } |
|
| 61 | +public function getJsOptions(): string |
|
| 62 | +{ |
|
| 63 | +return $this->getLibOption('js.app.options', ''); |
|
| 64 | +} |
|
| 65 | 65 | |
| 66 | - /** |
|
| 66 | +/** |
|
| 67 | 67 | * Check if the assets of this plugin shall be included in Jaxon generated code. |
| 68 | 68 | * |
| 69 | 69 | * @param AbstractPlugin $xPlugin |
| 70 | 70 | * |
| 71 | 71 | * @return bool |
| 72 | 72 | */ |
| 73 | - public function shallIncludeAssets(AbstractPlugin $xPlugin): bool |
|
| 74 | - { |
|
| 75 | - $sPluginOptionName = 'assets.include.' . $xPlugin->getName(); |
|
| 76 | - if($this->hasLibOption($sPluginOptionName)) |
|
| 77 | - { |
|
| 78 | - return $this->getLibOption($sPluginOptionName); |
|
| 79 | - } |
|
| 80 | - return $this->getLibOption('assets.include.all', true); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 73 | +public function shallIncludeAssets(AbstractPlugin $xPlugin): bool |
|
| 74 | +{ |
|
| 75 | +$sPluginOptionName = 'assets.include.' . $xPlugin->getName(); |
|
| 76 | +if($this->hasLibOption($sPluginOptionName)) |
|
| 77 | +{ |
|
| 78 | +return $this->getLibOption($sPluginOptionName); |
|
| 79 | +} |
|
| 80 | +return $this->getLibOption('assets.include.all', true); |
|
| 81 | +} |
|
| 82 | + |
|
| 83 | +/** |
|
| 84 | 84 | * Get the HTML tags to include Jaxon javascript files into the page |
| 85 | 85 | * |
| 86 | 86 | * @return array |
| 87 | 87 | */ |
| 88 | - public function getJsLibFiles(): array |
|
| 89 | - { |
|
| 90 | - $sJsExtension = $this->getLibOption('js.app.minify') ? '.min.js' : '.js'; |
|
| 91 | - // The URI for the javascript library files |
|
| 92 | - $sJsLibUri = $this->getLibOption('js.lib.uri', self::JS_LIB_URL); |
|
| 93 | - $sJsLibUri = rtrim($sJsLibUri, '/'); |
|
| 94 | - |
|
| 95 | - // Add component files to the javascript file array; |
|
| 96 | - $aJsFiles = [ |
|
| 97 | - $this->getLibOption('js.lib.jq', "$sJsLibUri/libs/chibi/chibi$sJsExtension"), |
|
| 98 | - "$sJsLibUri/jaxon.core$sJsExtension", |
|
| 99 | - ]; |
|
| 100 | - if($this->getLibOption('core.debug.on')) |
|
| 101 | - { |
|
| 102 | - $sLanguage = $this->getLibOption('core.language'); |
|
| 103 | - $aJsFiles[] = "$sJsLibUri/jaxon.debug$sJsExtension"; |
|
| 104 | - $aJsFiles[] = "$sJsLibUri/lang/jaxon.$sLanguage$sJsExtension"; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $aJsFiles; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 88 | +public function getJsLibFiles(): array |
|
| 89 | +{ |
|
| 90 | +$sJsExtension = $this->getLibOption('js.app.minify') ? '.min.js' : '.js'; |
|
| 91 | +// The URI for the javascript library files |
|
| 92 | +$sJsLibUri = $this->getLibOption('js.lib.uri', self::JS_LIB_URL); |
|
| 93 | +$sJsLibUri = rtrim($sJsLibUri, '/'); |
|
| 94 | + |
|
| 95 | +// Add component files to the javascript file array; |
|
| 96 | +$aJsFiles = [ |
|
| 97 | +$this->getLibOption('js.lib.jq', "$sJsLibUri/libs/chibi/chibi$sJsExtension"), |
|
| 98 | +"$sJsLibUri/jaxon.core$sJsExtension", |
|
| 99 | +]; |
|
| 100 | +if($this->getLibOption('core.debug.on')) |
|
| 101 | +{ |
|
| 102 | +$sLanguage = $this->getLibOption('core.language'); |
|
| 103 | +$aJsFiles[] = "$sJsLibUri/jaxon.debug$sJsExtension"; |
|
| 104 | +$aJsFiles[] = "$sJsLibUri/lang/jaxon.$sLanguage$sJsExtension"; |
|
| 105 | +} |
|
| 106 | + |
|
| 107 | +return $aJsFiles; |
|
| 108 | +} |
|
| 109 | + |
|
| 110 | +/** |
|
| 111 | 111 | * Get the javascript file name |
| 112 | 112 | * |
| 113 | 113 | * @return bool |
| 114 | 114 | */ |
| 115 | - public function shallCreateJsFiles(): bool |
|
| 116 | - { |
|
| 117 | - // Check config options |
|
| 118 | - // - The js.app.export option must be set to true |
|
| 119 | - // - The js.app.uri and js.app.dir options must be set to non null values |
|
| 120 | - if(!$this->getLibOption('js.app.export', false) || |
|
| 121 | - !$this->getLibOption('js.app.uri') || !$this->getLibOption('js.app.dir')) |
|
| 122 | - { |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - return true; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 115 | +public function shallCreateJsFiles(): bool |
|
| 116 | +{ |
|
| 117 | +// Check config options |
|
| 118 | +// - The js.app.export option must be set to true |
|
| 119 | +// - The js.app.uri and js.app.dir options must be set to non null values |
|
| 120 | +if(!$this->getLibOption('js.app.export', false) || |
|
| 121 | +!$this->getLibOption('js.app.uri') || !$this->getLibOption('js.app.dir')) |
|
| 122 | +{ |
|
| 123 | +return false; |
|
| 124 | +} |
|
| 125 | +return true; |
|
| 126 | +} |
|
| 127 | + |
|
| 128 | +/** |
|
| 129 | 129 | * Write javascript files and return the corresponding URI |
| 130 | 130 | * |
| 131 | 131 | * @param CodeGenerator $xCodeGenerator |
| 132 | 132 | * |
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - public function createJsFiles(CodeGenerator $xCodeGenerator): string |
|
| 136 | - { |
|
| 137 | - // Check dir access |
|
| 138 | - $sJsFileName = $this->getLibOption('js.app.file') ?: $xCodeGenerator->getHash(); |
|
| 139 | - $sJsDirectory = rtrim($this->getLibOption('js.app.dir'), '\/') . DIRECTORY_SEPARATOR; |
|
| 140 | - // - The js.app.dir must be writable |
|
| 141 | - if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
|
| 142 | - { |
|
| 143 | - return ''; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $sJsFilePath = $sJsDirectory . $sJsFileName . '.js'; |
|
| 147 | - $sJsMinFilePath = $sJsDirectory . $sJsFileName . '.min.js'; |
|
| 148 | - $sJsFileUri = rtrim($this->getLibOption('js.app.uri'), '/') . "/$sJsFileName"; |
|
| 149 | - |
|
| 150 | - if(!is_file($sJsFilePath) && |
|
| 151 | - !@file_put_contents($sJsFilePath, $xCodeGenerator->getJsScript())) |
|
| 152 | - { |
|
| 153 | - return ''; |
|
| 154 | - } |
|
| 155 | - if(!$this->getLibOption('js.app.minify', false)) |
|
| 156 | - { |
|
| 157 | - return $sJsFileUri . '.js'; |
|
| 158 | - } |
|
| 159 | - if(!is_file($sJsMinFilePath) && |
|
| 160 | - !$this->xMinifier->minify($sJsFilePath, $sJsMinFilePath)) |
|
| 161 | - { |
|
| 162 | - // If the file cannot be minified, return the plain js file. |
|
| 163 | - return $sJsFileUri . '.js'; |
|
| 164 | - } |
|
| 165 | - return $sJsFileUri . '.min.js'; |
|
| 166 | - } |
|
| 135 | +public function createJsFiles(CodeGenerator $xCodeGenerator): string |
|
| 136 | +{ |
|
| 137 | +// Check dir access |
|
| 138 | +$sJsFileName = $this->getLibOption('js.app.file') ?: $xCodeGenerator->getHash(); |
|
| 139 | +$sJsDirectory = rtrim($this->getLibOption('js.app.dir'), '\/') . DIRECTORY_SEPARATOR; |
|
| 140 | +// - The js.app.dir must be writable |
|
| 141 | +if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
|
| 142 | +{ |
|
| 143 | +return ''; |
|
| 144 | +} |
|
| 145 | + |
|
| 146 | +$sJsFilePath = $sJsDirectory . $sJsFileName . '.js'; |
|
| 147 | +$sJsMinFilePath = $sJsDirectory . $sJsFileName . '.min.js'; |
|
| 148 | +$sJsFileUri = rtrim($this->getLibOption('js.app.uri'), '/') . "/$sJsFileName"; |
|
| 149 | + |
|
| 150 | +if(!is_file($sJsFilePath) && |
|
| 151 | +!@file_put_contents($sJsFilePath, $xCodeGenerator->getJsScript())) |
|
| 152 | +{ |
|
| 153 | +return ''; |
|
| 154 | +} |
|
| 155 | +if(!$this->getLibOption('js.app.minify', false)) |
|
| 156 | +{ |
|
| 157 | +return $sJsFileUri . '.js'; |
|
| 158 | +} |
|
| 159 | +if(!is_file($sJsMinFilePath) && |
|
| 160 | +!$this->xMinifier->minify($sJsFilePath, $sJsMinFilePath)) |
|
| 161 | +{ |
|
| 162 | +// If the file cannot be minified, return the plain js file. |
|
| 163 | +return $sJsFileUri . '.js'; |
|
| 164 | +} |
|
| 165 | +return $sJsFileUri . '.min.js'; |
|
| 166 | +} |
|
| 167 | 167 | } |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | public function __construct(string $sDirectory) |
| 29 | 29 | { |
| 30 | 30 | $itFile = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sDirectory)); |
| 31 | - foreach($itFile as $xFile) |
|
| 31 | + foreach ($itFile as $xFile) |
|
| 32 | 32 | { |
| 33 | 33 | $this->insert($xFile); |
| 34 | 34 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | private function checkDirectory(string $sDirectory): string |
| 69 | 69 | { |
| 70 | 70 | $sDirectory = rtrim(trim($sDirectory), '/\\'); |
| 71 | - if(!is_dir($sDirectory)) |
|
| 71 | + if (!is_dir($sDirectory)) |
|
| 72 | 72 | { |
| 73 | 73 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 74 | 74 | } |
@@ -81,11 +81,11 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function checkOptions(string $sCallable, $xOptions): array |
| 83 | 83 | { |
| 84 | - if(is_string($xOptions)) |
|
| 84 | + if (is_string($xOptions)) |
|
| 85 | 85 | { |
| 86 | 86 | $xOptions = ['namespace' => $xOptions]; |
| 87 | 87 | } |
| 88 | - if(!is_array($xOptions)) |
|
| 88 | + if (!is_array($xOptions)) |
|
| 89 | 89 | { |
| 90 | 90 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 91 | 91 | } |
@@ -93,14 +93,14 @@ discard block |
||
| 93 | 93 | $xOptions['directory'] = $this->checkDirectory($sCallable); |
| 94 | 94 | // Check the namespace |
| 95 | 95 | $sNamespace = $xOptions['namespace'] ?? ''; |
| 96 | - if(!($xOptions['namespace'] = trim($sNamespace, ' \\'))) |
|
| 96 | + if (!($xOptions['namespace'] = trim($sNamespace, ' \\'))) |
|
| 97 | 97 | { |
| 98 | 98 | $xOptions['namespace'] = ''; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Change the keys in $xOptions to have "\" as separator |
| 102 | 102 | $_aOptions = []; |
| 103 | - foreach($xOptions as $sName => $aOption) |
|
| 103 | + foreach ($xOptions as $sName => $aOption) |
|
| 104 | 104 | { |
| 105 | 105 | $sName = trim(str_replace('.', '\\', $sName), ' \\'); |
| 106 | 106 | $_aOptions[$sName] = $aOption; |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | public function register(string $sType, string $sCallable, array $aOptions): bool |
| 115 | 115 | { |
| 116 | 116 | // The $sCallable var is not used here because the checkOptions() method copied it into the $aOptions array. |
| 117 | - if(($aOptions['namespace'])) |
|
| 117 | + if (($aOptions['namespace'])) |
|
| 118 | 118 | { |
| 119 | 119 | $this->xRegistry->registerNamespace($aOptions['namespace'], $aOptions); |
| 120 | 120 | return true; |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | public function configure(string $sName, string $sValue): void |
| 106 | 106 | { |
| 107 | - switch($sName) |
|
| 107 | + switch ($sName) |
|
| 108 | 108 | { |
| 109 | 109 | case 'class': // The user function is a method in the given class |
| 110 | 110 | $this->xPhpFunction = [$sValue, $this->xPhpFunction]; |
@@ -128,12 +128,12 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | public function call(array $aArgs = []): void |
| 130 | 130 | { |
| 131 | - if($this->sInclude !== '') |
|
| 131 | + if ($this->sInclude !== '') |
|
| 132 | 132 | { |
| 133 | 133 | require_once $this->sInclude; |
| 134 | 134 | } |
| 135 | 135 | // If the function is an alias for a class method, then instantiate the class |
| 136 | - if(is_array($this->xPhpFunction) && is_string($this->xPhpFunction[0])) |
|
| 136 | + if (is_array($this->xPhpFunction) && is_string($this->xPhpFunction[0])) |
|
| 137 | 137 | { |
| 138 | 138 | $sClassName = $this->xPhpFunction[0]; |
| 139 | 139 | $this->xPhpFunction[0] = $this->di->h($sClassName) ? |
@@ -28,29 +28,29 @@ discard block |
||
| 28 | 28 | |
| 29 | 29 | class CallableFunction |
| 30 | 30 | { |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * A string or an array which defines the registered PHP function |
| 33 | 33 | * |
| 34 | 34 | * @var string|array |
| 35 | 35 | */ |
| 36 | - private $xPhpFunction; |
|
| 36 | +private $xPhpFunction; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * The path and file name of the include file where the function is defined |
| 40 | 40 | * |
| 41 | 41 | * @var string |
| 42 | 42 | */ |
| 43 | - private $sInclude = ''; |
|
| 43 | +private $sInclude = ''; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * An associative array containing call options that will be sent |
| 47 | 47 | * to the browser with the client script. |
| 48 | 48 | * |
| 49 | 49 | * @var array |
| 50 | 50 | */ |
| 51 | - private $aOptions = []; |
|
| 51 | +private $aOptions = []; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 53 | +/** |
|
| 54 | 54 | * The constructor |
| 55 | 55 | * |
| 56 | 56 | * @param Container $di |
@@ -58,43 +58,43 @@ discard block |
||
| 58 | 58 | * @param string $sJsFunction |
| 59 | 59 | * @param string $sPhpFunction |
| 60 | 60 | */ |
| 61 | - public function __construct(private Container $di, private string $sFunction, |
|
| 62 | - private string $sJsFunction, string $sPhpFunction) |
|
| 63 | - { |
|
| 64 | - $this->xPhpFunction = $sPhpFunction; |
|
| 65 | - } |
|
| 61 | +public function __construct(private Container $di, private string $sFunction, |
|
| 62 | +private string $sJsFunction, string $sPhpFunction) |
|
| 63 | +{ |
|
| 64 | +$this->xPhpFunction = $sPhpFunction; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * Get the name of the function being referenced |
| 69 | 69 | * |
| 70 | 70 | * @return string |
| 71 | 71 | */ |
| 72 | - public function getName(): string |
|
| 73 | - { |
|
| 74 | - return $this->sFunction; |
|
| 75 | - } |
|
| 72 | +public function getName(): string |
|
| 73 | +{ |
|
| 74 | +return $this->sFunction; |
|
| 75 | +} |
|
| 76 | 76 | |
| 77 | - /** |
|
| 77 | +/** |
|
| 78 | 78 | * Get name of the corresponding javascript function |
| 79 | 79 | * |
| 80 | 80 | * @return string |
| 81 | 81 | */ |
| 82 | - public function getJsName(): string |
|
| 83 | - { |
|
| 84 | - return $this->sJsFunction; |
|
| 85 | - } |
|
| 82 | +public function getJsName(): string |
|
| 83 | +{ |
|
| 84 | +return $this->sJsFunction; |
|
| 85 | +} |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | +/** |
|
| 88 | 88 | * Get the config options of the function being referenced |
| 89 | 89 | * |
| 90 | 90 | * @return array |
| 91 | 91 | */ |
| 92 | - public function getOptions(): array |
|
| 93 | - { |
|
| 94 | - return $this->aOptions; |
|
| 95 | - } |
|
| 92 | +public function getOptions(): array |
|
| 93 | +{ |
|
| 94 | +return $this->aOptions; |
|
| 95 | +} |
|
| 96 | 96 | |
| 97 | - /** |
|
| 97 | +/** |
|
| 98 | 98 | * Set call options for this instance |
| 99 | 99 | * |
| 100 | 100 | * @param string $sName The name of the configuration option |
@@ -102,20 +102,20 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @return void |
| 104 | 104 | */ |
| 105 | - public function configure(string $sName, string $sValue): void |
|
| 106 | - { |
|
| 107 | - switch($sName) |
|
| 108 | - { |
|
| 109 | - case 'class': // The user function is a method in the given class |
|
| 110 | - $this->xPhpFunction = [$sValue, $this->xPhpFunction]; |
|
| 111 | - break; |
|
| 112 | - case 'include': |
|
| 113 | - $this->sInclude = $sValue; |
|
| 114 | - break; |
|
| 115 | - default: |
|
| 116 | - $this->aOptions[$sName] = $sValue; |
|
| 117 | - break; |
|
| 118 | - } |
|
| 105 | +public function configure(string $sName, string $sValue): void |
|
| 106 | +{ |
|
| 107 | +switch($sName) |
|
| 108 | +{ |
|
| 109 | +case 'class': // The user function is a method in the given class |
|
| 110 | +$this->xPhpFunction = [$sValue, $this->xPhpFunction]; |
|
| 111 | +break; |
|
| 112 | +case 'include': |
|
| 113 | +$this->sInclude = $sValue; |
|
| 114 | +break; |
|
| 115 | +default: |
|
| 116 | +$this->aOptions[$sName] = $sValue; |
|
| 117 | +break; |
|
| 118 | +} |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | /** |
@@ -92,8 +92,7 @@ discard block |
||
| 92 | 92 | if(is_string($xOptions)) |
| 93 | 93 | { |
| 94 | 94 | $xOptions = ['include' => $xOptions]; |
| 95 | - } |
|
| 96 | - elseif(!is_array($xOptions)) |
|
| 95 | + } elseif(!is_array($xOptions)) |
|
| 97 | 96 | { |
| 98 | 97 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 99 | 98 | } |
@@ -236,8 +235,7 @@ discard block |
||
| 236 | 235 | { |
| 237 | 236 | /** @var CallableFunction */ |
| 238 | 237 | $xFunction = $this->getCallable($sRequestedFunction); |
| 239 | - } |
|
| 240 | - catch(Exception $e) |
|
| 238 | + } catch(Exception $e) |
|
| 241 | 239 | { |
| 242 | 240 | // Unable to find the requested function |
| 243 | 241 | $this->throwException($e, $this->xTranslator->trans('errors.functions.invalid', |
@@ -246,8 +244,7 @@ discard block |
||
| 246 | 244 | try |
| 247 | 245 | { |
| 248 | 246 | $xFunction->call($this->xTarget->args()); |
| 249 | - } |
|
| 250 | - catch(Exception $e) |
|
| 247 | + } catch(Exception $e) |
|
| 251 | 248 | { |
| 252 | 249 | // Unable to execute the requested function |
| 253 | 250 | $this->throwException($e, $this->xTranslator->trans('errors.functions.call', |
@@ -86,15 +86,15 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | public function checkOptions(string $sCallable, $xOptions): array |
| 88 | 88 | { |
| 89 | - if(!$this->xValidator->validateFunction(trim($sCallable))) |
|
| 89 | + if (!$this->xValidator->validateFunction(trim($sCallable))) |
|
| 90 | 90 | { |
| 91 | 91 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 92 | 92 | } |
| 93 | - if(is_string($xOptions)) |
|
| 93 | + if (is_string($xOptions)) |
|
| 94 | 94 | { |
| 95 | 95 | $xOptions = ['include' => $xOptions]; |
| 96 | 96 | } |
| 97 | - elseif(!is_array($xOptions)) |
|
| 97 | + elseif (!is_array($xOptions)) |
|
| 98 | 98 | { |
| 99 | 99 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 100 | 100 | } |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | $sPhpFunction = trim($sCallable); |
| 116 | 116 | $sFunction = $sPhpFunction; |
| 117 | 117 | // Check if an alias is defined |
| 118 | - if(isset($aOptions['alias'])) |
|
| 118 | + if (isset($aOptions['alias'])) |
|
| 119 | 119 | { |
| 120 | 120 | $sFunction = (string)$aOptions['alias']; |
| 121 | 121 | unset($aOptions['alias']); |
@@ -139,13 +139,13 @@ discard block |
||
| 139 | 139 | public function getCallable(string $sCallable): CallableFunction|null |
| 140 | 140 | { |
| 141 | 141 | $sFunction = trim($sCallable); |
| 142 | - if(!isset($this->aFunctions[$sFunction])) |
|
| 142 | + if (!isset($this->aFunctions[$sFunction])) |
|
| 143 | 143 | { |
| 144 | 144 | return null; |
| 145 | 145 | } |
| 146 | 146 | $xCallable = new CallableFunction($this->di, $sFunction, |
| 147 | 147 | $this->sPrefix . $sFunction, $this->aFunctions[$sFunction]); |
| 148 | - foreach($this->aOptions[$sFunction] as $sName => $sValue) |
|
| 148 | + foreach ($this->aOptions[$sFunction] as $sName => $sValue) |
|
| 149 | 149 | { |
| 150 | 150 | $xCallable->configure($sName, $sValue); |
| 151 | 151 | } |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | private function getCallableScript(CallableFunction $xFunction): string |
| 163 | 163 | { |
| 164 | 164 | $aOptions = []; |
| 165 | - foreach($xFunction->getOptions() as $sKey => $sValue) |
|
| 165 | + foreach ($xFunction->getOptions() as $sKey => $sValue) |
|
| 166 | 166 | { |
| 167 | 167 | $aOptions[] = "$sKey: $sValue"; |
| 168 | 168 | } |
@@ -170,8 +170,7 @@ discard block |
||
| 170 | 170 | return $this->xTemplateEngine->render('jaxon::callables/function.js', [ |
| 171 | 171 | 'sName' => $xFunction->getName(), |
| 172 | 172 | 'sJsName' => $xFunction->getJsName(), |
| 173 | - 'sArguments' => count($aOptions) === 0 ? 'args' : |
|
| 174 | - 'args, { ' . implode(',', $aOptions) . ' }', |
|
| 173 | + 'sArguments' => count($aOptions) === 0 ? 'args' : 'args, { ' . implode(',', $aOptions) . ' }', |
|
| 175 | 174 | ]); |
| 176 | 175 | } |
| 177 | 176 | |
@@ -181,7 +180,7 @@ discard block |
||
| 181 | 180 | public function getScript(): string |
| 182 | 181 | { |
| 183 | 182 | $code = ''; |
| 184 | - foreach(array_keys($this->aFunctions) as $sFunction) |
|
| 183 | + foreach (array_keys($this->aFunctions) as $sFunction) |
|
| 185 | 184 | { |
| 186 | 185 | $xFunction = $this->getCallable($sFunction); |
| 187 | 186 | $code .= $this->getCallableScript($xFunction); |
@@ -219,8 +218,7 @@ discard block |
||
| 219 | 218 | private function throwException(Exception $xException, string $sErrorMessage): void |
| 220 | 219 | { |
| 221 | 220 | $this->di->getLogger()->error($xException->getMessage()); |
| 222 | - throw new RequestException($sErrorMessage . (!$this->bDebug ? '' : |
|
| 223 | - "\n" . $xException->getMessage())); |
|
| 221 | + throw new RequestException($sErrorMessage . (!$this->bDebug ? '' : "\n" . $xException->getMessage())); |
|
| 224 | 222 | } |
| 225 | 223 | |
| 226 | 224 | /** |
@@ -232,7 +230,7 @@ discard block |
||
| 232 | 230 | $sRequestedFunction = $this->xTarget->getFunctionName(); |
| 233 | 231 | |
| 234 | 232 | // Security check: make sure the requested function was registered. |
| 235 | - if(!$this->xValidator->validateFunction($sRequestedFunction) || |
|
| 233 | + if (!$this->xValidator->validateFunction($sRequestedFunction) || |
|
| 236 | 234 | !isset($this->aFunctions[$sRequestedFunction])) |
| 237 | 235 | { |
| 238 | 236 | // Unable to find the requested function |
@@ -245,7 +243,7 @@ discard block |
||
| 245 | 243 | /** @var CallableFunction */ |
| 246 | 244 | $xFunction = $this->getCallable($sRequestedFunction); |
| 247 | 245 | } |
| 248 | - catch(Exception $e) |
|
| 246 | + catch (Exception $e) |
|
| 249 | 247 | { |
| 250 | 248 | // Unable to find the requested function |
| 251 | 249 | $this->throwException($e, $this->xTranslator->trans('errors.functions.invalid', |
@@ -255,7 +253,7 @@ discard block |
||
| 255 | 253 | { |
| 256 | 254 | $xFunction->call($this->xTarget->args()); |
| 257 | 255 | } |
| 258 | - catch(Exception $e) |
|
| 256 | + catch (Exception $e) |
|
| 259 | 257 | { |
| 260 | 258 | // Unable to execute the requested function |
| 261 | 259 | $this->throwException($e, $this->xTranslator->trans('errors.functions.call', |
@@ -43,21 +43,21 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | class CallableFunctionPlugin extends AbstractRequestPlugin |
| 45 | 45 | { |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * The registered functions names |
| 48 | 48 | * |
| 49 | 49 | * @var array |
| 50 | 50 | */ |
| 51 | - protected $aFunctions = []; |
|
| 51 | +protected $aFunctions = []; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 53 | +/** |
|
| 54 | 54 | * The registered functions options |
| 55 | 55 | * |
| 56 | 56 | * @var array |
| 57 | 57 | */ |
| 58 | - protected $aOptions = []; |
|
| 58 | +protected $aOptions = []; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The constructor |
| 62 | 62 | * |
| 63 | 63 | * @param string $sPrefix |
@@ -67,41 +67,41 @@ discard block |
||
| 67 | 67 | * @param Translator $xTranslator |
| 68 | 68 | * @param Validator $xValidator |
| 69 | 69 | */ |
| 70 | - public function __construct(private string $sPrefix, private bool $bDebug, |
|
| 71 | - private Container $di, private TemplateEngine $xTemplateEngine, |
|
| 72 | - private Translator $xTranslator, private Validator $xValidator) |
|
| 73 | - {} |
|
| 70 | +public function __construct(private string $sPrefix, private bool $bDebug, |
|
| 71 | +private Container $di, private TemplateEngine $xTemplateEngine, |
|
| 72 | +private Translator $xTranslator, private Validator $xValidator) |
|
| 73 | +{} |
|
| 74 | 74 | |
| 75 | - /** |
|
| 75 | +/** |
|
| 76 | 76 | * @inheritDoc |
| 77 | 77 | */ |
| 78 | - public function getName(): string |
|
| 79 | - { |
|
| 80 | - return Jaxon::CALLABLE_FUNCTION; |
|
| 81 | - } |
|
| 78 | +public function getName(): string |
|
| 79 | +{ |
|
| 80 | +return Jaxon::CALLABLE_FUNCTION; |
|
| 81 | +} |
|
| 82 | 82 | |
| 83 | - /** |
|
| 83 | +/** |
|
| 84 | 84 | * @inheritDoc |
| 85 | 85 | * @throws SetupException |
| 86 | 86 | */ |
| 87 | - public function checkOptions(string $sCallable, $xOptions): array |
|
| 88 | - { |
|
| 89 | - if(!$this->xValidator->validateFunction(trim($sCallable))) |
|
| 90 | - { |
|
| 91 | - throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
| 92 | - } |
|
| 93 | - if(is_string($xOptions)) |
|
| 94 | - { |
|
| 95 | - $xOptions = ['include' => $xOptions]; |
|
| 96 | - } |
|
| 97 | - elseif(!is_array($xOptions)) |
|
| 98 | - { |
|
| 99 | - throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
| 100 | - } |
|
| 101 | - return $xOptions; |
|
| 102 | - } |
|
| 87 | +public function checkOptions(string $sCallable, $xOptions): array |
|
| 88 | +{ |
|
| 89 | +if(!$this->xValidator->validateFunction(trim($sCallable))) |
|
| 90 | +{ |
|
| 91 | +throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
| 92 | +} |
|
| 93 | +if(is_string($xOptions)) |
|
| 94 | +{ |
|
| 95 | +$xOptions = ['include' => $xOptions]; |
|
| 96 | +} |
|
| 97 | +elseif(!is_array($xOptions)) |
|
| 98 | +{ |
|
| 99 | +throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
| 100 | +} |
|
| 101 | +return $xOptions; |
|
| 102 | +} |
|
| 103 | 103 | |
| 104 | - /** |
|
| 104 | +/** |
|
| 105 | 105 | * Register a user defined function |
| 106 | 106 | * |
| 107 | 107 | * @param string $sType The type of request handler being registered |
@@ -110,156 +110,156 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @return bool |
| 112 | 112 | */ |
| 113 | - public function register(string $sType, string $sCallable, array $aOptions): bool |
|
| 114 | - { |
|
| 115 | - $sPhpFunction = trim($sCallable); |
|
| 116 | - $sFunction = $sPhpFunction; |
|
| 117 | - // Check if an alias is defined |
|
| 118 | - if(isset($aOptions['alias'])) |
|
| 119 | - { |
|
| 120 | - $sFunction = (string)$aOptions['alias']; |
|
| 121 | - unset($aOptions['alias']); |
|
| 122 | - } |
|
| 123 | - $this->aFunctions[$sFunction] = $sPhpFunction; |
|
| 124 | - $this->aOptions[$sFunction] = $aOptions; |
|
| 125 | - return true; |
|
| 126 | - } |
|
| 113 | +public function register(string $sType, string $sCallable, array $aOptions): bool |
|
| 114 | +{ |
|
| 115 | +$sPhpFunction = trim($sCallable); |
|
| 116 | +$sFunction = $sPhpFunction; |
|
| 117 | +// Check if an alias is defined |
|
| 118 | +if(isset($aOptions['alias'])) |
|
| 119 | +{ |
|
| 120 | +$sFunction = (string)$aOptions['alias']; |
|
| 121 | +unset($aOptions['alias']); |
|
| 122 | +} |
|
| 123 | +$this->aFunctions[$sFunction] = $sPhpFunction; |
|
| 124 | +$this->aOptions[$sFunction] = $aOptions; |
|
| 125 | +return true; |
|
| 126 | +} |
|
| 127 | 127 | |
| 128 | - /** |
|
| 128 | +/** |
|
| 129 | 129 | * @inheritDoc |
| 130 | 130 | */ |
| 131 | - public function getHash(): string |
|
| 132 | - { |
|
| 133 | - return md5(implode('', array_keys($this->aFunctions))); |
|
| 134 | - } |
|
| 131 | +public function getHash(): string |
|
| 132 | +{ |
|
| 133 | +return md5(implode('', array_keys($this->aFunctions))); |
|
| 134 | +} |
|
| 135 | 135 | |
| 136 | - /** |
|
| 136 | +/** |
|
| 137 | 137 | * @inheritDoc |
| 138 | 138 | */ |
| 139 | - public function getCallable(string $sCallable): CallableFunction|null |
|
| 140 | - { |
|
| 141 | - $sFunction = trim($sCallable); |
|
| 142 | - if(!isset($this->aFunctions[$sFunction])) |
|
| 143 | - { |
|
| 144 | - return null; |
|
| 145 | - } |
|
| 146 | - $xCallable = new CallableFunction($this->di, $sFunction, |
|
| 147 | - $this->sPrefix . $sFunction, $this->aFunctions[$sFunction]); |
|
| 148 | - foreach($this->aOptions[$sFunction] as $sName => $sValue) |
|
| 149 | - { |
|
| 150 | - $xCallable->configure($sName, $sValue); |
|
| 151 | - } |
|
| 152 | - return $xCallable; |
|
| 153 | - } |
|
| 139 | +public function getCallable(string $sCallable): CallableFunction|null |
|
| 140 | +{ |
|
| 141 | +$sFunction = trim($sCallable); |
|
| 142 | +if(!isset($this->aFunctions[$sFunction])) |
|
| 143 | +{ |
|
| 144 | +return null; |
|
| 145 | +} |
|
| 146 | +$xCallable = new CallableFunction($this->di, $sFunction, |
|
| 147 | +$this->sPrefix . $sFunction, $this->aFunctions[$sFunction]); |
|
| 148 | +foreach($this->aOptions[$sFunction] as $sName => $sValue) |
|
| 149 | +{ |
|
| 150 | +$xCallable->configure($sName, $sValue); |
|
| 151 | +} |
|
| 152 | +return $xCallable; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * Generate the javascript function stub that is sent to the browser on initial page load |
| 157 | 157 | * |
| 158 | 158 | * @param CallableFunction $xFunction |
| 159 | 159 | * |
| 160 | 160 | * @return string |
| 161 | 161 | */ |
| 162 | - private function getCallableScript(CallableFunction $xFunction): string |
|
| 163 | - { |
|
| 164 | - $aOptions = []; |
|
| 165 | - foreach($xFunction->getOptions() as $sKey => $sValue) |
|
| 166 | - { |
|
| 167 | - $aOptions[] = "$sKey: $sValue"; |
|
| 168 | - } |
|
| 162 | +private function getCallableScript(CallableFunction $xFunction): string |
|
| 163 | +{ |
|
| 164 | +$aOptions = []; |
|
| 165 | +foreach($xFunction->getOptions() as $sKey => $sValue) |
|
| 166 | +{ |
|
| 167 | +$aOptions[] = "$sKey: $sValue"; |
|
| 168 | +} |
|
| 169 | 169 | |
| 170 | - return $this->xTemplateEngine->render('jaxon::callables/function.js', [ |
|
| 171 | - 'sName' => $xFunction->getName(), |
|
| 172 | - 'sJsName' => $xFunction->getJsName(), |
|
| 173 | - 'sArguments' => count($aOptions) === 0 ? 'args' : |
|
| 174 | - 'args, { ' . implode(',', $aOptions) . ' }', |
|
| 175 | - ]); |
|
| 176 | - } |
|
| 170 | +return $this->xTemplateEngine->render('jaxon::callables/function.js', [ |
|
| 171 | +'sName' => $xFunction->getName(), |
|
| 172 | +'sJsName' => $xFunction->getJsName(), |
|
| 173 | +'sArguments' => count($aOptions) === 0 ? 'args' : |
|
| 174 | + 'args, { ' . implode(',', $aOptions) . ' }', |
|
| 175 | +]); |
|
| 176 | +} |
|
| 177 | 177 | |
| 178 | - /** |
|
| 178 | +/** |
|
| 179 | 179 | * @inheritDoc |
| 180 | 180 | */ |
| 181 | - public function getScript(): string |
|
| 182 | - { |
|
| 183 | - $code = ''; |
|
| 184 | - foreach(array_keys($this->aFunctions) as $sFunction) |
|
| 185 | - { |
|
| 186 | - $xFunction = $this->getCallable($sFunction); |
|
| 187 | - $code .= $this->getCallableScript($xFunction); |
|
| 188 | - } |
|
| 189 | - return $code; |
|
| 190 | - } |
|
| 181 | +public function getScript(): string |
|
| 182 | +{ |
|
| 183 | +$code = ''; |
|
| 184 | +foreach(array_keys($this->aFunctions) as $sFunction) |
|
| 185 | +{ |
|
| 186 | +$xFunction = $this->getCallable($sFunction); |
|
| 187 | +$code .= $this->getCallableScript($xFunction); |
|
| 188 | +} |
|
| 189 | +return $code; |
|
| 190 | +} |
|
| 191 | 191 | |
| 192 | - /** |
|
| 192 | +/** |
|
| 193 | 193 | * @inheritDoc |
| 194 | 194 | */ |
| 195 | - public static function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
| 196 | - { |
|
| 197 | - $aCall = $xRequest->getAttribute('jxncall'); |
|
| 198 | - // throw new \Exception(json_encode(['call' => $aCall])); |
|
| 199 | - return $aCall !== null && ($aCall['type'] ?? '') === 'func' && isset($aCall['name']); |
|
| 200 | - } |
|
| 195 | +public static function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
| 196 | +{ |
|
| 197 | +$aCall = $xRequest->getAttribute('jxncall'); |
|
| 198 | +// throw new \Exception(json_encode(['call' => $aCall])); |
|
| 199 | +return $aCall !== null && ($aCall['type'] ?? '') === 'func' && isset($aCall['name']); |
|
| 200 | +} |
|
| 201 | 201 | |
| 202 | - /** |
|
| 202 | +/** |
|
| 203 | 203 | * @inheritDoc |
| 204 | 204 | */ |
| 205 | - public function setTarget(ServerRequestInterface $xRequest): Target |
|
| 206 | - { |
|
| 207 | - $aCall = $xRequest->getAttribute('jxncall'); |
|
| 208 | - $this->xTarget = Target::makeFunction(trim($aCall['name'])); |
|
| 209 | - return $this->xTarget; |
|
| 210 | - } |
|
| 205 | +public function setTarget(ServerRequestInterface $xRequest): Target |
|
| 206 | +{ |
|
| 207 | +$aCall = $xRequest->getAttribute('jxncall'); |
|
| 208 | +$this->xTarget = Target::makeFunction(trim($aCall['name'])); |
|
| 209 | +return $this->xTarget; |
|
| 210 | +} |
|
| 211 | 211 | |
| 212 | - /** |
|
| 212 | +/** |
|
| 213 | 213 | * @param Exception $xException |
| 214 | 214 | * @param string $sErrorMessage |
| 215 | 215 | * |
| 216 | 216 | * @throws RequestException |
| 217 | 217 | * @return never |
| 218 | 218 | */ |
| 219 | - private function throwException(Exception $xException, string $sErrorMessage): void |
|
| 220 | - { |
|
| 221 | - $this->di->getLogger()->error($xException->getMessage()); |
|
| 222 | - throw new RequestException($sErrorMessage . (!$this->bDebug ? '' : |
|
| 223 | - "\n" . $xException->getMessage())); |
|
| 224 | - } |
|
| 219 | +private function throwException(Exception $xException, string $sErrorMessage): void |
|
| 220 | +{ |
|
| 221 | +$this->di->getLogger()->error($xException->getMessage()); |
|
| 222 | +throw new RequestException($sErrorMessage . (!$this->bDebug ? '' : |
|
| 223 | +"\n" . $xException->getMessage())); |
|
| 224 | +} |
|
| 225 | 225 | |
| 226 | - /** |
|
| 226 | +/** |
|
| 227 | 227 | * @inheritDoc |
| 228 | 228 | * @throws RequestException |
| 229 | 229 | */ |
| 230 | - public function processRequest(): void |
|
| 231 | - { |
|
| 232 | - $sRequestedFunction = $this->xTarget->getFunctionName(); |
|
| 230 | +public function processRequest(): void |
|
| 231 | +{ |
|
| 232 | +$sRequestedFunction = $this->xTarget->getFunctionName(); |
|
| 233 | 233 | |
| 234 | - // Security check: make sure the requested function was registered. |
|
| 235 | - if(!$this->xValidator->validateFunction($sRequestedFunction) || |
|
| 236 | - !isset($this->aFunctions[$sRequestedFunction])) |
|
| 237 | - { |
|
| 238 | - // Unable to find the requested function |
|
| 239 | - throw new RequestException($this->xTranslator->trans('errors.functions.invalid', |
|
| 240 | - ['name' => $sRequestedFunction])); |
|
| 241 | - } |
|
| 234 | +// Security check: make sure the requested function was registered. |
|
| 235 | +if(!$this->xValidator->validateFunction($sRequestedFunction) || |
|
| 236 | +!isset($this->aFunctions[$sRequestedFunction])) |
|
| 237 | +{ |
|
| 238 | +// Unable to find the requested function |
|
| 239 | +throw new RequestException($this->xTranslator->trans('errors.functions.invalid', |
|
| 240 | + ['name' => $sRequestedFunction])); |
|
| 241 | +} |
|
| 242 | 242 | |
| 243 | - try |
|
| 244 | - { |
|
| 245 | - /** @var CallableFunction */ |
|
| 246 | - $xFunction = $this->getCallable($sRequestedFunction); |
|
| 247 | - } |
|
| 248 | - catch(Exception $e) |
|
| 249 | - { |
|
| 250 | - // Unable to find the requested function |
|
| 251 | - $this->throwException($e, $this->xTranslator->trans('errors.functions.invalid', |
|
| 252 | - ['name' => $sRequestedFunction])); |
|
| 253 | - } |
|
| 254 | - try |
|
| 255 | - { |
|
| 256 | - $xFunction->call($this->xTarget->args()); |
|
| 257 | - } |
|
| 258 | - catch(Exception $e) |
|
| 259 | - { |
|
| 260 | - // Unable to execute the requested function |
|
| 261 | - $this->throwException($e, $this->xTranslator->trans('errors.functions.call', |
|
| 262 | - ['name' => $sRequestedFunction])); |
|
| 263 | - } |
|
| 264 | - } |
|
| 243 | +try |
|
| 244 | +{ |
|
| 245 | +/** @var CallableFunction */ |
|
| 246 | +$xFunction = $this->getCallable($sRequestedFunction); |
|
| 247 | +} |
|
| 248 | +catch(Exception $e) |
|
| 249 | +{ |
|
| 250 | +// Unable to find the requested function |
|
| 251 | +$this->throwException($e, $this->xTranslator->trans('errors.functions.invalid', |
|
| 252 | + ['name' => $sRequestedFunction])); |
|
| 253 | +} |
|
| 254 | +try |
|
| 255 | +{ |
|
| 256 | +$xFunction->call($this->xTarget->args()); |
|
| 257 | +} |
|
| 258 | +catch(Exception $e) |
|
| 259 | +{ |
|
| 260 | +// Unable to execute the requested function |
|
| 261 | +$this->throwException($e, $this->xTranslator->trans('errors.functions.call', |
|
| 262 | + ['name' => $sRequestedFunction])); |
|
| 263 | +} |
|
| 264 | +} |
|
| 265 | 265 | } |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | public function onChange(Config $xConfig, string $sName): void |
| 27 | 27 | { |
| 28 | 28 | // Set the library language any time the config is changed. |
| 29 | - if($sName === '' || $sName === 'core.language') |
|
| 29 | + if ($sName === '' || $sName === 'core.language') |
|
| 30 | 30 | { |
| 31 | 31 | $this->setLocale($xConfig->getOption('core.language')); |
| 32 | 32 | } |
@@ -20,15 +20,15 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Translator extends BaseTranslator implements ConfigListenerInterface |
| 22 | 22 | { |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * @inheritDoc |
| 25 | 25 | */ |
| 26 | - public function onChange(Config $xConfig, string $sName): void |
|
| 27 | - { |
|
| 28 | - // Set the library language any time the config is changed. |
|
| 29 | - if($sName === '' || $sName === 'core.language') |
|
| 30 | - { |
|
| 31 | - $this->setLocale($xConfig->getOption('core.language')); |
|
| 32 | - } |
|
| 33 | - } |
|
| 26 | +public function onChange(Config $xConfig, string $sName): void |
|
| 27 | +{ |
|
| 28 | +// Set the library language any time the config is changed. |
|
| 29 | +if($sName === '' || $sName === 'core.language') |
|
| 30 | +{ |
|
| 31 | +$this->setLocale($xConfig->getOption('core.language')); |
|
| 32 | +} |
|
| 33 | +} |
|
| 34 | 34 | } |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | public function get(string $key, mixed $default = null): mixed |
| 32 | 32 | { |
| 33 | 33 | $value = $this->values[$key] ?? $default; |
| 34 | - if(is_callable($value)) |
|
| 34 | + if (is_callable($value)) |
|
| 35 | 35 | { |
| 36 | 36 | $value = $value(); |
| 37 | 37 | // Save the value returned by the callback in the stash. |
@@ -6,38 +6,38 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Stash |
| 8 | 8 | { |
| 9 | - /** |
|
| 9 | +/** |
|
| 10 | 10 | * @var array |
| 11 | 11 | */ |
| 12 | - private array $values = []; |
|
| 12 | +private array $values = []; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 14 | +/** |
|
| 15 | 15 | * @param string $key |
| 16 | 16 | * @param mixed $value |
| 17 | 17 | * |
| 18 | 18 | * @return void |
| 19 | 19 | */ |
| 20 | - public function set(string $key, mixed $value): void |
|
| 21 | - { |
|
| 22 | - $this->values[$key] = $value; |
|
| 23 | - } |
|
| 20 | +public function set(string $key, mixed $value): void |
|
| 21 | +{ |
|
| 22 | +$this->values[$key] = $value; |
|
| 23 | +} |
|
| 24 | 24 | |
| 25 | - /** |
|
| 25 | +/** |
|
| 26 | 26 | * @param string $key |
| 27 | 27 | * @param mixed $default |
| 28 | 28 | * |
| 29 | 29 | * @return mixed |
| 30 | 30 | */ |
| 31 | - public function get(string $key, mixed $default = null): mixed |
|
| 32 | - { |
|
| 33 | - $value = $this->values[$key] ?? $default; |
|
| 34 | - if(is_callable($value)) |
|
| 35 | - { |
|
| 36 | - $value = $value(); |
|
| 37 | - // Save the value returned by the callback in the stash. |
|
| 38 | - $this->values[$key] = $value; |
|
| 39 | - } |
|
| 31 | +public function get(string $key, mixed $default = null): mixed |
|
| 32 | +{ |
|
| 33 | +$value = $this->values[$key] ?? $default; |
|
| 34 | +if(is_callable($value)) |
|
| 35 | +{ |
|
| 36 | +$value = $value(); |
|
| 37 | +// Save the value returned by the callback in the stash. |
|
| 38 | +$this->values[$key] = $value; |
|
| 39 | +} |
|
| 40 | 40 | |
| 41 | - return $value; |
|
| 42 | - } |
|
| 41 | +return $value; |
|
| 42 | +} |
|
| 43 | 43 | } |
@@ -91,11 +91,11 @@ discard block |
||
| 91 | 91 | 'export' => $bExport, |
| 92 | 92 | 'minify' => $bMinify, |
| 93 | 93 | ]; |
| 94 | - if($sUri !== '') |
|
| 94 | + if ($sUri !== '') |
|
| 95 | 95 | { |
| 96 | 96 | $aJsOptions['uri'] = $sUri; |
| 97 | 97 | } |
| 98 | - if($sDir !== '') |
|
| 98 | + if ($sDir !== '') |
|
| 99 | 99 | { |
| 100 | 100 | $aJsOptions['dir'] = $sDir; |
| 101 | 101 | } |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | { |
| 152 | 152 | // Popping the callbacks makes each of them to be called once. |
| 153 | 153 | $aBootCallbacks = $this->xCallbackManager->popBootCallbacks(); |
| 154 | - foreach($aBootCallbacks as $aBootCallback) |
|
| 154 | + foreach ($aBootCallbacks as $aBootCallback) |
|
| 155 | 155 | { |
| 156 | 156 | call_user_func($aBootCallback); |
| 157 | 157 | } |
@@ -23,58 +23,58 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | class Bootstrap |
| 25 | 25 | { |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * The library options |
| 28 | 28 | * |
| 29 | 29 | * @var array |
| 30 | 30 | */ |
| 31 | - private $aLibOptions = []; |
|
| 31 | +private $aLibOptions = []; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * The application options |
| 35 | 35 | * |
| 36 | 36 | * @var array |
| 37 | 37 | */ |
| 38 | - private $aAppOptions = []; |
|
| 38 | +private $aAppOptions = []; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * The class constructor |
| 42 | 42 | * |
| 43 | 43 | * @param ConfigManager $xConfigManager |
| 44 | 44 | * @param PackageManager $xPackageManager |
| 45 | 45 | * @param CallbackManager $xCallbackManager |
| 46 | 46 | */ |
| 47 | - public function __construct(private ConfigManager $xConfigManager, |
|
| 48 | - private PackageManager $xPackageManager, private CallbackManager $xCallbackManager) |
|
| 49 | - {} |
|
| 47 | +public function __construct(private ConfigManager $xConfigManager, |
|
| 48 | +private PackageManager $xPackageManager, private CallbackManager $xCallbackManager) |
|
| 49 | +{} |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * Set the library options |
| 53 | 53 | * |
| 54 | 54 | * @param array $aLibOptions The library options |
| 55 | 55 | * |
| 56 | 56 | * @return Bootstrap |
| 57 | 57 | */ |
| 58 | - public function lib(array $aLibOptions): Bootstrap |
|
| 59 | - { |
|
| 60 | - $this->aLibOptions = $aLibOptions; |
|
| 61 | - return $this; |
|
| 62 | - } |
|
| 58 | +public function lib(array $aLibOptions): Bootstrap |
|
| 59 | +{ |
|
| 60 | +$this->aLibOptions = $aLibOptions; |
|
| 61 | +return $this; |
|
| 62 | +} |
|
| 63 | 63 | |
| 64 | - /** |
|
| 64 | +/** |
|
| 65 | 65 | * Set the applications options |
| 66 | 66 | * |
| 67 | 67 | * @param array $aAppOptions The application options |
| 68 | 68 | * |
| 69 | 69 | * @return Bootstrap |
| 70 | 70 | */ |
| 71 | - public function app(array $aAppOptions): Bootstrap |
|
| 72 | - { |
|
| 73 | - $this->aAppOptions = $aAppOptions; |
|
| 74 | - return $this; |
|
| 75 | - } |
|
| 71 | +public function app(array $aAppOptions): Bootstrap |
|
| 72 | +{ |
|
| 73 | +$this->aAppOptions = $aAppOptions; |
|
| 74 | +return $this; |
|
| 75 | +} |
|
| 76 | 76 | |
| 77 | - /** |
|
| 77 | +/** |
|
| 78 | 78 | * Set the javascript asset |
| 79 | 79 | * |
| 80 | 80 | * @param bool $bExport Whether to export the js code in a file |
@@ -84,75 +84,75 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @return Bootstrap |
| 86 | 86 | */ |
| 87 | - public function asset(bool $bExport, bool $bMinify, string $sUri = '', string $sDir = ''): Bootstrap |
|
| 88 | - { |
|
| 89 | - // Jaxon library settings |
|
| 90 | - $aJsOptions = [ |
|
| 91 | - 'export' => $bExport, |
|
| 92 | - 'minify' => $bMinify, |
|
| 93 | - ]; |
|
| 94 | - if($sUri !== '') |
|
| 95 | - { |
|
| 96 | - $aJsOptions['uri'] = $sUri; |
|
| 97 | - } |
|
| 98 | - if($sDir !== '') |
|
| 99 | - { |
|
| 100 | - $aJsOptions['dir'] = $sDir; |
|
| 101 | - } |
|
| 102 | - $this->xConfigManager->setOptions($aJsOptions, 'js.app'); |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 87 | +public function asset(bool $bExport, bool $bMinify, string $sUri = '', string $sDir = ''): Bootstrap |
|
| 88 | +{ |
|
| 89 | +// Jaxon library settings |
|
| 90 | +$aJsOptions = [ |
|
| 91 | +'export' => $bExport, |
|
| 92 | +'minify' => $bMinify, |
|
| 93 | +]; |
|
| 94 | +if($sUri !== '') |
|
| 95 | +{ |
|
| 96 | +$aJsOptions['uri'] = $sUri; |
|
| 97 | +} |
|
| 98 | +if($sDir !== '') |
|
| 99 | +{ |
|
| 100 | +$aJsOptions['dir'] = $sDir; |
|
| 101 | +} |
|
| 102 | +$this->xConfigManager->setOptions($aJsOptions, 'js.app'); |
|
| 103 | +return $this; |
|
| 104 | +} |
|
| 105 | 105 | |
| 106 | - /** |
|
| 106 | +/** |
|
| 107 | 107 | * Set the Jaxon application options. |
| 108 | 108 | * |
| 109 | 109 | * @return void |
| 110 | 110 | * @throws SetupException |
| 111 | 111 | */ |
| 112 | - private function setupApp(): void |
|
| 113 | - { |
|
| 114 | - // Save the app config. |
|
| 115 | - $this->xConfigManager->setAppOptions($this->aAppOptions); |
|
| 116 | - // Register user functions and classes |
|
| 117 | - $this->xPackageManager->registerFromConfig(); |
|
| 118 | - } |
|
| 112 | +private function setupApp(): void |
|
| 113 | +{ |
|
| 114 | +// Save the app config. |
|
| 115 | +$this->xConfigManager->setAppOptions($this->aAppOptions); |
|
| 116 | +// Register user functions and classes |
|
| 117 | +$this->xPackageManager->registerFromConfig(); |
|
| 118 | +} |
|
| 119 | 119 | |
| 120 | - /** |
|
| 120 | +/** |
|
| 121 | 121 | * Wraps the module/package/bundle setup method. |
| 122 | 122 | * |
| 123 | 123 | * @return void |
| 124 | 124 | * @throws SetupException |
| 125 | 125 | */ |
| 126 | - public function setup(): void |
|
| 127 | - { |
|
| 128 | - // Prevent the Jaxon library from sending the response or exiting |
|
| 129 | - $this->xConfigManager->setOptions([ |
|
| 130 | - 'response' => [ |
|
| 131 | - 'send' => false, |
|
| 132 | - ], |
|
| 133 | - 'process' => [ |
|
| 134 | - 'exit' => false, |
|
| 135 | - ], |
|
| 136 | - ], 'core'); |
|
| 137 | - // Setup the lib config options. |
|
| 138 | - $this->xConfigManager->setOptions($this->aLibOptions); |
|
| 126 | +public function setup(): void |
|
| 127 | +{ |
|
| 128 | +// Prevent the Jaxon library from sending the response or exiting |
|
| 129 | +$this->xConfigManager->setOptions([ |
|
| 130 | +'response' => [ |
|
| 131 | + 'send' => false, |
|
| 132 | +], |
|
| 133 | +'process' => [ |
|
| 134 | + 'exit' => false, |
|
| 135 | +], |
|
| 136 | +], 'core'); |
|
| 137 | +// Setup the lib config options. |
|
| 138 | +$this->xConfigManager->setOptions($this->aLibOptions); |
|
| 139 | 139 | |
| 140 | - // Setup the app. |
|
| 141 | - $this->setupApp(); |
|
| 142 | - } |
|
| 140 | +// Setup the app. |
|
| 141 | +$this->setupApp(); |
|
| 142 | +} |
|
| 143 | 143 | |
| 144 | - /** |
|
| 144 | +/** |
|
| 145 | 145 | * These callbacks are called once, after the library is initialized. |
| 146 | 146 | * |
| 147 | 147 | * @return void |
| 148 | 148 | */ |
| 149 | - public function onBoot(): void |
|
| 150 | - { |
|
| 151 | - // Popping the callbacks makes each of them to be called once. |
|
| 152 | - $aBootCallbacks = $this->xCallbackManager->popBootCallbacks(); |
|
| 153 | - foreach($aBootCallbacks as $aBootCallback) |
|
| 154 | - { |
|
| 155 | - call_user_func($aBootCallback); |
|
| 156 | - } |
|
| 157 | - } |
|
| 149 | +public function onBoot(): void |
|
| 150 | +{ |
|
| 151 | +// Popping the callbacks makes each of them to be called once. |
|
| 152 | +$aBootCallbacks = $this->xCallbackManager->popBootCallbacks(); |
|
| 153 | +foreach($aBootCallbacks as $aBootCallback) |
|
| 154 | +{ |
|
| 155 | +call_user_func($aBootCallback); |
|
| 156 | +} |
|
| 157 | +} |
|
| 158 | 158 | } |