| Total Complexity | 43 |
| Total Lines | 468 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Ajax 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 Ajax, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class Ajax |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * @var Ajax |
||
| 62 | */ |
||
| 63 | private static $xInstance = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Container |
||
| 67 | */ |
||
| 68 | private static $xContainer = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Bootstrap |
||
| 72 | */ |
||
| 73 | protected $xBootstrap; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Translator |
||
| 77 | */ |
||
| 78 | protected $xTranslator; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var ConfigManager |
||
| 82 | */ |
||
| 83 | protected $xConfigManager; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var PluginManager |
||
| 87 | */ |
||
| 88 | protected $xPluginManager; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var ResponseManager |
||
| 92 | */ |
||
| 93 | protected $xResponseManager; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | private static function initInstance() |
||
| 99 | { |
||
| 100 | // Set the attributes from the container |
||
| 101 | self::$xInstance->xBootstrap = self::$xContainer->g(Bootstrap::class); |
||
| 102 | self::$xInstance->xTranslator = self::$xContainer->g(Translator::class); |
||
| 103 | self::$xInstance->xConfigManager = self::$xContainer->g(ConfigManager::class); |
||
| 104 | self::$xInstance->xPluginManager = self::$xContainer->g(PluginManager::class); |
||
| 105 | self::$xInstance->xResponseManager = self::$xContainer->g(ResponseManager::class); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return Ajax |
||
| 110 | */ |
||
| 111 | public static function getInstance(): ?Ajax |
||
| 112 | { |
||
| 113 | if(self::$xInstance === null) |
||
| 114 | { |
||
| 115 | self::$xInstance = new Ajax(); |
||
| 116 | self::$xContainer = new Container(self::$xInstance); |
||
| 117 | self::initInstance(); |
||
| 118 | } |
||
| 119 | // Call the on boot callbacks on each call to the jaxon() function. |
||
| 120 | self::$xInstance->xBootstrap->onBoot(); |
||
| 121 | return self::$xInstance; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The constructor |
||
| 126 | */ |
||
| 127 | private function __construct() |
||
| 128 | {} |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getVersion(): string |
||
| 134 | { |
||
| 135 | return Jaxon::VERSION; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return Container |
||
| 140 | */ |
||
| 141 | public function di(): ?Container |
||
| 142 | { |
||
| 143 | return self::$xContainer; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return LoggerInterface |
||
| 148 | */ |
||
| 149 | public function logger(): LoggerInterface |
||
| 150 | { |
||
| 151 | return $this->di()->getLogger(); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the value of a config option |
||
| 156 | * |
||
| 157 | * @param string $sName The option name |
||
| 158 | * @param mixed $sValue The option value |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function setOption(string $sName, $sValue) |
||
| 163 | { |
||
| 164 | $this->xConfigManager->setOption($sName, $sValue); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the value of a config option |
||
| 169 | * |
||
| 170 | * @param string $sName The option name |
||
| 171 | * @param mixed|null $xDefault The default value, to be returned if the option is not defined |
||
| 172 | * |
||
| 173 | * @return mixed |
||
| 174 | */ |
||
| 175 | public function getOption(string $sName, $xDefault = null) |
||
| 176 | { |
||
| 177 | return $this->xConfigManager->getOption($sName, $xDefault); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check the presence of a config option |
||
| 182 | * |
||
| 183 | * @param string $sName The option name |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function hasOption(string $sName): bool |
||
| 188 | { |
||
| 189 | return $this->xConfigManager->hasOption($sName); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Read the options from the file, if provided, and return the config |
||
| 194 | * |
||
| 195 | * @param string $sConfigFile The full path to the config file |
||
| 196 | * @param string $sConfigSection The section of the config file to be loaded |
||
| 197 | * |
||
| 198 | * @return ConfigManager |
||
| 199 | * @throws SetupException |
||
| 200 | */ |
||
| 201 | public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager |
||
| 202 | { |
||
| 203 | if(!empty(($sConfigFile = trim($sConfigFile)))) |
||
| 204 | { |
||
| 205 | $this->xConfigManager->load($sConfigFile, trim($sConfigSection)); |
||
| 206 | } |
||
| 207 | return $this->xConfigManager; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return Translator |
||
| 212 | */ |
||
| 213 | public function translator(): Translator |
||
| 214 | { |
||
| 215 | return $this->xTranslator; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the global Response object |
||
| 220 | * |
||
| 221 | * @return ResponseInterface |
||
| 222 | */ |
||
| 223 | public function getResponse(): ResponseInterface |
||
| 224 | { |
||
| 225 | return $this->xResponseManager->getResponse(); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Create a new Jaxon response object |
||
| 230 | * |
||
| 231 | * @return Response |
||
| 232 | */ |
||
| 233 | public function newResponse(): Response |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Register a plugin |
||
| 240 | * |
||
| 241 | * Below is a table for priorities and their description: |
||
| 242 | * - 0 to 999: Plugins that are part of or extensions to the jaxon core |
||
| 243 | * - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
||
| 244 | * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 245 | * |
||
| 246 | * @param string $sClassName The plugin class |
||
| 247 | * @param string $sPluginName The plugin name |
||
| 248 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | * @throws SetupException |
||
| 252 | */ |
||
| 253 | public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
||
| 254 | { |
||
| 255 | $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Register a package |
||
| 260 | * |
||
| 261 | * @param string $sClassName The package class |
||
| 262 | * @param array $xPkgOptions The user provided package options |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | * @throws SetupException |
||
| 266 | */ |
||
| 267 | public function registerPackage(string $sClassName, array $xPkgOptions = []) |
||
| 268 | { |
||
| 269 | $this->di()->getPackageManager()->registerPackage($sClassName, $xPkgOptions); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Register request handlers, including functions, callable classes and directories. |
||
| 274 | * |
||
| 275 | * @param string $sType The type of request handler being registered |
||
| 276 | * Options include: |
||
| 277 | * - Jaxon::CALLABLE_FUNCTION: a function declared at global scope |
||
| 278 | * - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered |
||
| 279 | * - Jaxon::CALLABLE_DIR: a directory containing classes to be registered |
||
| 280 | * @param string $sName |
||
| 281 | * When registering a function, this is the name of the function |
||
| 282 | * When registering a callable class, this is the class name |
||
| 283 | * When registering a callable directory, this is the full path to the directory |
||
| 284 | * @param array|string $xOptions The related options |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | * @throws SetupException |
||
| 288 | */ |
||
| 289 | public function register(string $sType, string $sName, $xOptions = []) |
||
| 290 | { |
||
| 291 | $this->xPluginManager->registerCallable($sType, $sName, $xOptions); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return Factory |
||
| 296 | */ |
||
| 297 | public function factory(): Factory |
||
| 298 | { |
||
| 299 | return $this->di()->getFactory(); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return PsrFactory |
||
| 304 | */ |
||
| 305 | public function psr(): PsrFactory |
||
| 306 | { |
||
| 307 | return $this->di()->getPsrFactory(); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $sClassName The class name |
||
| 312 | * |
||
| 313 | * @return RequestFactory|null |
||
| 314 | * @throws SetupException |
||
| 315 | */ |
||
| 316 | public function request(string $sClassName = ''): ?RequestFactory |
||
| 317 | { |
||
| 318 | return $this->factory()->request($sClassName); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
| 323 | * |
||
| 324 | * The javascript code returned by this function is dependent on the plugins |
||
| 325 | * that are included and the functions and classes that are registered. |
||
| 326 | * |
||
| 327 | * @param bool $bIncludeJs Also get the JS files |
||
| 328 | * @param bool $bIncludeCss Also get the CSS files |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | * @throws UriException |
||
| 332 | */ |
||
| 333 | public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
||
| 334 | { |
||
| 335 | return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return the javascript header code and file includes |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function getJs(): string |
||
| 344 | { |
||
| 345 | return $this->di()->getCodeGenerator()->getJs(); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return the CSS header code and file includes |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getCss(): string |
||
| 354 | { |
||
| 355 | return $this->di()->getCodeGenerator()->getCss(); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Determine if a call is a jaxon request or a page load request |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | public function canProcessRequest(): bool |
||
| 364 | { |
||
| 365 | return $this->di()->getRequestHandler()->canProcessRequest(); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
| 370 | * |
||
| 371 | * This is the main server side engine for Jaxon. |
||
| 372 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
| 373 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
| 374 | * headers or HTML is output from your script. |
||
| 375 | * |
||
| 376 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | * |
||
| 380 | * @throws RequestException |
||
| 381 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
| 382 | */ |
||
| 383 | public function processRequest() |
||
| 384 | { |
||
| 385 | // Check to see if headers have already been sent out, in which case we can't do our job |
||
| 386 | if(headers_sent($sFilename, $nLineNumber)) |
||
| 387 | { |
||
| 388 | $sMessage = $this->xTranslator->trans('errors.output.already-sent', [ |
||
| 389 | 'location' => $sFilename . ':' . $nLineNumber |
||
| 390 | ]) . "\n" . $this->xTranslator->trans('errors.output.advice'); |
||
| 391 | throw new RequestException($sMessage); |
||
| 392 | } |
||
| 393 | |||
| 394 | $this->di()->getRequestHandler()->processRequest(); |
||
| 395 | |||
| 396 | if($this->xConfigManager->getOption('core.response.send')) |
||
| 397 | { |
||
| 398 | $this->sendResponse(); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Prints the response to the output stream, thus sending the response to the browser |
||
| 404 | * |
||
| 405 | * @return void |
||
| 406 | */ |
||
| 407 | public function sendResponse() |
||
| 408 | { |
||
| 409 | if(empty($sContent = $this->xResponseManager->getOutput())) |
||
| 410 | { |
||
| 411 | return; |
||
| 412 | } |
||
| 413 | if($this->di()->getRequest()->getMethod() === 'GET') |
||
| 414 | { |
||
| 415 | header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
||
| 416 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
||
| 417 | header("Cache-Control: no-cache, must-revalidate"); |
||
| 418 | header("Pragma: no-cache"); |
||
| 419 | } |
||
| 420 | header('content-type: ' . $this->xResponseManager->getContentType()); |
||
| 421 | print $sContent; |
||
| 422 | if($this->xConfigManager->getOption('core.process.exit')) |
||
| 423 | { |
||
| 424 | exit(); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get a registered response plugin |
||
| 430 | * |
||
| 431 | * @param string $sName The name of the plugin |
||
| 432 | * |
||
| 433 | * @return ResponsePlugin|null |
||
| 434 | */ |
||
| 435 | public function plugin(string $sName): ?ResponsePlugin |
||
| 436 | { |
||
| 437 | return $this->xPluginManager->getResponsePlugin($sName); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get a package instance |
||
| 442 | * |
||
| 443 | * @param string $sClassName The package class name |
||
| 444 | * |
||
| 445 | * @return Package|null |
||
| 446 | */ |
||
| 447 | public function package(string $sClassName): ?Package |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return UploadHandler|null |
||
| 454 | */ |
||
| 455 | public function upload(): ?UploadHandler |
||
| 456 | { |
||
| 457 | return $this->di()->getUploadHandler(); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @return CallbackManager |
||
| 462 | */ |
||
| 463 | public function callback(): CallbackManager |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return TemplateEngine |
||
| 470 | */ |
||
| 471 | public function template(): TemplateEngine |
||
| 472 | { |
||
| 473 | return $this->di()->getTemplateEngine(); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return App |
||
| 478 | */ |
||
| 479 | public function app(): App |
||
| 480 | { |
||
| 481 | return $this->di()->getApp(); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @return ViewRenderer |
||
| 486 | */ |
||
| 487 | public function view(): ViewRenderer |
||
| 488 | { |
||
| 489 | return $this->di()->getViewRenderer(); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return Paginator |
||
| 494 | */ |
||
| 495 | public function paginator(): Paginator |
||
| 496 | { |
||
| 497 | return $this->di()->getPaginator(); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @return DialogLibraryManager |
||
| 502 | */ |
||
| 503 | public function dialog(): DialogLibraryManager |
||
| 504 | { |
||
| 505 | return $this->di()->getDialogLibraryManager(); |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return SessionInterface|null |
||
| 510 | */ |
||
| 511 | public function session(): ?SessionInterface |
||
| 512 | { |
||
| 513 | return $this->di()->getSessionManager(); |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return void |
||
| 518 | * @throws SetupException |
||
| 519 | */ |
||
| 520 | public function reset() |
||
| 526 | } |
||
| 527 | } |
||
| 528 |