| Total Complexity | 50 |
| Total Lines | 364 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CodeGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CodeGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class CodeGenerator |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var Jaxon |
||
| 36 | */ |
||
| 37 | private $jaxon; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Config |
||
| 41 | */ |
||
| 42 | protected $xConfig; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var UriDetector |
||
| 46 | */ |
||
| 47 | private $xUriDetector; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Minifier |
||
| 51 | */ |
||
| 52 | private $xMinifier; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Default library URL |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The class names of objects that generate code |
||
| 63 | * |
||
| 64 | * @var array<string> |
||
| 65 | */ |
||
| 66 | protected $aClassNames = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The Jaxon template engine |
||
| 70 | * |
||
| 71 | * @var TemplateEngine |
||
| 72 | */ |
||
| 73 | protected $xTemplateEngine; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The constructor |
||
| 77 | * |
||
| 78 | * @param Jaxon $jaxon |
||
| 79 | * @param Config $xConfig |
||
| 80 | * @param UriDetector $xUriDetector |
||
| 81 | * @param TemplateEngine $xTemplateEngine The template engine |
||
| 82 | * @param Minifier $xMinifier |
||
| 83 | */ |
||
| 84 | public function __construct(Jaxon $jaxon, Config $xConfig, UriDetector $xUriDetector, |
||
| 85 | TemplateEngine $xTemplateEngine, Minifier $xMinifier) |
||
| 86 | { |
||
| 87 | $this->jaxon = $jaxon; |
||
| 88 | $this->xConfig = $xConfig; |
||
| 89 | $this->xUriDetector = $xUriDetector; |
||
| 90 | $this->xTemplateEngine = $xTemplateEngine; |
||
| 91 | $this->xMinifier = $xMinifier; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the mappings between previous and current config options |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | private function getOptionVars(): array |
||
| 100 | { |
||
| 101 | return [ |
||
| 102 | 'sResponseType' => 'JSON', |
||
| 103 | 'sVersion' => $this->xConfig->getOption('core.version'), |
||
| 104 | 'sLanguage' => $this->xConfig->getOption('core.language'), |
||
| 105 | 'bLanguage' => $this->xConfig->hasOption('core.language'), |
||
| 106 | 'sRequestURI' => $this->xConfig->getOption('core.request.uri'), |
||
| 107 | 'sDefaultMode' => $this->xConfig->getOption('core.request.mode'), |
||
| 108 | 'sDefaultMethod' => $this->xConfig->getOption('core.request.method'), |
||
| 109 | 'sCsrfMetaName' => $this->xConfig->getOption('core.request.csrf_meta'), |
||
| 110 | 'bDebug' => $this->xConfig->getOption('core.debug.on'), |
||
| 111 | 'bVerboseDebug' => $this->xConfig->getOption('core.debug.verbose'), |
||
| 112 | 'sDebugOutputID' => $this->xConfig->getOption('core.debug.output_id'), |
||
| 113 | 'nResponseQueueSize' => $this->xConfig->getOption('js.lib.queue_size'), |
||
| 114 | 'sStatusMessages' => $this->xConfig->getOption('js.lib.show_status') ? 'true' : 'false', |
||
| 115 | 'sWaitCursor' => $this->xConfig->getOption('js.lib.show_cursor') ? 'true' : 'false', |
||
| 116 | 'sDefer' => $this->xConfig->getOption('js.app.options', ''), |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Render a template in the 'plugins' subdir |
||
| 122 | * |
||
| 123 | * @param string $sTemplate The template filename |
||
| 124 | * @param array $aVars The template variables |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | private function _render(string $sTemplate, array $aVars = []): string |
||
| 129 | { |
||
| 130 | $aVars['sJsOptions'] = $this->xConfig->getOption('js.app.options', ''); |
||
| 131 | return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Add a new generator to the list |
||
| 136 | * |
||
| 137 | * @param string $sClassName The code generator class |
||
| 138 | * @param int $nPriority The desired priority, used to order the plugins |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function addGenerator(string $sClassName, int $nPriority) |
||
| 143 | { |
||
| 144 | while(isset($this->aClassNames[$nPriority])) |
||
| 145 | { |
||
| 146 | $nPriority++; |
||
| 147 | } |
||
| 148 | $this->aClassNames[$nPriority] = $sClassName; |
||
| 149 | // Sort the array by ascending keys |
||
| 150 | ksort($this->aClassNames); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Generate a hash for all the javascript code generated by the library |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | private function getHash(): string |
||
| 159 | { |
||
| 160 | $sHash = $this->jaxon->getVersion(); |
||
| 161 | foreach($this->aClassNames as $sClassName) |
||
| 162 | { |
||
| 163 | $xGenerator = $this->jaxon->di()->get($sClassName); |
||
| 164 | $sHash .= $xGenerator->getHash(); |
||
| 165 | } |
||
| 166 | return md5($sHash); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Check if all plugins assets shall be included in Jaxon generated code. |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | protected function shallIncludeAllAssets(): bool |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if the assets of this plugin shall be included in Jaxon generated code. |
||
| 181 | * |
||
| 182 | * @param Plugin $xPlugin |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | protected function shallIncludeAssets(Plugin $xPlugin): bool |
||
| 187 | { |
||
| 188 | $sPluginOptionName = 'assets.include.' . $xPlugin->getName(); |
||
| 189 | return $this->xConfig->getOption($sPluginOptionName, true); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getCss(): string |
||
| 198 | { |
||
| 199 | $bIncludeAllAssets = $this->shallIncludeAllAssets(); |
||
| 200 | $sCssCode = ''; |
||
| 201 | foreach($this->aClassNames as $sClassName) |
||
| 202 | { |
||
| 203 | $xGenerator = $this->jaxon->di()->get($sClassName); |
||
| 204 | if(is_subclass_of($xGenerator, Plugin::class) && |
||
| 205 | (!$this->shallIncludeAssets($xGenerator) || !$bIncludeAllAssets)) |
||
| 206 | { |
||
| 207 | continue; |
||
| 208 | } |
||
| 209 | $sCssCode = rtrim($sCssCode, " \n") . "\n" . $xGenerator->getCss(); |
||
| 210 | } |
||
| 211 | return rtrim($sCssCode, " \n") . "\n"; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getJs(): string |
||
| 220 | { |
||
| 221 | $sJsExtension = $this->xConfig->getOption('js.app.minify') ? '.min.js' : '.js'; |
||
| 222 | |||
| 223 | // The URI for the javascript library files |
||
| 224 | $sJsLibUri = rtrim($this->xConfig->getOption('js.lib.uri', self::JS_LIB_URL), '/') . '/'; |
||
| 225 | // Add component files to the javascript file array; |
||
| 226 | $aJsFiles = [$sJsLibUri . 'jaxon.core' . $sJsExtension]; |
||
| 227 | if($this->xConfig->getOption('core.debug.on')) |
||
| 228 | { |
||
| 229 | $sLanguage = $this->xConfig->getOption('core.language'); |
||
| 230 | $aJsFiles[] = $sJsLibUri . 'jaxon.debug' . $sJsExtension; |
||
| 231 | $aJsFiles[] = $sJsLibUri . 'lang/jaxon.' . $sLanguage . $sJsExtension; |
||
| 232 | /*if($this->xConfig->getOption('core.debug.verbose')) |
||
| 233 | { |
||
| 234 | $aJsFiles[] = $sJsLibUri . 'jaxon.verbose' . $sJsExtension; |
||
| 235 | }*/ |
||
| 236 | } |
||
| 237 | $sJsFiles = $this->_render('includes.js', ['aUrls' => $aJsFiles]); |
||
| 238 | |||
| 239 | $bIncludeAllAssets = $this->shallIncludeAllAssets(); |
||
| 240 | $sJsCode = ''; |
||
| 241 | foreach($this->aClassNames as $sClassName) |
||
| 242 | { |
||
| 243 | $xGenerator = $this->jaxon->di()->get($sClassName); |
||
| 244 | if(is_subclass_of($xGenerator, Plugin::class) && |
||
| 245 | (!$this->shallIncludeAssets($xGenerator) || !$bIncludeAllAssets)) |
||
| 246 | { |
||
| 247 | continue; |
||
| 248 | } |
||
| 249 | $sJsCode = rtrim($sJsCode, " \n") . "\n" . $xGenerator->getJs(); |
||
| 250 | } |
||
| 251 | return $sJsFiles . "\n" . rtrim($sJsCode, " \n") . "\n"; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the javascript code to be sent to the browser |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | private function _getScript(): string |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the javascript code to include directly in HTML |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | private function _getInlineScript(): string |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the javascript file name |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | private function getJsFileName(): string |
||
| 306 | { |
||
| 307 | // Check config options |
||
| 308 | // - The js.app.export option must be set to true |
||
| 309 | // - The js.app.uri and js.app.dir options must be set to non null values |
||
| 310 | if(!$this->xConfig->getOption('js.app.export', false) || |
||
| 311 | !$this->xConfig->getOption('js.app.uri', false) || |
||
| 312 | !$this->xConfig->getOption('js.app.dir', false)) |
||
| 313 | { |
||
| 314 | return ''; |
||
| 315 | } |
||
| 316 | |||
| 317 | // The file name |
||
| 318 | return $this->xConfig->getOption('js.app.file', $this->getHash()); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Write javascript files and return the corresponding URI |
||
| 323 | * |
||
| 324 | * @param string $sJsDirectory |
||
| 325 | * @param string $sJsFileName |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function createFiles(string $sJsDirectory, string $sJsFileName): string |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the javascript code to be sent to the browser |
||
| 362 | * |
||
| 363 | * @param bool $bIncludeJs Also get the JS files |
||
| 364 | * @param bool $bIncludeCss Also get the CSS files |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | * @throws UriException |
||
| 368 | */ |
||
| 369 | public function getScript(bool $bIncludeJs, bool $bIncludeCss): string |
||
| 396 | ]); |
||
| 397 | } |
||
| 398 | } |
||
| 399 |