| Total Complexity | 40 |
| Total Lines | 544 |
| Duplicated Lines | 0 % |
| Changes | 26 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Jaxon 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 Jaxon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class Jaxon implements LoggerAwareInterface |
||
| 61 | { |
||
| 62 | use LoggerAwareTrait; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Package version number |
||
| 66 | * |
||
| 67 | * @const string |
||
| 68 | */ |
||
| 69 | const VERSION = 'Jaxon 4.0.0-dev'; |
||
| 70 | |||
| 71 | /* |
||
| 72 | * Request plugins |
||
| 73 | */ |
||
| 74 | const CALLABLE_CLASS = 'CallableClass'; |
||
| 75 | const CALLABLE_DIR = 'CallableDir'; |
||
| 76 | const CALLABLE_FUNCTION = 'CallableFunction'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * A static instance on this class |
||
| 80 | * |
||
| 81 | * @var Jaxon |
||
| 82 | */ |
||
| 83 | private static $xInstance = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The DI container |
||
| 87 | * |
||
| 88 | * @var Container |
||
| 89 | */ |
||
| 90 | private static $xContainer = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Translator |
||
| 94 | */ |
||
| 95 | protected $xTranslator; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var ConfigManager |
||
| 99 | */ |
||
| 100 | protected $xConfigManager; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var PluginManager |
||
| 104 | */ |
||
| 105 | protected $xPluginManager; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var CodeGenerator |
||
| 109 | */ |
||
| 110 | protected $xCodeGenerator; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var CallableRegistry |
||
| 114 | */ |
||
| 115 | protected $xClassRegistry; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var RequestHandler |
||
| 119 | */ |
||
| 120 | protected $xRequestHandler; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var ResponseManager |
||
| 124 | */ |
||
| 125 | protected $xResponseManager; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | private static function initInstance() |
||
| 131 | { |
||
| 132 | // Save the Jaxon and container instances in the DI |
||
| 133 | self::$xContainer->val(Jaxon::class, self::$xInstance); |
||
| 134 | self::$xContainer->val(Container::class, self::$xContainer); |
||
| 135 | // Set the attributes from the container |
||
| 136 | self::$xInstance->xTranslator = self::$xContainer->g(Translator::class); |
||
| 137 | self::$xInstance->xConfigManager = self::$xContainer->g(ConfigManager::class); |
||
| 138 | self::$xInstance->xPluginManager = self::$xContainer->g(PluginManager::class); |
||
| 139 | self::$xInstance->xCodeGenerator = self::$xContainer->g(CodeGenerator::class); |
||
| 140 | self::$xInstance->xClassRegistry = self::$xContainer->g(CallableRegistry::class); |
||
| 141 | self::$xInstance->xRequestHandler = self::$xContainer->g(RequestHandler::class); |
||
| 142 | self::$xInstance->xResponseManager = self::$xContainer->g(ResponseManager::class); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the static instance |
||
| 147 | * |
||
| 148 | * @return Jaxon |
||
| 149 | */ |
||
| 150 | public static function getInstance(): ?Jaxon |
||
| 151 | { |
||
| 152 | if(self::$xInstance === null) |
||
| 153 | { |
||
| 154 | self::$xContainer = new Container(self::getDefaultOptions()); |
||
| 155 | self::$xInstance = new Jaxon(); |
||
| 156 | self::initInstance(); |
||
| 157 | } |
||
| 158 | return self::$xInstance; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * The constructor |
||
| 163 | */ |
||
| 164 | private function __construct() |
||
| 165 | { |
||
| 166 | // Set the default logger |
||
| 167 | $this->setLogger(new NullLogger()); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The current Jaxon version |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getVersion(): string |
||
| 176 | { |
||
| 177 | return self::VERSION; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the default options of all components of the library |
||
| 182 | * |
||
| 183 | * @return array<string,string|bool|integer> |
||
| 184 | */ |
||
| 185 | private static function getDefaultOptions(): array |
||
| 186 | { |
||
| 187 | // The default configuration settings. |
||
| 188 | return [ |
||
| 189 | 'core.version' => self::VERSION, |
||
| 190 | 'core.language' => 'en', |
||
| 191 | 'core.encoding' => 'utf-8', |
||
| 192 | 'core.decode_utf8' => false, |
||
| 193 | 'core.prefix.function' => 'jaxon_', |
||
| 194 | 'core.prefix.class' => 'Jaxon', |
||
| 195 | // 'core.request.uri' => '', |
||
| 196 | 'core.request.mode' => 'asynchronous', |
||
| 197 | 'core.request.method' => 'POST', // W3C: Method is case sensitive |
||
| 198 | 'core.response.send' => true, |
||
| 199 | 'core.response.merge.ap' => true, |
||
| 200 | 'core.response.merge.js' => true, |
||
| 201 | 'core.debug.on' => false, |
||
| 202 | 'core.debug.verbose' => false, |
||
| 203 | 'core.process.exit' => true, |
||
| 204 | 'core.process.clean' => false, |
||
| 205 | 'core.process.timeout' => 6000, |
||
| 206 | 'core.error.handle' => false, |
||
| 207 | 'core.error.log_file' => '', |
||
| 208 | 'core.jquery.no_conflict' => false, |
||
| 209 | 'core.upload.enabled' => true, |
||
| 210 | 'js.lib.output_id' => 0, |
||
| 211 | 'js.lib.queue_size' => 0, |
||
| 212 | 'js.lib.load_timeout' => 2000, |
||
| 213 | 'js.lib.show_status' => false, |
||
| 214 | 'js.lib.show_cursor' => true, |
||
| 215 | 'js.app.dir' => '', |
||
| 216 | 'js.app.minify' => true, |
||
| 217 | 'js.app.options' => '', |
||
| 218 | ]; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the DI container |
||
| 223 | * |
||
| 224 | * @return Container |
||
| 225 | */ |
||
| 226 | public function di(): ?Container |
||
| 227 | { |
||
| 228 | return self::$xContainer; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get the logger |
||
| 233 | * |
||
| 234 | * @return LoggerInterface |
||
| 235 | */ |
||
| 236 | public function logger(): LoggerInterface |
||
| 237 | { |
||
| 238 | return $this->logger; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set the value of a config option |
||
| 243 | * |
||
| 244 | * @param string $sName The option name |
||
| 245 | * @param mixed $sValue The option value |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function setOption(string $sName, $sValue) |
||
| 250 | { |
||
| 251 | $this->xConfigManager->setOption($sName, $sValue); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the value of a config option |
||
| 256 | * |
||
| 257 | * @param string $sName The option name |
||
| 258 | * @param mixed|null $xDefault The default value, to be returned if the option is not defined |
||
| 259 | * |
||
| 260 | * @return mixed The option value, or null if the option is unknown |
||
| 261 | */ |
||
| 262 | public function getOption(string $sName, $xDefault = null) |
||
| 263 | { |
||
| 264 | return $this->xConfigManager->getOption($sName, $xDefault); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Check the presence of a config option |
||
| 269 | * |
||
| 270 | * @param string $sName The option name |
||
| 271 | * |
||
| 272 | * @return bool True if the option exists, and false if not |
||
| 273 | */ |
||
| 274 | public function hasOption(string $sName): bool |
||
| 275 | { |
||
| 276 | return $this->xConfigManager->hasOption($sName); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get a translated string |
||
| 281 | * |
||
| 282 | * @param string $sText The key of the translated string |
||
| 283 | * @param array $aPlaceHolders The placeholders of the translated string |
||
| 284 | * @param string $sLanguage The language of the translated string |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
||
| 289 | { |
||
| 290 | return $this->xTranslator->trans($sText, $aPlaceHolders, $sLanguage); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get the Global Response object |
||
| 295 | * |
||
| 296 | * @return AbstractResponse |
||
| 297 | */ |
||
| 298 | public function getResponse(): AbstractResponse |
||
| 299 | { |
||
| 300 | if(($xResponse = $this->xResponseManager->getResponse())) |
||
| 301 | { |
||
| 302 | return $xResponse; |
||
| 303 | } |
||
| 304 | return $this->di()->getResponse(); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Create a new Jaxon response object |
||
| 309 | * |
||
| 310 | * @return Response |
||
| 311 | */ |
||
| 312 | public function newResponse(): Response |
||
| 313 | { |
||
| 314 | return $this->di()->newResponse(); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Register a plugin |
||
| 319 | * |
||
| 320 | * Below is a table for priorities and their description: |
||
| 321 | * - 0 to 999: Plugins that are part of or extensions to the jaxon core |
||
| 322 | * - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
||
| 323 | * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 324 | * |
||
| 325 | * @param string $sClassName The plugin class |
||
| 326 | * @param string $sPluginName The plugin name |
||
| 327 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | * @throws SetupException |
||
| 331 | */ |
||
| 332 | public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
||
| 333 | { |
||
| 334 | $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Register a package |
||
| 339 | * |
||
| 340 | * @param string $sClassName The package class |
||
| 341 | * @param array $xPkgOptions The user provided package options |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | * @throws SetupException |
||
| 345 | */ |
||
| 346 | public function registerPackage(string $sClassName, array $xPkgOptions = []) |
||
| 347 | { |
||
| 348 | $this->xPluginManager->registerPackage($sClassName, $xPkgOptions); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Register request handlers, including functions, callable classes and directories. |
||
| 353 | * |
||
| 354 | * @param string $sType The type of request handler being registered |
||
| 355 | * Options include: |
||
| 356 | * - Jaxon::CALLABLE_FUNCTION: a function declared at global scope |
||
| 357 | * - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered |
||
| 358 | * - Jaxon::CALLABLE_DIR: a directory containing classes to be registered |
||
| 359 | * @param string $sName |
||
| 360 | * When registering a function, this is the name of the function |
||
| 361 | * When registering a callable class, this is the class name |
||
| 362 | * When registering a callable directory, this is the full path to the directory |
||
| 363 | * @param array|string $xOptions The related options |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | * @throws SetupException |
||
| 367 | */ |
||
| 368 | public function register(string $sType, string $sName, $xOptions = []) |
||
| 369 | { |
||
| 370 | $this->xPluginManager->registerCallable($sType, $sName, $xOptions); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get an instance of a registered class |
||
| 375 | * |
||
| 376 | * @param string $sClassName The class name |
||
| 377 | * |
||
| 378 | * @return null|object |
||
| 379 | * @throws SetupException |
||
| 380 | */ |
||
| 381 | public function instance(string $sClassName) |
||
| 382 | { |
||
| 383 | $xCallable = $this->xClassRegistry->getCallableObject($sClassName); |
||
| 384 | return ($xCallable) ? $xCallable->getRegisteredObject() : null; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the factory |
||
| 389 | * |
||
| 390 | * @return Factory |
||
| 391 | */ |
||
| 392 | public function factory(): Factory |
||
| 393 | { |
||
| 394 | return $this->di()->g(Factory::class); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get a request to a registered class |
||
| 399 | * |
||
| 400 | * @param string $sClassName The class name |
||
| 401 | * |
||
| 402 | * @return RequestFactory|null |
||
| 403 | * @throws SetupException |
||
| 404 | */ |
||
| 405 | public function request(string $sClassName = ''): ?RequestFactory |
||
| 406 | { |
||
| 407 | return $this->factory()->request($sClassName); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
| 412 | * |
||
| 413 | * The javascript code returned by this function is dependent on the plugins |
||
| 414 | * that are included and the functions and classes that are registered. |
||
| 415 | * |
||
| 416 | * @param bool $bIncludeJs Also get the JS files |
||
| 417 | * @param bool $bIncludeCss Also get the CSS files |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | * @throws UriException |
||
| 421 | */ |
||
| 422 | public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
||
| 423 | { |
||
| 424 | return $this->xCodeGenerator->getScript($bIncludeJs, $bIncludeCss); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Print the jaxon Javascript header and wrapper code into your page |
||
| 429 | * |
||
| 430 | * The javascript code returned by this function is dependent on the plugins |
||
| 431 | * that are included and the functions and classes that are registered. |
||
| 432 | * |
||
| 433 | * @param bool $bIncludeJs Also print the JS files |
||
| 434 | * @param bool $bIncludeCss Also print the CSS files |
||
| 435 | * |
||
| 436 | * @return void |
||
| 437 | * @throws UriException |
||
| 438 | */ |
||
| 439 | public function printScript(bool $bIncludeJs = false, bool $bIncludeCss = false) |
||
| 440 | { |
||
| 441 | print $this->getScript($bIncludeJs, $bIncludeCss); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return the javascript header code and file includes |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getJs(): string |
||
| 450 | { |
||
| 451 | return $this->xCodeGenerator->getJs(); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return the CSS header code and file includes |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function getCss(): string |
||
| 460 | { |
||
| 461 | return $this->xCodeGenerator->getCss(); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Determine if a call is a jaxon request or a page load request |
||
| 466 | * |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | public function canProcessRequest(): bool |
||
| 470 | { |
||
| 471 | return $this->xRequestHandler->canProcessRequest(); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
| 476 | * |
||
| 477 | * This is the main server side engine for Jaxon. |
||
| 478 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
| 479 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
| 480 | * headers or HTML is output from your script. |
||
| 481 | * |
||
| 482 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | * |
||
| 486 | * @throws RequestException |
||
| 487 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
| 488 | */ |
||
| 489 | public function processRequest() |
||
| 490 | { |
||
| 491 | // Check to see if headers have already been sent out, in which case we can't do our job |
||
| 492 | if(headers_sent($sFilename, $nLineNumber)) |
||
| 493 | { |
||
| 494 | echo $this->xTranslator->trans('errors.output.already-sent', [ |
||
| 495 | 'location' => $sFilename . ':' . $nLineNumber |
||
| 496 | ]), "\n", $this->xTranslator->trans('errors.output.advice'); |
||
| 497 | exit(); |
||
| 498 | } |
||
| 499 | |||
| 500 | $this->xRequestHandler->processRequest(); |
||
| 501 | |||
| 502 | if(($this->xConfigManager->getOption('core.response.send'))) |
||
| 503 | { |
||
| 504 | $this->xResponseManager->sendOutput(); |
||
| 505 | if(($this->xConfigManager->getOption('core.process.exit'))) |
||
| 506 | { |
||
| 507 | exit(); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get a registered response plugin |
||
| 514 | * |
||
| 515 | * @param string $sName The name of the plugin |
||
| 516 | * |
||
| 517 | * @return ResponsePlugin|null |
||
| 518 | */ |
||
| 519 | public function plugin(string $sName): ?ResponsePlugin |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get a package instance |
||
| 526 | * |
||
| 527 | * @param string $sClassName The package class name |
||
| 528 | * |
||
| 529 | * @return Package|null |
||
| 530 | */ |
||
| 531 | public function package(string $sClassName): ?Package |
||
| 532 | { |
||
| 533 | return $this->xPluginManager->getPackage($sClassName); |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get the upload plugin |
||
| 538 | * |
||
| 539 | * @return UploadHandler|null |
||
| 540 | */ |
||
| 541 | public function upload(): ?UploadHandler |
||
| 542 | { |
||
| 543 | return $this->di()->getUploadHandler(); |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Get the request callback manager |
||
| 548 | * |
||
| 549 | * @return CallbackManager |
||
| 550 | */ |
||
| 551 | public function callback(): CallbackManager |
||
| 552 | { |
||
| 553 | return $this->xRequestHandler->getCallbackManager(); |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get the dialog wrapper |
||
| 558 | * |
||
| 559 | * @return Dialog |
||
| 560 | */ |
||
| 561 | public function dialog(): Dialog |
||
| 562 | { |
||
| 563 | return $this->di()->getDialog(); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the template engine |
||
| 568 | * |
||
| 569 | * @return TemplateEngine |
||
| 570 | */ |
||
| 571 | public function template(): TemplateEngine |
||
| 572 | { |
||
| 573 | return $this->di()->getTemplateEngine(); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get the App instance |
||
| 578 | * |
||
| 579 | * @return App |
||
| 580 | */ |
||
| 581 | public function app(): App |
||
| 582 | { |
||
| 583 | return $this->di()->getApp(); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Get the view renderer |
||
| 588 | * |
||
| 589 | * @return ViewRenderer |
||
| 590 | */ |
||
| 591 | public function view(): ViewRenderer |
||
| 592 | { |
||
| 593 | return $this->di()->getViewRenderer(); |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Get the session manager |
||
| 598 | * |
||
| 599 | * @return SessionInterface |
||
| 600 | */ |
||
| 601 | public function session(): SessionInterface |
||
| 604 | } |
||
| 605 | } |
||
| 606 |