| Total Complexity | 48 |
| Total Lines | 582 |
| Duplicated Lines | 0 % |
| Changes | 24 | ||
| 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 |
||
| 65 | class Jaxon |
||
| 66 | { |
||
| 67 | /** |
||
| 68 | * Package version number |
||
| 69 | * |
||
| 70 | * @const string |
||
| 71 | */ |
||
| 72 | const VERSION = 'Jaxon 4.0.0-dev'; |
||
| 73 | |||
| 74 | /* |
||
| 75 | * Request plugins |
||
| 76 | */ |
||
| 77 | const CALLABLE_CLASS = 'CallableClass'; |
||
| 78 | const CALLABLE_DIR = 'CallableDir'; |
||
| 79 | const CALLABLE_FUNCTION = 'CallableFunction'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * A static instance on this class |
||
| 83 | * |
||
| 84 | * @var Jaxon |
||
| 85 | */ |
||
| 86 | private static $xInstance = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The DI container |
||
| 90 | * |
||
| 91 | * @var Container |
||
| 92 | */ |
||
| 93 | private static $xContainer = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var Bootstrap |
||
| 97 | */ |
||
| 98 | protected $xBootstrap; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var Translator |
||
| 102 | */ |
||
| 103 | protected $xTranslator; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var ConfigManager |
||
| 107 | */ |
||
| 108 | protected $xConfigManager; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var PluginManager |
||
| 112 | */ |
||
| 113 | protected $xPluginManager; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var PackageManager |
||
| 117 | */ |
||
| 118 | protected $xPackageManager; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var CodeGenerator |
||
| 122 | */ |
||
| 123 | protected $xCodeGenerator; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var CallableRegistry |
||
| 127 | */ |
||
| 128 | protected $xClassRegistry; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var CallbackManager |
||
| 132 | */ |
||
| 133 | protected $xCallbackManager; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var ResponseManager |
||
| 137 | */ |
||
| 138 | protected $xResponseManager; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | private static function initInstance() |
||
| 144 | { |
||
| 145 | // Set the attributes from the container |
||
| 146 | self::$xInstance->xBootstrap = self::$xContainer->g(Bootstrap::class); |
||
| 147 | self::$xInstance->xTranslator = self::$xContainer->g(Translator::class); |
||
| 148 | self::$xInstance->xConfigManager = self::$xContainer->g(ConfigManager::class); |
||
| 149 | self::$xInstance->xPluginManager = self::$xContainer->g(PluginManager::class); |
||
| 150 | self::$xInstance->xPackageManager = self::$xContainer->g(PackageManager::class); |
||
| 151 | self::$xInstance->xCodeGenerator = self::$xContainer->g(CodeGenerator::class); |
||
| 152 | self::$xInstance->xClassRegistry = self::$xContainer->g(CallableRegistry::class); |
||
| 153 | self::$xInstance->xCallbackManager = self::$xContainer->g(CallbackManager::class); |
||
| 154 | self::$xInstance->xResponseManager = self::$xContainer->g(ResponseManager::class); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get the static instance |
||
| 159 | * |
||
| 160 | * @return Jaxon |
||
| 161 | */ |
||
| 162 | public static function getInstance(): ?Jaxon |
||
| 163 | { |
||
| 164 | if(self::$xInstance === null) |
||
| 165 | { |
||
| 166 | self::$xInstance = new Jaxon(); |
||
| 167 | self::$xContainer = new Container(self::$xInstance); |
||
| 168 | self::initInstance(); |
||
| 169 | } |
||
| 170 | // Call the on boot callbacks on each call to the jaxon() function. |
||
| 171 | self::$xInstance->xBootstrap->onBoot(); |
||
| 172 | return self::$xInstance; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * The constructor |
||
| 177 | */ |
||
| 178 | private function __construct() |
||
| 179 | {} |
||
| 180 | |||
| 181 | /** |
||
| 182 | * The current Jaxon version |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getVersion(): string |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the DI container |
||
| 193 | * |
||
| 194 | * @return Container |
||
| 195 | */ |
||
| 196 | public function di(): ?Container |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the logger |
||
| 203 | * |
||
| 204 | * @return LoggerInterface |
||
| 205 | */ |
||
| 206 | public function logger(): LoggerInterface |
||
| 207 | { |
||
| 208 | return $this->di()->logger(); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the value of a config option |
||
| 213 | * |
||
| 214 | * @param string $sName The option name |
||
| 215 | * @param mixed $sValue The option value |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function setOption(string $sName, $sValue) |
||
| 220 | { |
||
| 221 | $this->xConfigManager->setOption($sName, $sValue); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the value of a config option |
||
| 226 | * |
||
| 227 | * @param string $sName The option name |
||
| 228 | * @param mixed|null $xDefault The default value, to be returned if the option is not defined |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function getOption(string $sName, $xDefault = null) |
||
| 233 | { |
||
| 234 | return $this->xConfigManager->getOption($sName, $xDefault); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Check the presence of a config option |
||
| 239 | * |
||
| 240 | * @param string $sName The option name |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function hasOption(string $sName): bool |
||
| 245 | { |
||
| 246 | return $this->xConfigManager->hasOption($sName); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Read the options from the file, if provided, and return the config |
||
| 251 | * |
||
| 252 | * @param string $sConfigFile The full path to the config file |
||
| 253 | * @param string $sConfigSection The section of the config file to be loaded |
||
| 254 | * |
||
| 255 | * @return ConfigManager |
||
| 256 | * @throws SetupException |
||
| 257 | */ |
||
| 258 | public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the translator |
||
| 269 | * |
||
| 270 | * @return Translator |
||
| 271 | */ |
||
| 272 | public function translator(): Translator |
||
| 273 | { |
||
| 274 | return $this->xTranslator; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the Response object |
||
| 279 | * |
||
| 280 | * @return ResponseInterface |
||
| 281 | */ |
||
| 282 | public function getResponse(): ResponseInterface |
||
| 283 | { |
||
| 284 | return $this->xResponseManager->getResponse(); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Create a new Jaxon response object |
||
| 289 | * |
||
| 290 | * @return Response |
||
| 291 | */ |
||
| 292 | public function newResponse(): Response |
||
| 293 | { |
||
| 294 | return $this->di()->newResponse(); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Register a plugin |
||
| 299 | * |
||
| 300 | * Below is a table for priorities and their description: |
||
| 301 | * - 0 to 999: Plugins that are part of or extensions to the jaxon core |
||
| 302 | * - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
||
| 303 | * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 304 | * |
||
| 305 | * @param string $sClassName The plugin class |
||
| 306 | * @param string $sPluginName The plugin name |
||
| 307 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | * @throws SetupException |
||
| 311 | */ |
||
| 312 | public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
||
| 313 | { |
||
| 314 | $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Register a package |
||
| 319 | * |
||
| 320 | * @param string $sClassName The package class |
||
| 321 | * @param array $xPkgOptions The user provided package options |
||
| 322 | * |
||
| 323 | * @return void |
||
| 324 | * @throws SetupException |
||
| 325 | */ |
||
| 326 | public function registerPackage(string $sClassName, array $xPkgOptions = []) |
||
| 327 | { |
||
| 328 | $this->xPackageManager->registerPackage($sClassName, $xPkgOptions); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Register request handlers, including functions, callable classes and directories. |
||
| 333 | * |
||
| 334 | * @param string $sType The type of request handler being registered |
||
| 335 | * Options include: |
||
| 336 | * - Jaxon::CALLABLE_FUNCTION: a function declared at global scope |
||
| 337 | * - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered |
||
| 338 | * - Jaxon::CALLABLE_DIR: a directory containing classes to be registered |
||
| 339 | * @param string $sName |
||
| 340 | * When registering a function, this is the name of the function |
||
| 341 | * When registering a callable class, this is the class name |
||
| 342 | * When registering a callable directory, this is the full path to the directory |
||
| 343 | * @param array|string $xOptions The related options |
||
| 344 | * |
||
| 345 | * @return void |
||
| 346 | * @throws SetupException |
||
| 347 | */ |
||
| 348 | public function register(string $sType, string $sName, $xOptions = []) |
||
| 349 | { |
||
| 350 | $this->xPluginManager->registerCallable($sType, $sName, $xOptions); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get an instance of a registered class |
||
| 355 | * |
||
| 356 | * @param string $sClassName The class name |
||
| 357 | * |
||
| 358 | * @return null|object |
||
| 359 | * @throws SetupException |
||
| 360 | */ |
||
| 361 | public function instance(string $sClassName) |
||
| 362 | { |
||
| 363 | $xCallable = $this->xClassRegistry->getCallableObject($sClassName); |
||
| 364 | return ($xCallable) ? $xCallable->getRegisteredObject() : null; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the factory |
||
| 369 | * |
||
| 370 | * @return Factory |
||
| 371 | */ |
||
| 372 | public function factory(): Factory |
||
| 373 | { |
||
| 374 | return $this->di()->getFactory(); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the PSR factory |
||
| 379 | * |
||
| 380 | * @return PsrFactory |
||
| 381 | */ |
||
| 382 | public function psr(): PsrFactory |
||
| 383 | { |
||
| 384 | return $this->di()->getPsrFactory(); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get a request to a registered class |
||
| 389 | * |
||
| 390 | * @param string $sClassName The class name |
||
| 391 | * |
||
| 392 | * @return RequestFactory|null |
||
| 393 | * @throws SetupException |
||
| 394 | */ |
||
| 395 | public function request(string $sClassName = ''): ?RequestFactory |
||
| 396 | { |
||
| 397 | return $this->factory()->request($sClassName); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
| 402 | * |
||
| 403 | * The javascript code returned by this function is dependent on the plugins |
||
| 404 | * that are included and the functions and classes that are registered. |
||
| 405 | * |
||
| 406 | * @param bool $bIncludeJs Also get the JS files |
||
| 407 | * @param bool $bIncludeCss Also get the CSS files |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | * @throws UriException |
||
| 411 | */ |
||
| 412 | public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
||
| 413 | { |
||
| 414 | return $this->xCodeGenerator->getScript($bIncludeJs, $bIncludeCss); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return the javascript header code and file includes |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getJs(): string |
||
| 423 | { |
||
| 424 | return $this->xCodeGenerator->getJs(); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Return the CSS header code and file includes |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function getCss(): string |
||
| 433 | { |
||
| 434 | return $this->xCodeGenerator->getCss(); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Determine if a call is a jaxon request or a page load request |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function canProcessRequest(): bool |
||
| 443 | { |
||
| 444 | return $this->di()->getRequestHandler()->canProcessRequest(); |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
| 449 | * |
||
| 450 | * This is the main server side engine for Jaxon. |
||
| 451 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
| 452 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
| 453 | * headers or HTML is output from your script. |
||
| 454 | * |
||
| 455 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
| 456 | * |
||
| 457 | * @return void |
||
| 458 | * |
||
| 459 | * @throws RequestException |
||
| 460 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
| 461 | */ |
||
| 462 | public function processRequest() |
||
| 463 | { |
||
| 464 | // Check to see if headers have already been sent out, in which case we can't do our job |
||
| 465 | if(headers_sent($sFilename, $nLineNumber)) |
||
| 466 | { |
||
| 467 | $sMessage = $this->xTranslator->trans('errors.output.already-sent', [ |
||
| 468 | 'location' => $sFilename . ':' . $nLineNumber |
||
| 469 | ]) . "\n" . $this->xTranslator->trans('errors.output.advice'); |
||
| 470 | throw new RequestException($sMessage); |
||
| 471 | } |
||
| 472 | |||
| 473 | $this->di()->getRequestHandler()->processRequest(); |
||
| 474 | |||
| 475 | // Clean the processing buffer |
||
| 476 | if(($this->xConfigManager->getOption('core.process.clean'))) |
||
| 477 | { |
||
| 478 | $er = error_reporting(0); |
||
| 479 | while(ob_get_level() > 0) |
||
| 480 | { |
||
| 481 | ob_end_clean(); |
||
| 482 | } |
||
| 483 | error_reporting($er); |
||
| 484 | } |
||
| 485 | if($this->xConfigManager->getOption('core.response.send')) |
||
| 486 | { |
||
| 487 | $this->sendResponse(); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Prints the response to the output stream, thus sending the response to the browser |
||
| 493 | * |
||
| 494 | * @return void |
||
| 495 | */ |
||
| 496 | public function sendResponse() |
||
| 497 | { |
||
| 498 | if(empty($sContent = $this->xResponseManager->getOutput())) |
||
| 499 | { |
||
| 500 | return; |
||
| 501 | } |
||
| 502 | if($this->di()->getRequest()->getMethod() === 'GET') |
||
| 503 | { |
||
| 504 | header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
||
| 505 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
||
| 506 | header("Cache-Control: no-cache, must-revalidate"); |
||
| 507 | header("Pragma: no-cache"); |
||
| 508 | } |
||
| 509 | header('content-type: ' . $this->xResponseManager->getContentType()); |
||
| 510 | print $sContent; |
||
| 511 | if($this->xConfigManager->getOption('core.process.exit')) |
||
| 512 | { |
||
| 513 | exit(); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Get a registered response plugin |
||
| 519 | * |
||
| 520 | * @param string $sName The name of the plugin |
||
| 521 | * |
||
| 522 | * @return ResponsePlugin|null |
||
| 523 | */ |
||
| 524 | public function plugin(string $sName): ?ResponsePlugin |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get a package instance |
||
| 531 | * |
||
| 532 | * @param string $sClassName The package class name |
||
| 533 | * |
||
| 534 | * @return Package|null |
||
| 535 | */ |
||
| 536 | public function package(string $sClassName): ?Package |
||
| 537 | { |
||
| 538 | return $this->xPackageManager->getPackage($sClassName); |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Get the upload plugin |
||
| 543 | * |
||
| 544 | * @return UploadHandler|null |
||
| 545 | */ |
||
| 546 | public function upload(): ?UploadHandler |
||
| 547 | { |
||
| 548 | return $this->di()->getUploadHandler(); |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get the request callback manager |
||
| 553 | * |
||
| 554 | * @return CallbackManager |
||
| 555 | */ |
||
| 556 | public function callback(): CallbackManager |
||
| 557 | { |
||
| 558 | return $this->di()->getCallbackManager(); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get the template engine |
||
| 563 | * |
||
| 564 | * @return TemplateEngine |
||
| 565 | */ |
||
| 566 | public function template(): TemplateEngine |
||
| 567 | { |
||
| 568 | return $this->di()->getTemplateEngine(); |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Get the App instance |
||
| 573 | * |
||
| 574 | * @return App |
||
| 575 | */ |
||
| 576 | public function app(): App |
||
| 577 | { |
||
| 578 | return $this->di()->getApp(); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get the view renderer |
||
| 583 | * |
||
| 584 | * @return ViewRenderer |
||
| 585 | */ |
||
| 586 | public function view(): ViewRenderer |
||
| 587 | { |
||
| 588 | return $this->di()->getViewRenderer(); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get the paginator |
||
| 593 | * |
||
| 594 | * @return Paginator |
||
| 595 | */ |
||
| 596 | public function paginator(): Paginator |
||
| 597 | { |
||
| 598 | return $this->di()->getPaginator(); |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Get the Dialog plugin |
||
| 603 | * |
||
| 604 | * @return DialogPlugin |
||
| 605 | */ |
||
| 606 | public function dialog(): DialogPlugin |
||
| 607 | { |
||
| 608 | return $this->di()->getDialogPlugin(); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Register a javascript dialog library adapter. |
||
| 613 | * |
||
| 614 | * @param string $sClassName |
||
| 615 | * @param string $sName |
||
| 616 | * |
||
| 617 | * @return void |
||
| 618 | * @throws SetupException |
||
| 619 | */ |
||
| 620 | public function registerDialogLibrary(string $sClassName, string $sName) |
||
| 621 | { |
||
| 622 | $this->di()->getDialogLibraryManager()->registerLibrary($sClassName, $sName); |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Get the session manager |
||
| 627 | * |
||
| 628 | * @return SessionInterface |
||
| 629 | */ |
||
| 630 | public function session(): SessionInterface |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Reset the library and container instances |
||
| 637 | * |
||
| 638 | * @return void |
||
| 639 | * @throws SetupException |
||
| 640 | */ |
||
| 641 | public function reset() |
||
| 642 | { |
||
| 643 | self::$xInstance = null; |
||
| 644 | self::$xContainer = null; |
||
| 645 | // Need to register the default plugins. |
||
| 646 | self::getInstance()->di()->getPluginManager()->registerPlugins(); |
||
| 647 | } |
||
| 648 | } |
||
| 649 |