| Total Complexity | 44 |
| Total Lines | 294 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Generator |
||
| 22 | { |
||
| 23 | use \Jaxon\Features\Config; |
||
| 24 | use \Jaxon\Features\Minifier; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Default library URL |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The objects that generate code |
||
| 35 | * |
||
| 36 | * @var array<GeneratorContract> |
||
| 37 | */ |
||
| 38 | protected $aGenerators = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The Jaxon template engine |
||
| 42 | * |
||
| 43 | * @var TemplateEngine |
||
| 44 | */ |
||
| 45 | protected $xTemplateEngine; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The constructor |
||
| 49 | * |
||
| 50 | * @param TemplateEngine $xTemplateEngine The template engine |
||
| 51 | */ |
||
| 52 | public function __construct(TemplateEngine $xTemplateEngine) |
||
| 53 | { |
||
| 54 | $this->xTemplateEngine = $xTemplateEngine; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get the correspondances between previous and current config options |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | private function getOptionVars() |
||
| 63 | { |
||
| 64 | return [ |
||
| 65 | 'sResponseType' => 'JSON', |
||
| 66 | 'sVersion' => $this->getOption('core.version'), |
||
| 67 | 'sLanguage' => $this->getOption('core.language'), |
||
| 68 | 'bLanguage' => $this->hasOption('core.language') ? true : false, |
||
| 69 | 'sRequestURI' => $this->getOption('core.request.uri'), |
||
| 70 | 'sDefaultMode' => $this->getOption('core.request.mode'), |
||
| 71 | 'sDefaultMethod' => $this->getOption('core.request.method'), |
||
| 72 | 'sCsrfMetaName' => $this->getOption('core.request.csrf_meta'), |
||
| 73 | 'bDebug' => $this->getOption('core.debug.on'), |
||
| 74 | 'bVerboseDebug' => $this->getOption('core.debug.verbose'), |
||
| 75 | 'sDebugOutputID' => $this->getOption('core.debug.output_id'), |
||
| 76 | 'nResponseQueueSize' => $this->getOption('js.lib.queue_size'), |
||
| 77 | 'sStatusMessages' => $this->getOption('js.lib.show_status') ? 'true' : 'false', |
||
| 78 | 'sWaitCursor' => $this->getOption('js.lib.show_cursor') ? 'true' : 'false', |
||
| 79 | 'sDefer' => $this->getOption('js.app.options', ''), |
||
| 80 | ]; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Render a template in the 'plugins' subdir |
||
| 85 | * |
||
| 86 | * @param string $sTemplate The template filename |
||
| 87 | * @param array $aVars The template variables |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | private function _render($sTemplate, array $aVars = []) |
||
| 92 | { |
||
| 93 | $aVars['sJsOptions'] = $this->getOption('js.app.options', ''); |
||
| 94 | return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Add a new generator to the list |
||
| 99 | * |
||
| 100 | * @param GeneratorContract $xGenerator The code generator |
||
| 101 | * @param integer $nPriority The desired priority, used to order the plugins |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function addGenerator(GeneratorContract $xGenerator, $nPriority) |
||
| 106 | { |
||
| 107 | while(isset($this->aGenerators[$nPriority])) |
||
| 108 | { |
||
| 109 | $nPriority++; |
||
| 110 | } |
||
| 111 | $this->aGenerators[$nPriority] = $xGenerator; |
||
| 112 | // Sort the array by ascending keys |
||
| 113 | ksort($this->aGenerators); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Generate a hash for all the javascript code generated by the library |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | private function getHash() |
||
| 122 | { |
||
| 123 | $sHash = jaxon()->getVersion(); |
||
| 124 | foreach($this->aGenerators as $xGenerator) |
||
| 125 | { |
||
| 126 | $sHash .= $xGenerator->getHash(); |
||
| 127 | } |
||
| 128 | return md5($sHash); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getCss() |
||
| 137 | { |
||
| 138 | $sCssCode = ''; |
||
| 139 | foreach($this->aGenerators as $xGenerator) |
||
| 140 | { |
||
| 141 | $sCssCode = rtrim($sCssCode, " \n") . "\n" . $xGenerator->getCss(); |
||
| 142 | } |
||
| 143 | return rtrim($sCssCode, " \n") . "\n"; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getJs() |
||
| 152 | { |
||
| 153 | $sJsExtension = $this->getOption('js.app.minify') ? '.min.js' : '.js'; |
||
| 154 | |||
| 155 | // The URI for the javascript library files |
||
| 156 | $sJsLibUri = rtrim($this->getOption('js.lib.uri', self::JS_LIB_URL), '/') . '/'; |
||
| 157 | // Add component files to the javascript file array; |
||
| 158 | $aJsFiles = [$sJsLibUri . 'jaxon.core' . $sJsExtension]; |
||
| 159 | if($this->getOption('core.debug.on')) |
||
| 160 | { |
||
| 161 | $sLanguage = $this->getOption('core.language'); |
||
| 162 | $aJsFiles[] = $sJsLibUri . 'jaxon.debug' . $sJsExtension; |
||
| 163 | $aJsFiles[] = $sJsLibUri . 'lang/jaxon.' . $sLanguage . $sJsExtension; |
||
| 164 | /*if($this->getOption('core.debug.verbose')) |
||
| 165 | { |
||
| 166 | $aJsFiles[] = $sJsLibUri . 'jaxon.verbose' . $sJsExtension; |
||
| 167 | }*/ |
||
| 168 | } |
||
| 169 | $sJsFiles = $this->_render('includes.js', ['aUrls' => $aJsFiles]); |
||
| 170 | |||
| 171 | $sJsCode = ''; |
||
| 172 | foreach($this->aGenerators as $xGenerator) |
||
| 173 | { |
||
| 174 | $sJsCode = rtrim($sJsCode, " \n") . "\n" . $xGenerator->getJs(); |
||
| 175 | } |
||
| 176 | return $sJsFiles . "\n" . rtrim($sJsCode, " \n") . "\n"; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the javascript code to be sent to the browser |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | private function _getScript() |
||
| 185 | { |
||
| 186 | $sScript = ''; |
||
| 187 | $sReadyScript = ''; |
||
| 188 | foreach($this->aGenerators as $xGenerator) |
||
| 189 | { |
||
| 190 | $sScript .= rtrim($xGenerator->getScript(), " \n") . "\n"; |
||
| 191 | if($xGenerator->readyEnabled() && !$xGenerator->readyInlined()) |
||
| 192 | { |
||
| 193 | // Ready code which can nbe exported to an external file. |
||
| 194 | $sReadyScript .= rtrim($xGenerator->getReadyScript(), " \n") . "\n"; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | // These three parts are always rendered together |
||
| 199 | $aConfigVars = $this->getOptionVars(); |
||
| 200 | return $this->_render('config.js', $aConfigVars) . "\n" . $sScript . "\n" . |
||
| 201 | $this->_render('ready.js', ['sScript' => $sReadyScript]); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the javascript code to include directly in HTML |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | private function _getInlineScript() |
||
| 210 | { |
||
| 211 | $sScript = ''; |
||
| 212 | foreach($this->aGenerators as $xGenerator) |
||
| 213 | { |
||
| 214 | if($xGenerator->readyEnabled() && $xGenerator->readyInlined()) |
||
| 215 | { |
||
| 216 | // Ready code which must be inlined in HTML. |
||
| 217 | $sScript .= rtrim($xGenerator->getReadyScript(), " \n") . "\n"; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | return $this->_render('ready.js', ['sScript' => $sScript]); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the javascript file name |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | private function getJsFileName() |
||
| 229 | { |
||
| 230 | // Check config options |
||
| 231 | // - The js.app.export option must be set to true |
||
| 232 | // - The js.app.uri and js.app.dir options must be set to non null values |
||
| 233 | if(!$this->getOption('js.app.export') || |
||
| 234 | !$this->getOption('js.app.uri') || |
||
| 235 | !$this->getOption('js.app.dir')) |
||
| 236 | { |
||
| 237 | return ''; |
||
| 238 | } |
||
| 239 | |||
| 240 | // The file name |
||
| 241 | return $this->hasOption('js.app.file') ? $this->getOption('js.app.file') : $this->getHash(); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Write javascript files and return the corresponding URI |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function createFiles($sJsDirectory, $sJsFileName) |
||
| 250 | { |
||
| 251 | // Check dir access |
||
| 252 | // - The js.app.dir must be writable |
||
| 253 | if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
||
| 254 | { |
||
| 255 | return ''; |
||
| 256 | } |
||
| 257 | |||
| 258 | $sOutFile = $sJsFileName . '.js'; |
||
| 259 | $sMinFile = $sJsFileName . '.min.js'; |
||
| 260 | if(!is_file($sJsDirectory . $sOutFile)) |
||
| 261 | { |
||
| 262 | if(!file_put_contents($sJsDirectory . $sOutFile, $this->_getScript())) |
||
| 263 | { |
||
| 264 | return ''; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | if(($this->getOption('js.app.minify')) && !is_file($sJsDirectory . $sMinFile)) |
||
| 268 | { |
||
| 269 | if(!$this->minify($sJsDirectory . $sOutFile, $sJsDirectory . $sMinFile)) |
||
| 270 | { |
||
| 271 | return ''; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $sJsAppUri = rtrim($this->getOption('js.app.uri'), '/') . '/'; |
||
| 276 | $sJsExtension = $this->getOption('js.app.minify') ? '.min.js' : '.js'; |
||
| 277 | return $sJsAppUri . $sJsFileName . $sJsExtension; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the javascript code to be sent to the browser |
||
| 282 | * |
||
| 283 | * @param boolean $bIncludeJs Also get the JS files |
||
| 284 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getScript($bIncludeJs, $bIncludeCss) |
||
| 315 | ]); |
||
| 316 | } |
||
| 317 | } |
||
| 318 |