@@ -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 | } |